source: EcnlProtoTool/trunk/mruby-2.1.1/test/t/float.rb@ 439

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

mrubyを2.1.1に更新

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