Skip to content

Commit

Permalink
Add tests for isless() and < failure cases
Browse files Browse the repository at this point in the history
Also throw a more specific ArgumentError.
  • Loading branch information
nalimilan committed Feb 25, 2017
1 parent 0a1cb4c commit 9e59866
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 82 deletions.
8 changes: 4 additions & 4 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ Base.isequal(x::Any, y::CategoricalValue) = isequal(y, x)
Base.hash(x::CategoricalValue, h::UInt) = hash(index(x.pool)[x.level], h)

function Base.isless{S, T}(x::CategoricalValue{S}, y::CategoricalValue{T})
error("CategoricalValue objects with different pools cannot be tested for order")
throw(ArgumentError("CategoricalValue objects with different pools cannot be tested for order"))
end

# Method defined even on unordered values so that sort() works
function Base.isless{T}(x::CategoricalValue{T}, y::CategoricalValue{T})
if x.pool !== y.pool
error("CategoricalValue objects with different pools cannot be tested for order")
throw(ArgumentError("CategoricalValue objects with different pools cannot be tested for order"))
else
return order(x.pool)[x.level] < order(y.pool)[y.level]
end
end

function Base.:<{T}(x::CategoricalValue{T}, y::CategoricalValue{T})
if x.pool !== y.pool
error("CategoricalValue objects with different pools cannot be tested for order")
throw(ArgumentError("CategoricalValue objects with different pools cannot be tested for order"))
elseif !isordered(x.pool) # !isordered(y.pool) is implied by x.pool === y.pool
error("Unordered CategoricalValue objects cannot be tested for order using <. Use isless instead, or call the ordered! function on the parent array to change this")
throw(ArgumentError("Unordered CategoricalValue objects cannot be tested for order using <. Use isless instead, or call the ordered! function on the parent array to change this"))
else
return order(x.pool)[x.level] < order(y.pool)[y.level]
end
Expand Down
170 changes: 92 additions & 78 deletions test/10_isless.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ module TestIsLess
v2 = CategoricalValue(2, pool)
v3 = CategoricalValue(3, pool)

@test_throws Exception v1 < v1
@test_throws Exception v1 < v2
@test_throws Exception v1 < v3
@test_throws Exception v2 < v1
@test_throws Exception v2 < v2
@test_throws Exception v2 < v3
@test_throws Exception v3 < v1
@test_throws Exception v3 < v2
@test_throws Exception v3 < v3

@test_throws Exception v1 <= v1
@test_throws Exception v1 <= v2
@test_throws Exception v1 <= v3
@test_throws Exception v2 <= v1
@test_throws Exception v2 <= v2
@test_throws Exception v2 <= v3
@test_throws Exception v3 <= v1
@test_throws Exception v3 <= v2
@test_throws Exception v3 <= v3

@test_throws Exception v1 > v1
@test_throws Exception v1 > v2
@test_throws Exception v1 > v3
@test_throws Exception v2 > v1
@test_throws Exception v2 > v2
@test_throws Exception v2 > v3
@test_throws Exception v3 > v1
@test_throws Exception v3 > v2
@test_throws Exception v3 > v3

@test_throws Exception v1 >= v1
@test_throws Exception v1 >= v2
@test_throws Exception v1 >= v3
@test_throws Exception v2 >= v1
@test_throws Exception v2 >= v2
@test_throws Exception v2 >= v3
@test_throws Exception v3 >= v1
@test_throws Exception v3 >= v2
@test_throws Exception v3 >= v3
@test_throws ArgumentError v1 < v1
@test_throws ArgumentError v1 < v2
@test_throws ArgumentError v1 < v3
@test_throws ArgumentError v2 < v1
@test_throws ArgumentError v2 < v2
@test_throws ArgumentError v2 < v3
@test_throws ArgumentError v3 < v1
@test_throws ArgumentError v3 < v2
@test_throws ArgumentError v3 < v3

@test_throws ArgumentError v1 <= v1
@test_throws ArgumentError v1 <= v2
@test_throws ArgumentError v1 <= v3
@test_throws ArgumentError v2 <= v1
@test_throws ArgumentError v2 <= v2
@test_throws ArgumentError v2 <= v3
@test_throws ArgumentError v3 <= v1
@test_throws ArgumentError v3 <= v2
@test_throws ArgumentError v3 <= v3

@test_throws ArgumentError v1 > v1
@test_throws ArgumentError v1 > v2
@test_throws ArgumentError v1 > v3
@test_throws ArgumentError v2 > v1
@test_throws ArgumentError v2 > v2
@test_throws ArgumentError v2 > v3
@test_throws ArgumentError v3 > v1
@test_throws ArgumentError v3 > v2
@test_throws ArgumentError v3 > v3

