--- source: src/main.rs expression: compiled input_file: test-data/lua5.4-tests/math.lua --- print("testing numbers and math lib"); local minint = math.mininteger local maxint = math.maxinteger local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 assert((1 << intbits) == 0); assert(minint == 1 << (intbits - 1)); assert(maxint == minint - 1); local floatbits = 24 { local p = 2.0 ^ floatbits while p < p + 1.0 { p = p * 2.0 floatbits = floatbits + 1 } } local fn isNaN(x) { return (x != x) } assert(isNaN(0 / 0)); assert(!isNaN(1 / 0)); { local x = 2.0 ^ floatbits assert(x > x - 1.0 && x == x + 1.0); print(string.format("%d-bit integers, %d-bit (mantissa) floats", intbits, floatbits)); } assert(math.type(0) == "integer" && math.type(0.0) == "float" && !math.type("10")); local fn checkerror(msg, f, ...) { local s, err = pcall(f, ...) assert(!s && string.find(err, msg)); } local msgf2i = "number.* has no integer representation" global fn eq(a, b, limit) { if !limit { if floatbits >= 50 { limit = 1e-11 } else { limit = 1e-5 } } return a == b || math.abs(a - b) <= limit } global fn eqT(a, b) { return a == b && math.type(a) == math.type(b) } assert(0e12 == 0 && 0.0 == 0 && 0.0 == 0 && 0.2e2 == 20 && 2.0e-1 == 0.2); { local a, b, c = "2", " 3e0 ", " 10 " assert(a + b == 5 && -b == -3 && b + "2" == 5 && "10" - c == 0); assert(type(a) == 'string' && type(b) == 'string' && type(c) == 'string'); assert(a == "2" && b == " 3e0 " && c == " 10 " && -c == -" 10 "); assert(c % a == 0 && a ^ b == 08); a = 0 assert(a == -a && 0 == -0); } { local x = -1 local mz = 0 / x t = { 0 = 10, 20, 30, 40, 50 } assert(t[(mz)] == t[(0)] && t[(-0)] == t[(0)]); } { local a, b = math.modf(3.5) assert(a == 3.0 && b == 0.5); a, b = math.modf(-2.5) assert(a == -2.0 && b == -0.5); a, b = math.modf(-3e23) assert(a == -3e23 && b == 0.0); a, b = math.modf(3e35) assert(a == 3e35 && b == 0.0); a, b = math.modf(-1 / 0) assert(a == -1 / 0 && b == 0.0); a, b = math.modf(1 / 0) assert(a == 1 / 0 && b == 0.0); a, b = math.modf(0 / 0) assert(isNaN(a) && isNaN(b)); a, b = math.modf(3) assert(eqT(a, 3) && eqT(b, 0.0)); a, b = math.modf(minint) assert(eqT(a, minint) && eqT(b, 0.0)); } assert(math.huge > 10e30); assert(-math.huge < -10e30); assert(minint < minint + 1); assert(maxint - 1 < maxint); assert(0 - minint == minint); assert(minint * minint == 0); assert(maxint * maxint * maxint == maxint); for _, i with pairs({ -16, -15, -3, -2, -1, 0, 1, 2, 3, 15 }) { for _, j with pairs({ -16, -15, -3, -2, -1, 1, 2, 3, 15 }) { for _, ti with pairs({ 0, 0.0 }) { for _, tj with pairs({ 0, 0.0 }) { local x = i + ti local y = j + tj assert(i /_ j == math.floor(i / j)); } } } } assert(1 /_ 0.0 == 1 / 0); assert(-1 /_ 0.0 == -1 / 0); assert(eqT(3.5 /_ 1.5, 2.0)); assert(eqT(3.5 /_ -1.5, -3.0)); { local x, y x = 1 assert(x /_ 0.0 == 1 / 0); x = 1.0 assert(x /_ 0 == 1 / 0); x = 3.5 assert(eqT(x /_ 1, 3.0)); assert(eqT(x /_ -1, -4.0)); x = 3.5 y = 1.5 assert(eqT(x /_ y, 2.0)); x = 3.5 y = -1.5 assert(eqT(x /_ y, -3.0)); } assert(maxint /_ maxint == 1); assert(maxint /_ 1 == maxint); assert((maxint - 1) /_ maxint == 0); assert(maxint /_ (maxint - 1) == 1); assert(minint /_ minint == 1); assert(minint /_ minint == 1); assert((minint + 1) /_ minint == 0); assert(minint /_ (minint + 1) == 1); assert(minint /_ 1 == minint); assert(minint /_ -1 == -minint); assert(minint /_ -2 == 2 ^ (intbits - 2)); assert(maxint /_ -1 == -maxint); { assert(2 ^ -3 == 1 / 2 ^ 3); assert(eq((-3) ^ -3, 1 / (-3) ^ 3)); for i = -3, 3 { for j = -3, 3 { if !_port || i != 0 || j > 0 { assert(eq(i ^ j, 1 / i ^ (-j))); } } } } if floatbits < intbits { assert(2.0 ^ floatbits == (1 << floatbits)); assert(2.0 ^ floatbits - 1.0 == (1 << floatbits) - 1.0); assert(2.0 ^ floatbits - 1.0 != (1 << floatbits)); assert(2.0 ^ floatbits + 1.0 != (1 << floatbits) + 1); } else { assert(maxint == maxint + 0.0); assert(maxint - 1 == maxint - 1.0); assert(minint + 1 == minint + 1.0); assert(maxint != maxint - 1.0); } assert(maxint + 0.0 == 2.0 ^ (intbits - 1) - 1.0); assert(minint + 0.0 == minint); assert(minint + 0.0 == -2.0 ^ (intbits - 1)); assert(1 < 1.1); assert(!(1 < 0.9)); assert(1 <= 1.1); assert(!(1 <= 0.9)); assert(-1 < -0.9); assert(!(-1 < -1.1)); assert(1 <= 1.1); assert(!(-1 <= -1.1)); assert(-1 < -0.9); assert(!(-1 < -1.1)); assert(-1 <= -0.9); assert(!(-1 <= -1.1)); assert(minint <= minint + 0.0); assert(minint + 0.0 <= minint); assert(!(minint < minint + 0.0)); assert(!(minint + 0.0 < minint)); assert(maxint < minint * -1.0); assert(maxint <= minint * -1.0); { local fmaxi1 = 2 ^ (intbits - 1) assert(maxint < fmaxi1); assert(maxint <= fmaxi1); assert(!(fmaxi1 <= maxint)); assert(minint <= -2 ^ (intbits - 1)); assert(-2 ^ (intbits - 1) <= minint); } if floatbits < intbits { print("testing order (floats cannot represent all integers)"); local fmax = 2 ^ floatbits local ifmax = fmax | 0 assert(fmax < ifmax + 1); assert(fmax - 1 < ifmax); assert(-(fmax - 1) > -ifmax); assert(!(fmax <= ifmax - 1)); assert(-fmax > -(ifmax + 1)); assert(!(-fmax >= -(ifmax - 1))); assert(fmax / 2 - 0.5 < ifmax /_ 2); assert(-(fmax / 2 - 0.5) > -ifmax /_ 2); assert(maxint < 2 ^ intbits); assert(minint > -2 ^ intbits); assert(maxint <= 2 ^ intbits); assert(minint >= -2 ^ intbits); } else { print("testing order (floats can represent all integers)"); assert(maxint < maxint + 1.0); assert(maxint < maxint + 0.5); assert(maxint - 1.0 < maxint); assert(maxint - 0.5 < maxint); assert(!(maxint + 0.0 < maxint)); assert(maxint + 0.0 <= maxint); assert(!(maxint < maxint + 0.0)); assert(maxint + 0.0 <= maxint); assert(maxint <= maxint + 0.0); assert(!(maxint + 1.0 <= maxint)); assert(!(maxint + 0.5 <= maxint)); assert(!(maxint <= maxint - 1.0)); assert(!(maxint <= maxint - 0.5)); assert(minint < minint + 1.0); assert(minint < minint + 0.5); assert(minint <= minint + 0.5); assert(minint - 1.0 < minint); assert(minint - 1.0 <= minint); assert(!(minint + 0.0 < minint)); assert(!(minint + 0.5 < minint)); assert(!(minint < minint + 0.0)); assert(minint + 0.0 <= minint); assert(minint <= minint + 0.0); assert(!(minint + 1.0 <= minint)); assert(!(minint + 0.5 <= minint)); assert(!(minint <= minint - 1.0)); } { local NaN = 0 / 0 assert(!(NaN < 0)); assert(!(NaN > minint)); assert(!(NaN <= -9)); assert(!(NaN <= maxint)); assert(!(NaN < maxint)); assert(!(minint <= NaN)); assert(!(minint < NaN)); assert(!(4 <= NaN)); assert(!(4 < NaN)); } local fn checkcompt(msg, code) { checkerror(msg, assert(load(code))); } checkcompt("divide by zero", "return 2 // 0"); checkcompt(msgf2i, "return 2.3 >> 0"); checkcompt(msgf2i, ("return 2.0^%d & 1")::format(intbits - 1)); checkcompt("field 'huge'", "return math.huge << 1"); checkcompt(msgf2i, ("return 1 | 2.0^%d")::format(intbits - 1)); checkcompt(msgf2i, "return 2.3 ~ 0.0"); local fn f2i(x) { return x | x } checkerror(msgf2i, f2i, math.huge); checkerror(msgf2i, f2i, -math.huge); checkerror(msgf2i, f2i, 0 / 0); if floatbits < intbits { assert(maxint + 1.0 == maxint + 0.0); assert(minint - 1.0 == minint + 0.0); checkerror(msgf2i, f2i, maxint + 0.0); assert(f2i(2.0 ^ (intbits - 2)) == 1 << (intbits - 2)); assert(f2i(-2.0 ^ (intbits - 2)) == -(1 << (intbits - 2))); assert((2.0 ^ (floatbits - 1) + 1.0) /_ 1 == (1 << (floatbits - 1)) + 1); local mf = maxint - (1 << (floatbits - intbits)) + 1 assert(f2i(mf + 0.0) == mf); mf = mf + 1 assert(f2i(mf + 0.0) != mf); } else { assert(maxint + 1.0 > maxint); assert(minint - 1.0 < minint); assert(f2i(maxint + 0.0) == maxint); checkerror("no integer rep", f2i, maxint + 1.0); checkerror("no integer rep", f2i, minint - 1.0); } assert(f2i(minint + 0.0) == minint); assert("2" + 1 == 3); assert("2 " + 1 == 3); assert(" -2 " + 1 == -1); assert(" -0xa " + 1 == -9); { assert(eqT(tonumber(tostring(maxint)), maxint)); assert(eqT(tonumber(tostring(minint)), minint)); local fn incd(n) { local s = string.format("%d", n) s = string.gsub(s, "%d$", fn (d) { assert(d != '9'); return string.char(string.byte(d) + 1) }) return s } assert(eqT(tonumber(incd(maxint)), maxint + 1.0)); assert(eqT(tonumber(incd(minint)), minint - 1.0)); assert(eqT(tonumber("1" .. string.rep("0", 30)), 1e30)); assert(eqT(tonumber("-1" .. string.rep("0", 30)), -1e30)); assert(eqT(tonumber("0x1" .. string.rep("0", 30)), 0)); assert(minint == load("return " .. minint)()); assert(eqT(maxint, load("return " .. maxint)())); assert(eqT(10000000000000000000000.0, 10000000000000000000000)); assert(eqT(-10000000000000000000000.0, -10000000000000000000000)); } assert(tonumber(3.4) == 3.4); assert(eqT(tonumber(3), 3)); assert(eqT(tonumber(maxint), maxint) && eqT(tonumber(minint), minint)); assert(tonumber(1 / 0) == 1 / 0); assert(tonumber("0") == 0); assert(!tonumber("")); assert(!tonumber(" ")); assert(!tonumber("-")); assert(!tonumber(" -0x ")); assert(!tonumber({})); assert(tonumber('+0.01') == 1 / 100 && tonumber('+.01') == 0.01 && tonumber('.01') == 0.01 && tonumber('-1.') == -1 && tonumber('+1.') == 1); assert(!tonumber('+ 0.01') && !tonumber('+.e1') && !tonumber('1e') && !tonumber('1.0e+') && !tonumber('.')); assert(tonumber('-012') == -010 - 2); assert(tonumber('-1.2e2') == - - -120); assert(tonumber("0xffffffffffff") == (1 << (4 * 12)) - 1); assert(tonumber("0x" .. string.rep("f", (intbits /_ 4))) == -1); assert(tonumber("-0x" .. string.rep("f", (intbits /_ 4))) == 1); assert(tonumber(' 001010 ', 2) == 10); assert(tonumber(' 001010 ', 10) == 001010); assert(tonumber(' -1010 ', 2) == -10); assert(tonumber('10', 36) == 36); assert(tonumber(' -10 ', 36) == -36); assert(tonumber(' +1Z ', 36) == 36 + 35); assert(tonumber(' -1z ', 36) == -36 + -35); assert(tonumber('-fFfa', 16) == -(10 + (16 * (15 + (16 * (15 + (16 * 15))))))); assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2 ^ (intbits - 2)); assert(tonumber('ffffFFFF', 16) + 1 == (1 << 32)); assert(tonumber('0ffffFFFF', 16) + 1 == (1 << 32)); assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40)); for i = 2, 36 { local i2 = i * i local i10 = i2 * i2 * i2 * i2 * i2 assert(tonumber('\t10000000000\t', i) == i10); } if !_soft { assert(tonumber("0x" .. string.rep("f", 13) .. ".0") == 2.0 ^ (4 * 13) - 1); assert(tonumber("0x" .. string.rep("f", 150) .. ".0") == 2.0 ^ (4 * 150) - 1); assert(tonumber("0x" .. string.rep("f", 300) .. ".0") == 2.0 ^ (4 * 300) - 1); assert(tonumber("0x" .. string.rep("f", 500) .. ".0") == 2.0 ^ (4 * 500) - 1); assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3); assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10); assert(tonumber('0x0.' .. string.rep('0', 13) .. "1") == 2.0 ^ (-4 * 14)); assert(tonumber('0x0.' .. string.rep('0', 150) .. "1") == 2.0 ^ (-4 * 151)); assert(tonumber('0x0.' .. string.rep('0', 300) .. "1") == 2.0 ^ (-4 * 301)); assert(tonumber('0x0.' .. string.rep('0', 500) .. "1") == 2.0 ^ (-4 * 501)); assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0); assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 7.25); } local fn f(...) { if select('#', ...) == 1 { return (...) } else { return "***" } } assert(!f(tonumber('fFfa', 15))); assert(!f(tonumber('099', 8))); assert(!f(tonumber('1\0', 2))); assert(!f(tonumber('', 8))); assert(!f(tonumber(' ', 9))); assert(!f(tonumber(' ', 9))); assert(!f(tonumber('0xf', 10))); assert(!f(tonumber('inf'))); assert(!f(tonumber(' INF '))); assert(!f(tonumber('Nan'))); assert(!f(tonumber('nan'))); assert(!f(tonumber(' '))); assert(!f(tonumber(''))); assert(!f(tonumber('1 a'))); assert(!f(tonumber('1 a', 2))); assert(!f(tonumber('1\0'))); assert(!f(tonumber('1 \0'))); assert(!f(tonumber('1\0 '))); assert(!f(tonumber('e1'))); assert(!f(tonumber('e 1'))); assert(!f(tonumber(' 3.4.5 '))); assert(!tonumber('0x')); assert(!tonumber('x')); assert(!tonumber('x3')); assert(!tonumber('0x3.3.3')); assert(!tonumber('00x2')); assert(!tonumber('0x 2')); assert(!tonumber('0 x2')); assert(!tonumber('23x')); assert(!tonumber('- 0xaa')); assert(!tonumber('-0xaaP ')); assert(!tonumber('0x0.51p')); assert(!tonumber('0x5p+-2')); assert(0x10 == 16 && 0xfff == 2 ^ 12 - 1 && 0XFB == 251); assert(0 == 0 && 0 == 0); assert(0xFFFFFFFF == (1 << 32) - 1); assert(tonumber('+0x2') == 2); assert(tonumber('-0xaA') == -170); assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1); assert(0e+1 == 0 && 0xE + 1 == 15 && 0xe - 1 == 13); assert(tonumber(' 0x2.5 ') == 0x25 / 16); assert(tonumber(' -0x2.5 ') == -0x25 / 16); assert(tonumber(' +0x0.51p+8 ') == 0x51); assert(0.9999999997671694 == 1 - '0x.00000001'); assert('0xA.a' + 0 == 10 + 10 / 16); assert(170 == 0XAA); assert(1 == 1); assert(1.0625 == '0x1.' + '+0x.1'); assert(703711 == 11259376); assert(1.1 == 1.0 + 0.1); assert(100.0 == 1e2 && 0.01 == 1e-2); assert(1111111111 - 1111111110 == 1000.00e-03); assert(1.1 == '1.' + '.1'); assert(tonumber('1111111111') - tonumber('1111111110') == tonumber(" +0.001e+3 \n\t")); assert(0.1e-30 > 0.9e-31 && 0.9e30 < 0.1e31); assert(0.123456 > 0.123455); assert(tonumber('+1.23E18') == 1.23 * 10.0 ^ 18); assert(!(1 < 1) && (1 < 2) && !(2 < 1)); assert(!('a' < 'a') && ('a' < 'b') && !('b' < 'a')); assert((1 <= 1) && (1 <= 2) && !(2 <= 1)); assert(('a' <= 'a') && ('a' <= 'b') && !('b' <= 'a')); assert(!(1 > 1) && !(1 > 2) && (2 > 1)); assert(!('a' > 'a') && !('a' > 'b') && ('b' > 'a')); assert((1 >= 1) && !(1 >= 2) && (2 >= 1)); assert(('a' >= 'a') && !('a' >= 'b') && ('b' >= 'a')); assert(1.3 < 1.4 && 1.3 <= 1.4 && !(1.3 < 1.3) && 1.3 <= 1.3); assert(eqT(-4 % 3, 2)); assert(eqT(4 % -3, -2)); assert(eqT(-4.0 % 3, 2.0)); assert(eqT(4 % -3.0, -2.0)); assert(eqT(4 % -5, -1)); assert(eqT(4 % -5.0, -1.0)); assert(eqT(4 % 5, 4)); assert(eqT(4 % 5.0, 4.0)); assert(eqT(-4 % -5, -4)); assert(eqT(-4 % -5.0, -4.0)); assert(eqT(-4 % 5, 1)); assert(eqT(-4 % 5.0, 1.0)); assert(eqT(4.25 % 4, 0.25)); assert(eqT(10.0 % 2, 0.0)); assert(eqT(-10.0 % 2, 0.0)); assert(eqT(-10.0 % -2, 0.0)); assert(math.pi - math.pi % 1 == 3); assert(math.pi - math.pi % 0.001 == 3.141); { local i, j = 0, 20000 while i < j { local m = (i + j) /_ 2 if 10 ^ -m > 0 { i = m + 1 } else { j = m } } local b = 10 ^ -(i - (i /_ 10)) assert(b > 0 && b * b == 0); local delta = b / 1000 assert(eq((2.1 * b) % (2 * b), (0.1 * b), delta)); assert(eq((-2.1 * b) % (2 * b), (2 * b) - (0.1 * b), delta)); assert(eq((2.1 * b) % (-2 * b), (0.1 * b) - (2 * b), delta)); assert(eq((-2.1 * b) % (-2 * b), (-0.1 * b), delta)); } for i = -10, 10 { for j = -10, 10 { if j != 0 { assert((i + 0.0) % j == i % j); } } } for i = 0, 10 { for j = -10, 10 { if j != 0 { assert((2 ^ i) % j == (1 << i) % j); } } } { local i = 10 while (1 << i) > 0 { assert((1 << i) % 3 == i % 2 + 1); i = i + 1 } i = 10 while 2 ^ i < math.huge { assert(2 ^ i % 3 == i % 2 + 1); i = i + 1 } } assert(eqT(minint % minint, 0)); assert(eqT(maxint % maxint, 0)); assert((minint + 1) % minint == minint + 1); assert((maxint - 1) % maxint == maxint - 1); assert(minint % maxint == maxint - 1); assert(minint % -1 == 0); assert(minint % -2 == 0); assert(maxint % -2 == -1); if !_port { local fn anan(x) { assert(isNaN(x)); } anan(0.0 % 0); anan(1.3 % 0); anan(math.huge % 1); anan(math.huge % 1e30); anan(-math.huge % 1e30); anan(-math.huge % -1e30); assert(1 % math.huge == 1); assert(1e30 % math.huge == 1e30); assert(1e30 % -math.huge == -math.huge); assert(-1 % math.huge == math.huge); assert(-1 % -math.huge == -1); } assert(math.ult(3, 4)); assert(!math.ult(4, 4)); assert(math.ult(-2, -1)); assert(math.ult(2, -1)); assert(!math.ult(-2, -2)); assert(math.ult(maxint, minint)); assert(!math.ult(minint, maxint)); assert(eq(math.sin(-9.8) ^ 2 + math.cos(-9.8) ^ 2, 1)); assert(eq(math.tan(math.pi / 4), 1)); assert(eq(math.sin(math.pi / 2), 1) && eq(math.cos(math.pi / 2), 0)); assert(eq(math.atan(1), math.pi / 4) && eq(math.acos(0), math.pi / 2) && eq(math.asin(1), math.pi / 2)); assert(eq(math.deg(math.pi / 2), 90) && eq(math.rad(90), math.pi / 2)); assert(math.abs(-10.43) == 10.43); assert(eqT(math.abs(minint), minint)); assert(eqT(math.abs(maxint), maxint)); assert(eqT(math.abs(-maxint), maxint)); assert(eq(math.atan(1, 0), math.pi / 2)); assert(math.fmod(10, 3) == 1); assert(eq(math.sqrt(10) ^ 2, 10)); assert(eq(math.log(2, 10), math.log(2) / math.log(10))); assert(eq(math.log(2, 2), 1)); assert(eq(math.log(9, 3), 2)); assert(eq(math.exp(0), 1)); assert(eq(math.sin(10), math.sin(10 % (2 * math.pi)))); assert(tonumber(' 1.3e-2 ') == 1.3e-2); assert(tonumber(' -1.00000000000001 ') == -1.00000000000001); assert(8388609 + -8388609 == 0); assert(8388608 + -8388608 == 0); assert(8388607 + -8388607 == 0); { assert(eqT(math.floor(3.4), 3)); assert(eqT(math.ceil(3.4), 4)); assert(eqT(math.floor(-3.4), -4)); assert(eqT(math.ceil(-3.4), -3)); assert(eqT(math.floor(maxint), maxint)); assert(eqT(math.ceil(maxint), maxint)); assert(eqT(math.floor(minint), minint)); assert(eqT(math.floor(minint + 0.0), minint)); assert(eqT(math.ceil(minint), minint)); assert(eqT(math.ceil(minint + 0.0), minint)); assert(math.floor(1e50) == 1e50); assert(math.ceil(1e50) == 1e50); assert(math.floor(-1e50) == -1e50); assert(math.ceil(-1e50) == -1e50); for _, p with pairs({ 31, 32, 63, 64 }) { assert(math.floor(2 ^ p) == 2 ^ p); assert(math.floor(2 ^ p + 0.5) == 2 ^ p); assert(math.ceil(2 ^ p) == 2 ^ p); assert(math.ceil(2 ^ p - 0.5) == 2 ^ p); } checkerror("number expected", math.floor, {}); checkerror("number expected", math.ceil, print); assert(eqT(math.tointeger(minint), minint)); assert(eqT(math.tointeger(minint .. ""), minint)); assert(eqT(math.tointeger(maxint), maxint)); assert(eqT(math.tointeger(maxint .. ""), maxint)); assert(eqT(math.tointeger(minint + 0.0), minint)); assert(!math.tointeger(0.0 - minint)); assert(!math.tointeger(math.pi)); assert(!math.tointeger(-math.pi)); assert(math.floor(math.huge) == math.huge); assert(math.ceil(math.huge) == math.huge); assert(!math.tointeger(math.huge)); assert(math.floor(-math.huge) == -math.huge); assert(math.ceil(-math.huge) == -math.huge); assert(!math.tointeger(-math.huge)); assert(math.tointeger("34.0") == 34); assert(!math.tointeger("34.3")); assert(!math.tointeger({})); assert(!math.tointeger(0 / 0)); } for i = -6, 6 { for j = -6, 6 { if j != 0 { local mi = math.fmod(i, j) local mf = math.fmod(i + 0.0, j) assert(mi == mf); assert(math.type(mi) == 'integer' && math.type(mf) == 'float'); if (i >= 0 && j >= 0) || (i <= 0 && j <= 0) || mi == 0 { assert(eqT(mi, i % j)); } } } } assert(eqT(math.fmod(minint, minint), 0)); assert(eqT(math.fmod(maxint, maxint), 0)); assert(eqT(math.fmod(minint + 1, minint), minint + 1)); assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1)); checkerror("zero", math.fmod, 3, 0); { checkerror("value expected", math.max); checkerror("value expected", math.min); assert(eqT(math.max(3), 3)); assert(eqT(math.max(3, 5, 9, 1), 9)); assert(math.max(maxint, 10e60) == 10e60); assert(eqT(math.max(minint, minint + 1), minint + 1)); assert(eqT(math.min(3), 3)); assert(eqT(math.min(3, 5, 9, 1), 1)); assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2); assert(math.min(1.9, 1.7, 1.72) == 1.7); assert(math.min(-10e60, minint) == -10e60); assert(eqT(math.min(maxint, maxint - 1), maxint - 1)); assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)); } local a, b = '10', '20' assert(a * b == 200 && a + b == 30 && a - b == -10 && a / b == 0.5 && -b == -20); assert(a == '10' && b == '20'); { print("testing -0 and NaN"); local mz = -0.0 local z = 0.0 assert(mz == z); assert(1 / mz < 0 && 0 < 1 / z); local a = { mz = 1 } assert(a[(z)] == 1 && a[(mz)] == 1); a[(z)] = 2 assert(a[(z)] == 2 && a[(mz)] == 2); local inf = math.huge * 2 + 1 local mz = -1 / inf local z = 1 / inf assert(mz == z); assert(1 / mz < 0 && 0 < 1 / z); local NaN = inf - inf assert(NaN != NaN); assert(!(NaN < NaN)); assert(!(NaN <= NaN)); assert(!(NaN > NaN)); assert(!(NaN >= NaN)); assert(!(0 < NaN) && !(NaN < 0)); local NaN1 = 0 / 0 assert(NaN != NaN1 && !(NaN <= NaN1) && !(NaN1 <= NaN)); local a = {} assert(!pcall(rawset, a, NaN, 1)); assert(a[(NaN)] == undef); a[(1)] = 1 assert(!pcall(rawset, a, NaN, 1)); assert(a[(NaN)] == undef); local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0" assert(a1 == a2 && a2 == a4 && a1 != a3); assert(a3 == a5); } print("testing 'math.random'"); local random, max, min = math.random, math.max, math.min local fn testnear(val, ref, tol) { return (math.abs(val - ref) < ref * tol) } { local h = 0x7a7040a5 local l = 0xa323c9d6 math.randomseed(1007); local res = (h << 32 | l) & ^^(^^0 << intbits) assert(random(0) == res); math.randomseed(1007, 0); local res if floatbits <= 32 { res = (h >> (32 - floatbits)) % 2 ^ 32 } else { res = (h % 2 ^ 32) * 2 ^ (floatbits - 32) + ((l >> (64 - floatbits)) % 2 ^ 32) } local rand = random() assert(eq(rand, 0.4782753376376966, 2 ^ -floatbits)); assert(rand * 2 ^ floatbits == res); } { local x, y = math.randomseed() local res = math.random(0) x, y = math.randomseed(x, y) assert(math.random(0) == res); math.randomseed(x, y); assert(math.random(0) == res); print(string.format("random seeds: %d, %d", x, y)); } { local randbits = math.min(floatbits, 64) local mult = 2 ^ randbits local counts = {} for i = 1, randbits { counts[(i)] = 0 } local up = -math.huge local low = math.huge local rounds = 100 * randbits local totalrounds = 0 for i = 0, rounds { local t = random() assert(0 <= t && t < 1); up = max(up, t) low = min(low, t) assert(t * mult % 1 == 0); local bit = i % randbits if (t * 2 ^ bit) % 1 >= 0.5 { counts[(bit + 1)] = counts[(bit + 1)] + 1 } } totalrounds = totalrounds + rounds if !(eq(up, 1, 0.001) && eq(low, 0, 0.001)) { } local expected = (totalrounds / randbits / 2) for i = 1, randbits { if !testnear(counts[(i)], expected, 0.10) { } } print(string.format("float random range in %d calls: [%f, %f]", totalrounds, low, up)); } { local up = 0 local low = 0 local counts = {} for i = 1, intbits { counts[(i)] = 0 } local rounds = 100 * intbits local totalrounds = 0 for i = 0, rounds { local t = random(0) up = max(up, t) low = min(low, t) local bit = i % intbits counts[(bit + 1)] = counts[(bit + 1)] + ((t >> bit) & 1) } totalrounds = totalrounds + rounds local lim = maxint >> 10 if !(maxint - up < lim && low - minint < lim) { } local expected = (totalrounds / intbits / 2) for i = 1, intbits { if !testnear(counts[(i)], expected, 0.10) { } } print(string.format("integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]", totalrounds, (minint - low) / minint * 1e6, (maxint - up) / maxint * 1e6)); } { local count = { 0, 0, 0, 0, 0, 0 } local rep = 200 local totalrep = 0 for i = 1, rep * 6 { local r = random(6) count[(r)] = count[(r)] + 1 } totalrep = totalrep + rep for i = 1, 6 { if !testnear(count[(i)], totalrep, 0.05) { } } } { local fn aux(x1, x2) { local mark = {} local count = 0 while true { local t = random(x1, x2) assert(x1 <= t && t <= x2); if !mark[(t)] { mark[(t)] = true count = count + 1 if count == x2 - x1 + 1 { } } } } aux(-10, 0); aux(1, 6); aux(1, 2); aux(1, 13); aux(1, 31); aux(1, 32); aux(1, 33); aux(-10, 10); aux(-10, -10); aux(minint, minint); aux(maxint, maxint); aux(minint, minint + 9); aux(maxint - 3, maxint); } { local fn aux(p1, p2) { local max = minint local min = maxint local n = 100 local mark = {} local count = 0 for _ = 1, n { local t = random(p1, p2) if !mark[(t)] { assert(p1 <= t && t <= p2); max = math.max(max, t) min = math.min(min, t) mark[(t)] = true count = count + 1 } } if !(count >= n * 0.8) { } local diff = (p2 - p1) >> 4 if !(min < p1 + diff && max > p2 - diff) { } } aux(0, maxint); aux(1, maxint); aux(3, maxint /_ 3); aux(minint, -1); aux(minint /_ 2, maxint /_ 2); aux(minint, maxint); aux(minint + 1, maxint); aux(minint, maxint - 1); aux(0, 1 << (intbits - 5)); } assert(!pcall(random, 1, 2, 3)); assert(!pcall(random, minint + 1, minint)); assert(!pcall(random, maxint, maxint - 1)); assert(!pcall(random, maxint, minint)); print('OK');