5
5
package mat32
6
6
7
7
import (
8
- "github.com/stretchr/testify/assert"
8
+ "fmt"
9
+ "reflect"
9
10
"testing"
11
+
12
+ "github.com/stretchr/testify/assert"
10
13
)
11
14
12
15
func TestDense_AddScalar (t * testing.T ) {
@@ -1277,6 +1280,141 @@ func TestDense_DoNonZero(t *testing.T) {
1277
1280
})
1278
1281
}
1279
1282
1283
+ func TestResizeVector (t * testing.T ) {
1284
+ t .Parallel ()
1285
+
1286
+ cases := []struct {
1287
+ x []Float
1288
+ size int
1289
+ expected []Float
1290
+ }{
1291
+ {[]Float {}, 0 , []Float {}},
1292
+ {[]Float {}, 1 , []Float {0 }},
1293
+ {[]Float {}, 2 , []Float {0 , 0 }},
1294
+
1295
+ {[]Float {1 }, 0 , []Float {}},
1296
+ {[]Float {1 }, 1 , []Float {1 }},
1297
+ {[]Float {1 }, 2 , []Float {1 , 0 }},
1298
+ {[]Float {1 }, 3 , []Float {1 , 0 , 0 }},
1299
+
1300
+ {[]Float {1 , 2 }, 0 , []Float {}},
1301
+ {[]Float {1 , 2 }, 1 , []Float {1 }},
1302
+ {[]Float {1 , 2 }, 2 , []Float {1 , 2 }},
1303
+ {[]Float {1 , 2 }, 3 , []Float {1 , 2 , 0 }},
1304
+ {[]Float {1 , 2 }, 4 , []Float {1 , 2 , 0 , 0 }},
1305
+ }
1306
+
1307
+ for _ , c := range cases {
1308
+ testName := fmt .Sprintf ("ResizeVector(%#v, %d) == %#v" , c .x , c .size , c .expected )
1309
+ t .Run (testName , func (t * testing.T ) {
1310
+ x := NewVecDense (c .x )
1311
+ y := x .ResizeVector (c .size )
1312
+ if ! reflect .DeepEqual (y .Data (), c .expected ) {
1313
+ t .Fatalf ("expected %#v, actual %#v" , c .expected , y .Data ())
1314
+ }
1315
+ if ! reflect .DeepEqual (x .Data (), c .x ) {
1316
+ t .Fatalf ("the input vector was modified: %#v" , x .Data ())
1317
+ }
1318
+ })
1319
+ }
1320
+ }
1321
+
1322
+ func TestPadColumns (t * testing.T ) {
1323
+ t .Parallel ()
1324
+
1325
+ cases := []struct {
1326
+ x Matrix
1327
+ cols int
1328
+ expected Matrix
1329
+ }{
1330
+ {NewDense (0 , 0 , []Float {}), 0 , NewDense (0 , 0 , []Float {})},
1331
+ {NewDense (0 , 0 , []Float {}), 1 , NewDense (0 , 1 , []Float {})},
1332
+ {NewDense (0 , 0 , []Float {}), 2 , NewDense (0 , 2 , []Float {})},
1333
+
1334
+ {NewDense (0 , 1 , []Float {}), 0 , NewDense (0 , 1 , []Float {})},
1335
+ {NewDense (0 , 1 , []Float {}), 1 , NewDense (0 , 2 , []Float {})},
1336
+ {NewDense (0 , 1 , []Float {}), 2 , NewDense (0 , 3 , []Float {})},
1337
+
1338
+ {NewDense (1 , 0 , []Float {}), 0 , NewDense (1 , 0 , []Float {})},
1339
+ {NewDense (1 , 0 , []Float {}), 1 , NewDense (1 , 1 , []Float {0 })},
1340
+ {NewDense (1 , 0 , []Float {}), 2 , NewDense (1 , 2 , []Float {0 , 0 })},
1341
+
1342
+ {NewDense (1 , 1 , []Float {1 }), 0 , NewDense (1 , 1 , []Float {1 })},
1343
+ {NewDense (1 , 1 , []Float {1 }), 1 , NewDense (1 , 2 , []Float {1 , 0 })},
1344
+ {NewDense (1 , 1 , []Float {1 }), 2 , NewDense (1 , 3 , []Float {1 , 0 , 0 })},
1345
+
1346
+ {NewDense (2 , 1 , []Float {1 , 2 }), 0 , NewDense (2 , 1 , []Float {1 , 2 })},
1347
+ {NewDense (2 , 1 , []Float {1 , 2 }), 1 , NewDense (2 , 2 , []Float {1 , 0 , 2 , 0 })},
1348
+ {NewDense (2 , 1 , []Float {1 , 2 }), 2 , NewDense (2 , 3 , []Float {1 , 0 , 0 , 2 , 0 , 0 })},
1349
+
1350
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 0 , NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 })},
1351
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 1 , NewDense (2 , 3 , []Float {1 , 2 , 0 , 3 , 4 , 0 })},
1352
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 2 , NewDense (2 , 4 , []Float {1 , 2 , 0 , 0 , 3 , 4 , 0 , 0 })},
1353
+ }
1354
+
1355
+ for _ , c := range cases {
1356
+ testName := fmt .Sprintf ("PadColumns(%.0f, %d) == %.0f" , c .x , c .cols , c .expected )
1357
+ t .Run (testName , func (t * testing.T ) {
1358
+ xCopy := c .x .Clone ()
1359
+ y := c .x .(* Dense ).PadColumns (c .cols )
1360
+ if ! matrixEqual (y , c .expected ) {
1361
+ t .Fatalf ("expected %.0f, actual %.0f" , c .expected , y )
1362
+ }
1363
+ if ! matrixEqual (c .x , xCopy ) {
1364
+ t .Fatalf ("the input vector was modified: %.0f" , c .x )
1365
+ }
1366
+ })
1367
+ }
1368
+ }
1369
+
1370
+ func TestPadRows (t * testing.T ) {
1371
+ t .Parallel ()
1372
+
1373
+ cases := []struct {
1374
+ x Matrix
1375
+ cols int
1376
+ expected Matrix
1377
+ }{
1378
+ {NewDense (0 , 0 , []Float {}), 0 , NewDense (0 , 0 , []Float {})},
1379
+ {NewDense (0 , 0 , []Float {}), 1 , NewDense (1 , 0 , []Float {})},
1380
+ {NewDense (0 , 0 , []Float {}), 2 , NewDense (2 , 0 , []Float {})},
1381
+
1382
+ {NewDense (1 , 0 , []Float {}), 0 , NewDense (1 , 0 , []Float {})},
1383
+ {NewDense (1 , 0 , []Float {}), 1 , NewDense (2 , 0 , []Float {})},
1384
+ {NewDense (1 , 0 , []Float {}), 2 , NewDense (3 , 0 , []Float {})},
1385
+
1386
+ {NewDense (0 , 1 , []Float {}), 0 , NewDense (0 , 1 , []Float {})},
1387
+ {NewDense (0 , 1 , []Float {}), 1 , NewDense (1 , 1 , []Float {0 })},
1388
+ {NewDense (0 , 1 , []Float {}), 2 , NewDense (2 , 1 , []Float {0 , 0 })},
1389
+
1390
+ {NewDense (1 , 1 , []Float {1 }), 0 , NewDense (1 , 1 , []Float {1 })},
1391
+ {NewDense (1 , 1 , []Float {1 }), 1 , NewDense (2 , 1 , []Float {1 , 0 })},
1392
+ {NewDense (1 , 1 , []Float {1 }), 2 , NewDense (3 , 1 , []Float {1 , 0 , 0 })},
1393
+
1394
+ {NewDense (1 , 2 , []Float {1 , 2 }), 0 , NewDense (1 , 2 , []Float {1 , 2 })},
1395
+ {NewDense (1 , 2 , []Float {1 , 2 }), 1 , NewDense (2 , 2 , []Float {1 , 2 , 0 , 0 })},
1396
+ {NewDense (1 , 2 , []Float {1 , 2 }), 2 , NewDense (3 , 2 , []Float {1 , 2 , 0 , 0 , 0 , 0 })},
1397
+
1398
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 0 , NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 })},
1399
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 1 , NewDense (3 , 2 , []Float {1 , 2 , 3 , 4 , 0 , 0 })},
1400
+ {NewDense (2 , 2 , []Float {1 , 2 , 3 , 4 }), 2 , NewDense (4 , 2 , []Float {1 , 2 , 3 , 4 , 0 , 0 , 0 , 0 })},
1401
+ }
1402
+
1403
+ for _ , c := range cases {
1404
+ testName := fmt .Sprintf ("PadRows(%.0f, %d) == %.0f" , c .x , c .cols , c .expected )
1405
+ t .Run (testName , func (t * testing.T ) {
1406
+ xCopy := c .x .Clone ()
1407
+ y := c .x .(* Dense ).PadRows (c .cols )
1408
+ if ! matrixEqual (y , c .expected ) {
1409
+ t .Fatalf ("expected %.0f, actual %.0f" , c .expected , y )
1410
+ }
1411
+ if ! matrixEqual (c .x , xCopy ) {
1412
+ t .Fatalf ("the input vector was modified: %.0f" , c .x )
1413
+ }
1414
+ })
1415
+ }
1416
+ }
1417
+
1280
1418
func TestDense_String (t * testing.T ) {
1281
1419
d := NewVecDense ([]Float {1 , 2 , 3 })
1282
1420
assert .Equal (t , "[1 2 3]" , d .String ())
@@ -1291,3 +1429,7 @@ func assertSliceEqualApprox(t *testing.T, expected, actual []Float) {
1291
1429
t .Helper ()
1292
1430
assert .InDeltaSlice (t , expected , actual , 1.0e-04 )
1293
1431
}
1432
+
1433
+ func matrixEqual (a , b Matrix ) bool {
1434
+ return SameDims (a , b ) && reflect .DeepEqual (a .Data (), b .Data ())
1435
+ }
0 commit comments