source: EcnlProtoTool/trunk/mruby-1.3.0/test/t/float.rb@ 331

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 4.3 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)
151 assert_raise(FloatDomainError) { Float::INFINITY.to_i }
152 assert_raise(FloatDomainError) { (-Float::INFINITY).to_i }
153 assert_raise(FloatDomainError) { Float::NAN.to_i }
154end
155
156assert('Float#truncate', '15.2.9.3.15') do
157 assert_equal( 3, 3.123456789.truncate)
158 assert_equal(-3, -3.1.truncate)
159end
160
161assert('Float#divmod') do
162 def check_floats exp, act
163 assert_float exp[0], act[0]
164 assert_float exp[1], act[1]
165 end
166
167 # Note: quotients are Float because mruby does not have Bignum.
168 check_floats [ 0, 0.0], 0.0.divmod(1)
169 check_floats [ 0, 1.1], 1.1.divmod(3)
170 check_floats [ 3, 0.2], 3.2.divmod(1)
171 check_floats [ 2, 6.3], 20.3.divmod(7)
172 check_floats [-1, 1.6], -3.4.divmod(5)
173 check_floats [-2, -0.5], 25.5.divmod(-13)
174 check_floats [ 1, -6.6], -13.6.divmod(-7)
175 check_floats [ 3, 0.2], 9.8.divmod(3.2)
176end
177
178assert('Float#nan?') do
179 assert_true (0.0/0.0).nan?
180 assert_false 0.0.nan?
181 assert_false (1.0/0.0).nan?
182 assert_false (-1.0/0.0).nan?
183end
184
185assert('Float#<<') do
186 # Left Shift by one
187 assert_equal 46, 23.0 << 1
188
189 # Left Shift by a negative is Right Shift
190 assert_equal 23, 46.0 << -1
191end
192
193assert('Float#>>') do
194 # Right Shift by one
195 assert_equal 23, 46.0 >> 1
196
197 # Right Shift by a negative is Left Shift
198 assert_equal 46, 23.0 >> -1
199
200 # Don't raise on large Right Shift
201 assert_equal 0, 23.0 >> 128
202
203 # Don't raise on large Right Shift
204 assert_equal(-1, -23.0 >> 128)
205end
Note: See TracBrowser for help on using the repository browser.