github.com/qiaogw/arrgo@v0.0.8/numeric_arrb_test.go (about) 1 package arrgo 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestArrayBCond1(t *testing.T) { 9 arr := ArrayB(nil) 10 if SameBoolSlice(arr.Data, []bool{}) != true { 11 t.Error("ArrayB Data should be []bool{}, got ", arr.Data) 12 } 13 if SameIntSlice(arr.Shape, []int{0}) != true { 14 t.Error("ArrayB Shape should be []int{0}, got ", arr.Shape) 15 } 16 if SameIntSlice(arr.Strides, []int{0, 1}) != true { 17 t.Error("ArrayB Strides should be []int{0, 1}, got ", arr.Shape) 18 } 19 } 20 21 func TestArrayBCond2(t *testing.T) { 22 arr := ArrayB([]bool{true, true, true}) 23 if SameBoolSlice(arr.Data, []bool{true, true, true}) != true { 24 t.Error("ArrayB Data should be []bool{true, true, true}, got ", arr.Data) 25 } 26 if SameIntSlice(arr.Shape, []int{3}) != true { 27 t.Error("ArrayB Shape should be []int{3}, got ", arr.Shape) 28 } 29 if SameIntSlice(arr.Strides, []int{3, 1}) != true { 30 t.Error("ArrayB Strides should be []int{3, 1}, got ", arr.Shape) 31 } 32 } 33 34 func TestArrayBCond3ExceptionTwoNegtiveDims(t *testing.T) { 35 defer func() { 36 r := recover() 37 if r != SHAPE_ERROR { 38 t.Error("Exepcted Shape error, got ", r) 39 } 40 }() 41 42 ArrayB([]bool{true, true, true}, -1, -1, 4) 43 } 44 45 func TestArrayBCond3ExceptionLengError(t *testing.T) { 46 defer func() { 47 r := recover() 48 if r != SHAPE_ERROR { 49 t.Error("Exepcted Shape error, got ", r) 50 } 51 }() 52 53 ArrayB([]bool{true, true, true}, 3, 4, 5) 54 } 55 56 func TestArrayBCond3ExceptionDivError(t *testing.T) { 57 defer func() { 58 r := recover() 59 if r != SHAPE_ERROR { 60 t.Error("Exepcted Shape error, got ", r) 61 } 62 }() 63 64 ArrayB([]bool{true, true, true, true}, -1, 3) 65 } 66 67 func TestArrayBCond3(t *testing.T) { 68 arr := ArrayB([]bool{true, true, true, true}, 2, 2) 69 if !SameIntSlice(arr.Shape, []int{2, 2}) { 70 t.Error("Expected [true, true, true, true], got ", arr.Shape) 71 } 72 if !SameIntSlice(arr.Strides, []int{4, 2, 1}) { 73 t.Error("Expected [4,2,1], got", arr.Strides) 74 } 75 if !SameBoolSlice(arr.Data, []bool{true, true, true, true}) { 76 t.Error("Expected [true, true, true, true], got ", arr.Data) 77 } 78 79 arr = ArrayB([]bool{true, true, true, true}, 2, -1) 80 if !SameIntSlice(arr.Shape, []int{2, 2}) { 81 t.Error("Expected [2, 2], got ", arr.Shape) 82 } 83 if !SameIntSlice(arr.Strides, []int{4, 2, 1}) { 84 t.Error("Expected [4,2,1], got", arr.Strides) 85 } 86 if !SameBoolSlice(arr.Data, []bool{true, true, true, true}) { 87 t.Error("Expected [true, true, true, true], got ", arr.Data) 88 } 89 } 90 91 func TestArrayBCond4(t *testing.T) { 92 arr := ArrayB(nil, 2, 3) 93 if SameBoolSlice(arr.Data, []bool{false, false, false, false, false, false}) != true { 94 t.Error("ArrayB Data should be []bool{false, false, false, false, false, false}, got ", arr.Data) 95 } 96 if SameIntSlice(arr.Shape, []int{2, 3}) != true { 97 t.Error("ArrayB Shape should be []int{2, 3}, got ", arr.Shape) 98 } 99 if SameIntSlice(arr.Strides, []int{6, 3, 1}) != true { 100 t.Error("ArrayB Strides should be []int{6, 3, 1}, got ", arr.Shape) 101 } 102 103 defer func() { 104 err := recover() 105 if err != SHAPE_ERROR { 106 t.Error("should panic Shape error, got ", err) 107 } 108 }() 109 110 ArrayB(nil, -1, 2, 3) 111 } 112 113 func TestFillB(t *testing.T) { 114 arr := FillB(true, 3) 115 116 if !SameIntSlice(arr.Shape, []int{3}) { 117 t.Errorf("Expected [3], got %v", arr.Shape) 118 } 119 120 if !SameIntSlice(arr.Strides, []int{3, 1}) { 121 t.Errorf("Expected [3, 1], got %v", arr.Strides) 122 } 123 124 if !SameBoolSlice(arr.Data, []bool{true, true, true}) { 125 t.Errorf("Expected [true, true, true], got %v", arr.Data) 126 } 127 } 128 129 func TestFillBException(t *testing.T) { 130 defer func() { 131 r := recover() 132 133 if r != SHAPE_ERROR { 134 t.Errorf("Expected SHAPE_ERROR, got %v", r) 135 } 136 }() 137 138 FillB(true) 139 } 140 141 func TestEmptyB(t *testing.T) { 142 arr := EmptyB(3) 143 if !SameBoolSlice(arr.Data, []bool{false, false, false}) { 144 t.Errorf("Expected [false, false, false], got %v", arr.Data) 145 } 146 } 147 148 func TestArrb_AllTrues(t *testing.T) { 149 arr := ArrayB([]bool{true, true}) 150 if arr.AllTrues() != true { 151 t.Errorf("Expected true, got %t", arr.AllTrues()) 152 } 153 154 arr = ArrayB([]bool{true, false}) 155 if arr.AllTrues() != false { 156 t.Errorf("EXepcted false, got %t", arr.AllTrues()) 157 } 158 } 159 160 func TestArrb_AnyTrue(t *testing.T) { 161 arr := ArrayB([]bool{true, true}) 162 if arr.AnyTrue() != true { 163 t.Errorf("Expected true, got %t", arr.AnyTrue()) 164 } 165 166 arr = ArrayB([]bool{true, false}) 167 if arr.AnyTrue() != true { 168 t.Errorf("EXepcted true, got %t", arr.AnyTrue()) 169 } 170 171 arr = ArrayB([]bool{false, false}) 172 if arr.AnyTrue() != false { 173 t.Errorf("EXepcted false, got %t", arr.AnyTrue()) 174 } 175 } 176 177 func TestArrb_String(t *testing.T) { 178 var arr *Arrb 179 180 if arr.String() != "<nil>" { 181 t.Errorf("Expected <nil>, git %s", arr.String()) 182 } 183 184 arr = EmptyB(2) 185 arr.Shape = nil 186 if arr.String() != "<nil>" { 187 t.Errorf("Expected <nil>, git %s", arr.String()) 188 } 189 190 arr = EmptyB(2) 191 arr.Strides = make([]int, 2) 192 if arr.String() != "[]" { 193 t.Errorf("Expected [], got %s", arr.String()) 194 } 195 196 arr = ArrayB([]bool{true, false}, 2, 1) 197 if strings.Replace(arr.String(), "\n", ":", -1) != "[[true] : [false]]" { 198 t.Errorf("Expected [[true]\n[false]], got %s", arr.String()) 199 } 200 } 201 202 func TestArrb_Sum(t *testing.T) { 203 arr := ArrayB([]bool{true, true}) 204 if arr.Sum() != 2 { 205 t.Errorf("Expected 2, got %d", arr.Sum()) 206 } 207 208 arr = ArrayB([]bool{true, false}) 209 if arr.Sum() != 1 { 210 t.Errorf("Expected 1, got %d", arr.Sum()) 211 } 212 213 arr = ArrayB([]bool{false, false}) 214 if arr.Sum() != 0 { 215 t.Errorf("Expected 0, got %d", arr.Sum()) 216 } 217 }