|
1333 | 1333 | @test isordered(tₒ.b) == false
|
1334 | 1334 | end
|
1335 | 1335 |
|
| 1336 | + @testset "Sort" begin |
| 1337 | + a = [5, 3, 1, 2] |
| 1338 | + b = [2, 4, 8, 5] |
| 1339 | + c = [3, 2, 1, 4] |
| 1340 | + d = [4, 3, 7, 5] |
| 1341 | + t = Table(; a, b, c, d) |
| 1342 | + |
| 1343 | + T = Sort(:a) |
| 1344 | + n, c = apply(T, t) |
| 1345 | + @test Tables.schema(t) == Tables.schema(n) |
| 1346 | + @test n.a == [1, 2, 3, 5] |
| 1347 | + @test n.b == [8, 5, 4, 2] |
| 1348 | + @test n.c == [1, 4, 2, 3] |
| 1349 | + @test n.d == [7, 5, 3, 4] |
| 1350 | + @test isrevertible(T) == true |
| 1351 | + tₒ = revert(T, n, c) |
| 1352 | + @test t == tₒ |
| 1353 | + |
| 1354 | + # descending order test |
| 1355 | + T = Sort(:b, rev=true) |
| 1356 | + n, c = apply(T, t) |
| 1357 | + @test Tables.schema(t) == Tables.schema(n) |
| 1358 | + @test n.a == [1, 2, 3, 5] |
| 1359 | + @test n.b == [8, 5, 4, 2] |
| 1360 | + @test n.c == [1, 4, 2, 3] |
| 1361 | + @test n.d == [7, 5, 3, 4] |
| 1362 | + tₒ = revert(T, n, c) |
| 1363 | + @test t == tₒ |
| 1364 | + |
| 1365 | + # random test |
| 1366 | + a = rand(200) |
| 1367 | + b = rand(200) |
| 1368 | + c = rand(200) |
| 1369 | + d = rand(200) |
| 1370 | + t = Table(; a, b, c, d) |
| 1371 | + |
| 1372 | + T = Sort(:c) |
| 1373 | + n, c = apply(T, t) |
| 1374 | + |
| 1375 | + @test Tables.schema(t) == Tables.schema(n) |
| 1376 | + @test issetequal(Tables.rowtable(t), Tables.rowtable(n)) |
| 1377 | + @test issorted(Tables.getcolumn(n, :c)) |
| 1378 | + tₒ = revert(T, n, c) |
| 1379 | + @test t == tₒ |
| 1380 | + |
| 1381 | + # sort by multiple columns |
| 1382 | + a = [-2, -1, -2, 2, 1, -1, 1, 2] |
| 1383 | + b = [-8, -4, 6, 9, 8, 2, 2, -8] |
| 1384 | + c = [-3, 6, 5, 4, -8, -7, -1, -10] |
| 1385 | + t = Table(; a, b, c) |
| 1386 | + |
| 1387 | + T = Sort(1, 2) |
| 1388 | + n, c = apply(T, t) |
| 1389 | + @test n.a == [-2, -2, -1, -1, 1, 1, 2, 2] |
| 1390 | + @test n.b == [-8, 6, -4, 2, 2, 8, -8, 9] |
| 1391 | + @test n.c == [-3, 5, 6, -7, -1, -8, -10, 4] |
| 1392 | + tₒ = revert(T, n, c) |
| 1393 | + @test t == tₒ |
| 1394 | + |
| 1395 | + T = Sort([:a, :c], rev=true) |
| 1396 | + n, c = apply(T, t) |
| 1397 | + @test n.a == [2, 2, 1, 1, -1, -1, -2, -2] |
| 1398 | + @test n.b == [9, -8, 2, 8, -4, 2, 6, -8] |
| 1399 | + @test n.c == [4, -10, -1, -8, 6, -7, 5, -3] |
| 1400 | + tₒ = revert(T, n, c) |
| 1401 | + @test t == tₒ |
| 1402 | + |
| 1403 | + T = Sort(("b", "c"), by=row -> abs.(row)) |
| 1404 | + n, c = apply(T, t) |
| 1405 | + @test n.a == [1, -1, -1, -2, -2, 1, 2, 2] |
| 1406 | + @test n.b == [2, 2, -4, 6, -8, 8, -8, 9] |
| 1407 | + @test n.c == [-1, -7, 6, 5, -3, -8, -10, 4] |
| 1408 | + tₒ = revert(T, n, c) |
| 1409 | + @test t == tₒ |
| 1410 | + |
| 1411 | + # throws: Sort without arguments |
| 1412 | + @test_throws ArgumentError Sort() |
| 1413 | + @test_throws ArgumentError Sort(()) |
| 1414 | + |
| 1415 | + # throws: empty selection |
| 1416 | + @test_throws AssertionError apply(Sort(r"x"), t) |
| 1417 | + @test_throws AssertionError apply(Sort(Symbol[]), t) |
| 1418 | + @test_throws AssertionError apply(Sort(String[]), t) |
| 1419 | + |
| 1420 | + # throws: columns that do not exist in the original table |
| 1421 | + @test_throws AssertionError apply(Sort([:d, :e]), t) |
| 1422 | + @test_throws AssertionError apply(Sort(("d", "e")), t) |
| 1423 | + |
| 1424 | + # Invalid kwarg |
| 1425 | + @test_throws MethodError apply(Sort(:a, :b, test=1), t) |
| 1426 | + end |
| 1427 | + |
1336 | 1428 | @testset "EigenAnalysis" begin
|
1337 | 1429 | # PCA test
|
1338 | 1430 | x = rand(Normal(0, 10), 1500)
|
|
0 commit comments