@test_throws ArgumentError v1 >= v1
@test_throws ArgumentError v1 >= v2
@test_throws ArgumentError v1 >= v3
@test_throws ArgumentError v2 >= v1
@test_throws ArgumentError v2 >= v2
@test_throws ArgumentError v2 >= v3
@test_throws ArgumentError v3 >= v1
@test_throws ArgumentError v3 >= v2
@test_throws ArgumentError v3 >= v3

@test isless(v1, v1) === false
@test isless(v1, v2) === true
Expand Down Expand Up @@ -167,45 +167,45 @@ module TestIsLess
@test ordered!(pool, false) === pool
@test isordered(pool) === false

@test_throws Exception v1 < v1
@test_throws Exception v1 < v2
@test_throws Exception v1 < v3
@test_throws Exception v2 < v1
@test_throws Exception v2 < v2
@test_throws Exception v2 < v3
@test_throws Exception v3 < v1
@test_throws Exception v3 < v2
@test_throws Exception v3 < v3

@test_throws Exception v1 <= v1
@test_throws Exception v1 <= v2
@test_throws Exception v1 <= v3
@test_throws Exception v2 <= v1
@test_throws Exception v2 <= v2
@test_throws Exception v2 <= v3
@test_throws Exception v3 <= v1
@test_throws Exception v3 <= v2
@test_throws Exception v3 <= v3

@test_throws Exception v1 > v1
@test_throws Exception v1 > v2
@test_throws Exception v1 > v3
@test_throws Exception v2 > v1
@test_throws Exception v2 > v2
@test_throws Exception v2 > v3
@test_throws Exception v3 > v1
@test_throws Exception v3 > v2
@test_throws Exception v3 > v3

@test_throws Exception v1 >= v1
@test_throws Exception v1 >= v2
@test_throws Exception v1 >= v3
@test_throws Exception v2 >= v1
@test_throws Exception v2 >= v2
@test_throws Exception v2 >= v3
@test_throws Exception v3 >= v1
@test_throws Exception v3 >= v2
@test_throws Exception v3 >= v3
@test_throws ArgumentError v1 < v1
@test_throws ArgumentError v1 < v2
@test_throws ArgumentError v1 < v3
@test_throws ArgumentError v2 < v1
@test_throws ArgumentError v2 < v2
@test_throws ArgumentError v2 < v3
@test_throws ArgumentError v3 < v1
@test_throws ArgumentError v3 < v2
@test_throws ArgumentError v3 < v3

@test_throws ArgumentError v1 <= v1
@test_throws ArgumentError v1 <= v2
@test_throws ArgumentError v1 <= v3
@test_throws ArgumentError v2 <= v1
@test_throws ArgumentError v2 <= v2
@test_throws ArgumentError v2 <= v3
@test_throws ArgumentError v3 <= v1
@test_throws ArgumentError v3 <= v2
@test_throws ArgumentError v3 <= v3

@test_throws ArgumentError v1 > v1
@test_throws ArgumentError v1 > v2
@test_throws ArgumentError v1 > v3
@test_throws ArgumentError v2 > v1
@test_throws ArgumentError v2 > v2
@test_throws ArgumentError v2 > v3
@test_throws ArgumentError v3 > v1
@test_throws ArgumentError v3 > v2
@test_throws ArgumentError v3 > v3

@test_throws ArgumentError v1 >= v1
@test_throws ArgumentError v1 >= v2
@test_throws ArgumentError v1 >= v3
@test_throws ArgumentError v2 >= v1
@test_throws ArgumentError v2 >= v2
@test_throws ArgumentError v2 >= v3
@test_throws ArgumentError v3 >= v1
@test_throws ArgumentError v3 >= v2
@test_throws ArgumentError v3 >= v3

@test isless(v1, v1) === false
@test isless(v1, v2) === false
Expand All @@ -216,4 +216,18 @@ module TestIsLess
@test isless(v3, v1) === true
@test isless(v3, v2) === false
@test isless(v3, v3) === false


# Test that ordering comparisons between pools fail
ordered!(pool, true)
pool2 = CategoricalPool([1, 2, 3])
ordered!(pool2, true)

v = CategoricalValue(1, pool2)

@test_throws ArgumentError v < v1
@test_throws ArgumentError v <= v1
@test_throws ArgumentError v > v1
@test_throws ArgumentError v >= v1
@test_throws ArgumentError isless(v, v1)
end

0 comments on commit 9e59866

Please sign in to comment.