Skip to content

Commit 022c368

Browse files
committed
absorb testsetup in testsuite
1 parent b1803db commit 022c368

File tree

3 files changed

+71
-66
lines changed

3 files changed

+71
-66
lines changed

test/runtests.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
using Test
22
using TestExtras
3-
using Random
4-
# using TensorKit: TensorKitSectors
53
using TensorKitSectors
6-
using TensorOperations
7-
using Base.Iterators: take, product
8-
using LinearAlgebra: LinearAlgebra
94

10-
const TKS = TensorKitSectors
11-
12-
include("testsetup.jl")
13-
using .TestSetup
145
include("newsectors.jl")
156
using .NewSectors
167

test/testsetup.jl

Lines changed: 0 additions & 48 deletions
This file was deleted.

test/testsuite.jl

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
"""
22
module SectorTestSuite
33
4-
Lightweight testsuite registration for sector tests, inspired by the GPUArrays
5-
testsuite style. Each logical group of tests is registered under a string key
6-
and can be iterated for every sector type.
4+
Test suite and utilities that ensure a reusable way of verifying the required interface for a `Sector` type.
5+
Framework based on the GPUArrays testsuite.
6+
7+
Downstream packages may include this test suite as follows:
8+
9+
```julia
10+
import TensorKitSectors
11+
testsuite_path = joinpath(
12+
dirname(dirname(pathof(TensorKitSectors))), # TensorKitSectors root
13+
"test", "testsuite.jl"
14+
)
15+
include(testsuite_path)
16+
17+
SectorTestSuite.test_sectortype(MySectorType)
18+
```
19+
20+
Additionally, this test suite exports the following convenience testing utilities:
21+
* [`smallset`](@ref)
22+
* [`randsector`](@ref)
23+
* [`hasfusiontensor`](@ref)
724
"""
825
module SectorTestSuite
926

10-
export tests, @testsuite, @testinferred
27+
export smallset, randsector, hasfusiontensor
1128

1229
using Test
1330
using TestExtras
1431
using TensorKitSectors
15-
const TKS = TensorKitSectors
32+
using TensorKitSectors: type_repr
33+
using Random
34+
using Base.Iterators: take, product
1635

1736
const tests = Dict()
1837

@@ -21,7 +40,8 @@ const tests = Dict()
2140
# test code here
2241
end
2342
24-
Register a sector testsuite. The body is executed with a single argument `I`, the concrete `Sector` type under test.
43+
Register a sector testsuite.
44+
The body is executed with a single argument `I`, the concrete `Sector` type under test.
2545
"""
2646
macro testsuite(name, ex)
2747
safe_name = lowercase(replace(replace(name, " " => "_"), "/" => "_"))
@@ -34,9 +54,11 @@ macro testsuite(name, ex)
3454
end
3555

3656
"""
37-
Runs the entire TensorKitSectors test suite on sector type `I`
57+
test_sectortype(I::Type)
58+
59+
Runs the entire TensorKitSectors test suite on sector type `I`.
3860
"""
39-
function test(I::Type)
61+
function test_sectortype(I::Type)
4062
return @testset "$(TKS.type_repr(I))" begin
4163
for (name, fun) in tests
4264
code = quote
@@ -47,13 +69,19 @@ function test(I::Type)
4769
end
4870
end
4971

72+
"""
73+
@testinferred [AllowedTypes] ex
74+
75+
Like `Test.@inferred`, but registers failures through a test, rather than an error.
76+
"""
5077
macro testinferred(ex)
5178
return _inferred(ex, __module__)
5279
end
5380
macro testinferred(ex, allow)
5481
return _inferred(ex, __module__, allow)
5582
end
5683

84+
# Implementation copied from Test._inferred:
5785
function _inferred(ex, mod, allow = :(Union{}))
5886
if Meta.isexpr(ex, :ref)
5987
ex = Expr(:call, :getindex, ex.args...)
@@ -99,7 +127,41 @@ function _inferred(ex, mod, allow = :(Union{}))
99127
return Base.remove_linenums!(result)
100128
end
101129

102-
include("testsetup.jl")
130+
smallset(::Type{I}) where {I <: Sector} = take(values(I), 5)
131+
function smallset(::Type{ProductSector{Tuple{I1, I2}}}) where {I1, I2}
132+
iter = product(smallset(I1), smallset(I2))
133+
s = collect(i j for (i, j) in iter if dim(i) * dim(j) <= 6)
134+
return length(s) > 6 ? rand(s, 6) : s
135+
end
136+
function smallset(::Type{ProductSector{Tuple{I1, I2, I3}}}) where {I1, I2, I3}
137+
iter = product(smallset(I1), smallset(I2), smallset(I3))
138+
s = collect(i j k for (i, j, k) in iter if dim(i) * dim(j) * dim(k) <= 6)
139+
return length(s) > 6 ? rand(s, 6) : s
140+
end
141+
142+
function randsector(::Type{I}) where {I <: Sector}
143+
s = collect(smallset(I))
144+
a = Random.rand(s)
145+
while isunit(a) # don't use trivial label
146+
a = Random.rand(s)
147+
end
148+
return a
149+
end
150+
randsector(::Type{I}) where {I <: Union{Trivial, PlanarTrivial}} = unit(I)
151+
152+
function hasfusiontensor(I::Type{<:Sector})
153+
try
154+
fusiontensor(unit(I), unit(I), unit(I))
155+
return true
156+
catch e
157+
if e isa MethodError
158+
return false
159+
else
160+
rethrow(e)
161+
end
162+
end
163+
end
164+
103165
include("sectors.jl")
104166

105167
end # module SectorTestSuite

0 commit comments

Comments
 (0)