Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed May 5, 2020
1 parent cdda88c commit 4b1b39a
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/digitset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end
Base.in(x::Int, s::DigitSet) = isodd(s.x >>> unsigned(x))
Base.length(x::DigitSet) = count_ones(x.x)

Base.maximim(x::DigitSet, ::Unsafe) = Sys.WORD_SIZE - 1 - leading_zeros(x.x)
Base.maximum(x::DigitSet, ::Unsafe) = Sys.WORD_SIZE - 1 - leading_zeros(x.x)
function Base.maximum(x::DigitSet)
isempty(x) && throw(ArgumentError("collection must be non-empty"))
return maximum(x, unsafe)
Expand Down
1 change: 0 additions & 1 deletion src/onehotvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ end
Base.getindex(v::OneHotVector, ::Colon) = v
Base.argmax(v::OneHotVector) = v.index
Base.argmin(v::OneHotVector) = 1 + ((v.index == 1) & (length(v) != 1))
Base.iterate(v::OneHotVector, i=1) = i > length(v) ? nothing : (i == v.index, i+1)
Base.count(::typeof(identity), v::OneHotVector) = 1
Base.allunique(v::OneHotVector) = length(v) < 3

Expand Down
6 changes: 5 additions & 1 deletion src/stackvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct StackVector{L} <: AbstractVector{Bool}

function StackVector{L}(x::UInt, ::Unsafe) where L
L isa Int || throw(TypeError(:StackVector, "", Int, typeof(L)))
((L Sys.WORD_SIZE) & (L > -1)) || throw(DomainError(M, "L must be 0:$(Sys.WORD_SIZE)"))
((L Sys.WORD_SIZE) & (L > -1)) || throw(DomainError(L, "L must be 0:$(Sys.WORD_SIZE)"))
new(x)
end
end
Expand All @@ -20,6 +20,9 @@ end

StackVector{L}() where L = StackVector{L}(UInt(0), unsafe)
StackVector(x...) = StackVector{Sys.WORD_SIZE}(x...)
@noinline function toofew(L)
throw(ArgumentError("StackVector{$L} must be constructed from $L elements"))
end

function StackVector{L}(itr) where L
bits = zero(UInt)
Expand All @@ -30,6 +33,7 @@ function StackVector{L}(itr) where L
val = convert(UInt, convert(Bool, i))
bits |= (val << ((index-1) & 63))
end
index == L || toofew(L)
StackVector{L}(bits, unsafe)
end

Expand Down
50 changes: 50 additions & 0 deletions test/onehotvector.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@testset "Construction" begin
@test OneHotVector(10, 1) == OneHotVector(10, 1)
@test_throws ArgumentError OneHotVector(1, 10)
@test_throws ArgumentError OneHotVector(0, 1)

arr = zeros(Bool, 4)
@test_throws ArgumentError OneHotVector(arr)
arr[2] = true
@test OneHotVector(arr) == OneHotVector(4, 2)
arr[4] = true
@test_throws ArgumentError OneHotVector(arr)
end

@testset "Basics" begin
@test eltype(OneHotVector) == Bool
@test length(OneHotVector(1, 1)) == 1
@test length(OneHotVector(23241, 9)) == 23241
end

@testset "Getindex" begin
v = OneHotVector(11, 3)
@test_throws BoundsError v[0]
@test_throws BoundsError v[-5]
@test_throws BoundsError v[15]

@test !(v[11])
@test v[3]

@test first(OneHotVector(1, 1))
end

@testset "Misc" begin
for len in [1, 2, 4, 9, 11, 22, 115, 500]
for i in 1:5
v = OneHotVector(len, rand(1:len))
= collect(v)

for F in [argmax, argmin, sum, count, allunique,
x -> findfirst(isodd, x), x -> findfirst(isequal(4), x)]
@test F(v) == F(v´)
end

for F in [reverse, x -> circshift(x, 1), x -> circshift(x, -5),
x -> circshift(x, 500), x -> filter(iseven, x),
x -> filter(y -> true, x), x -> filter(isequal(3), x)]
@test collect(F(v)) == F(v´)
end
end
end
end
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ using StackCollections
include("digitset.jl")
end

#=

@testset "StackSet" begin
include("stackset.jl")
end

@testset "OneHotVector" begin
include("onehotvector.jl")
end

