File: round_spec.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 (82 lines) | stat: -rw-r--r-- 3,215 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
require File.expand_path('../../../spec_helper', __FILE__)

describe "Float#round" do
  it "returns the nearest Integer" do
    5.5.round.should  eql( 6 )
    0.4.round.should  eql( 0 )
    -2.8.round.should eql( -3)
    0.0.round.should  eql( 0 )
    0.49999999999999994.round.should eql(0) # see http://jira.codehaus.org/browse/JRUBY-5048
  end

  it "raises FloatDomainError for exceptional values" do
    lambda { (+infinity_value).round }.should raise_error(FloatDomainError)
    lambda { (-infinity_value).round }.should raise_error(FloatDomainError)
    lambda { nan_value.round }.should raise_error(FloatDomainError)
  end

  ruby_version_is "1.9" do
    it "rounds self to an optionally given precision" do
      5.5.round(0).should eql(6)
      5.7.round(1).should eql(5.7)
      1.2345678.round(2).should == 1.23
      123456.78.round(-2).should eql(123500) # rounded up
      -123456.78.round(-2).should eql(-123500)
      12.345678.round(3.999).should == 12.346
    end

    it "returns zero when passed a negative argument with magitude greater the magitude of the whole number portion of the Float" do
      0.8346268.round(-1).should eql(0)
    end

    it "raises a TypeError when its argument can not be converted to an Integer" do
      lambda { 1.0.round("4") }.should raise_error(TypeError)
      lambda { 1.0.round(nil) }.should raise_error(TypeError)
    end

    it "raises FloatDomainError for exceptional values when passed a non-positive precision" do
      lambda { Float::INFINITY.round( 0) }.should raise_error(FloatDomainError)
      lambda { Float::INFINITY.round(-2) }.should raise_error(FloatDomainError)
      lambda { (-Float::INFINITY).round( 0) }.should raise_error(FloatDomainError)
      lambda { (-Float::INFINITY).round(-2) }.should raise_error(FloatDomainError)
    end

    it "raises RangeError for NAN when passed a non-positive precision" do
      lambda { Float::NAN.round(0) }.should raise_error(RangeError)
      lambda { Float::NAN.round(-2) }.should raise_error(RangeError)
    end

    it "returns self for exceptional values when passed a non-negative precision" do
      Float::INFINITY.round(2).should == Float::INFINITY
      (-Float::INFINITY).round(2).should == -Float::INFINITY
      Float::NAN.round(2).should be_nan
    end

    ruby_bug "redmine:5227",  "1.9.2" do
      it "works for corner cases" do
        42.0.round(308).should eql(42.0)
        1.0e307.round(2).should eql(1.0e307)
      end
    end

    ruby_bug "redmine:5271",  "1.9.3.0" do
      it "returns rounded values for big argument" do
        0.42.round(2.0**30).should == 0.42
      end
    end

    ruby_bug "redmine #5272", "1.9.3" do
      it "returns rounded values for big values" do
        +2.5e20.round(-20).should   eql( +3 * 10 ** 20  )
        +2.4e20.round(-20).should   eql( +2 * 10 ** 20  )
        -2.5e20.round(-20).should   eql( -3 * 10 ** 20  )
        -2.4e20.round(-20).should   eql( -2 * 10 ** 20  )
        +2.5e200.round(-200).should eql( +3 * 10 ** 200 )
        +2.4e200.round(-200).should eql( +2 * 10 ** 200 )
        -2.5e200.round(-200).should eql( -3 * 10 ** 200 )
        -2.4e200.round(-200).should eql( -2 * 10 ** 200 )
      end
    end
  end

end