Skip to content

Commit

Permalink
Move test_elem methods in respective src files
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Jan 13, 2025
1 parent 928624f commit 74c9ce0
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 165 deletions.
17 changes: 9 additions & 8 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ using .PrettyPrinting

import .PrettyPrinting: expressify


################################################################################
#
# Conformance tests (function stubs for TestExt)
#
################################################################################

include("ConformanceTests.jl")

###############################################################################
#
# Generic algorithms defined on abstract types
Expand Down Expand Up @@ -364,14 +373,6 @@ getindex(S::Set, i::Int) = gen(S, i)

include("error.jl")

################################################################################
#
# Conformance tests (function stubs for TestExt)
#
################################################################################

include("ConformanceTests.jl")


# Generic functions to be defined after all rings
include("broadcasting.jl")
Expand Down
97 changes: 7 additions & 90 deletions src/ConformanceTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ module ConformanceTests

using ..AbstractAlgebra

# This file mostly contains function stubs.
# The actual implementation are in the folder `ext/TestExt/`.


# helper
function equality(a, b)
if is_exact_type(typeof(a)) && is_exact_type(typeof(b))
Expand All @@ -30,95 +26,16 @@ const default_adhoc_partner_rings = [

adhoc_partner_rings(R::NCRing) = default_adhoc_partner_rings

# To be implemented in the src files of the respective types.
# This function is used to generate a pseudo-random element in the Ring conformance tests.
function test_elem end


###############################################################################
#
# add methods for test_elem on ring elements here
# The following function stubs' actual implementations are in the folder `ext/TestExt/`.
#
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)
d = test_elem(B)
return is_zero(d) ? R(n) : R(n, d)
end

function test_elem(R::AbstractAlgebra.FinField)
return rand(R)
end

function test_elem(R::AbstractAlgebra.Floats{T}) where T
return rand(T)*rand(-100:100)
end

function test_elem(Rx::AbstractAlgebra.PolyRing)
R = base_ring(Rx)
return Rx(elem_type(R)[test_elem(R) for i in 1:rand(0:6)])
end

function test_elem(Rx::AbstractAlgebra.MPolyRing)
R = base_ring(Rx)
num_gens = ngens(Rx)
iszero(num_gens) && return Rx(test_elem(R))
len_bound = 8
exp_bound = rand(1:5)
len = rand(0:len_bound)
coeffs = [test_elem(R) for _ in 1:len]
exps = [[rand(0:exp_bound) for _ in 1:num_gens] for _ in 1:len]
return Rx(coeffs, exps)
end

function test_elem(S::Union{AbstractAlgebra.MatSpace,
AbstractAlgebra.MatRing})
R = base_ring(S)
return S(elem_type(R)[test_elem(R) for i in 1:nrows(S), j in 1:ncols(S)])
end

function test_elem(R::AbstractAlgebra.EuclideanRingResidueRing)
return R(test_elem(base_ring(R)))
end

function test_elem(Rx::AbstractAlgebra.SeriesRing)
R = base_ring(Rx)
prec = rand(3:10)
len = rand(0:prec-1)
val = rand(0:prec-len)
# FIXME: constructors don't seem to catch use of negative val
@assert val >= 0
A = elem_type(R)[test_elem(R) for i in 1:len]
if len > 0 && is_zero(A[1])
A[1] = one(R)
end
if elem_type(Rx) <: RelPowerSeriesRingElem
@assert prec >= len + val
return Rx(A, len, prec, val)
else
@assert prec >= len
return Rx(A, len, prec)
end
end

function test_elem(S::AbstractAlgebra.FreeAssociativeAlgebra)
f = S()
g = gens(S)
R = base_ring(S)
isempty(g) && return S(test_elem(R))
len_bound = 8
exp_bound = 6
for i in 1:rand(0:len_bound)
f += test_elem(R) * prod(rand(g) for _ in 1:rand(0:exp_bound); init = S(1))
end
return f
end

###############################################################################

function test_iterate end

Expand Down
19 changes: 19 additions & 0 deletions src/FreeAssociativeAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ function rand(S::FreeAssociativeAlgebra, term_range, exp_bound, v...)
rand(Random.default_rng(), S, term_range, exp_bound, v...)
end

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(S::FreeAssociativeAlgebra)
f = S()
g = gens(S)
R = base_ring(S)
isempty(g) && return S(ConformanceTests.test_elem(R))
len_bound = 8
exp_bound = 6
for i in 1:rand(0:len_bound)
f += ConformanceTests.test_elem(R) * prod(rand(g) for _ in 1:rand(0:exp_bound); init = S(1))
end
return f
end

###############################################################################
#
# free_associative_algebra constructor
Expand Down
22 changes: 22 additions & 0 deletions src/LaurentMPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ function rand(S::LaurentMPolyRing, term_range, exp_bound, v...)
rand(Random.default_rng(), S, term_range, exp_bound, v...)
end

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function test_elem(R::LaurentMPolyRing{BigInt})
n = rand(1:10)
# R: length between 1 and 9
# R: exponents between -n and n
# ZZ: coeffs between -99 and 99
rand(R, 1:9, -n:n, -99:99)
end

function test_elem(R::LaurentMPolyRing{<:ResElem{BigInt}})
n = rand(1:5)
# R: length between 1 and 9
# R: exponents between -n and n
# ZZ/6ZZ: coeffs between ??? <- TODO
rand(R, 1:4, -n:n, 1:10)
end

