Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Sep 21, 2023
1 parent 2c75f77 commit 6bb3cd7
Showing 1 changed file with 98 additions and 104 deletions.
202 changes: 98 additions & 104 deletions NDTensors/src/SmallVectors/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,116 +1,110 @@
module TestSmallVectors
include("SmallVectors/src/SmallVectors.jl")
using .SmallVectors
using NDTensors.SmallVectors
using StaticArrays
using Test

function test()
@testset "SmallVectors" begin
x = SmallVector{10}([1, 3, 5])
mx = MSmallVector(x)
@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!, :(SmallVectors.append), [1, 3, 5, item], ([item],), no_broken...),
(:prepend!, :(SmallVectors.prepend), [item, 1, 3, 5], ([item],), no_broken...),
# (:splice!, :(SmallVectors.splice), [1, item, 3], (2, [item],), true, true, true, true), # Not implemented
(: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...),
(
:(SmallVectors.insertsorted!),
:(SmallVectors.insertsorted),
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:(SmallVectors.insertsorted!),
:(SmallVectors.insertsorted),
[1, 3, 3, 5],
(3,),
no_broken...,
),
(
:(SmallVectors.insertsortedunique!),
:(SmallVectors.insertsortedunique),
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:(SmallVectors.insertsortedunique!),
:(SmallVectors.insertsortedunique),
[1, 3, 5],
(3,),
no_broken...,
),
(
:(SmallVectors.mergesorted!),
:(SmallVectors.mergesorted),
[1, 2, 3, 3, 5],
([2, 3],),
no_broken...,
),
(
:(SmallVectors.mergesortedunique!),
:(SmallVectors.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
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!, :(SmallVectors.append), [1, 3, 5, item], ([item],), no_broken...),
(:prepend!, :(SmallVectors.prepend), [item, 1, 3, 5], ([item],), no_broken...),
# (:splice!, :(SmallVectors.splice), [1, item, 3], (2, [item],), true, true, true, true), # Not implemented
(: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...),
(
:(SmallVectors.insertsorted!),
:(SmallVectors.insertsorted),
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:(SmallVectors.insertsorted!),
:(SmallVectors.insertsorted),
[1, 3, 3, 5],
(3,),
no_broken...,
),
(
:(SmallVectors.insertsortedunique!),
:(SmallVectors.insertsortedunique),
[1, 2, 3, 5],
(2,),
no_broken...,
),
(
:(SmallVectors.insertsortedunique!),
:(SmallVectors.insertsortedunique),
[1, 3, 5],
(3,),
no_broken...,
),
(
:(SmallVectors.mergesorted!),
:(SmallVectors.mergesorted),
[1, 2, 3, 3, 5],
([2, 3],),
no_broken...,
),
(
:(SmallVectors.mergesortedunique!),
:(SmallVectors.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

# 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
end
end
ans, args = [1, 3, 5, item], ([item],)
@test @inferred(vcat(x, args...)) == ans
@test @allocated(vcat(x, args...)) == 0
end
TestSmallVectors.test()

0 comments on commit 6bb3cd7

Please sign in to comment.