Skip to content

Commit

Permalink
Add more exhaustive mutating op tests to ring conformance test
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Oct 1, 2024
1 parent 11bbd58 commit 92c311e
Showing 1 changed file with 117 additions and 42 deletions.
159 changes: 117 additions & 42 deletions test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,113 @@ function equality(a::T, b::T) where T <: AbstractAlgebra.NCRingElement
end
end

function test_mutating_op_like_zero(f::Function, f!::Function, a)
A = deepcopy(a)
a = f!(a)
@test equality(a, f(A))
a = deepcopy(A)
end

function test_mutating_op_like_neg(f::Function, f!::Function, a, b)
A = deepcopy(a)
B = deepcopy(b)
a = f!(a, b)
@test equality(a, f(B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a)
@test equality(a, f(A))
a = deepcopy(A)
end

function test_mutating_op_like_add(f::Function, f!::Function, a, b, c)
A = deepcopy(a)
B = deepcopy(b)
C = deepcopy(c)

a = f!(a, b, c)
@test equality(a, f(B, C))
@test b == B
@test c == C
a = deepcopy(A)
b = deepcopy(B)
c = deepcopy(C)

a = f!(a, a, b)
@test equality(a, f(A, B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, b, a)
@test equality(a, f(B, A))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, b, b)
@test equality(a, f(B, B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, a, a)
@test equality(a, f(A, A))
a = deepcopy(A)

a = f!(a, b)
@test equality(a, f(A, B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, a)
@test equality(a, f(A, A))
a = deepcopy(A)
end

function test_mutating_op_like_addmul(f::Function, f!_::Function, a, b, c)
A = deepcopy(a)
B = deepcopy(b)
C = deepcopy(c)

f!(a, b, c, ::Nothing) = f!_(a, b, c)
f!(a, b, c, t) = f!_(a, b, c, t)

for t in [nothing, zero(parent(a)), deepcopy(a)]
a = f!(a, b, c, t)
@test equality(a, f(A, B, C))
@test b == B
@test c == C
a = deepcopy(A)
b = deepcopy(B)
c = deepcopy(C)

a = f!(a, a, b, t)
@test equality(a, f(A, A, B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, b, a, t)
@test equality(a, f(A, B, A))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, b, b, t)
@test equality(a, f(A, B, B))
@test b == B
a = deepcopy(A)
b = deepcopy(B)

a = f!(a, a, a, t)
@test equality(a, f(A, A, A))
a = deepcopy(A)
end
end

function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50)

Expand Down Expand Up @@ -171,50 +278,18 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50)
a = test_elem(R)::T
b = test_elem(R)::T
c = test_elem(R)::T
A = deepcopy(a)
B = deepcopy(b)
C = deepcopy(c)

test_mutating_op_like_zero(zero, zero!, a)
test_mutating_op_like_zero(one, one!, a)

x = deepcopy(a)
@test iszero(zero!(x))

ab = a*b
x = R()
@test mul!(x, a, b) == ab
x = deepcopy(b)
@test mul!(x, a, b) == ab
x = deepcopy(a)
@test mul!(x, x, b) == ab
x = deepcopy(b)
@test mul!(x, a, x) == ab

ab = a+b
x = R()
@test add!(x, a, b) == ab
x = deepcopy(b)
@test add!(x, a, b) == ab
x = deepcopy(a)
@test add!(x, x, b) == ab
x = deepcopy(b)
@test add!(x, a, x) == ab
x = deepcopy(a)
@test add!(x, b) == ab

# isapprox as BigFloat may fuse
# matrices don't implement addmul!
#t = R()
#x = deepcopy(a)
#@test equality(addmul!(x, b, c, t), a+b*c)
#x = deepcopy(a)
#@test equality(addmul!(x, x, c, t), a+a*c)
#x = deepcopy(a)
#@test equality(addmul!(x, b, x, t), a+b*a)
#x = deepcopy(a)
#@test equality(addmul!(x, x, x, t), a+a*a)
test_mutating_op_like_neg(-, neg!, a, b)

@test A == a
@test B == b
@test C == c
test_mutating_op_like_add(+, add!, a, b, c)
test_mutating_op_like_add(-, sub!, a, b, c)
test_mutating_op_like_add(*, mul!, a, b, c)

test_mutating_op_like_addmul((a, b, c) -> a + b*c, addmul!, a, b, c)
test_mutating_op_like_addmul((a, b, c) -> a - b*c, submul!, a, b, c)
end
end
end
Expand Down

0 comments on commit 92c311e

Please sign in to comment.