###############################################################################
#
# laurent_polynomial_ring constructor
Expand Down
18 changes: 18 additions & 0 deletions src/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,24 @@ function rand(S::MPolyRing, term_range, exp_bound, v...)
rand(Random.default_rng(), S, term_range, exp_bound, v...)
end

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(Rx::MPolyRing)
R = base_ring(Rx)
num_gens = ngens(Rx)
iszero(num_gens) && return Rx(ConformanceTests.test_elem(R))
len_bound = 8
exp_bound = rand(1:5)
len = rand(0:len_bound)
coeffs = [ConformanceTests.test_elem(R) for _ in 1:len]
exps = [[rand(0:exp_bound) for _ in 1:num_gens] for _ in 1:len]
return Rx(coeffs, exps)
end

###############################################################################
#
# polynomial_ring constructor
Expand Down
11 changes: 11 additions & 0 deletions src/MatRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,17 @@ end
randmat_with_rank(S::MatRing{T}, rank::Int, v...) where {T <: RingElement} =
randmat_with_rank(Random.default_rng(), S, rank, v...)

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(S::MatRing)
R = base_ring(S)
return S(elem_type(R)[ConformanceTests.test_elem(R) for i in 1:nrows(S), j in 1:ncols(S)])
end

###############################################################################
#
# Identity matrix
Expand Down
11 changes: 11 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6690,6 +6690,17 @@ end
randmat_with_rank(S::MatSpace{T}, rank::Int, v...) where {T <: RingElement} =
randmat_with_rank(Random.default_rng(), S, rank, v...)

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(S::MatSpace)
R = base_ring(S)
return S(elem_type(R)[ConformanceTests.test_elem(R) for i in 1:nrows(S), j in 1:ncols(S)])
end

################################################################################
#
# Matrix constructors
Expand Down
11 changes: 11 additions & 0 deletions src/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3345,6 +3345,17 @@ rand(rng::AbstractRNG, S::PolyRing, deg::Int, v...) =

rand(S::PolyRing, degs, v...) = rand(Random.default_rng(), S, degs, v...)

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(Rx::PolyRing)
R = base_ring(Rx)
return Rx(elem_type(R)[ConformanceTests.test_elem(R) for i in 1:rand(0:6)])
end

###############################################################################
#
# Polynomial substitution
Expand Down
25 changes: 25 additions & 0 deletions src/RelSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,31 @@ rand(rng::AbstractRNG, S::SeriesRing, val_range::AbstractUnitRange{Int}, v...) =

rand(S::SeriesRing, val_range, v...) = rand(Random.default_rng(), S, val_range, v...)

###############################################################################
#
# Conformance test element generation
#
###############################################################################
function ConformanceTests.test_elem(Rx::SeriesRing)
R = base_ring(Rx)
prec = rand(3:10)
len = rand(0:prec-1)
val = rand(0:prec-len)
# FIXME: constructors don't seem to catch use of negative val
@assert val >= 0
A = elem_type(R)[ConformanceTests.test_elem(R) for i in 1:len]
if len > 0 && is_zero(A[1])
A[1] = one(R)
end
if elem_type(Rx) <: RelPowerSeriesRingElem
@assert prec >= len + val
return Rx(A, len, prec, val)
else
@assert prec >= len
return Rx(A, len, prec)
end
end

###############################################################################
#
# power_series_ring constructor
Expand Down
10 changes: 10 additions & 0 deletions src/algorithms/FinField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ end

Base.length(f::FinField) = BigInt(order(f))
Base.eltype(::Type{F}) where {F<:FinField} = elem_type(F)

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function ConformanceTests.test_elem(R::FinField)
return rand(R)
end
10 changes: 10 additions & 0 deletions src/generic/AbsMSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,16 @@ function promote_rule(::Type{AbsMSeries{T, V}}, ::Type{U}) where
promote_rule(T, U) == T ? AbsMSeries{T, V} : Union{}
end

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function test_elem(R::Generic.AbsMSeriesRing{BigInt})
rand(R, 0:12, -10:10)
end

###############################################################################
#
# Parent object call overload
Expand Down
17 changes: 17 additions & 0 deletions src/generic/FactoredFraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,23 @@ function _gcdhelper(b::FactoredFracFieldElem{T}, c::FactoredFracFieldElem{T}) wh
return (z, bbar, cbar)
end

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function test_elem(FF::Generic.FactoredFracField{BigInt})
limit = 10
t = one(FF)
for i in 1:abs(rand(Int)%limit)
s = FF(rand(Int)%(20*limit))
e = rand(Int)%limit
t *= iszero(s) ? s^abs(e) : s^e
end
return t
end

###############################################################################
#
# FactoredFractionField constructor
Expand Down
10 changes: 10 additions & 0 deletions src/generic/FunctionField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,16 @@ rand(rng::AbstractRNG, K::FunctionField, v...) = rand(rng, make(K, v...))

rand(K::FunctionField, v...) = rand(Random.default_rng(), K, v...)

###############################################################################
#
# Conformance test element generation
#
###############################################################################

function test_elem(R::FunctionField{Rational{BigInt}})
rand(R, 1:10, -10:10)
end

###############################################################################
#
# Promotion rules
Expand Down
Loading

0 comments on commit 74c9ce0

Please sign in to comment.