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"""
825module SectorTestSuite
926
10- export tests, @testsuite , @testinferred
27+ export smallset, randsector, hasfusiontensor
1128
1229using Test
1330using TestExtras
1431using TensorKitSectors
15- const TKS = TensorKitSectors
32+ using TensorKitSectors: type_repr
33+ using Random
34+ using Base. Iterators: take, product
1635
1736const 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"""
2646macro testsuite (name, ex)
2747 safe_name = lowercase (replace (replace (name, " " => " _" ), " /" => " _" ))
@@ -34,9 +54,11 @@ macro testsuite(name, ex)
3454end
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
4870end
4971
72+ """
73+ @testinferred [AllowedTypes] ex
74+
75+ Like `Test.@inferred`, but registers failures through a test, rather than an error.
76+ """
5077macro testinferred (ex)
5178 return _inferred (ex, __module__)
5279end
5380macro testinferred (ex, allow)
5481 return _inferred (ex, __module__, allow)
5582end
5683
84+ # Implementation copied from Test._inferred:
5785function _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)
100128end
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+
103165include (" sectors.jl" )
104166
105167end # module SectorTestSuite
0 commit comments