File: abs_spec.rb

package info (click to toggle)
ruby3.4 3.4.5-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 154,784 kB
  • sloc: ruby: 1,259,653; ansic: 829,955; yacc: 28,233; pascal: 7,359; sh: 3,864; python: 1,799; cpp: 1,158; asm: 808; makefile: 801; javascript: 414; lisp: 109; perl: 62; awk: 36; sed: 4; xml: 4
file content (50 lines) | stat: -rw-r--r-- 1,515 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
require_relative '../../spec_helper'
require 'bigdecimal'

describe "BigDecimal#abs" do
  before :each do
    @one = BigDecimal("1")
    @zero = BigDecimal("0")
    @zero_pos = BigDecimal("+0")
    @zero_neg = BigDecimal("-0")
    @two = BigDecimal("2")
    @three = BigDecimal("3")
    @mixed = BigDecimal("1.23456789")
    @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")
  end

  it "returns the absolute value" do
    pos_int = BigDecimal("2E5555")
    neg_int = BigDecimal("-2E5555")
    pos_frac = BigDecimal("2E-9999")
    neg_frac = BigDecimal("-2E-9999")

    pos_int.abs.should == pos_int
    neg_int.abs.should == pos_int
    pos_frac.abs.should == pos_frac
    neg_frac.abs.should == pos_frac
    @one.abs.should == 1
    @two.abs.should == 2
    @three.abs.should == 3
    @mixed.abs.should == @mixed
    @one_minus.abs.should == @one
  end

  it "properly handles special values" do
    @infinity.abs.should == @infinity
    @infinity_minus.abs.should == @infinity
    @nan.abs.should.nan? # have to do it this way, since == doesn't work on NaN
    @zero.abs.should == 0
    @zero.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
    @zero_pos.abs.should == 0
    @zero_pos.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
    @zero_neg.abs.should == 0
    @zero_neg.abs.sign.should == BigDecimal::SIGN_POSITIVE_ZERO
  end

end