mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
@@ -65,3 +65,16 @@ def report()
|
||||
print("\n")
|
||||
end
|
||||
|
||||
##
|
||||
# Performs fuzzy check for equality on methods returning floats
|
||||
# on the basis of the Math::TOLERANCE constant.
|
||||
def check_float(a, b)
|
||||
a = a.to_f
|
||||
b = b.to_f
|
||||
if a.finite? and b.finite?
|
||||
(a-b).abs < Math::TOLERANCE
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
##
|
||||
# Float ISO Test
|
||||
|
||||
assert('Float', '15.2.9') do
|
||||
Float.class == Class
|
||||
end
|
||||
|
||||
assert('Float#+', '15.2.9.3.1') do
|
||||
a = 3.123456788 + 0.000000001
|
||||
b = 3.123456789 + 1
|
||||
|
||||
check_float(a, 3.123456789) and
|
||||
check_float(b, 4.123456789)
|
||||
end
|
||||
|
||||
assert('Float#-', '15.2.9.3.2') do
|
||||
a = 3.123456790 - 0.000000001
|
||||
b = 5.123456789 - 1
|
||||
|
||||
check_float(a, 3.123456789) and
|
||||
check_float(b, 4.123456789)
|
||||
end
|
||||
|
||||
assert('Float#*', '15.2.9.3.3') do
|
||||
a = 3.123456789 * 3.123456789
|
||||
b = 3.123456789 * 1
|
||||
|
||||
check_float(a, 9.75598231275019) and
|
||||
check_float(b, 3.123456789)
|
||||
end
|
||||
|
||||
assert('Float#/', '15.2.9.3.4') do
|
||||
a = 3.123456789 / 3.123456789
|
||||
b = 3.123456789 / 1
|
||||
|
||||
check_float(a, 1.0) and
|
||||
check_float(b, 3.123456789)
|
||||
end
|
||||
|
||||
assert('Float#%', '15.2.9.3.5') do
|
||||
a = 3.123456789 % 3.123456789
|
||||
b = 3.123456789 % 1
|
||||
|
||||
check_float(a, 0.0) and
|
||||
check_float(b, 0.123456789)
|
||||
end
|
||||
|
||||
assert('Float#<=>', '15.2.9.3.6') do
|
||||
a = 3.123456789 <=> 3.123456788
|
||||
b = 3.123456789 <=> 3.123456789
|
||||
c = 3.123456789 <=> 3.123456790
|
||||
a2 = 3.123456789 <=> 3
|
||||
c2 = 3.123456789 <=> 4
|
||||
|
||||
a == 1 and b == 0 and c == -1 and
|
||||
a2 == 1 and c2 == -1
|
||||
end
|
||||
|
||||
assert('Float#==', '15.2.9.3.7') do
|
||||
3.1 == 3.1 and not (3.1 == 3.2)
|
||||
end
|
||||
|
||||
assert('Float#ceil', '15.2.9.3.8') do
|
||||
3.123456789.ceil == 4
|
||||
end
|
||||
|
||||
assert('Float#finite?', '15.2.9.3.9') do
|
||||
3.123456789.finite? and
|
||||
not (1.0 / 0.0).finite?
|
||||
end
|
||||
|
||||
assert('Float#floor', '15.2.9.3.10') do
|
||||
3.123456789.floor == 3
|
||||
end
|
||||
|
||||
assert('Float#infinite?', '15.2.9.3.11') do
|
||||
not 3.123456789.infinite? and
|
||||
(1.0 / 0.0).infinite?
|
||||
end
|
||||
|
||||
assert('Float#round', '15.2.9.3.12') do
|
||||
a = 3.123456789.round
|
||||
b = 3.5.round
|
||||
c = 3.499999999.round
|
||||
|
||||
a == 3 and b == 4 and c == 3
|
||||
end
|
||||
|
||||
assert('Float#to_f', '15.2.9.3.13') do
|
||||
a = 3.123456789
|
||||
|
||||
check_float(a.to_f, a)
|
||||
end
|
||||
|
||||
assert('Float#to_i', '15.2.9.3.14') do
|
||||
3.123456789.to_i == 3
|
||||
end
|
||||
|
||||
assert('Float#truncate', '15.2.9.3.15') do
|
||||
3.123456789.truncate == 3
|
||||
end
|
||||
|
||||
@@ -56,3 +56,116 @@ assert('Integer#==', '15.2.8.3.7') do
|
||||
a == false and b == true
|
||||
end
|
||||
|
||||
assert('Integer#~', '15.2.8.3.8') do
|
||||
# Complement
|
||||
~0 == -1 and ~2 == -3
|
||||
end
|
||||
|
||||
assert('Integer#&', '15.2.8.3.9') do
|
||||
# Bitwise AND
|
||||
# 0101 (5)
|
||||
# & 0011 (3)
|
||||
# = 0001 (1)
|
||||
5 & 3 == 1
|
||||
end
|
||||
|
||||
assert('Integer#|', '15.2.8.3.10') do
|
||||
# Bitwise OR
|
||||
# 0101 (5)
|
||||
# | 0011 (3)
|
||||
# = 0111 (7)
|
||||
5 | 3 == 7
|
||||
end
|
||||
|
||||
assert('Integer#^', '15.2.8.3.11') do
|
||||
# Bitwise XOR
|
||||
# 0101 (5)
|
||||
# ^ 0011 (3)
|
||||
# = 0110 (6)
|
||||
5 ^ 3 == 6
|
||||
end
|
||||
|
||||
assert('Integer#<<', '15.2.8.3.12') do
|
||||
# Left Shift by one
|
||||
# 00010111 (23)
|
||||
# = 00101110 (46)
|
||||
23 << 1 == 46
|
||||
end
|
||||
|
||||
assert('Integer#>>', '15.2.8.3.13') do
|
||||
# Right Shift by one
|
||||
# 00101110 (46)
|
||||
# = 00010111 (23)
|
||||
46 >> 1 == 23
|
||||
end
|
||||
|
||||
assert('Integer#ceil', '15.2.8.3.14') do
|
||||
10.ceil == 10
|
||||
end
|
||||
|
||||
assert('Integer#downto', '15.2.8.3.15') do
|
||||
a = 0
|
||||
3.downto(1) do |i|
|
||||
a += i
|
||||
end
|
||||
a == 6
|
||||
end
|
||||
|
||||
assert('Integer#eql?', '15.2.8.3.16') do
|
||||
a = 1.eql?(1)
|
||||
b = 1.eql?(2)
|
||||
c = 1.eql?(nil)
|
||||
|
||||
a == true and b == false and c == false
|
||||
end
|
||||
|
||||
assert('Integer#floor', '15.2.8.3.17') do
|
||||
a = 1.floor
|
||||
|
||||
a == 1
|
||||
end
|
||||
|
||||
assert('Integer#next', '15.2.8.3.19') do
|
||||
1.next == 2
|
||||
end
|
||||
|
||||
assert('Integer#round', '15.2.8.3.20') do
|
||||
1.round == 1
|
||||
end
|
||||
|
||||
assert('Integer#succ', '15.2.8.3.21') do
|
||||
1.succ == 2
|
||||
end
|
||||
|
||||
assert('Integer#times', '15.2.8.3.22') do
|
||||
a = 0
|
||||
3.times do
|
||||
a += 1
|
||||
end
|
||||
a == 3
|
||||
end
|
||||
|
||||
assert('Integer#to_f', '15.2.8.3.23') do
|
||||
1.to_f == 1.0
|
||||
end
|
||||
|
||||
assert('Integer#to_i', '15.2.8.3.24') do
|
||||
1.to_i == 1
|
||||
end
|
||||
|
||||
assert('Integer#to_s', '15.2.8.3.25') do
|
||||
1.to_s == '1' and -1.to_s == "-1"
|
||||
end
|
||||
|
||||
assert('Integer#truncate', '15.2.8.3.26') do
|
||||
1.truncate == 1
|
||||
end
|
||||
|
||||
assert('Integer#upto', '15.2.8.3.27') do
|
||||
a = 0
|
||||
1.upto(3) do |i|
|
||||
a += i
|
||||
end
|
||||
a == 6
|
||||
end
|
||||
|
||||
|
||||
+24
-37
@@ -1,25 +1,12 @@
|
||||
##
|
||||
# Math Test
|
||||
|
||||
##
|
||||
# Performs fuzzy check for equality on methods returning floats
|
||||
# on the basis of the Math::TOLERANCE constant.
|
||||
def check(a,b)
|
||||
a = a.to_f
|
||||
b = b.to_f
|
||||
if a.finite? and b.finite?
|
||||
(a-b).abs < Math::TOLERANCE
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
assert('Math.sin 0') do
|
||||
check(Math.sin(0), 0)
|
||||
check_float(Math.sin(0), 0)
|
||||
end
|
||||
|
||||
assert('Math.sin PI/2') do
|
||||
check(Math.sin(Math::PI / 2), 1)
|
||||
check_float(Math.sin(Math::PI / 2), 1)
|
||||
end
|
||||
|
||||
|
||||
@@ -32,61 +19,61 @@ assert('Fundamental trig identities') do
|
||||
s = Math.sin(a)
|
||||
c = Math.cos(a)
|
||||
t = Math.tan(a)
|
||||
result &= check(s, Math.cos(ca))
|
||||
result &= check(t, 1 / Math.tan(ca))
|
||||
result &= check(s ** 2 + c ** 2, 1)
|
||||
result &= check(t ** 2 + 1, (1/c) ** 2)
|
||||
result &= check((1/t) ** 2 + 1, (1/s) ** 2)
|
||||
result &= check_float(s, Math.cos(ca))
|
||||
result &= check_float(t, 1 / Math.tan(ca))
|
||||
result &= check_float(s ** 2 + c ** 2, 1)
|
||||
result &= check_float(t ** 2 + 1, (1/c) ** 2)
|
||||
result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
assert('Math.erf 0') do
|
||||
check(Math.erf(0), 0)
|
||||
check_float(Math.erf(0), 0)
|
||||
end
|
||||
|
||||
assert('Math.exp 0') do
|
||||
check(Math.exp(0), 1.0)
|
||||
check_float(Math.exp(0), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.exp 1') do
|
||||
check(Math.exp(1), 2.718281828459045)
|
||||
check_float(Math.exp(1), 2.718281828459045)
|
||||
end
|
||||
|
||||
assert('Math.exp 1.5') do
|
||||
check(Math.exp(1.5), 4.4816890703380645)
|
||||
check_float(Math.exp(1.5), 4.4816890703380645)
|
||||
end
|
||||
|
||||
assert('Math.log 1') do
|
||||
check(Math.log(1), 0)
|
||||
check_float(Math.log(1), 0)
|
||||
end
|
||||
|
||||
assert('Math.log E') do
|
||||
check(Math.log(Math::E), 1.0)
|
||||
check_float(Math.log(Math::E), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log E**3') do
|
||||
check(Math.log(Math::E**3), 3.0)
|
||||
check_float(Math.log(Math::E**3), 3.0)
|
||||
end
|
||||
|
||||
assert('Math.log2 1') do
|
||||
check(Math.log2(1), 0.0)
|
||||
check_float(Math.log2(1), 0.0)
|
||||
end
|
||||
|
||||
assert('Math.log2 2') do
|
||||
check(Math.log2(2), 1.0)
|
||||
check_float(Math.log2(2), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 1') do
|
||||
check(Math.log10(1), 0.0)
|
||||
check_float(Math.log10(1), 0.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 10') do
|
||||
check(Math.log10(10), 1.0)
|
||||
check_float(Math.log10(10), 1.0)
|
||||
end
|
||||
|
||||
assert('Math.log10 10**100') do
|
||||
check(Math.log10(10**100), 100.0)
|
||||
check_float(Math.log10(10**100), 100.0)
|
||||
end
|
||||
|
||||
assert('Math.cbrt') do
|
||||
@@ -94,25 +81,25 @@ assert('Math.cbrt') do
|
||||
cub = [-8, -1, 0, 1, 8]
|
||||
result = true
|
||||
cub.each_with_index do |v,i|
|
||||
result &= check(Math.cbrt(v), num[i])
|
||||
result &= check_float(Math.cbrt(v), num[i])
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
assert('Math.hypot') do
|
||||
check(Math.hypot(3, 4), 5.0)
|
||||
check_float(Math.hypot(3, 4), 5.0)
|
||||
end
|
||||
|
||||
assert('Math.frexp 1234') do
|
||||
n = 1234
|
||||
fraction, exponent = Math.frexp(n)
|
||||
check(Math.ldexp(fraction, exponent), n)
|
||||
check_float(Math.ldexp(fraction, exponent), n)
|
||||
end
|
||||
|
||||
assert('Math.erf 1') do
|
||||
check(Math.erf(1), 0.842700792949715)
|
||||
check_float(Math.erf(1), 0.842700792949715)
|
||||
end
|
||||
|
||||
assert('Math.erfc 1') do
|
||||
check(Math.erfc(1), 0.157299207050285)
|
||||
check_float(Math.erfc(1), 0.157299207050285)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user