@@ -974,9 +974,124 @@ end
974
974
end
975
975
976
976
@testset " broadcast" for x in (CategoricalArray (1 : 3 ),
977
- CategoricalArray {Union{Int,Missing}} (1 : 3 ))
977
+ CategoricalArray {Union{Int,Missing}} (1 : 3 ),
978
+ CategoricalArray ([" a" , " b" , " c" ]))
978
979
x[1 : 2 ] .= x[3 ]
979
- @test x == [3 , 3 , 3 ]
980
+ @test x == fill (get (x[3 ]), 3 )
981
+ end
982
+
983
+ @testset " append! ordered=$ordered " for ordered in (false , true )
984
+ @testset " append! String" begin
985
+ a = [" a" , " b" , " c" ]
986
+ x = CategoricalVector {String} (a, ordered= ordered)
987
+
988
+ append! (x, x)
989
+ @test length (x) == 6
990
+ @test x == [" a" , " b" , " c" , " a" , " b" , " c" ]
991
+ @test isordered (x) === ordered
992
+ @test levels (x) == [" a" , " b" , " c" ]
993
+
994
+ b = [" z" ," y" ," x" ]
995
+ y = CategoricalVector {String} (b)
996
+ append! (x, y)
997
+ @test isordered (x) === ordered
998
+ @test length (x) == 9
999
+ @test x == [" a" , " b" , " c" , " a" , " b" , " c" , " z" , " y" , " x" ]
1000
+ @test levels (x) == [" a" , " b" , " c" , " x" , " y" , " z" ]
1001
+
1002
+ z1 = view (CategoricalVector {String} ([" ex1" , " ex2" ]), 1 )
1003
+ z2 = view (CategoricalVector {String} ([" ex3" , " ex4" ]), 1 : 1 )
1004
+ append! (x, z1)
1005
+ append! (x, z2)
1006
+ @test isordered (x) === ordered
1007
+ @test length (x) == 11
1008
+ @test x == [" a" , " b" , " c" , " a" , " b" , " c" , " z" , " y" , " x" , " ex1" , " ex3" ]
1009
+ @test levels (x) == [" a" , " b" , " c" , " x" , " y" , " z" , " ex1" , " ex2" , " ex3" , " ex4" ]
1010
+ end
1011
+
1012
+ @testset " append! Float64" begin
1013
+ a = [- 1.0 , 0.0 , 1.0 ]
1014
+ x = CategoricalVector {Float64} (a, ordered= ordered)
1015
+
1016
+ append! (x, x)
1017
+ @test length (x) == 6
1018
+ @test x == [- 1.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 1.0 ]
1019
+ @test isordered (x) === ordered
1020
+ @test levels (x) == [- 1.0 , 0.0 , 1.0 ]
1021
+
1022
+ b = [2.5 , 3.0 , 3.5 ]
1023
+ y = CategoricalVector {Float64} (b, ordered= ordered)
1024
+ append! (x, y)
1025
+ @test length (x) == 9
1026
+ @test x == [- 1.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 1.0 , 2.5 , 3.0 , 3.5 ]
1027
+ @test isordered (x) === ordered
1028
+ @test levels (x) == [- 1.0 , 0.0 , 1.0 , 2.5 , 3.0 , 3.5 ]
1029
+
1030
+ z1 = view (CategoricalVector {Float64} ([100.0 , 101.0 ]), 1 )
1031
+ z2 = view (CategoricalVector {Float64} ([102.0 , 103.0 ]), 1 : 1 )
1032
+ append! (x, z1)
1033
+ append! (x, z2)
1034
+ @test length (x) == 11
1035
+ @test x == [- 1.0 , 0.0 , 1.0 , - 1.0 , 0.0 , 1.0 , 2.5 , 3.0 , 3.5 , 100.0 , 102.0 ]
1036
+ @test isordered (x) === ordered
1037
+ @test levels (x) == [- 1.0 , 0.0 , 1.0 , 2.5 , 3.0 , 3.5 , 100.0 , 101.0 , 102.0 , 103.0 ]
1038
+ end
1039
+ end
1040
+
1041
+ @testset " append! ordered=$ordered " for ordered in (false , true )
1042
+ cases = ([" b" , " a" , missing ], Union{String, Missing}[" b" , " a" , " b" ])
1043
+ @testset " String, has missing: $(any (ismissing .(a))) " for a in cases
1044
+ x = CategoricalVector {Union{String, Missing}} (a, ordered= ordered)
1045
+
1046
+ append! (x, x)
1047
+ @test x ≅ [a; a]
1048
+ @test levels (x) == [" a" , " b" ]
1049
+ @test isordered (x) === ordered
1050
+ @test length (x) == 6
1051
+
1052
+ b = [" x" ," y" ,missing ]
1053
+ y = CategoricalVector {Union{String, Missing}} (b)
1054
+ append! (x, y)
1055
+ @test length (x) == 9
1056
+ @test isordered (x) === ordered
1057
+ @test levels (x) == [" a" , " b" , " x" , " y" ]
1058
+ @test x ≅ [a; a; b]
1059
+ z1 = view (CategoricalVector {Union{String, Missing}} ([missing , " ex2" ]), 1 )
1060
+ z2 = view (CategoricalVector {Union{String, Missing}} ([" ex3" , " ex4" ]), 1 : 1 )
1061
+ append! (x, z1)
1062
+ append! (x, z2)
1063
+ @test length (x) == 11
1064
+ @test isordered (x) === ordered
1065
+ @test levels (x) == [" a" , " b" , " x" , " y" , " ex2" , " ex3" , " ex4" ]
1066
+ @test x ≅ [a; a; b; missing ; " ex3" ]
1067
+ end
1068
+
1069
+ @testset " Float64" begin
1070
+ a = 0.0 : 0.5 : 1.0
1071
+ x = CategoricalVector {Union{Float64, Missing}} (a, ordered= ordered)
1072
+
1073
+ append! (x, x)
1074
+ @test length (x) == 6
1075
+ @test x == [a; a]
1076
+ @test isordered (x) === ordered
1077
+ @test levels (x) == [0.0 , 0.5 , 1.0 ]
1078
+
1079
+ b = [2.5 , 3.0 , missing ]
1080
+ y = CategoricalVector {Union{Float64, Missing}} (b)
1081
+ append! (x, y)
1082
+ @test length (x) == 9
1083
+ @test x ≅ [a; a; b]
1084
+ @test isordered (x) === ordered
1085
+ @test levels (x) == [0.0 , 0.5 , 1.0 , 2.5 , 3.0 ]
1086
+ z1 = view (CategoricalVector {Union{Float64, Missing}} ([missing , 101.0 ]), 1 )
1087
+ z2 = view (CategoricalVector {Union{Float64, Missing}} ([102.0 , 103.0 ]), 1 : 1 )
1088
+ append! (x, z1)
1089
+ append! (x, z2)
1090
+ @test length (x) == 11
1091
+ @test x ≅ [a; a; b; missing ; 102.0 ]
1092
+ @test isordered (x) === ordered
1093
+ @test levels (x) == [0.0 , 0.5 , 1.0 , 2.5 , 3.0 , 101.0 , 102.0 , 103.0 ]
1094
+ end
980
1095
end
981
1096
982
1097
end
0 commit comments