Skip to content

Commit

Permalink
fix nonsensical operate test set
Browse files Browse the repository at this point in the history
Currently the test set starts with the line
`@testset "$(typeof(x))" for x in [2, big(3)]`, which points to the
test not working as intended, because the array constructor
(`[2, big(3)]`) promotes all elements to a common type, so naming
the test set `"$(typeof(x))"` doesn't make sense.

Furthermore, after the above is fixed, the test fails because the `!==`
check is performed independently of whether the type is mutable.

TODO: including `Rational{BigInt}` among the tested types reveals what
seems to be a bug: #240.

TODO: including `abs` among the tested operations reveals that it lacks
an `operate` method: #241.
  • Loading branch information
nsajko committed Nov 27, 2023
1 parent 6e6a854 commit 7471e85
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,35 @@ end
end

@testset "operate" begin
@testset "$(typeof(x))" for x in [2, big(3)]
@testset "$op" for op in [+, *, gcd, lcm]
@test MA.operate(op, x) !== x
Types = (Int, BigInt, Rational{Int})
type_is_mutable = function(op::F, ::Type{T}, ::V) where {F, T, V<:Val}
f = _ -> T
MA.mutability(T, op, ntuple(f, V())...) == MA.IsMutable()
end
@testset "$T" for T in Types
arity_is_mutable = (op, n) -> type_is_mutable(op, T, Val(n))
x = T(7)
@testset "1-ary $op" for op in [+, *, gcd, lcm]
a = op(x)
b = MA.operate(op, x)
is_mutable = arity_is_mutable(op, 1)
@test a == b
is_mutable && (@test a !== b)
end
ops = [+, *, MA.add_mul, MA.sub_mul, MA.add_dot, gcd, lcm]
@testset "$op" for op in ops
@test MA.operate(op, x, x, x, x) !== op(x, x, x, x)
@testset "4-ary $op" for op in ops
a = op(x, x, x, x)
b = MA.operate(op, x, x, x, x)
is_mutable = arity_is_mutable(op, 4)
@test a == b
is_mutable && (@test a !== b)
end
@testset "$op" for op in [-, /, div]
@test MA.operate(op, x, x) !== op(x, x)
@testset "2-ary $op" for op in [-, /, div]
a = op(x, x)
b = MA.operate(op, x, x)
is_mutable = arity_is_mutable(op, 2)
@test a == b
is_mutable && (@test a !== b)
end
end
end
Expand Down

0 comments on commit 7471e85

Please sign in to comment.