source: EcnlProtoTool/trunk/mruby-1.2.0/test/t/float.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 4.2 KB
Line 
1##
2# Float ISO Test
3
4assert('Float', '15.2.9') do
5 assert_equal Class, Float.class
6end
7
8assert('Float#+', '15.2.9.3.1') do
9 a = 3.123456788 + 0.000000001
10 b = 3.123456789 + 1
11
12 assert_float(3.123456789, a)
13 assert_float(4.123456789, b)
14
15 assert_raise(TypeError){ 0.0+nil }
16 assert_raise(TypeError){ 1.0+nil }
17end
18
19assert('Float#-', '15.2.9.3.2') do
20 a = 3.123456790 - 0.000000001
21 b = 5.123456789 - 1
22
23 assert_float(3.123456789, a)
24 assert_float(4.123456789, b)
25end
26
27assert('Float#*', '15.2.9.3.3') do
28 a = 3.125 * 3.125
29 b = 3.125 * 1
30
31 assert_float(9.765625, a)
32 assert_float(3.125 , b)
33end
34
35assert('Float#/', '15.2.9.3.4') do
36 a = 3.123456789 / 3.123456789
37 b = 3.123456789 / 1
38
39 assert_float(1.0 , a)
40 assert_float(3.123456789, b)
41end
42
43assert('Float#%', '15.2.9.3.5') do
44 a = 3.125 % 3.125
45 b = 3.125 % 1
46
47 assert_float(0.0 , a)
48 assert_float(0.125, b)
49end
50
51assert('Float#<=>', '15.2.9.3.6') do
52 a = 3.125 <=> 3.123
53 b = 3.125 <=> 3.125
54 c = 3.125 <=> 3.126
55 a2 = 3.125 <=> 3
56 c2 = 3.125 <=> 4
57
58 assert_equal( 1, a)
59 assert_equal( 0, b)
60 assert_equal(-1, c)
61 assert_equal( 1, a2)
62 assert_equal(-1, c2)
63end
64
65assert('Float#==', '15.2.9.3.7') do
66 assert_true 3.1 == 3.1
67 assert_false 3.1 == 3.2
68end
69
70assert('Float#ceil', '15.2.9.3.8') do
71 a = 3.123456789.ceil
72 b = 3.0.ceil
73 c = -3.123456789.ceil
74 d = -3.0.ceil
75
76 assert_equal( 4, a)
77 assert_equal( 3, b)
78 assert_equal(-3, c)
79 assert_equal(-3, d)
80end
81
82assert('Float#finite?', '15.2.9.3.9') do
83 assert_true 3.123456789.finite?
84 assert_false (1.0 / 0.0).finite?
85end
86
87assert('Float#floor', '15.2.9.3.10') do
88 a = 3.123456789.floor
89 b = 3.0.floor
90 c = -3.123456789.floor
91 d = -3.0.floor
92
93 assert_equal( 3, a)
94 assert_equal( 3, b)
95 assert_equal(-4, c)
96 assert_equal(-3, d)
97end
98
99assert('Float#infinite?', '15.2.9.3.11') do
100 a = 3.123456789.infinite?
101 b = (1.0 / 0.0).infinite?
102 c = (-1.0 / 0.0).infinite?
103
104 assert_nil a
105 assert_equal( 1, b)
106 assert_equal(-1, c)
107end
108
109assert('Float#round', '15.2.9.3.12') do
110 a = 3.123456789.round
111 b = 3.5.round
112 c = 3.4999.round
113 d = (-3.123456789).round
114 e = (-3.5).round
115 f = 12345.67.round(-1)
116 g = 3.423456789.round(0)
117 h = 3.423456789.round(1)
118 i = 3.423456789.round(3)
119
120 assert_equal( 3, a)
121 assert_equal( 4, b)
122 assert_equal( 3, c)
123 assert_equal( -3, d)
124 assert_equal( -4, e)
125 assert_equal(12350, f)
126 assert_equal( 3, g)
127 assert_float( 3.4, h)
128 assert_float(3.423, i)
129
130 assert_equal(42.0, 42.0.round(307))
131 assert_equal(1.0e307, 1.0e307.round(2))
132
133 inf = 1.0/0.0
134 assert_raise(FloatDomainError){ inf.round }
135 assert_raise(FloatDomainError){ inf.round(-1) }
136 assert_equal(inf, inf.round(1))
137 nan = 0.0/0.0
138 assert_raise(FloatDomainError){ nan.round }
139 assert_raise(FloatDomainError){ nan.round(-1) }
140 assert_true(nan.round(1).nan?)
141end
142
143assert('Float#to_f', '15.2.9.3.13') do
144 a = 3.123456789
145
146 assert_float(a, a.to_f)
147end
148
149assert('Float#to_i', '15.2.9.3.14') do
150 assert_equal(3, 3.123456789.to_i)
151end
152
153assert('Float#truncate', '15.2.9.3.15') do
154 assert_equal( 3, 3.123456789.truncate)
155 assert_equal(-3, -3.1.truncate)
156end
157
158assert('Float#divmod') do
159 def check_floats exp, act
160 assert_float exp[0], act[0]
161 assert_float exp[1], act[1]
162 end
163
164 # Note: quotients are Float because mruby does not have Bignum.
165 check_floats [ 0, 0.0], 0.0.divmod(1)
166 check_floats [ 0, 1.1], 1.1.divmod(3)
167 check_floats [ 3, 0.2], 3.2.divmod(1)
168 check_floats [ 2, 6.3], 20.3.divmod(7)
169 check_floats [-1, 1.6], -3.4.divmod(5)
170 check_floats [-2, -0.5], 25.5.divmod(-13)
171 check_floats [ 1, -6.6], -13.6.divmod(-7)
172 check_floats [ 3, 0.2], 9.8.divmod(3.2)
173end
174
175assert('Float#nan?') do
176 assert_true (0.0/0.0).nan?
177 assert_false 0.0.nan?
178 assert_false (1.0/0.0).nan?
179 assert_false (-1.0/0.0).nan?
180end
181
182assert('Float#<<') do
183 # Left Shift by one
184 assert_equal 46, 23.0 << 1
185
186 # Left Shift by a negative is Right Shift
187 assert_equal 23, 46.0 << -1
188end
189
190assert('Float#>>') do
191 # Right Shift by one
192 assert_equal 23, 46.0 >> 1
193
194 # Right Shift by a negative is Left Shift
195 assert_equal 46, 23.0 >> -1
196
197 # Don't raise on large Right Shift
198 assert_equal 0, 23.0 >> 128
199
200 # Don't raise on large Right Shift
201 assert_equal(-1, -23.0 >> 128)
202end
Note: See TracBrowser for help on using the repository browser.