github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/dedup/dedup_test.go (about) 1 package dedup_test 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 "github.com/lmorg/murex/utils/dedup" 8 ) 9 10 type testSortT struct { 11 Input []string 12 Expected []string 13 ExpLength int 14 } 15 16 func TestSortAndDedupString(t *testing.T) { 17 tests := []testSortT{ 18 { 19 Input: []string{}, 20 Expected: []string{}, 21 ExpLength: 0, 22 }, 23 { 24 Input: []string{"a", "a"}, 25 Expected: []string{"a"}, 26 ExpLength: 1, 27 }, 28 { 29 Input: []string{"a", "a", "a"}, 30 Expected: []string{"a"}, 31 ExpLength: 1, 32 }, 33 { 34 Input: []string{"a", "a", "b"}, 35 Expected: []string{"a", "b"}, 36 ExpLength: 2, 37 }, 38 { 39 Input: []string{"a", "a", "b", "c"}, 40 Expected: []string{"a", "b", "c"}, 41 ExpLength: 3, 42 }, 43 { 44 Input: []string{"a", "a", "b", "c", "c"}, 45 Expected: []string{"a", "b", "c"}, 46 ExpLength: 3, 47 }, 48 { 49 Input: []string{"c", "a", "a", "b", "c"}, 50 Expected: []string{"a", "b", "c"}, 51 ExpLength: 3, 52 }, 53 { 54 Input: []string{"a", "f", "f", "c", "g", "d", "a", "b", "e", "a", "b", "b"}, 55 Expected: []string{"a", "b", "c", "d", "e", "f", "g"}, 56 ExpLength: 7, 57 }, 58 { 59 Input: []string{"bee", "cee", "aee", "cee", "dee", "gee", "eff", "eee", "cee", "bee", "cee"}, 60 Expected: []string{"aee", "bee", "cee", "dee", "eee", "eff", "gee"}, 61 ExpLength: 7, 62 }, 63 { 64 Input: []string{"f:", "foo:", "fo:", "b:", "bar:", "ba:"}, 65 Expected: []string{"b:", "ba:", "bar:", "f:", "fo:", "foo:"}, 66 ExpLength: 6, 67 }, 68 { 69 Input: []string{"foobar", "foo", "bar"}, 70 Expected: []string{"bar", "foo", "foobar"}, 71 ExpLength: 3, 72 }, 73 { 74 Input: []string{"bar", "foo", "foobar"}, 75 Expected: []string{"bar", "foo", "foobar"}, 76 ExpLength: 3, 77 }, 78 { 79 Input: []string{"-bar", "foo", "foobar"}, 80 Expected: []string{"foo", "foobar", "-bar"}, 81 ExpLength: 3, 82 }, 83 { 84 Input: []string{"bar", "-foo", "foobar"}, 85 Expected: []string{"bar", "foobar", "-foo"}, 86 ExpLength: 3, 87 }, 88 { 89 Input: []string{"bar", "foo", "-foobar"}, 90 Expected: []string{"bar", "foo", "-foobar"}, 91 ExpLength: 3, 92 }, 93 { 94 Input: []string{"", " ", "foo", "bar"}, 95 Expected: []string{"", " ", "bar", "foo"}, 96 ExpLength: 4, 97 }, 98 { 99 Input: []string{"bar", "foo", "", " "}, 100 Expected: []string{"", " ", "bar", "foo"}, 101 ExpLength: 4, 102 }, 103 } 104 105 count.Tests(t, len(tests)) 106 for i, test := range tests { 107 s := make([]string, len(test.Input)) 108 copy(s, test.Input) 109 ActLength := dedup.SortAndDedupString(s) 110 111 if test.ExpLength != ActLength { 112 t.Errorf("Return integer doesn't match expected in test %d", i) 113 t.Logf(" Input: %v", test.Input) 114 t.Logf(" Expected: %v", test.Expected) 115 t.Logf(" Actual: %v", s[:ActLength]) 116 t.Logf(" Uncropped:%v", s) 117 t.Logf(" Exp Len: %d", test.ExpLength) 118 t.Logf(" Act Len: %d", ActLength) 119 continue 120 } 121 122 for j := 0; j < ActLength; j++ { 123 if s[j] != test.Expected[j] { 124 t.Errorf("Slice element %d doesn't match expected in test %d", j, i) 125 t.Logf(" Input: %v", test.Input) 126 t.Logf(" Expected: %v", test.Expected) 127 t.Logf(" Actual: %v", s[:ActLength]) 128 t.Logf(" Uncropped:%v", s) 129 t.Logf(" Exp Len: %d", test.ExpLength) 130 t.Logf(" Act Len: %d", ActLength) 131 t.Logf(" Exp Str: %v", test.Expected[j]) 132 t.Logf(" Act Str: %v", s[j]) 133 continue 134 } 135 } 136 } 137 }