github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zarray/slice_test.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package zarray_test 5 6 import ( 7 "testing" 8 9 "github.com/sohaha/zlsgo" 10 "github.com/sohaha/zlsgo/zarray" 11 "github.com/sohaha/zlsgo/ztype" 12 ) 13 14 var l = []int{0, 1, 2, 3, 4, 5} 15 var l2 = []int{0, 1, 2, 3, 4, 5, 2, 34, 5, 6, 7, 98, 6, 67, 54, 543, 345, 435, 43543, 435, 3, 2, 42, 3423, 54, 6, 5} 16 17 func TestShuffle(t *testing.T) { 18 t.Log(zarray.Shuffle(l)) 19 t.Log(zarray.Shuffle(l2)) 20 } 21 22 func TestRand(t *testing.T) { 23 t.Log(zarray.Rand(l)) 24 } 25 26 func TestReverse(t *testing.T) { 27 t.Log(zarray.Reverse(l)) 28 } 29 30 func TestFilter(t *testing.T) { 31 tt := zlsgo.NewTest(t) 32 nl := zarray.Filter(l, func(index int, item int) bool { 33 t.Log(index, item) 34 return item%2 == 0 35 }) 36 tt.Equal([]int{0, 2, 4}, nl) 37 } 38 39 func TestMap(t *testing.T) { 40 tt := zlsgo.NewTest(t) 41 s := []int{1, 2, 3} 42 nl := zarray.Map(s, func(i int, v int) string { 43 return ztype.ToString(v) + "//" 44 }) 45 tt.Equal([]string{"1//", "2//", "3//"}, nl) 46 } 47 48 func TestParallelMap(t *testing.T) { 49 tt := zlsgo.NewTest(t) 50 expected := zarray.Map(l2, func(i int, v int) string { 51 return ztype.ToString(v) + "//" 52 }) 53 54 actual := zarray.ParallelMap(l2, func(i int, v int) string { 55 return ztype.ToString(v) + "//" 56 }, uint(len(l2)+1)) 57 tt.Equal(expected, actual) 58 } 59 60 func TestDiff(t *testing.T) { 61 tt := zlsgo.NewTest(t) 62 63 n1, n2 := zarray.Diff(l2, l) 64 65 t.Log(l2, l) 66 t.Log(n1, n2) 67 tt.Equal([]int{34, 6, 7, 98, 6, 67, 54, 543, 345, 435, 43543, 435, 42, 3423, 54, 6}, n1) 68 tt.Equal([]int{}, n2) 69 } 70 71 func TestPop(t *testing.T) { 72 tt := zlsgo.NewTest(t) 73 74 l1 := []int{0, 1, 2, 3, 4, 5} 75 76 tt.Equal(5, zarray.Pop(&l1)) 77 tt.Equal(4, zarray.Pop(&l1)) 78 79 tt.Equal([]int{0, 1, 2, 3}, l1) 80 } 81 82 func TestShift(t *testing.T) { 83 tt := zlsgo.NewTest(t) 84 85 l1 := []int{0, 1, 2, 3, 4, 5} 86 87 tt.Equal(0, zarray.Shift(&l1)) 88 tt.Equal(1, zarray.Shift(&l1)) 89 90 tt.Equal([]int{2, 3, 4, 5}, l1) 91 } 92 93 func TestContains(t *testing.T) { 94 tt := zlsgo.NewTest(t) 95 tt.EqualTrue(!zarray.Contains(l, 54)) 96 tt.EqualTrue(!zarray.Contains(l, 6)) 97 tt.EqualTrue(zarray.Contains(l2, 5)) 98 tt.EqualTrue(zarray.Contains(l2, 6)) 99 tt.EqualTrue(zarray.Contains(l2, 54)) 100 } 101 102 func TestUnique(t *testing.T) { 103 tt := zlsgo.NewTest(t) 104 a := append(l, l2...) 105 unia := zarray.Unique(a) 106 tt.Equal(18, len(unia)) 107 tt.EqualTrue(len(a) != len(unia)) 108 t.Log(unia) 109 } 110 111 func TestFind(t *testing.T) { 112 tt := zlsgo.NewTest(t) 113 a := []map[string]string{ 114 {"name": "a"}, 115 {"name": "b"}, 116 {"name": "c"}, 117 } 118 119 v, ok := zarray.Find(a, func(_ int, v map[string]string) bool { 120 return v["name"] == "b" 121 }) 122 tt.EqualTrue(ok) 123 tt.Equal("b", v["name"]) 124 125 v, ok = zarray.Find(a, func(_ int, v map[string]string) bool { 126 return v["name"] == "z" 127 }) 128 tt.EqualTrue(!ok) 129 tt.Equal("", v["name"]) 130 }