|
| 1 | +@testsuite "accumulations" (AT, eltypes)->begin |
| 2 | + @testset "$ET" for ET in eltypes |
| 3 | + range = ET <: Real ? (ET(1):ET(10)) : ET |
| 4 | + |
| 5 | + # 1d arrays |
| 6 | + for num_elems in 1:256 |
| 7 | + @test compare(A->accumulate(+, A; init=zero(ET)), AT, rand(range, num_elems)) |
| 8 | + end |
| 9 | + |
| 10 | + for num_elems = rand(1:100, 10) |
| 11 | + @test compare(A->accumulate(+, A; init=zero(ET)), AT, rand(range, num_elems)) |
| 12 | + end |
| 13 | + |
| 14 | + for _ in 1:10 # nd arrays reduced as 1d |
| 15 | + n1 = rand(1:10) |
| 16 | + n2 = rand(1:10) |
| 17 | + n3 = rand(1:10) |
| 18 | + @test compare(A->accumulate(+, A; init=zero(ET)), AT, rand(range, n1, n2, n3)) |
| 19 | + end |
| 20 | + |
| 21 | + for num_elems = rand(1:100, 10) # init value |
| 22 | + init = rand(range) |
| 23 | + @test compare(A->accumulate(+, A; init), AT, rand(range, num_elems)) |
| 24 | + end |
| 25 | + |
| 26 | + |
| 27 | + # nd arrays |
| 28 | + for dims in 1:4 # corner cases |
| 29 | + for isize in 1:3 |
| 30 | + for jsize in 1:3 |
| 31 | + for ksize in 1:3 |
| 32 | + @test compare(A->accumulate(+, A; dims, init=zero(ET)), AT, rand(range, isize, jsize, ksize)) |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + for _ in 1:10 |
| 39 | + for dims in 1:3 |
| 40 | + n1 = rand(1:10) |
| 41 | + n2 = rand(1:10) |
| 42 | + n3 = rand(1:10) |
| 43 | + @test compare(A->accumulate(+, A; dims, init=zero(ET)), AT, rand(range, n1, n2, n3)) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + for _ in 1:10 # init value |
| 48 | + for dims in 1:3 |
| 49 | + n1 = rand(1:10) |
| 50 | + n2 = rand(1:10) |
| 51 | + n3 = rand(1:10) |
| 52 | + init = rand(range) |
| 53 | + @test compare(A->accumulate(+, A; init, dims), AT, rand(range, n1, n2, n3)) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # Larger containers to try and detect weird bugs |
| 58 | + for n in (0, 1, 2, 3, 10, 10_000, 16384, 16384+1) # small, large, odd & even, pow2 and not |
| 59 | + # Skip large tests on small datatypes |
| 60 | + n >= 10000 && sizeof(real(ET)) <= 2 && continue |
| 61 | + |
| 62 | + @test compare(x->accumulate(+, x), AT, rand(range, n)) |
| 63 | + @test compare(x->accumulate(+, x), AT, rand(range, n, 2)) |
| 64 | + @test compare(Base.Fix2((x,y)->accumulate(+, x; init=y), rand(range)), AT, rand(range, n)) |
| 65 | + end |
| 66 | + |
| 67 | + # in place |
| 68 | + @test compare(x->(accumulate!(+, x, copy(x)); x), AT, rand(range, 2)) |
| 69 | + |
| 70 | + @test_throws ArgumentError("accumulate does not support the keyword arguments [:bad_kwarg]") accumulate(+, AT(rand(ET, 10)); bad_kwarg="bad") |
| 71 | + end |
| 72 | +end |
| 73 | + |
| 74 | +@testsuite "accumulations/cumsum & cumprod" (AT, eltypes)->begin |
| 75 | + @test compare(cumsum, AT, rand(Bool, 16)) |
| 76 | + |
| 77 | + @testset "$ET" for ET in eltypes |
| 78 | + range = ET <: Real ? (ET(1):ET(10)) : ET |
| 79 | + |
| 80 | + # cumsum |
| 81 | + for num_elems in rand(1:100, 10) |
| 82 | + @test compare(A->cumsum(A; dims=1), AT, rand(range, num_elems)) |
| 83 | + end |
| 84 | + |
| 85 | + for _ in 1:10 |
| 86 | + for dims in 1:3 |
| 87 | + n1 = rand(1:10) |
| 88 | + n2 = rand(1:10) |
| 89 | + n3 = rand(1:10) |
| 90 | + @test compare(A->cumsum(A; dims), AT, rand(range, n1, n2, n3)) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + |
| 95 | + # cumprod |
| 96 | + range = ET <: Real ? (ET(1):ET(10)) : ET |
| 97 | + @test compare(A->cumprod(A; dims=1), AT, ones(ET, 100_000)) |
| 98 | + |
| 99 | + for _ in 1:10 |
| 100 | + for dims in 1:3 |
| 101 | + n1 = rand(1:10) |
| 102 | + n2 = rand(1:10) |
| 103 | + n3 = rand(1:10) |
| 104 | + @test compare(A->cumprod(A; dims), AT, rand(range, n1, n2, n3)) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments