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