@testset "StackVector" begin
include("stackvector.jl")
end
=#
129 changes: 129 additions & 0 deletions test/stackvector.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
random_stackvector(L::Integer) = StackVector{L}(rand(Bool, L))

@testset "Construction" begin
@test StackVector() == StackVector{Sys.WORD_SIZE}()

unsafe = StackCollections.unsafe
@test StackVector{Sys.WORD_SIZE}() == StackVector()
@test StackVector{Sys.WORD_SIZE}(UInt(11), unsafe) == StackVector(UInt(11), unsafe)
@test StackVector{Sys.WORD_SIZE}(UInt(0), unsafe) == StackVector(UInt(0), unsafe)

@test_throws TypeError StackVector{Int}()
@test_throws TypeError StackVector{0x01}()

@test_throws DomainError StackVector{-1}()
@test_throws DomainError StackVector{99}()
@test_throws DomainError StackVector{65}()

# Too few
@test_throws ArgumentError StackVector{2}([true])
@test_throws ArgumentError StackVector{4}([true, false])
@test_throws ArgumentError StackVector{9}([true, false, true, true, false])

# Too many
@test_throws BoundsError StackVector{0}([1])
@test_throws BoundsError StackVector{4}([true, false, true, false, false])
@test_throws BoundsError StackVector(rand(Bool, 100))

# Type can't be converted
@test_throws MethodError StackVector{2}(["y", "n"])
@test_throws MethodError StackVector{2}([isodd, iseven])

# Value can't be converted
@test_throws InexactError StackVector{11}(fill(-3, 11))
@test_throws InexactError StackVector{3}(fill(2, 3))
end

@testset "Basic" begin
@test eltype(StackVector{5}) == Bool
for L in [0, 1, 5, 10, 32, 64]
v = random_stackvector(L)
@test length(v) == L
end
end

@testset "Get/setindex" begin
v = StackVector{6}([0, 1, 1, 1, 0, 0])
@test_throws BoundsError v[0]
@test_throws BoundsError v[-4]
@test_throws BoundsError v[8]

@test v[1] == false
@test v[3] == true

@test setindex(v, true, 5) == StackVector{6}([0, 1, 1, 1, 1, 0])
@test setindex(v, false, 2) == StackVector{6}([0, 0, 1, 1, 0, 0])
@test setindex(v, true, 3) == StackVector{6}([0, 1, 1, 1, 0, 0])
@test setindex(v, false, 1) == StackVector{6}([0, 1, 1, 1, 0, 0])

for L in [1, 5, 10, 32, 64]
for i in 1:10
v = random_stackvector(L)
= collect(v)
val = rand(Bool)
ind = rand(1:L)
v´[ind] = val
v = setindex(v, val, ind)
@test collect(v) ==
end
end
end

@testset "Misc" begin
@test isempty(StackVector{0}())
@test !isempty(StackVector{9}())
@test !isempty(random_stackvector(9))
@test isempty(random_stackvector(0))

@test_throws ArgumentError maximum(random_stackvector(0))
@test_throws ArgumentError minimum(random_stackvector(0))

@test_throws ArgumentError argmin(random_stackvector(0))
@test_throws ArgumentError argmax(random_stackvector(0))

for L in [1, 5, 10, 32, 64]
for i in 1:10
v = random_stackvector(L)
= collect(v)
@test iterate(v)[1] == iterate(v´)[1]
@test (true in v) == (true in v´)
@test (false in v) == (false in v´)
for F in [maximum, minimum, sum, argmin, argmax]
@test F(v) == F(v´)
end
@test [!i for i in v´] == collect(!v)
end
end
end

@testset "Predicates" begin
for (N, F) in enumerate([reverse, x -> circshift(x, 0), x -> circshift(x, -3)])
#reverse])
#x -> circshift(x, 0), x -> circshift(x, -3), x -> circshift(x, 500)
#])
for L in [1, 5, 10, 32, 64]
for i in 1:10
v = random_stackvector(L)
= collect(v)
r = F(v)
= F(v´)
if=== nothing
@test r === nothing
else
@test collect(r) ==
if collect(r) !=
println(r)
println(r´)
println(F, " ", N)
println()
end
end
end
end
end
end

#=
x -> findfirst(isodd, x), x -> findfirst(y -> true, x),
x -> findfirst(y -> false, x),
=#

0 comments on commit 4b1b39a

Please sign in to comment.