github.com/searKing/golang/go@v1.2.117/exp/slices/logic_test.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slices_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 slices_ "github.com/searKing/golang/go/exp/slices" 12 ) 13 14 func TestOrFunc(t *testing.T) { 15 tests := []struct { 16 data []int 17 want bool 18 }{ 19 {nil, true}, 20 {[]int{}, true}, 21 {[]int{0}, false}, 22 {[]int{1, 0}, true}, 23 {[]int{1, 2}, true}, 24 {[]int{0, 1, 2}, true}, 25 } 26 for _, tt := range tests { 27 t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) { 28 { 29 got := slices_.OrFunc(tt.data, func(v int) bool { return v != 0 }) 30 if got != tt.want { 31 t.Errorf("and of %v = %t", tt.data, tt.want) 32 t.Errorf(" got %t", got) 33 } 34 } 35 }) 36 } 37 } 38 39 func TestAndFunc(t *testing.T) { 40 tests := []struct { 41 data []int 42 want bool 43 }{ 44 {nil, true}, 45 {[]int{}, true}, 46 {[]int{0}, false}, 47 {[]int{1, 0}, false}, 48 {[]int{1, 2}, true}, 49 {[]int{0, 1, 2}, false}, 50 } 51 for _, tt := range tests { 52 t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) { 53 { 54 got := slices_.AndFunc(tt.data, func(v int) bool { return v != 0 }) 55 if got != tt.want { 56 t.Errorf("and of %v = %t", tt.data, tt.want) 57 t.Errorf(" got %t", got) 58 } 59 } 60 }) 61 } 62 } 63 64 func TestOr(t *testing.T) { 65 tests := []struct { 66 data []int 67 want bool 68 }{ 69 {nil, true}, 70 {[]int{}, true}, 71 {[]int{0}, false}, 72 {[]int{1, 0}, true}, 73 {[]int{1, 2}, true}, 74 {[]int{0, 1, 2}, true}, 75 } 76 for _, tt := range tests { 77 t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) { 78 { 79 got := slices_.Or(tt.data...) 80 if got != tt.want { 81 t.Errorf("and of %v = %t", tt.data, tt.want) 82 t.Errorf(" got %t", got) 83 } 84 } 85 }) 86 } 87 } 88 89 func TestAnd(t *testing.T) { 90 tests := []struct { 91 data []int 92 want bool 93 }{ 94 {nil, true}, 95 {[]int{}, true}, 96 {[]int{0}, false}, 97 {[]int{1, 0}, false}, 98 {[]int{1, 2}, true}, 99 {[]int{0, 1, 2}, false}, 100 } 101 for _, tt := range tests { 102 t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) { 103 { 104 got := slices_.And(tt.data...) 105 if got != tt.want { 106 t.Errorf("and of %v = %t", tt.data, tt.want) 107 t.Errorf(" got %t", got) 108 } 109 } 110 }) 111 } 112 }