Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Sep 22, 2023
1 parent 0985fff commit 740cd02
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_itensors_base_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
version:
- '1.8'
- '1.6'
- '1'
os:
- ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_ndtensors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
version:
- '1.8'
- '1.6'
- '1'
os:
- ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion NDTensors/src/SmallVectors/src/msmallvector/msmallvector.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
MSmallVector
TODO: Make `buffer` field `const` (new in Julia 1.8)
"""
mutable struct MSmallVector{S,T} <: AbstractSmallVector{T}
const buffer::MVector{S,T}
buffer::MVector{S,T}
length::Int
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct SubSmallVector{T,P} <: AbstractSubSmallVector{T}
end

mutable struct SubMSmallVector{T,P<:AbstractVector{T}} <: AbstractSubSmallVector{T}
const parent::P
parent::P
start::Int
stop::Int
end
Expand Down
162 changes: 65 additions & 97 deletions NDTensors/src/SmallVectors/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,111 +22,79 @@ using NDTensors.SmallVectors:
mergesortedunique,
mergesortedunique!


function test_smallvectors()
x = SmallVector{10}([1, 3, 5])
mx = MSmallVector(x)
return @testset "SmallVectors" begin
x = SmallVector{10}([1, 3, 5])
mx = MSmallVector(x)

@test x isa SmallVector{10,Int}
@test mx isa MSmallVector{10,Int}
@test eltype(x) === Int
@test eltype(mx) === Int
@test x isa SmallVector{10,Int}
@test mx isa MSmallVector{10,Int}
@test eltype(x) === Int
@test eltype(mx) === Int

# TODO: Test construction has zero allocations.
# TODO: Extend construction to arbitrary collections, like tuple.
# TODO: Test construction has zero allocations.
# TODO: Extend construction to arbitrary collections, like tuple.

# conversion
@test @inferred(SmallVector(x)) == x
@test @allocated(SmallVector(x)) == 0
@test @inferred(SmallVector(mx)) == x
@test @allocated(SmallVector(mx)) == 0
# conversion
@test @inferred(SmallVector(x)) == x
@test @allocated(SmallVector(x)) == 0
@test @inferred(SmallVector(mx)) == x
@test @allocated(SmallVector(mx)) == 0

# length
@test @inferred(length(x)) == 3
@test @allocated(length(x)) == 0
@test @inferred(length(SmallVectors.buffer(x))) == 10
@test @allocated(length(SmallVectors.buffer(x))) == 0
# length
@test @inferred(length(x)) == 3
@test @allocated(length(x)) == 0
@test @inferred(length(SmallVectors.buffer(x))) == 10
@test @allocated(length(SmallVectors.buffer(x))) == 0

item = 115
no_broken = (false, false, false, false)
for (
f!, f, ans, args, f!_impl_broken, f!_noalloc_broken, f_impl_broken, f_noalloc_broken
) in [
(:push!, :push, [1, 3, 5, item], (item,), no_broken...),
(:append!, :append, [1, 3, 5, item], ([item],), no_broken...),
(:prepend!, :prepend, [item, 1, 3, 5], ([item],), no_broken...),
(:pushfirst!, :pushfirst, [item, 1, 3, 5], (item,), no_broken...),
(:setindex!, :setindex, [1, item, 5], (item, 2), no_broken...),
(:pop!, :pop, [1, 3], (), no_broken...),
(:popfirst!, :popfirst, [3, 5], (), no_broken...),
(:insert!, :insert, [1, item, 3, 5], (2, item), no_broken...),
(:deleteat!, :deleteat, [1, 5], (2,), no_broken...),
(:circshift!, :circshift, [5, 1, 3], (1,), no_broken...),
(:sort!, :sort, [1, 3, 5], (), no_broken...),
(
:insertsorted!,
:insertsorted,
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:insertsorted!,
:insertsorted,
[1, 3, 3, 5],
(3,),
no_broken...,
),
(
:insertsortedunique!,
:insertsortedunique,
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:insertsortedunique!,
:insertsortedunique,
[1, 3, 5],
(3,),
no_broken...,
),
(
:mergesorted!,
:mergesorted,
[1, 2, 3, 3, 5],
([2, 3],),
no_broken...,
),
(
:mergesortedunique!,
:mergesortedunique,
[1, 2, 3, 5],
([2, 3],),
no_broken...,
),
]
mx_tmp = copy(mx)
@eval begin
@test @inferred($f!(copy($mx), $args...)) == $ans broken = $f!_impl_broken
@test @allocated($f!($mx_tmp, $args...)) == 0 broken = $f!_noalloc_broken
@test @inferred($f($x, $args...)) == $ans broken = $f_impl_broken
@test @allocated($f($x, $args...)) == 0 broken = $f_noalloc_broken
item = 115
no_broken = (false, false, false, false)
for (
f!, f, ans, args, f!_impl_broken, f!_noalloc_broken, f_impl_broken, f_noalloc_broken
) in [
(:push!, :push, [1, 3, 5, item], (item,), no_broken...),
(:append!, :append, [1, 3, 5, item], ([item],), no_broken...),
(:prepend!, :prepend, [item, 1, 3, 5], ([item],), no_broken...),
(:pushfirst!, :pushfirst, [item, 1, 3, 5], (item,), no_broken...),
(:setindex!, :setindex, [1, item, 5], (item, 2), no_broken...),
(:pop!, :pop, [1, 3], (), no_broken...),
(:popfirst!, :popfirst, [3, 5], (), no_broken...),
(:insert!, :insert, [1, item, 3, 5], (2, item), no_broken...),
(:deleteat!, :deleteat, [1, 5], (2,), no_broken...),
(:circshift!, :circshift, [5, 1, 3], (1,), no_broken...),
(:sort!, :sort, [1, 3, 5], (), no_broken...),
(:insertsorted!, :insertsorted, [1, 2, 3, 5], (2,), no_broken...),
(:insertsorted!, :insertsorted, [1, 3, 3, 5], (3,), no_broken...),
(:insertsortedunique!, :insertsortedunique, [1, 2, 3, 5], (2,), no_broken...),
(:insertsortedunique!, :insertsortedunique, [1, 3, 5], (3,), no_broken...),
(:mergesorted!, :mergesorted, [1, 2, 3, 3, 5], ([2, 3],), no_broken...),
(:mergesortedunique!, :mergesortedunique, [1, 2, 3, 5], ([2, 3],), no_broken...),
]
mx_tmp = copy(mx)
@eval begin
@test @inferred($f!(copy($mx), $args...)) == $ans broken = $f!_impl_broken
@test @allocated($f!($mx_tmp, $args...)) == 0 broken = $f!_noalloc_broken
@test @inferred($f($x, $args...)) == $ans broken = $f_impl_broken
@test @allocated($f($x, $args...)) == 0 broken = $f_noalloc_broken
end
end
end

# Separated out since for some reason it breaks the `@inferred`
# check when `kwargs` are interpolated into `@eval`.
ans, kwargs = [5, 3, 1], (; rev=true)
mx_tmp = copy(mx)
@test @inferred(sort!(copy(mx); kwargs...)) == ans
@test @allocated(sort!(mx_tmp; kwargs...)) == 0
@test @inferred(sort(x; kwargs...)) == ans
@test @allocated(sort(x; kwargs...)) == 0
# Separated out since for some reason it breaks the `@inferred`
# check when `kwargs` are interpolated into `@eval`.
ans, kwargs = [5, 3, 1], (; rev=true)
mx_tmp = copy(mx)
@test @inferred(sort!(copy(mx); kwargs...)) == ans
@test @allocated(sort!(mx_tmp; kwargs...)) == 0
@test @inferred(sort(x; kwargs...)) == ans
@test @allocated(sort(x; kwargs...)) == 0

ans, args = [1, 3, 5, item], ([item],)
@test @inferred(vcat(x, args...)) == ans
@test @allocated(vcat(x, args...)) == 0
ans, args = [1, 3, 5, item], ([item],)
@test @inferred(vcat(x, args...)) == ans
@test @allocated(vcat(x, args...)) == 0
end
end

@testset "SmallVectors" test_smallvectors()
# TODO: switch to:
# @testset "SmallVectors" test_smallvectors()
# (new in Julia 1.9)
test_smallvectors()

0 comments on commit 740cd02

Please sign in to comment.