2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-04-24 09:38:01 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestComparable < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@o = Object.new
|
|
|
|
@o.extend(Comparable)
|
|
|
|
end
|
2009-11-07 22:59:42 -05:00
|
|
|
def cmp(b)
|
|
|
|
class << @o; self; end.class_eval {
|
|
|
|
undef :<=>
|
|
|
|
define_method(:<=>, b)
|
|
|
|
}
|
|
|
|
end
|
2008-04-24 09:38:01 -04:00
|
|
|
|
|
|
|
def test_equal
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o == nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o == nil)
|
2015-02-11 14:33:46 -05:00
|
|
|
cmp->(x) do nil; end
|
|
|
|
assert_equal(false, @o == nil)
|
|
|
|
|
2014-01-18 16:27:18 -05:00
|
|
|
cmp->(x) do raise NotImplementedError, "Not a RuntimeError" end
|
|
|
|
assert_raise(NotImplementedError) { @o == nil }
|
2015-02-11 14:33:46 -05:00
|
|
|
|
|
|
|
bug7688 = 'Comparable#== should not silently rescue' \
|
|
|
|
'any Exception [ruby-core:51389] [Bug #7688]'
|
|
|
|
cmp->(x) do raise StandardError end
|
|
|
|
assert_raise(StandardError, bug7688) { @o == nil }
|
|
|
|
cmp->(x) do "bad value"; end
|
|
|
|
assert_raise(ArgumentError, bug7688) { @o == nil }
|
2008-04-24 09:38:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gt
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o > nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o > nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do -1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o > nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ge
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o >= nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o >= nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do -1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o >= nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_lt
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o < nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o < nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do -1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o < nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_le
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o <= nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o <= nil)
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do -1; end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(true, @o <= nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_between
|
2009-11-07 22:59:42 -05:00
|
|
|
cmp->(x) do 0 <=> x end
|
2008-04-24 09:38:01 -04:00
|
|
|
assert_equal(false, @o.between?(1, 2))
|
|
|
|
assert_equal(false, @o.between?(-2, -1))
|
|
|
|
assert_equal(true, @o.between?(-1, 1))
|
|
|
|
assert_equal(true, @o.between?(0, 0))
|
|
|
|
end
|
|
|
|
|
2016-08-11 03:24:25 -04:00
|
|
|
def test_clamp
|
|
|
|
cmp->(x) do 0 <=> x end
|
|
|
|
assert_equal(1, @o.clamp(1, 2))
|
|
|
|
assert_equal(-1, @o.clamp(-2, -1))
|
2016-08-13 10:04:21 -04:00
|
|
|
assert_equal(@o, @o.clamp(-1, 3))
|
2016-08-11 03:24:25 -04:00
|
|
|
|
|
|
|
assert_equal(1, @o.clamp(1, 1))
|
2016-08-13 10:04:21 -04:00
|
|
|
assert_equal(@o, @o.clamp(0, 0))
|
2016-08-11 03:24:25 -04:00
|
|
|
|
|
|
|
assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') {
|
|
|
|
@o.clamp(2, 1)
|
|
|
|
}
|
2019-06-01 08:23:24 -04:00
|
|
|
end
|
2016-08-11 03:24:25 -04:00
|
|
|
|
2019-10-15 09:32:10 -04:00
|
|
|
def test_clamp_with_range
|
|
|
|
cmp->(x) do 0 <=> x end
|
|
|
|
assert_equal(1, @o.clamp(1..2))
|
|
|
|
assert_equal(-1, @o.clamp(-2..-1))
|
|
|
|
assert_equal(@o, @o.clamp(-1..3))
|
|
|
|
|
|
|
|
assert_equal(1, @o.clamp(1..1))
|
|
|
|
assert_equal(@o, @o.clamp(0..0))
|
|
|
|
|
2019-10-25 09:09:38 -04:00
|
|
|
assert_equal(1, @o.clamp(1..))
|
|
|
|
assert_equal(1, @o.clamp(1...))
|
|
|
|
assert_equal(@o, @o.clamp(0..))
|
|
|
|
assert_equal(@o, @o.clamp(0...))
|
|
|
|
assert_equal(@o, @o.clamp(..2))
|
|
|
|
assert_equal(-1, @o.clamp(-2..-1))
|
|
|
|
assert_equal(@o, @o.clamp(-2..0))
|
|
|
|
assert_equal(@o, @o.clamp(-2..))
|
|
|
|
assert_equal(@o, @o.clamp(-2...))
|
|
|
|
|
2019-10-25 13:15:18 -04:00
|
|
|
exc = [ArgumentError, 'cannot clamp with an exclusive range']
|
|
|
|
assert_raise_with_message(*exc) {@o.clamp(1...2)}
|
|
|
|
assert_raise_with_message(*exc) {@o.clamp(0...2)}
|
|
|
|
assert_raise_with_message(*exc) {@o.clamp(-1...0)}
|
|
|
|
assert_raise_with_message(*exc) {@o.clamp(...2)}
|
|
|
|
|
2019-10-15 09:32:10 -04:00
|
|
|
assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') {
|
|
|
|
@o.clamp(2..1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-24 09:38:01 -04:00
|
|
|
def test_err
|
|
|
|
assert_raise(ArgumentError) { 1.0 < nil }
|
|
|
|
assert_raise(ArgumentError) { 1.0 < Object.new }
|
2014-08-21 00:39:32 -04:00
|
|
|
e = EnvUtil.labeled_class("E\u{30a8 30e9 30fc}")
|
2014-08-19 00:59:44 -04:00
|
|
|
assert_raise_with_message(ArgumentError, /E\u{30a8 30e9 30fc}/) {
|
|
|
|
1.0 < e.new
|
|
|
|
}
|
2008-04-24 09:38:01 -04:00
|
|
|
end
|
2013-02-17 06:55:50 -05:00
|
|
|
|
|
|
|
def test_inversed_compare
|
|
|
|
bug7870 = '[ruby-core:52305] [Bug #7870]'
|
|
|
|
assert_nothing_raised(SystemStackError, bug7870) {
|
|
|
|
assert_nil(Time.new <=> "")
|
|
|
|
}
|
|
|
|
end
|
2013-10-09 00:53:18 -04:00
|
|
|
|
|
|
|
def test_no_cmp
|
|
|
|
bug9003 = '[ruby-core:57736] [Bug #9003]'
|
|
|
|
assert_nothing_raised(SystemStackError, bug9003) {
|
|
|
|
@o <=> @o.dup
|
|
|
|
}
|
|
|
|
end
|
2008-04-24 09:38:01 -04:00
|
|
|
end
|