Skip to content

Commit

Permalink
Add conformance tests for some adhoc operations (#1867)
Browse files Browse the repository at this point in the history
and fix bug in MPoly
  • Loading branch information
fingolfin authored Oct 30, 2024
1 parent f137e0a commit e25bc98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/generic/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,8 @@ function ==(a::MPoly, n::Union{Integer, Rational, AbstractFloat})
N = size(a.exps, 1)
if n == 0
return a.length == 0
elseif a.length == 0
return iszero(base_ring(a)(n))
elseif a.length == 1
return a.coeffs[1] == n && monomial_iszero(a.exps, 1, N)
end
Expand All @@ -2076,6 +2078,8 @@ function ==(a::MPoly{T}, n::T) where {T <: RingElem}
N = size(a.exps, 1)
if n == 0
return a.length == 0
elseif a.length == 0
return iszero(base_ring(a)(n))
elseif a.length == 1
return a.coeffs[1] == n && monomial_iszero(a.exps, 1, N)
end
Expand Down
67 changes: 56 additions & 11 deletions test/Rings-conformance-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@
# - test_MatSpace_interface(R)
# - test_MatAlgebra_interface(R)

#
#
#
const default_adhoc_partner_rings = [
AbstractAlgebra.Integers{BigInt}(),
AbstractAlgebra.Integers{Int}(),
AbstractAlgebra.Integers{UInt}(),
AbstractAlgebra.Integers{UInt8}(),
]

adhoc_partner_rings(R::NCRing) = default_adhoc_partner_rings

#
# add methods for test_elem on ring elements here
#

function test_elem(R::AbstractAlgebra.Integers)
n = big(2)^rand((1,1,1,2,3,10,31,32,33,63,64,65,100))
function test_elem(R::AbstractAlgebra.Integers{T}) where {T <: Signed}
n = T(2)^rand((1,1,1,2,3,10,31,32,33,63,64,65,100))
return rand(R, -n:n)
end

function test_elem(R::AbstractAlgebra.Integers{T}) where {T <: Unsigned}
n = T(2)^rand((1,1,1,2,3,10,31,32,33,63,64,65,100))
return rand(R, 0:n)
end

function test_elem(R::AbstractAlgebra.Rationals)
B = base_ring(R)
n = test_elem(B)
Expand Down Expand Up @@ -99,8 +116,8 @@ end


# helper
function equality(a::T, b::T) where T <: AbstractAlgebra.NCRingElement
if is_exact_type(T)
function equality(a, b)
if is_exact_type(typeof(a)) && is_exact_type(typeof(b))
return a == b
else
return isapprox(a, b)
Expand Down Expand Up @@ -157,7 +174,7 @@ function test_mutating_op_like_add(f::Function, f!::Function, A, B)
a = f!(a, b, b)
@test equality(a, f(B, B))
@test b == B

b = deepcopy(B)
b = f!(b, b, b)
@test equality(b, f(B, B))
Expand Down Expand Up @@ -187,25 +204,25 @@ function test_mutating_op_like_addmul(f::Function, f!_::Function, Z, A, B)
@test equality(z, f(Z, A, B))
@test a == A
@test b == B

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

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

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

b = deepcopy(B)
b = f!(b, b, b, t)
@test equality(b, f(B, B, B))
Expand Down Expand Up @@ -312,6 +329,34 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50)
end
end

if is_exact_type(T)
@testset "Adhoc operations with $S" for S in adhoc_partner_rings(R)
s0 = zero(S)
r0 = zero(R)
s1 = one(S)
r1 = one(R)
for i in 1:reps
s2 = test_elem(S)
r2 = R(s2)
x = test_elem(R)

for (s,r) in ((s0, r0), (s1, r1), (s2, r2))
@test equality(r, s)
@test equality(s, r)

@test equality(x + s, x + r)
@test equality(s + x, r + x)

@test equality(x - s, x - r)
@test equality(s - x, r - x)

@test equality(x * s, x * r)
@test equality(s * x, r * x)
end
end
end
end

if !(R isa AbstractAlgebra.Ring)
@testset "Basic functionality for noncommutative rings only" begin
for i in 1:reps
Expand Down Expand Up @@ -365,7 +410,7 @@ function test_NCRing_interface(R::AbstractAlgebra.NCRing; reps = 50)
a = test_elem(R)::T
b = test_elem(R)::T
c = test_elem(R)::T

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

Expand Down Expand Up @@ -537,7 +582,7 @@ function test_EuclideanRing_interface(R::AbstractAlgebra.Ring; reps = 20)
@test !(iszero(f) && iszero(g)) || iszero(gcd(f, g))
@test equality_up_to_units(gcd(f, g)*lcm(f, g), f*g)

g1 = gcd(f, gcd(g, m))
g1 = gcd(f, gcd(g, m))
g2 = gcd(gcd(f, g), m)
g3 = gcd(f, g, m)
g4 = gcd([f, g, m])
Expand Down

0 comments on commit e25bc98

Please sign in to comment.