Skip to content

Commit

Permalink
Merge 9e59866 into 74bf6cb
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Feb 25, 2017
2 parents 74bf6cb + 9e59866 commit 39299bc
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 82 deletions.
17 changes: 13 additions & 4 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,25 @@ 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
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; use 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 isless(order(x.pool)[x.level], order(y.pool)[y.level])
return order(x.pool)[x.level] < order(y.pool)[y.level]
end
end

Expand Down
210 changes: 132 additions & 78 deletions test/10_isless.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,55 @@ 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
@test isless(v1, v3) === true
@test isless(v2, v1) === false
@test isless(v2, v2) === false
@test isless(v2, v3) === true
@test isless(v3, v1) === false
@test isless(v3, v2) === false
@test isless(v3, v3) === false

@test ordered!(pool, true) === pool
@test isordered(pool) === true
Expand Down Expand Up @@ -91,6 +101,16 @@ module TestIsLess
@test (v3 >= v2) === true
@test (v3 >= v3) === true

@test isless(v1, v1) === false
@test isless(v1, v2) === true
@test isless(v1, v3) === true
@test isless(v2, v1) === false
@test isless(v2, v2) === false
@test isless(v2, v3) === true
@test isless(v3, v1) === false
@test isless(v3, v2) === false
@test isless(v3, v3) === false

@test levels!(pool, [2, 3, 1]) === pool
@test levels(pool) == [2, 3, 1]

Expand Down Expand Up @@ -134,46 +154,80 @@ module TestIsLess
@test (v3 >= v2) === true
@test (v3 >= v3) === true

@test isless(v1, v1) === false
@test isless(v1, v2) === false
@test isless(v1, v3) === false
@test isless(v2, v1) === true
@test isless(v2, v2) === false
@test isless(v2, v3) === true
@test isless(v3, v1) === true
@test isless(v3, v2) === false
@test isless(v3, v3) === false

@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
@test isless(v1, v3) === false
@test isless(v2, v1) === true
@test isless(v2, v2) === false
@test isless(v2, v3) === true
@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
9 changes: 9 additions & 0 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr
@test levels(x) == levels(y)
@test isordered(x)
end

# Test sort() on both unordered and ordered arrays
x = CA(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
@test sort(x) == A(["Young", "Young", "Middle", "Old"])
ordered!(x, true)
@test sort(x) == A(["Young", "Young", "Middle", "Old"])
@test sort!(x) === x
@test x == A(["Young", "Young", "Middle", "Old"])
end

end

0 comments on commit 39299bc

Please sign in to comment.