File: sub_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (70 lines) | stat: -rw-r--r-- 2,296 bytes parent folder | download | duplicates (6)
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
require_relative '../../spec_helper'
require 'bigdecimal'

describe "BigDecimal#sub" do

  before :each do
    @one = BigDecimal("1")
    @zero = BigDecimal("0")
    @two = BigDecimal("2")
    @three = BigDecimal("3")
    @nan = BigDecimal("NaN")
    @infinity = BigDecimal("Infinity")
    @infinity_minus = BigDecimal("-Infinity")
    @one_minus = BigDecimal("-1")
    @frac_1 = BigDecimal("1E-99999")
    @frac_2 = BigDecimal("0.9E-99999")
    @frac_3 = BigDecimal("12345E10")
    @frac_4 = BigDecimal("98765E10")
  end

  it "returns a - b with given precision" do
    # documentation states, that precision is optional
    # but implementation raises ArgumentError if not given.

    @two.sub(@one, 1).should == @one
    @one.sub(@two, 1).should == @one_minus
    @one.sub(@one_minus, 1).should == @two
    @frac_2.sub(@frac_1, 1000000).should == BigDecimal("-0.1E-99999")
    @frac_2.sub(@frac_1, 1).should == BigDecimal("-0.1E-99999")
    # the above two examples puzzle me.
    in_arow_one = BigDecimal("1.23456789")
    in_arow_two = BigDecimal("1.2345678")
    in_arow_one.sub(in_arow_two, 10).should == BigDecimal("0.9E-7")
    @two.sub(@two,1).should == @zero
    @frac_1.sub(@frac_1, 1000000).should == @zero
  end

  describe "with Object" do
    it "tries to coerce the other operand to self" do
      object = mock("Object")
      object.should_receive(:coerce).with(@frac_3).and_return([@frac_3, @frac_4])
      @frac_3.sub(object, 1).should == BigDecimal("-0.9E15")
    end
  end

  describe "with Rational" do
    it "produces a BigDecimal" do
      (@three - Rational(500, 2)).should == BigDecimal('-0.247e3')
    end
  end

  it "returns NaN if NaN is involved" do
    @one.sub(@nan, 1).should.nan?
    @nan.sub(@one, 1).should.nan?
  end

  it "returns NaN if both values are infinite with the same signs" do
    @infinity.sub(@infinity, 1).should.nan?
    @infinity_minus.sub(@infinity_minus, 1).should.nan?
  end

  it "returns Infinity or -Infinity if these are involved" do
    @infinity.sub(@infinity_minus, 1).should == @infinity
    @infinity_minus.sub(@infinity, 1).should == @infinity_minus
    @zero.sub(@infinity, 1).should == @infinity_minus
    @frac_2.sub( @infinity, 1).should == @infinity_minus
    @two.sub(@infinity, 1).should == @infinity_minus
  end

end