| 1 | package list |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/jesseduffield/generics/internal/testutils" |
| 7 | ) |
| 8 | |
| 9 | func TestEqual(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | first []int |
| 12 | second []int |
| 13 | expected bool |
| 14 | }{ |
| 15 | {[]int{}, []int{}, true}, |
| 16 | {[]int{1}, []int{1}, true}, |
| 17 | {[]int{}, []int{1}, false}, |
| 18 | {[]int{1, 2}, []int{1, 2}, true}, |
| 19 | {[]int{1, 2}, []int{2, 1}, false}, |
| 20 | {[]int{1, 2, 3}, []int{1, 2}, false}, |
| 21 | } |
| 22 | for _, test := range tests { |
| 23 | first := NewComparableFromSlice(test.first) |
| 24 | second := NewComparableFromSlice(test.second) |
| 25 | if first.Equal(second) != test.expected { |
| 26 | t.Errorf("Equal(%v, %v) = %v, expected %v", |
| 27 | test.first, test.second, first.Equal(second), test.expected, |
| 28 | ) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestCompact(t *testing.T) { |
| 34 | tests := []struct { |
| 35 | slice []int |
| 36 | expected []int |
| 37 | }{ |
| 38 | {[]int{}, []int{}}, |
| 39 | {[]int{1}, []int{1}}, |
| 40 | {[]int{1, 2}, []int{1, 2}}, |
| 41 | {[]int{1, 1, 2}, []int{1, 2}}, |
| 42 | {[]int{1, 2, 1}, []int{1, 2, 1}}, |
| 43 | {[]int{1, 1, 1}, []int{1}}, |
| 44 | } |
| 45 | for _, test := range tests { |
| 46 | list := NewComparableFromSlice(test.slice) |
| 47 | list.Compact() |
| 48 | testutils.ExpectSlice(t, test.expected, list.ToSlice()) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestIndex(t *testing.T) { |
| 53 | tests := []struct { |
| 54 | slice []int |
| 55 | value int |
| 56 | expected int |
| 57 | }{ |
| 58 | {[]int{}, 1, -1}, |
| 59 | {[]int{1}, 1, 0}, |
| 60 | {[]int{1, 1}, 1, 0}, |
| 61 | {[]int{1, 1}, 2, -1}, |
| 62 | {[]int{1, 2, 3}, 2, 1}, |
| 63 | {[]int{1, 2, 3}, 3, 2}, |
| 64 | } |
| 65 | for _, test := range tests { |
| 66 | list := NewComparableFromSlice(test.slice) |
| 67 | |
| 68 | if list.Index(test.value) != test.expected { |
| 69 | t.Errorf("Index(%v, %v) = %v, expected %v", test.slice, test.value, list.Index(test.value), test.expected) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestIndexFunc(t *testing.T) { |
| 75 | tests := []struct { |
| 76 | slice []int |
| 77 | f func(value int) bool |
| 78 | expected int |
| 79 | }{ |
| 80 | {[]int{}, func(value int) bool { return true }, -1}, |
| 81 | {[]int{1}, func(value int) bool { return true }, 0}, |
| 82 | {[]int{1, 1}, func(value int) bool { return true }, 0}, |
| 83 | {[]int{1, 1}, func(value int) bool { return false }, -1}, |
| 84 | {[]int{1, 2, 3}, func(value int) bool { return value == 2 }, 1}, |
| 85 | {[]int{1, 2, 3}, func(value int) bool { return value == 3 }, 2}, |
| 86 | } |
| 87 | for _, test := range tests { |
| 88 | list := NewComparableFromSlice(test.slice) |
| 89 | |
| 90 | if list.IndexFunc(test.f) != test.expected { |
| 91 | t.Errorf("IndexFunc(%v, func) = %v, expected %v", test.slice, list.IndexFunc(test.f), test.expected) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestContains(t *testing.T) { |
| 97 | tests := []struct { |
| 98 | slice []int |
| 99 | value int |
| 100 | expected bool |
| 101 | }{ |
| 102 | {[]int{}, 1, false}, |
| 103 | {[]int{1}, 1, true}, |
| 104 | {[]int{1, 1}, 1, true}, |
| 105 | {[]int{1, 1}, 2, false}, |
| 106 | {[]int{1, 2, 3}, 2, true}, |
| 107 | {[]int{1, 2, 3}, 3, true}, |
| 108 | } |
| 109 | for _, test := range tests { |
| 110 | list := NewComparableFromSlice(test.slice) |
| 111 | |
| 112 | if list.Contains(test.value) != test.expected { |
| 113 | t.Errorf("Contains(%v, %v) = %v, expected %v", test.slice, test.value, list.Contains(test.value), test.expected) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestContainsFunc(t *testing.T) { |
| 119 | tests := []struct { |
| 120 | slice []int |
| 121 | f func(value int) bool |
| 122 | expected bool |
| 123 | }{ |
| 124 | {[]int{}, func(value int) bool { return true }, false}, |
| 125 | {[]int{1}, func(value int) bool { return true }, true}, |
| 126 | {[]int{1, 1}, func(value int) bool { return true }, true}, |
| 127 | {[]int{1, 1}, func(value int) bool { return false }, false}, |
| 128 | {[]int{1, 2, 3}, func(value int) bool { return value == 2 }, true}, |
| 129 | {[]int{1, 2, 3}, func(value int) bool { return value == 3 }, true}, |
| 130 | } |
| 131 | for _, test := range tests { |
| 132 | list := NewComparableFromSlice(test.slice) |
| 133 | |
| 134 | if list.ContainsFunc(test.f) != test.expected { |
| 135 | t.Errorf("ContainsFunc(%v, func) = %v, expected %v", test.slice, list.ContainsFunc(test.f), test.expected) |
| 136 | } |
| 137 | } |
| 138 | } |