File: test_comparable.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (155 lines) | stat: -rw-r--r-- 4,554 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'test/unit'

class TestComparable < Test::Unit::TestCase
  class Smallest
    include Comparable
    def <=> (other)
      -1
    end
  end

  class Equal
    include Comparable
    def <=> (other)
      0
    end
  end

  class Biggest
    include Comparable
    def <=> (other)
      1
    end
  end

  class Different
    include Comparable
    def <=> (other)
      nil
    end
  end

  def setup
    @s = Smallest.new
    @e = Equal.new
    @b = Biggest.new
    @d = Different.new
  end

  def test_spaceship
    assert_equal(-1, @s <=> 3)
    assert_equal(-1, @s <=> 1)
    assert_equal(0, @e <=> 3)
    assert_equal(0, @e <=> 1)
    assert_equal(1, @b <=> 3)
    assert_equal(1, @b <=> 1)
    assert_equal(nil, @d <=> 3)
    assert_equal(nil, @d <=> 1)
  end

  def test_smaller
    assert_equal(true,  @s < 3)
    assert_equal(true,  @s <= 3)
    assert_equal(false, @s > 3)
    assert_equal(false, @s >= 3)
    assert_equal(false, @s == 3)
    assert_equal(false, @s.between?(1, 3))
  end

  def test_equal
    assert_equal(false, @e < 3)
    assert_equal(true,  @e <= 3)
    assert_equal(false, @e > 3)
    assert_equal(true, @e >= 3)
    assert_equal(true, @e == 3)
    assert_equal(true, @e.between?(1, 3))
  end

  def test_bigger
    assert_equal(false, @b < 3)
    assert_equal(false, @b <= 3)
    assert_equal(true,  @b > 3)
    assert_equal(true,  @b >= 3)
    assert_equal(false, @b == 3)
    assert_equal(false, @b.between?(1, 3))
  end

  def test_different
    assert_raises(ArgumentError) { @d < 3 }
    assert_raises(ArgumentError) { @d <= 3 }
    assert_raises(ArgumentError) { @d > 3 }
    assert_raises(ArgumentError) { @d >= 3 }
    assert(@d != 3)
    assert_raises(ArgumentError) { @d.between?(1, 3) }
  end

  def test_cmp_non_comparables
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    # should not raise errors :
    assert_equal nil, 0 <=> Time.now
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, nil <=> 42 if RUBY_VERSION > '1.9'
    # 1.8.7 NoMethodError: undefined method `<=>' for nil:NilClass
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, '42' <=> nil
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, Object.new <=> nil if RUBY_VERSION > '1.9'
    # 1.8.7 NoMethodError: undefined method `<=>' for #<Object:0xceb431e>
    assert $!.nil?, "$! not nil but: #{$!.inspect}"

    require 'bigdecimal'
    big = BigDecimal.new '-10000000000'
    assert_equal nil, big <=> Time.now
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, big <=> nil
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, big <=> Object.new
    assert $!.nil?, "$! not nil but: #{$!.inspect}"

    # Symbol only includes Comparable on 1.9
    assert_equal nil, :sym <=> nil if RUBY_VERSION > '1.9'
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal nil, :sym <=> 42 if RUBY_VERSION > '1.9'
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0,   :sym <=> :sym if RUBY_VERSION > '1.9'
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert (:zym <=> :sym) > 0 if RUBY_VERSION > '1.9'
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
  end

  def test_compareTo_non_comparables
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    # should not raise errors :
    # TODO: RubyFixnum#compareTo breaks the contract of not-throwing
    # with: TypeError: can't convert nil into Integer
    #assert_equal 0, invokeCompareTo(0, Time.now)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0, invokeCompareTo(nil, 42)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0, invokeCompareTo(Object.new, nil)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"

    require 'bigdecimal'
    big = BigDecimal.new '-10000000000'
    assert_equal 0, invokeCompareTo(big, Time.now)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0, invokeCompareTo(big, Object.new)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"

    assert_equal 0, invokeCompareTo(:sym, nil)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0, invokeCompareTo(:sym, 42)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert_equal 0, invokeCompareTo(:sym, :sym)
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
    assert invokeCompareTo(:zym, :sym) > 0
    assert $!.nil?, "$! not nil but: #{$!.inspect}"
  end

  private

  def invokeCompareTo(o1, o2)
    Java::DefaultPackageClass.compareTo(o1, o2)
  end

end