github.com/qiaogw/arrgo@v0.0.8/numeric_arrf_test.go (about) 1 package arrgo 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestArrayCond1(t *testing.T) { 9 arr := Array(nil) 10 if SameFloat64Slice(arr.Data, []float64{}) != true { 11 t.Error("array Data should be []float64{}, got ", arr.Data) 12 } 13 if SameIntSlice(arr.Shape, []int{0}) != true { 14 t.Error("array Shape should be []int{0}, got ", arr.Shape) 15 } 16 if SameIntSlice(arr.Strides, []int{0, 1}) != true { 17 t.Error("array Strides should be []int{0, 1}, got ", arr.Shape) 18 } 19 } 20 21 func TestArrayCond2(t *testing.T) { 22 arr := Array([]float64{1, 2, 3}) 23 if SameFloat64Slice(arr.Data, []float64{1, 2, 3}) != true { 24 t.Error("array Data should be []float64{1,2,3}, got ", arr.Data) 25 } 26 if SameIntSlice(arr.Shape, []int{3}) != true { 27 t.Error("array Shape should be []int{3}, got ", arr.Shape) 28 } 29 if SameIntSlice(arr.Strides, []int{3, 1}) != true { 30 t.Error("array Strides should be []int{3, 1}, got ", arr.Shape) 31 } 32 } 33 34 func TestArrayCond3ExceptionTwoNegtiveDims(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 Array([]float64{1, 2, 3, 4}, -1, -1, 4) 43 } 44 45 func TestArrayCond3ExceptionLengError(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 Array([]float64{1, 2, 3, 4}, 3, 4, 5) 54 } 55 56 func TestArrayCond3ExceptionDivError(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 Array([]float64{1, 2, 3, 4}, -1, 3) 65 } 66 67 func TestArrayCond3(t *testing.T) { 68 arr := Array([]float64{1, 2, 3, 4}, 2, 2) 69 if !SameIntSlice(arr.Shape, []int{2, 2}) { 70 t.Error("Expected [2, 2], 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 !SameFloat64Slice(arr.Data, []float64{1, 2, 3, 4}) { 76 t.Error("Expected [1,2,3,4], got ", arr.Data) 77 } 78 79 arr = Array([]float64{1, 2, 3, 4}, 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 !SameFloat64Slice(arr.Data, []float64{1, 2, 3, 4}) { 87 t.Error("Expected [1,2,3,4], got ", arr.Data) 88 } 89 } 90 91 func TestArrayCond4(t *testing.T) { 92 arr := Array(nil, 2, 3) 93 if SameFloat64Slice(arr.Data, []float64{0, 0, 0, 0, 0, 0}) != true { 94 t.Error("array Data should be []float64{0, 0, 0, 0, 0, 0}, got ", arr.Data) 95 } 96 if SameIntSlice(arr.Shape, []int{2, 3}) != true { 97 t.Error("array Shape should be []int{2, 3}, got ", arr.Shape) 98 } 99 if SameIntSlice(arr.Strides, []int{6, 3, 1}) != true { 100 t.Error("array 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 Array(nil, -1, 2, 3) 111 } 112 113 func TestArange(t *testing.T) { 114 a1 := Arange(3) 115 if !a1.Equal(Array([]float64{0, 1, 2})).AllTrues() { 116 t.Error("Expected [0, 1, 2], got ", a1) 117 } 118 119 a1 = Arange(-3) 120 if !a1.Equal(Array([]float64{0, -1, -2})).AllTrues() { 121 t.Error("Expected [0, -1, -2], got ", a1) 122 } 123 124 a1 = Arange(1, 3) 125 if !a1.Equal(Array([]float64{1, 2})).AllTrues() { 126 t.Error("Expected [1,2], got ", a1) 127 } 128 129 a1 = Arange(-1, 2) 130 if !a1.Equal(Array([]float64{-1, 0, 1})).AllTrues() { 131 t.Error("Expected [-1, 0, 1], got ", a1) 132 } 133 134 a1 = Arange(2, -1) 135 if !a1.Equal(Array([]float64{2, 1, 0})).AllTrues() { 136 t.Error("Expected [2, 1, 0], got ", a1) 137 } 138 139 a1 = Arange(1, 4, 2) 140 if !a1.Equal(Array([]float64{1, 3})).AllTrues() { 141 t.Error("Expected [1, 3], got ", a1) 142 } 143 144 a1 = Arange(4, -1, -2) 145 if !a1.Equal(Array([]float64{4, 2, 0})).AllTrues() { 146 t.Error("Expected [4, 2, 0], got ", a1) 147 } 148 } 149 150 func TestArangeIncrementExpection1(t *testing.T) { 151 defer func() { 152 r := recover() 153 if r != PARAMETER_ERROR { 154 t.Error("Expected PARAMTER ERROR, got ", r) 155 } 156 }() 157 158 Arange(1, 3, -2) 159 } 160 161 func TestArangeIncrementExpection2(t *testing.T) { 162 defer func() { 163 r := recover() 164 if r != PARAMETER_ERROR { 165 t.Error("Expected PARAMTER ERROR, got ", r) 166 } 167 }() 168 169 Arange(3, 1, 1) 170 } 171 172 func TestArangeNullParameterException(t *testing.T) { 173 defer func() { 174 r := recover() 175 if r != PARAMETER_ERROR { 176 t.Error("Expected PARAMETER ERROR, got ", r) 177 } 178 }() 179 180 Arange() 181 } 182 183 func TestArrf_IsEmpty(t *testing.T) { 184 empty := Array(nil) 185 186 if empty.IsEmpty() != true { 187 t.Error("Expected empty arra") 188 } 189 190 empty.Data = make([]float64, 0) 191 192 if empty.IsEmpty() != true { 193 t.Error("Expected empty arra") 194 } 195 } 196 197 func TestFill(t *testing.T) { 198 arr := Fill(1.0, 3) 199 200 if !SameIntSlice(arr.Shape, []int{3}) { 201 t.Error("Expected [3], got ", arr.Shape) 202 } 203 204 if !SameIntSlice(arr.Strides, []int{3, 1}) { 205 t.Error("Expected [3, 1], got ", arr.Strides) 206 } 207 208 if !SameFloat64Slice(arr.Data, []float64{1.0, 1.0, 1.0}) { 209 t.Error("Expected [1.0, 1.0, 1.0], got ", arr.Data) 210 } 211 } 212 213 func TestFillException(t *testing.T) { 214 defer func() { 215 r := recover() 216 217 if r != SHAPE_ERROR { 218 t.Error("Expected SHAPE_ERROR, got ", r) 219 } 220 }() 221 222 Fill(1.0) 223 } 224 225 func TestOnes(t *testing.T) { 226 arr := Ones(3) 227 228 if !SameIntSlice(arr.Shape, []int{3}) { 229 t.Error("Expected [3], got ", arr.Shape) 230 } 231 232 if !SameIntSlice(arr.Strides, []int{3, 1}) { 233 t.Error("Expected [3, 1], got ", arr.Strides) 234 } 235 236 if !SameFloat64Slice(arr.Data, []float64{1, 1, 1}) { 237 t.Error("Expected [1, 1, 1], got ", arr.Data) 238 } 239 } 240 241 func TestOnesLike(t *testing.T) { 242 originalArr := Ones(3) 243 arr := OnesLike(originalArr) 244 245 if !SameIntSlice(arr.Shape, []int{3}) { 246 t.Error("Expected [3], got ", arr.Shape) 247 } 248 249 if !SameIntSlice(arr.Strides, []int{3, 1}) { 250 t.Error("Expected [3, 1], got ", arr.Strides) 251 } 252 253 if !SameFloat64Slice(arr.Data, []float64{1, 1, 1}) { 254 t.Error("Expected [1, 1, 1], got ", arr.Data) 255 } 256 } 257 258 func TestZeros(t *testing.T) { 259 arr := Zeros(3) 260 261 if !SameIntSlice(arr.Shape, []int{3}) { 262 t.Error("Expected [3], got ", arr.Shape) 263 } 264 265 if !SameIntSlice(arr.Strides, []int{3, 1}) { 266 t.Error("Expected [3, 1], got ", arr.Strides) 267 } 268 269 if !SameFloat64Slice(arr.Data, []float64{0, 0, 0}) { 270 t.Error("Expected [0,0,0], got ", arr.Data) 271 } 272 } 273 274 func TestZerosLike(t *testing.T) { 275 orignalArr := Zeros(3) 276 arr := ZerosLike(orignalArr) 277 278 if !SameIntSlice(arr.Shape, []int{3}) { 279 t.Error("Expected [3], got ", arr.Shape) 280 } 281 282 if !SameIntSlice(arr.Strides, []int{3, 1}) { 283 t.Error("Expected [3, 1], got ", arr.Strides) 284 } 285 286 if !SameFloat64Slice(arr.Data, []float64{0, 0, 0}) { 287 t.Error("Expected [0,0,0], got ", arr.Data) 288 } 289 } 290 291 func TestArrf_At(t *testing.T) { 292 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 293 294 if arr.At(0, 1) != 2.0 { 295 t.Error("Expected 2.0, got ", arr.At(1, 0)) 296 } 297 298 if arr.At(0) != 1.0 { 299 t.Error("Expected 1.0, got ", arr.At(0)) 300 } 301 302 if arr.At(1) != 4.0 { 303 t.Error("Expected 4.0, got ", arr.At(1.0)) 304 } 305 } 306 307 func TestArrf_AtLongIndexException(t *testing.T) { 308 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 309 310 defer func() { 311 r := recover() 312 if r != INDEX_ERROR { 313 t.Error("Expected INDEX_ERROR, got ", r) 314 } 315 }() 316 317 arr.At(0, 0, 1) 318 } 319 320 func TestArrf_AtIndexOutofRangeException(t *testing.T) { 321 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 322 323 defer func() { 324 r := recover() 325 if r != INDEX_ERROR { 326 t.Error("Expected INDEX_ERROR, got ", r) 327 } 328 }() 329 330 arr.At(2, 0) 331 } 332 333 func TestArrf_ValIndex(t *testing.T) { 334 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 335 336 index := arr.valIndex(0, 1) 337 if index != 1 { 338 t.Error("Expected 1, got ", index) 339 } 340 341 index = arr.valIndex(0) 342 if index != 0 { 343 t.Error("Expected 0, got ", index) 344 } 345 346 index = arr.valIndex(1) 347 if index != 3 { 348 t.Error("Expected 3, got ", index) 349 } 350 } 351 352 func TestArrf_ValIndexExpection1(t *testing.T) { 353 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 354 defer func() { 355 r := recover() 356 if r != INDEX_ERROR { 357 t.Error("Expected INDEX_ERROR, got ", r) 358 } 359 }() 360 arr.valIndex(0, 1, 0) 361 } 362 363 func TestArrf_ValIndexExpection2(t *testing.T) { 364 arr := Array([]float64{1, 2, 3, 4, 5, 6}, 2, 3) 365 defer func() { 366 r := recover() 367 if r != INDEX_ERROR { 368 t.Error("Expected INDEX_ERROR, got ", r) 369 } 370 }() 371 arr.valIndex(2) 372 } 373 374 func TestArrf_Length(t *testing.T) { 375 arr := Array(nil, 2, 3) 376 377 if arr.Length() != 6 { 378 t.Error("Expected 6, got ", arr.Length()) 379 } 380 } 381 382 func TestEye(t *testing.T) { 383 arr := Eye(2) 384 385 if !arr.Equal(Array([]float64{1, 0, 0, 1}, 2, 2)).AllTrues() { 386 t.Error("Expected [1, 0, 0, 1], got ", arr) 387 } 388 389 defer func() { 390 r := recover() 391 if r != SHAPE_ERROR { 392 t.Error("Expected SHAPE_ERROR, got ", r) 393 } 394 }() 395 396 Eye(0) 397 } 398 399 func TestIdentity(t *testing.T) { 400 arr := Identity(2) 401 402 if !arr.Equal(Array([]float64{1, 0, 0, 1}, 2, 2)).AllTrues() { 403 t.Error("Expected [1, 0, 0, 1], got ", arr) 404 } 405 406 defer func() { 407 r := recover() 408 if r != SHAPE_ERROR { 409 t.Error("Expected SHAPE_ERROR, got ", r) 410 } 411 }() 412 413 Eye(0) 414 } 415 416 func TestArrf_Set(t *testing.T) { 417 arr := Zeros(3) 418 arr.Set(10, 1) 419 420 if arr.Get(1) != 10 { 421 t.Error("Expected 10, got ", arr.Get(10)) 422 } 423 } 424 425 func TestArrf_Values(t *testing.T) { 426 arr := Array([]float64{1, 2, 3}) 427 428 values := arr.Values() 429 430 if !SameFloat64Slice(values, []float64{1, 2, 3}) { 431 t.Error("Expected [1.0, 2.0, 3.0], got ", values) 432 } 433 values[0] = 100 434 435 if arr.Data[0] != 100 { 436 t.Error("Expected 100, got ", arr.Data[0]) 437 } 438 } 439 440 func TestLinspace(t *testing.T) { 441 arr := Linspace(1, 2, 5) 442 443 if !arr.Equal(Array([]float64{1, 1.25, 1.5, 1.75, 2})).AllTrues() { 444 t.Error("Expected [1, 1.25, 1.5, 1.75, 2], got ", arr) 445 } 446 447 arr = Linspace(2, 1, 5) 448 449 if !arr.Equal(Array([]float64{2, 1.75, 1.5, 1.25, 1})).AllTrues() { 450 t.Error("Expected [2, 1.75, 1.5, 1.25, 1], got ", arr) 451 } 452 453 arr = Linspace(-2, -1, 5) 454 455 if !arr.Equal(Array([]float64{-2, -1.75, -1.5, -1.25, -1})).AllTrues() { 456 t.Error("Expected [-2, -1.75, -1.5, -1.25, -1], got ", arr) 457 } 458 459 arr = Linspace(-1, -2, 5) 460 461 if !arr.Equal(Array([]float64{-1, -1.25, -1.5, -1.75, -2})).AllTrues() { 462 t.Error("Expected [-1, -1.25, -1.5, -1.75, -2], got ", arr) 463 } 464 465 arr = Linspace(-1, 2, 5) 466 467 if !arr.Equal(Array([]float64{-1, -0.25, 0.5, 1.25, 2})).AllTrues() { 468 t.Error("Expected [-1, -0.25, 0.5, 1.25, 2], got ", arr) 469 } 470 } 471 472 func TestArrf_Copy(t *testing.T) { 473 arr := Ones(2) 474 arrCopy := arr.Copy() 475 arr.Set(10, 0) 476 477 if !arrCopy.Equal(Array([]float64{1, 1})).AllTrues() { 478 t.Error("Expected [1, 1], got ", arrCopy) 479 } 480 } 481 482 func TestArrf_Ndims(t *testing.T) { 483 arr := Arange(10) 484 if arr.Ndims() != 1 { 485 t.Error("Expected 1, got ", arr.Ndims()) 486 } 487 488 arr.ReShape(2, 5) 489 if arr.Ndims() != 2 { 490 t.Error("Expected 2, got ", arr.Ndims()) 491 } 492 493 arr.ReShape(2, 5, 1) 494 if arr.Ndims() != 3 { 495 t.Error("Expected 3, got ", arr.Ndims()) 496 } 497 } 498 499 func TestArrf_Transpose(t *testing.T) { 500 arr := Arange(4).ReShape(2, 2) 501 502 if !arr.Equal(Array([]float64{0, 1, 2, 3}, 2, 2)).AllTrues() { 503 t.Error("Expected [[0,1],[2,3]], got ", arr) 504 } 505 506 arrTransposed := arr.Transpose() 507 if !arrTransposed.Equal(Array([]float64{0, 2, 1, 3}, 2, 2)).AllTrues() { 508 t.Error("Expected [[0,2,], [1,3]], got ", arrTransposed) 509 } 510 511 arrTransposed = arr.Transpose(1, 0) 512 if !arrTransposed.Equal(Array([]float64{0, 2, 1, 3}, 2, 2)).AllTrues() { 513 t.Error("Expected [[0,2,], [1,3]], got ", arrTransposed) 514 } 515 } 516 517 func TestArrf_TransposeException(t *testing.T) { 518 arr := Arange(4) 519 520 defer func() { 521 r := recover() 522 if r != DIMENTION_ERROR { 523 t.Error("Expected DIMENTION_ERROR, got ", r) 524 } 525 }() 526 arr.Transpose(0, 1) 527 } 528 529 func TestArrf_String(t *testing.T) { 530 var arr *Arrf 531 if arr.String() != "<nil>" { 532 t.Error("Expected <nil>, got ", arr.String()) 533 } 534 535 arr = Zeros(2) 536 arr.Data = nil 537 if arr.String() != "<nil>" { 538 t.Error("Expected <nil> got ", arr.String()) 539 } 540 541 arr = Array(nil, 1) 542 arr.Strides = make([]int, 2) 543 if arr.String() != "[]" { 544 t.Error("Expected [], got ", arr.String()) 545 } 546 547 arr = Arange(2) 548 if arr.String() != "[0 1]" { 549 t.Error("Expected [0 1], got ", arr.String()) 550 } 551 552 arr = Arange(2).ReShape(2, 1) 553 if strings.Replace(arr.String(), "\n", ":", -1) != "[[0] : [1]]" { 554 t.Error("Expected , got ", arr.String()) 555 } 556 }