@@ -111,9 +111,6 @@ const ≅ = isequal
111
111
@test isa (x, CategoricalVector{Union{Int, String, T}})
112
112
@test isordered (x)
113
113
@test levels (x) == [0 , " 2" , 4 , " 6" , 8 ]
114
-
115
- @test_throws ArgumentError cut ([- 0.0 , 0.0 ], 2 )
116
- @test_throws ArgumentError cut ([- 0.0 , 0.0 ], 2 , labels= [- 0.0 , 0.0 ])
117
114
end
118
115
119
116
@testset " cut with missing values in input" begin
144
141
@test levels (x) == [" Q1: [2.0, 3.5)" , " Q2: [3.5, 5.0]" ]
145
142
end
146
143
144
+ @testset " cut(x, n) with invalid n" begin
145
+ @test_throws ArgumentError cut (1 : 10 , 0 )
146
+ @test_throws ArgumentError cut (1 : 10 , - 1 )
147
+ end
148
+
147
149
@testset " cut with formatter function" begin
148
150
my_formatter (from, to, i; leftclosed, rightclosed) = " $i : $from -- $to "
149
151
@@ -185,11 +187,20 @@ end
185
187
x = [zeros (10 ); ones (10 )]
186
188
@test_throws ArgumentError cut (x, [0 , 0.1 , 0.1 , 10 ])
187
189
@test_throws ArgumentError cut (x, 10 )
190
+ y = cut (x, [0 , 0.1 , 10 , 10 ])
191
+ @test y == [fill (" [0.0, 0.1)" , 10 ); fill (" [0.1, 10.0)" , 10 )]
192
+ @test levels (y) == [" [0.0, 0.1)" , " [0.1, 10.0)" , " [10.0, 10.0]" ]
188
193
189
194
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 11 ])
190
195
y = cut (1 : 10 , [1 , 5 , 5 , 11 ], allowempty= true )
191
196
@test y == cut (1 : 10 , [1 , 5 , 11 ])
192
197
@test levels (y) == [" [1, 5)" , " (5, 5)" , " [5, 11]" ]
198
+ y = cut (1 : 10 , [1 , 5 , 11 , 11 ])
199
+ @test y == [fill (" [1, 5)" , 4 ); fill (" [5, 11)" , 6 )]
200
+ @test levels (y) == [" [1, 5)" , " [5, 11)" , " [11, 11]" ]
201
+ y = cut (1 : 10 , [1 , 5 , 10 , 10 ])
202
+ @test y == [fill (" [1, 5)" , 4 ); fill (" [5, 10)" , 5 ); " [10, 10]" ]
203
+ @test levels (y) == [" [1, 5)" , " [5, 10)" , " [10, 10]" ]
193
204
194
205
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 5 , 11 ])
195
206
@test_throws ArgumentError cut (1 : 10 , [1 , 5 , 5 , 11 ],
242
253
243
254
fmt = (from, to, i; leftclosed, rightclosed) -> (i % 2 == 0 ? to : 0.0 )
244
255
@test_throws ArgumentError cut (1 : 8 , 0 : 2 : 10 , labels= fmt)
256
+
257
+ @test_throws ArgumentError cut ([fill (1 , 10 ); 4 ], 2 )
258
+ @test_throws ArgumentError cut ([fill (1 , 10 ); 4 ], 3 )
259
+ x = cut ([fill (1 , 10 ); 4 ], 2 , allowempty= true )
260
+ @test unique (x) == [" Q2: [1.0, 4.0]" ]
261
+ x = cut ([fill (1 , 10 ); 4 ], 3 , allowempty= true )
262
+ @test unique (x) == [" Q3: [1.0, 4.0]" ]
263
+ @test levels (x) == [" Q1: (1.0, 1.0)" , " Q2: (1.0, 1.0)" , " Q3: [1.0, 4.0]" ]
264
+
265
+ x = cut ([fill (1 , 5 ); fill (4 , 5 )], 2 )
266
+ @test x == [fill (" Q1: [1.0, 2.5)" , 5 ); fill (" Q2: [2.5, 4.0]" , 5 )]
267
+ @test levels (x) == [" Q1: [1.0, 2.5)" , " Q2: [2.5, 4.0]" ]
268
+ @test_throws ArgumentError cut ([fill (1 , 5 ); fill (4 , 5 )], 3 )
269
+ x = cut ([fill (1 , 5 ); fill (4 , 5 )], 3 , allowempty= true )
270
+ @test x == [fill (" Q2: [1.0, 4.0)" , 5 ); fill (" Q3: [4.0, 4.0]" , 5 )]
271
+ @test levels (x) == [" Q1: (1.0, 1.0)" , " Q2: [1.0, 4.0)" , " Q3: [4.0, 4.0]" ]
272
+ end
273
+
274
+ @testset " cut with -0.0" begin
275
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], 2 )
276
+ @test x == [" Q1: [-0.0, 0.0)" , " Q2: [0.0, 0.0]" , " Q2: [0.0, 0.0]" , " Q1: [-0.0, 0.0)" ]
277
+ @test levels (x) == [" Q1: [-0.0, 0.0)" , " Q2: [0.0, 0.0]" ]
278
+
279
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], [- 0.0 , 0.0 , 0.0 ])
280
+ @test x == [" [-0.0, 0.0)" , " [0.0, 0.0]" , " [0.0, 0.0]" , " [-0.0, 0.0)" ]
281
+ @test levels (x) == [" [-0.0, 0.0)" , " [0.0, 0.0]" ]
282
+
283
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], [- 0.0 , 0.0 ])
284
+ @test x == fill (" [-0.0, 0.0]" , 4 )
285
+ @test levels (x) == [" [-0.0, 0.0]" ]
286
+
287
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], [0.0 ], extend= true )
288
+ @test x == fill (" [-0.0, 0.0]" , 4 )
289
+ @test levels (x) == [" [-0.0, 0.0]" ]
290
+
291
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], [- 0.0 ], extend= true )
292
+ @test x == fill (" [-0.0, 0.0]" , 4 )
293
+ @test levels (x) == [" [-0.0, 0.0]" ]
294
+
295
+ x = cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], 2 , labels= [- 0.0 , 0.0 ])
296
+ @test x == [- 0.0 , 0.0 , 0.0 , - 0.0 ]
297
+
298
+ @test_throws ArgumentError cut ([- 0.0 , 0.0 , 0.0 , - 0.0 ], [- 0.0 , - 0.0 , 0.0 ])
245
299
end
246
300
247
301
@testset " cut with extend=true" begin
276
330
@test x == [" [-1.0, 0.0)" , " [-1.0, 0.0)" , " [0.0, 1.0]" , " [0.0, 1.0]" , " [0.0, 1.0]" ]
277
331
end
278
332
333
+ @testset " cut with NaN and Inf" begin
334
+ @test_throws ArgumentError (" NaN values are not allowed in input vector" ) cut ([1 , NaN , 2 , 3 ], [1 , 10 ])
335
+ @test_throws ArgumentError (" NaN values are not allowed in input vector" ) cut ([1 , NaN , 2 , 3 ], [1 ], extend= true )
336
+ @test_throws ArgumentError (" NaN values are not allowed in input vector" ) cut ([1 , NaN , 2 , 3 ], 2 )
337
+ @test_throws ArgumentError (" NaN values are not allowed in breaks" ) cut ([1 , 2 ], [1 , NaN ])
338
+
339
+ x = cut ([1 , Inf ], [1 ], extend= true )
340
+ @test x ≅ [" [1.0, Inf]" , " [1.0, Inf]" ]
341
+ @test levels (x) == [" [1.0, Inf]" ]
342
+
343
+ x = cut ([1 , - Inf ], [1 ], extend= true )
344
+ @test x ≅ [" [-Inf, 1.0]" , " [-Inf, 1.0]" ]
345
+ @test levels (x) == [" [-Inf, 1.0]" ]
346
+
347
+ x = cut ([1 : 5 ; Inf ], [1 , 2 , Inf ])
348
+ @test x ≅ [" [1.0, 2.0)" ; fill (" [2.0, Inf]" , 5 )]
349
+ @test levels (x) == [" [1.0, 2.0)" , " [2.0, Inf]" ]
350
+
351
+ x = cut ([1 : 5 ; - Inf ], [- Inf , 2 , 5 ])
352
+ @test x ≅ [" [-Inf, 2.0)" ; fill (" [2.0, 5.0]" , 4 ); " [-Inf, 2.0)" ]
353
+ @test levels (x) == [" [-Inf, 2.0)" , " [2.0, 5.0]" ]
354
+
355
+ x = cut ([1 : 5 ; Inf ], 2 )
356
+ @test x ≅ [fill (" Q1: [1.0, 3.5)" , 3 ); fill (" Q2: [3.5, Inf]" , 3 )]
357
+ @test levels (x) == [" Q1: [1.0, 3.5)" , " Q2: [3.5, Inf]" ]
358
+
359
+ x = cut ([1 : 5 ; - Inf ], 2 )
360
+ @test x ≅ [fill (" Q1: [-Inf, 2.5)" , 2 ); fill (" Q2: [2.5, 5.0]" , 3 ); " Q1: [-Inf, 2.5)" ]
361
+ @test levels (x) == [" Q1: [-Inf, 2.5)" , " Q2: [2.5, 5.0]" ]
279
362
end
363
+
364
+ end
0 commit comments