File: uminus_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 (58 lines) | stat: -rw-r--r-- 2,079 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
require_relative '../../spec_helper'
require 'bigdecimal'

describe "BigDecimal#-@" do
  before :each do
    @one = BigDecimal("1")
    @zero = BigDecimal("0")
    @zero_pos = BigDecimal("+0")
    @zero_neg = BigDecimal("-0")
    @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")
    @big = BigDecimal("333E99999")
    @big_neg = BigDecimal("-333E99999")
    @values = [@one, @zero, @zero_pos, @zero_neg, @infinity,
      @infinity_minus, @one_minus, @frac_1, @frac_2, @big, @big_neg]
  end

  it "negates self" do
    @one.send(:-@).should == @one_minus
    @one_minus.send(:-@).should == @one
    @frac_1.send(:-@).should == BigDecimal("-1E-99999")
    @frac_2.send(:-@).should == BigDecimal("-0.9E-99999")
    @big.send(:-@).should == @big_neg
    @big_neg.send(:-@).should == @big
    BigDecimal("2.221").send(:-@).should == BigDecimal("-2.221")
    BigDecimal("2E10000").send(:-@).should == BigDecimal("-2E10000")
    some_number = BigDecimal("2455999221.5512")
    some_number_neg = BigDecimal("-2455999221.5512")
    some_number.send(:-@).should == some_number_neg
    (-BigDecimal("-5.5")).should == BigDecimal("5.5")
    another_number = BigDecimal("-8.551551551551551551")
    another_number_pos = BigDecimal("8.551551551551551551")
    another_number.send(:-@).should == another_number_pos
    @values.each do |val|
      (val.send(:-@).send(:-@)).should == val
    end
  end

  it "properly handles special values" do
    @infinity.send(:-@).should == @infinity_minus
    @infinity_minus.send(:-@).should == @infinity
    @infinity.send(:-@).infinite?.should == -1
    @infinity_minus.send(:-@).infinite?.should == 1

    @zero.send(:-@).should == @zero
    @zero.send(:-@).sign.should == -1
    @zero_pos.send(:-@).should == @zero
    @zero_pos.send(:-@).sign.should == -1
    @zero_neg.send(:-@).should == @zero
    @zero_neg.send(:-@).sign.should == 1

    @nan.send(:-@).should.nan?
  end
end