@@ -492,65 +492,52 @@ end
492
492
end
493
493
494
494
@testset " replace! & replace" begin
495
- maybe1 (v, p) = if p Some (v) end
496
- maybe2 (v, p) = if p v end
497
-
498
- for maybe = (maybe1, maybe2)
499
- a = [1 , 2 , 3 , 1 ]
500
- @test replace (x-> maybe (2 x, iseven (x)), a) == [1 , 4 , 3 , 1 ]
501
- @test replace! (x-> maybe (2 x, iseven (x)), a) === a
502
- @test a == [1 , 4 , 3 , 1 ]
503
- @test replace (a, 1 => 0 ) == [0 , 4 , 3 , 0 ]
504
- for count = (1 , 0x1 , big (1 ))
505
- @test replace (a, 1 => 0 , count= count) == [0 , 4 , 3 , 1 ]
506
- end
507
- @test replace! (a, 1 => 2 ) === a
508
- @test a == [2 , 4 , 3 , 2 ]
509
- @test replace! (x-> 2 x, a, count= 0x2 ) == [4 , 8 , 3 , 2 ]
510
-
511
- d = Dict (1 => 2 , 3 => 4 )
512
- @test replace (x-> x. first > 2 , d, 0 => 0 ) == Dict (1 => 2 , 0 => 0 )
513
- @test replace! (x-> maybe (x. first=> 2 * x. second, x. first > 2 ), d) === d
514
- @test d == Dict (1 => 2 , 3 => 8 )
515
- @test replace (d, (3 => 8 )=> (0 => 0 )) == Dict (1 => 2 , 0 => 0 )
516
- @test replace! (d, (3 => 8 )=> (2 => 2 )) === d
517
- @test d == Dict (1 => 2 , 2 => 2 )
518
- for count = (1 , 0x1 , big (1 ))
519
- @test replace (x-> x. second == 2 , d, 0 => 0 , count= count) in [Dict (1 => 2 , 0 => 0 ),
520
- Dict (2 => 2 , 0 => 0 )]
521
- end
522
- s = Set ([1 , 2 , 3 ])
523
- @test replace (x-> maybe (2 x, x> 1 ), s) == Set ([1 , 4 , 6 ])
524
- for count = (1 , 0x1 , big (1 ))
525
- @test replace (x-> maybe (2 x, x> 1 ), s, count= count) in [Set ([1 , 4 , 3 ]), Set ([1 , 2 , 6 ])]
526
- end
527
- @test replace (s, 1 => 4 ) == Set ([2 , 3 , 4 ])
528
- @test replace! (s, 1 => 2 ) === s
529
- @test s == Set ([2 , 3 ])
530
- @test replace! (x-> 2 x, s, count= 0x1 ) in [Set ([4 , 3 ]), Set ([2 , 6 ])]
495
+ a = [1 , 2 , 3 , 1 ]
496
+ @test replace (x -> iseven (x) ? 2 x : x, a) == [1 , 4 , 3 , 1 ]
497
+ @test replace! (x -> iseven (x) ? 2 x : x, a) === a
498
+ @test a == [1 , 4 , 3 , 1 ]
499
+ @test replace (a, 1 => 0 ) == [0 , 4 , 3 , 0 ]
500
+ for count = (1 , 0x1 , big (1 ))
501
+ @test replace (a, 1 => 0 , count= count) == [0 , 4 , 3 , 1 ]
502
+ end
503
+ @test replace! (a, 1 => 2 ) === a
504
+ @test a == [2 , 4 , 3 , 2 ]
505
+ @test replace! (x-> 2 x, a, count= 0x2 ) == [4 , 8 , 3 , 2 ]
506
+
507
+ d = Dict (1 => 2 , 3 => 4 )
508
+ @test replace (x-> x. first > 2 , d, 0 => 0 ) == Dict (1 => 2 , 0 => 0 )
509
+ @test replace! (x -> x. first > 2 ? x. first=> 2 * x. second : x, d) === d
510
+ @test d == Dict (1 => 2 , 3 => 8 )
511
+ @test replace (d, (3 => 8 )=> (0 => 0 )) == Dict (1 => 2 , 0 => 0 )
512
+ @test replace! (d, (3 => 8 )=> (2 => 2 )) === d
513
+ @test d == Dict (1 => 2 , 2 => 2 )
514
+ for count = (1 , 0x1 , big (1 ))
515
+ @test replace (x-> x. second == 2 , d, 0 => 0 , count= count) in [Dict (1 => 2 , 0 => 0 ),
516
+ Dict (2 => 2 , 0 => 0 )]
517
+ end
518
+ s = Set ([1 , 2 , 3 ])
519
+ @test replace (x -> x > 1 ? 2 x : x, s) == Set ([1 , 4 , 6 ])
520
+ for count = (1 , 0x1 , big (1 ))
521
+ @test replace (x -> x > 1 ? 2 x : x, s, count= count) in [Set ([1 , 4 , 3 ]), Set ([1 , 2 , 6 ])]
522
+ end
523
+ @test replace (s, 1 => 4 ) == Set ([2 , 3 , 4 ])
524
+ @test replace! (s, 1 => 2 ) === s
525
+ @test s == Set ([2 , 3 ])
526
+ @test replace! (x-> 2 x, s, count= 0x1 ) in [Set ([4 , 3 ]), Set ([2 , 6 ])]
531
527
532
- for count = (0 , 0x0 , big (0 ))
533
- @test replace ([1 , 2 ], 1 => 0 , 2 => 0 , count= count) == [1 , 2 ] # count=0 --> no replacements
534
- end
528
+ for count = (0 , 0x0 , big (0 ))
529
+ @test replace ([1 , 2 ], 1 => 0 , 2 => 0 , count= count) == [1 , 2 ] # count=0 --> no replacements
535
530
end
531
+
536
532
# test collisions with AbstractSet/AbstractDict
537
533
@test replace! (x-> 2 x, Set ([3 , 6 ])) == Set ([6 , 12 ])
538
534
@test replace! (x-> 2 x, Set ([1 : 20 ;])) == Set ([2 : 2 : 40 ;])
539
535
@test replace! (kv -> (2 kv[1 ] => kv[2 ]), Dict (1 => 2 , 2 => 4 , 4 => 8 , 8 => 16 )) == Dict (2 => 2 , 4 => 4 , 8 => 8 , 16 => 16 )
540
536
541
- # test Some(nothing)
537
+ # test nothing & Some(nothing) (should behave as any other value )
542
538
a = [1 , 2 , nothing , 4 ]
543
- @test replace (x -> x === nothing ? 0 : Some (nothing ), a) == [nothing , nothing , 0 , nothing ]
544
- @test replace (x -> x === nothing ? 0 : nothing , a) == [1 , 2 , 0 , 4 ]
545
- @test replace! (x -> x != = nothing ? Some (nothing ) : nothing , a) == [nothing , nothing , nothing , nothing ]
546
- @test replace (iseven, Any[1 , 2 , 3 , 4 ], nothing ) == [1 , nothing , 3 , nothing ]
547
- @test replace (Any[1 , 2 , 3 , 4 ], 1 => nothing , 3 => nothing ) == [nothing , 2 , nothing , 4 ]
548
- s = Set ([1 , 2 , nothing , 4 ])
549
- @test replace (x -> x === nothing ? 0 : Some (nothing ), s) == Set ([0 , nothing ])
550
- @test replace (x -> x === nothing ? 0 : nothing , s) == Set ([1 , 2 , 0 , 4 ])
551
- @test replace (x -> x != = nothing ? Some (nothing ) : nothing , s) == Set ([nothing ])
552
- @test replace (iseven, Set (Any[1 , 2 , 3 , 4 ]), nothing ) == Set ([1 , nothing , 3 , nothing ])
553
- @test replace (Set (Any[1 , 2 , 3 , 4 ]), 1 => nothing , 3 => nothing ) == Set ([nothing , 2 , nothing , 4 ])
539
+ @test replace (x -> x === nothing ? Some (nothing ) : nothing , a) == [nothing , nothing , Some (nothing ), nothing ]
540
+ @test replace (x -> x === nothing ? Some (nothing ) : nothing , Set (a)) == Set ([Some (nothing ), nothing ])
554
541
555
542
# avoid recursive call issue #25384
556
543
@test_throws MethodError replace! (" " )
0 commit comments