File: lt_spec.rb

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (89 lines) | stat: -rw-r--r-- 2,647 bytes parent folder | download
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
require File.dirname(__FILE__) + '/../../spec_helper'
require 'bigdecimal'

describe "BigDecimal#<" do
  before(:each) do
    @zero = BigDecimal("0")
    @zero_pos = BigDecimal("+0")
    @zero_neg = BigDecimal("-0")
    @mixed = BigDecimal("1.23456789")
    @pos_int = BigDecimal("2E5555")
    @neg_int = BigDecimal("-2E5555")
    @pos_frac = BigDecimal("2E-9999")
    @neg_frac = BigDecimal("-2E-9999")

    @int_mock = mock('123')
    class << @int_mock
      def coerce(other)
        return [other, BigDecimal('123')]
      end
      def < (other)
        BigDecimal('123') < other
      end
    end

    @values = [@mixed, @pos_int, @neg_int, @pos_frac, @neg_frac,
      -2**32, -2**31, -2**30, -2**16, -2**8, -100, -10, -1,
      @zero , 1, 2, 10, 2**8, 2**16, 2**32, @int_mock, @zero_pos, @zero_neg]

    @infinity = BigDecimal("Infinity")
    @infinity_neg = BigDecimal("-Infinity")
    @nan = BigDecimal("NaN")
  end
  
  it "returns true if a < b" do
    one = BigDecimal("1")
    two = BigDecimal("2")
    frac_1 = BigDecimal("1E-99999")
    frac_2 = BigDecimal("0.9E-99999")
    (@zero < one).should == true
    (two < @zero).should == false
    (frac_2 < frac_1).should == true
    (@neg_int < @pos_int).should == true
    (@pos_int < @neg_int).should == false
    (@neg_int < @pos_frac).should == true
    (@pos_frac < @neg_int).should == false
    (@zero < @zero_pos).should == false
    (@zero < @zero_neg).should == false
    (@zero_neg < @zero_pos).should == false
    (@zero_pos < @zero_neg).should == false
  end

  it "properly handles infinity values" do
    @values.each { |val|
      (val < @infinity).should == true
      (@infinity < val).should == false
      (val < @infinity_neg).should == false
      (@infinity_neg < val).should == true
    }
    (@infinity < @infinity).should == false
    (@infinity_neg < @infinity_neg).should == false
    (@infinity < @infinity_neg).should == false
    (@infinity_neg < @infinity).should == true
  end

  it "properly handles NaN values" do
    @values += [@infinity, @infinity_neg, @nan]
    @values << nil
    @values << Object.new
    @values.each { |val|
      (@nan < val).should == nil
    }

    lambda { 10 < @nan }.should raise_error(ArgumentError)
    (10.5 < @nan).should == false

    (@infinity < @nan).should == nil
    (@infinity_neg < @nan).should == nil
    (@zero < @nan).should == nil
  end

  it "returns nil if the argument is nil" do
    (@zero < nil).should == nil
    (@infinity < nil).should == nil
    (@infinity_neg < nil).should == nil
    (@mixed < nil).should == nil
    (@pos_int < nil).should == nil
    (@neg_frac < nil).should == nil
  end
end