forked from jakobnissen/StackCollections.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdda88c
commit 4b1b39a
Showing
6 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
v´ = 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
v´ = collect(v) | ||
val = rand(Bool) | ||
ind = rand(1:L) | ||
v´[ind] = val | ||
v = setindex(v, val, ind) | ||
@test collect(v) == 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) | ||
v´ = 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) | ||
v´ = collect(v) | ||
r = F(v) | ||
r´ = F(v´) | ||
if r´ === nothing | ||
@test r === nothing | ||
else | ||
@test collect(r) == r´ | ||
if collect(r) != 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), | ||
=# |