|
628 | 628 | @test n1 == n2
|
629 | 629 | end
|
630 | 630 |
|
| 631 | + @testset "Replace" begin |
| 632 | + a = [3, 2, 1, 4, 5, 3] |
| 633 | + b = [2, 4, 4, 5, 8, 5] |
| 634 | + c = [1, 1, 6, 2, 4, 1] |
| 635 | + d = [4, 3, 7, 5, 4, 1] |
| 636 | + e = [5, 5, 2, 6, 5, 2] |
| 637 | + f = [4, 4, 3, 4, 5, 2] |
| 638 | + t = Table(; a, b, c, d, e, f) |
| 639 | + |
| 640 | + # replace with a value of the same type |
| 641 | + T = Replace(1 => -1, 5 => -5) |
| 642 | + n, c = apply(T, t) |
| 643 | + @test n.a == [3, 2, -1, 4, -5, 3] |
| 644 | + @test n.b == [2, 4, 4, -5, 8, -5] |
| 645 | + @test n.c == [-1, -1, 6, 2, 4, -1] |
| 646 | + @test n.d == [4, 3, 7, -5, 4, -1] |
| 647 | + @test n.e == [-5, -5, 2, 6, -5, 2] |
| 648 | + @test n.f == [4, 4, 3, 4, -5, 2] |
| 649 | + @test isrevertible(T) == true |
| 650 | + tₒ = revert(T, n, c) |
| 651 | + @test t == tₒ |
| 652 | + |
| 653 | + # table schema after apply and revert |
| 654 | + T = Replace(1 => -1, 5 => -5) |
| 655 | + n, c = apply(T, t) |
| 656 | + types = Tables.schema(t).types |
| 657 | + @test types == Tables.schema(n).types |
| 658 | + tₒ = revert(T, n, c) |
| 659 | + @test types == Tables.schema(tₒ).types |
| 660 | + |
| 661 | + # replace with a value of another type |
| 662 | + T = Replace(1 => 1.5, 5 => 5.5, 4 => true) |
| 663 | + n, c = apply(T, t) |
| 664 | + @test n.a == Real[3, 2, 1.5, true, 5.5, 3] |
| 665 | + @test n.b == Real[2, true, true, 5.5, 8, 5.5] |
| 666 | + @test n.c == Real[1.5, 1.5, 6, 2, true, 1.5] |
| 667 | + @test n.d == Real[true, 3, 7, 5.5, true, 1.5] |
| 668 | + @test n.e == Real[5.5, 5.5, 2, 6, 5.5, 2] |
| 669 | + @test n.f == Real[true, true, 3, true, 5.5, 2] |
| 670 | + tₒ = revert(T, n, c) |
| 671 | + @test t == tₒ |
| 672 | + |
| 673 | + # table schema after apply and revert |
| 674 | + T = Replace(1 => 1.5, 5 => 5.5, 4 => true) |
| 675 | + n, c = apply(T, t) |
| 676 | + tₒ = revert(T, n, c) |
| 677 | + ttypes = Tables.schema(t).types |
| 678 | + ntypes = Tables.schema(n).types |
| 679 | + @test ntypes[1] == Real |
| 680 | + @test ntypes[2] == Real |
| 681 | + @test ntypes[3] == Real |
| 682 | + @test ntypes[4] == Real |
| 683 | + @test ntypes[5] == Real |
| 684 | + @test ntypes[6] == Real |
| 685 | + @test ttypes == Tables.schema(tₒ).types |
| 686 | + |
| 687 | + # no occurrences |
| 688 | + T = Replace(10 => 11, 20 => 30) |
| 689 | + n, c = apply(T, t) |
| 690 | + @test t == n |
| 691 | + tₒ = revert(T, n, c) |
| 692 | + @test t == tₒ |
| 693 | + |
| 694 | + # collumns with diferent types |
| 695 | + a = [3, 2, 1, 4, 5, 3] |
| 696 | + b = [2.5, 4.5, 4.7, 2.5, 2.5, 5.3] |
| 697 | + c = [true, false, false, false, true, false] |
| 698 | + d = ['a', 'b', 'c', 'd', 'e', 'a'] |
| 699 | + t = Table(; a, b, c, d) |
| 700 | + |
| 701 | + T = Replace(3 => -3, 2.5 => 2.0, true => false, 'a' => 'A') |
| 702 | + n, c = apply(T, t) |
| 703 | + @test n.a == [-3, 2, 1, 4, 5, -3] |
| 704 | + @test n.b == [2.0, 4.5, 4.7, 2.0, 2.0, 5.3] |
| 705 | + @test n.c == [false, false, false, false, false, false] |
| 706 | + @test n.d == ['A', 'b', 'c', 'd', 'e', 'A'] |
| 707 | + tₒ = revert(T, n, c) |
| 708 | + @test t == tₒ |
| 709 | + |
| 710 | + # throws |
| 711 | + @test_throws ArgumentError Replace() |
| 712 | + end |
| 713 | + |
631 | 714 | @testset "Coalesce" begin
|
632 | 715 | a = [3, 2, missing, 4, 5, 3]
|
633 | 716 | b = [missing, 4, 4, 5, 8, 5]
|
|
672 | 755 | @test ntypes[6] == Int
|
673 | 756 | @test ttypes == Tables.schema(tₒ).types
|
674 | 757 | end
|
675 |
| - |
| 758 | + |
676 | 759 | @testset "Coerce" begin
|
677 | 760 | x1 = [1.0, 2.0, 3.0, 4.0, 5.0]
|
678 | 761 | x2 = [1.0, 2.0, 3.0, 4.0, 5.0]
|
|
0 commit comments