@@ -47,7 +47,7 @@ let A = zeros(2, 2)
47
47
0.9103565379264364 0.17732884646626457 ]
48
48
end
49
49
let A = zeros (2 , 2 )
50
- @test_throws ArgumentError rand! (MersenneTwister (0 ), A, 5 )
50
+ @test_throws MethodError rand! (MersenneTwister (0 ), A, 5 )
51
51
@test rand (MersenneTwister (0 ), Int64, 1 ) == [- 3433174948434291912 ]
52
52
end
53
53
let A = zeros (Int64, 2 , 2 )
@@ -307,9 +307,32 @@ let a = [rand(RandomDevice(), UInt128) for i=1:10]
307
307
@test reduce (| , a)>>> 64 != 0
308
308
end
309
309
310
+ # wrapper around Float64 to check fallback random generators
311
+ struct FakeFloat64 <: AbstractFloat
312
+ x:: Float64
313
+ end
314
+ Base. rand (rng:: AbstractRNG , :: Random.SamplerTrivial{Random.CloseOpen01{FakeFloat64}} ) = FakeFloat64 (rand (rng))
315
+ for f in (:sqrt , :log , :log1p , :one , :zero , :abs , :+ , :- )
316
+ @eval Base.$ f (x:: FakeFloat64 ) = FakeFloat64 ($ f (x. x))
317
+ end
318
+ for f in (:+ , :- , :* , :/ )
319
+ @eval begin
320
+ Base.$ f (x:: FakeFloat64 , y:: FakeFloat64 ) = FakeFloat64 ($ f (x. x,y. x))
321
+ Base.$ f (x:: FakeFloat64 , y:: Real ) = FakeFloat64 ($ f (x. x,y))
322
+ Base.$ f (x:: Real , y:: FakeFloat64 ) = FakeFloat64 ($ f (x,y. x))
323
+ end
324
+ end
325
+ for f in (:< , :<= , :> , :>= , :(== ), :(!= ))
326
+ @eval begin
327
+ Base.$ f (x:: FakeFloat64 , y:: FakeFloat64 ) = $ f (x. x,y. x)
328
+ Base.$ f (x:: FakeFloat64 , y:: Real ) = $ f (x. x,y)
329
+ Base.$ f (x:: Real , y:: FakeFloat64 ) = $ f (x,y. x)
330
+ end
331
+ end
332
+
310
333
# test all rand APIs
311
334
for rng in ([], [MersenneTwister (0 )], [RandomDevice ()], [Xoshiro ()])
312
- ftypes = [Float16, Float32, Float64]
335
+ ftypes = [Float16, Float32, Float64, FakeFloat64, BigFloat ]
313
336
cftypes = [ComplexF16, ComplexF32, ComplexF64, ftypes... ]
314
337
types = [Bool, Char, BigFloat, Base. BitInteger_types... , ftypes... ]
315
338
randset = Set (rand (Int, 20 ))
@@ -406,15 +429,12 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()], [Xoshiro()])
406
429
rand! (rng... , BitMatrix (undef, 2 , 3 )) :: BitArray{2}
407
430
408
431
# Test that you cannot call randn or randexp with non-Float types.
409
- for r in [randn, randexp, randn!, randexp!]
410
- local r
432
+ for r in [randn, randexp]
411
433
@test_throws MethodError r (Int)
412
434
@test_throws MethodError r (Int32)
413
435
@test_throws MethodError r (Bool)
414
436
@test_throws MethodError r (String)
415
437
@test_throws MethodError r (AbstractFloat)
416
- # TODO (#17627): Consider adding support for randn(BigFloat) and removing this test.
417
- @test_throws MethodError r (BigFloat)
418
438
419
439
@test_throws MethodError r (Int64, (2 ,3 ))
420
440
@test_throws MethodError r (String, 1 )
@@ -664,7 +684,7 @@ let b = ['0':'9';'A':'Z';'a':'z']
664
684
end
665
685
666
686
# this shouldn't crash (#22403)
667
- @test_throws ArgumentError rand! (Union{UInt,Int}[1 , 2 , 3 ])
687
+ @test_throws MethodError rand! (Union{UInt,Int}[1 , 2 , 3 ])
668
688
669
689
@testset " $RNG () & Random.seed!(rng::$RNG ) initializes randomly" for RNG in (MersenneTwister, RandomDevice, Xoshiro)
670
690
m = RNG ()
736
756
737
757
struct RandomStruct23964 end
738
758
@testset " error message when rand not defined for a type" begin
739
- @test_throws ArgumentError rand (nothing )
740
- @test_throws ArgumentError rand (RandomStruct23964 ())
759
+ @test_throws MethodError rand (nothing )
760
+ @test_throws MethodError rand (RandomStruct23964 ())
741
761
end
742
762
743
763
@testset " rand(::$(typeof (RNG)) , ::UnitRange{$T }" for RNG ∈ (MersenneTwister (rand (UInt128)), RandomDevice (), Xoshiro ()),
0 commit comments