summaryrefslogtreecommitdiff
path: root/spec/ruby/language/numbers_spec.rb
blob: 418bc60fa635d8ad4ce2e29cd72bc5ed2f43cc39 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
require_relative '../spec_helper'

describe "A number literal" do

  it "can be a sequence of decimal digits" do
    435.should == 435
  end

  it "can have '_' characters between digits" do
    4_3_5_7.should == 4357
  end

  it "cannot have a leading underscore" do
    -> { eval("_4_2") }.should raise_error(NameError)
  end

  it "can have a decimal point" do
    4.35.should == 4.35
  end

  it "must have a digit before the decimal point" do
    0.75.should == 0.75
    -> { eval(".75")  }.should raise_error(SyntaxError)
    -> { eval("-.75") }.should raise_error(SyntaxError)
  end

  it "can have an exponent" do
    1.2e-3.should == 0.0012
  end

  it "can be a sequence of hexadecimal digits with a leading '0x'" do
    0xffff.should == 65535
  end

  it "can be a sequence of binary digits with a leading '0x'" do
    0b01011.should == 11
  end

  it "can be a sequence of octal digits with a leading '0'" do
    0377.should == 255
  end

  it "can be an integer literal with trailing 'r' to represent a Rational" do
    eval('3r').should == Rational(3, 1)
    eval('-3r').should == Rational(-3, 1)
  end

  it "can be a float literal with trailing 'r' to represent a Rational" do
    eval('0.0174532925199432957r').should == Rational(174532925199432957, 10000000000000000000)
  end

  it "can be an bignum literal with trailing 'r' to represent a Rational" do
    eval('1111111111111111111111111111111111111111111111r').should == Rational(1111111111111111111111111111111111111111111111, 1)
    eval('-1111111111111111111111111111111111111111111111r').should == Rational(-1111111111111111111111111111111111111111111111, 1)
  end

  it "can be a decimal literal with trailing 'r' to represent a Rational" do
    eval('0.3r').should == Rational(3, 10)
    eval('-0.3r').should == Rational(-3, 10)
  end

  it "can be a hexadecimal literal with trailing 'r' to represent a Rational" do
    eval('0xffr').should == Rational(255, 1)
    eval('-0xffr').should == Rational(-255, 1)
  end

  it "can be an octal literal with trailing 'r' to represent a Rational"  do
    eval('042r').should == Rational(34, 1)
    eval('-042r').should == Rational(-34, 1)
  end

  it "can be a binary literal with trailing 'r' to represent a Rational" do
    eval('0b1111r').should == Rational(15, 1)
    eval('-0b1111r').should == Rational(-15, 1)
  end

  it "can be an integer literal with trailing 'i' to represent a Complex" do
    eval('5i').should == Complex(0, 5)
    eval('-5i').should == Complex(0, -5)
  end

  it "can be a decimal literal with trailing 'i' to represent a Complex" do
    eval('0.6i').should == Complex(0, 0.6)
    eval('-0.6i').should == Complex(0, -0.6)
  end

  it "can be a hexadecimal literal with trailing 'i' to represent a Complex" do
    eval('0xffi').should == Complex(0, 255)
    eval('-0xffi').should == Complex(0, -255)
  end

  it "can be a octal literal with trailing 'i' to represent a Complex" do
    eval("042i").should == Complex(0, 34)
    eval("-042i").should == Complex(0, -34)
  end

  it "can be a binary literal with trailing 'i' to represent a Complex" do
    eval('0b1110i').should == Complex(0, 14)
    eval('-0b1110i').should == Complex(0, -14)
  end
end