lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/math.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | print("testing numbers and math lib") |
| 5 | |
| 6 | local math = require "math" |
| 7 | local string = require "string" |
| 8 | |
| 9 | global none |
| 10 | |
| 11 | global<const> print, assert, pcall, type, pairs, load |
| 12 | global<const> tonumber, tostring, select |
| 13 | |
| 14 | local<const> minint, maxint = math.mininteger, math.maxinteger |
| 15 | |
| 16 | local intbits <const> = math.floor(math.log(maxint, 2) + 0.5) + 1 |
| 17 | assert((1 << intbits) == 0) |
| 18 | |
| 19 | assert(minint == 1 << (intbits - 1)) |
| 20 | assert(maxint == minint - 1) |
| 21 | |
| 22 | -- number of bits in the mantissa of a floating-point number |
| 23 | local floatbits = 24 |
| 24 | do |
| 25 | local p = 2.0^floatbits |
| 26 | while p < p + 1.0 do |
| 27 | p = p * 2.0 |
| 28 | floatbits = floatbits + 1 |
| 29 | end |
| 30 | end |
| 31 | |
| 32 | |
| 33 | -- maximum exponent for a floating-point number |
| 34 | local maxexp = 0 |
| 35 | do |
| 36 | local p = 2.0 |
| 37 | while p < math.huge do |
| 38 | maxexp = maxexp + 1 |
| 39 | p = p + p |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | |
| 44 | local function isNaN (x) |
| 45 | return (x ~= x) |
| 46 | end |
| 47 | |
| 48 | assert(isNaN(0/0)) |
| 49 | assert(not isNaN(1/0)) |
| 50 | |
| 51 | |
| 52 | do |
| 53 | local x = 2.0^floatbits |
| 54 | assert(x > x - 1.0 and x == x + 1.0) |
| 55 | |
| 56 | local msg = " %d-bit integers, %d-bit*2^%d floats" |
| 57 | print(string.format(msg, intbits, floatbits, maxexp)) |
| 58 | end |
| 59 | |
| 60 | assert(math.type(0) == "integer" and math.type(0.0) == "float" |
| 61 | and not math.type("10")) |
| 62 | |
| 63 | |
| 64 | local function checkerror (msg, f, ...) |
| 65 | local s, err = pcall(f, ...) |
| 66 | assert(not s and string.find(err, msg)) |
| 67 | end |
| 68 | |
| 69 | local msgf2i = "number.* has no integer representation" |
| 70 | |
| 71 | -- float equality |
| 72 | local function eq (a,b,limit) |
| 73 | if not limit then |
| 74 | if floatbits >= 50 then limit = 1E-11 |
| 75 | else limit = 1E-5 |
| 76 | end |
| 77 | end |
| 78 | -- a == b needed for +inf/-inf |
| 79 | return a == b or math.abs(a-b) <= limit |
| 80 | end |
| 81 | |
| 82 | |
| 83 | -- equality with types |
| 84 | local function eqT (a,b) |
| 85 | return a == b and math.type(a) == math.type(b) |
| 86 | end |
| 87 | |
| 88 | |
| 89 | -- basic float notation |
| 90 | assert(0e12 == 0 and .0 == 0 and 0. == 0 and .2e2 == 20 and 2.E-1 == 0.2) |
| 91 | |
| 92 | do |
| 93 | local a,b,c = "2", " 3e0 ", " 10 " |
| 94 | assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0) |
| 95 | assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string') |
| 96 | assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ") |
| 97 | assert(c%a == 0 and a^b == 08) |
| 98 | a = 0 |
| 99 | assert(a == -a and 0 == -0) |
| 100 | end |
| 101 | |
| 102 | do |
| 103 | local x = -1 |
| 104 | local mz = 0/x -- minus zero |
| 105 | local t = {[0] = 10, 20, 30, 40, 50} |
| 106 | assert(t[mz] == t[0] and t[-0] == t[0]) |
| 107 | end |
| 108 | |
| 109 | do -- tests for 'modf' |
| 110 | local a,b = math.modf(3.5) |
| 111 | assert(a == 3.0 and b == 0.5) |
| 112 | a,b = math.modf(-2.5) |
| 113 | assert(a == -2.0 and b == -0.5) |
| 114 | a,b = math.modf(-3e23) |
| 115 | assert(a == -3e23 and b == 0.0) |
| 116 | a,b = math.modf(3e35) |
| 117 | assert(a == 3e35 and b == 0.0) |
| 118 | a,b = math.modf(-1/0) -- -inf |
| 119 | assert(a == -1/0 and b == 0.0) |
| 120 | a,b = math.modf(1/0) -- inf |
| 121 | assert(a == 1/0 and b == 0.0) |
| 122 | a,b = math.modf(0/0) -- NaN |
| 123 | assert(isNaN(a) and isNaN(b)) |
| 124 | a,b = math.modf(3) -- integer argument |
| 125 | assert(eqT(a, 3) and eqT(b, 0.0)) |
| 126 | a,b = math.modf(minint) |
| 127 | assert(eqT(a, minint) and eqT(b, 0.0)) |
| 128 | end |
| 129 | |
| 130 | assert(math.huge > 10e30) |
| 131 | assert(-math.huge < -10e30) |
| 132 | |
| 133 | |
| 134 | -- integer arithmetic |
| 135 | assert(minint < minint + 1) |
| 136 | assert(maxint - 1 < maxint) |
| 137 | assert(0 - minint == minint) |
| 138 | assert(minint * minint == 0) |
| 139 | assert(maxint * maxint * maxint == maxint) |
| 140 | |
| 141 | |
| 142 | -- testing floor division and conversions |
| 143 | |
| 144 | for _, i in pairs{-16, -15, -3, -2, -1, 0, 1, 2, 3, 15} do |
| 145 | for _, j in pairs{-16, -15, -3, -2, -1, 1, 2, 3, 15} do |
| 146 | for _, ti in pairs{0, 0.0} do -- try 'i' as integer and as float |
| 147 | for _, tj in pairs{0, 0.0} do -- try 'j' as integer and as float |
| 148 | local x = i + ti |
| 149 | local y = j + tj |
| 150 | assert(i//j == math.floor(i/j)) |
| 151 | end |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | assert(1//0.0 == 1/0) |
| 157 | assert(-1 // 0.0 == -1/0) |
| 158 | assert(eqT(3.5 // 1.5, 2.0)) |
| 159 | assert(eqT(3.5 // -1.5, -3.0)) |
| 160 | |
| 161 | do -- tests for different kinds of opcodes |
| 162 | local x, y |
| 163 | x = 1; assert(x // 0.0 == 1/0) |
| 164 | x = 1.0; assert(x // 0 == 1/0) |
| 165 | x = 3.5; assert(eqT(x // 1, 3.0)) |
| 166 | assert(eqT(x // -1, -4.0)) |
| 167 | |
| 168 | x = 3.5; y = 1.5; assert(eqT(x // y, 2.0)) |
| 169 | x = 3.5; y = -1.5; assert(eqT(x // y, -3.0)) |
| 170 | end |
| 171 | |
| 172 | assert(maxint // maxint == 1) |
| 173 | assert(maxint // 1 == maxint) |
| 174 | assert((maxint - 1) // maxint == 0) |
| 175 | assert(maxint // (maxint - 1) == 1) |
| 176 | assert(minint // minint == 1) |
| 177 | assert(minint // minint == 1) |
| 178 | assert((minint + 1) // minint == 0) |
| 179 | assert(minint // (minint + 1) == 1) |
| 180 | assert(minint // 1 == minint) |
| 181 | |
| 182 | assert(minint // -1 == -minint) |
| 183 | assert(minint // -2 == 2^(intbits - 2)) |
| 184 | assert(maxint // -1 == -maxint) |
| 185 | |
| 186 | |
| 187 | -- negative exponents |
| 188 | do |
| 189 | assert(2^-3 == 1 / 2^3) |
| 190 | assert(eq((-3)^-3, 1 / (-3)^3)) |
| 191 | for i = -3, 3 do -- variables avoid constant folding |
| 192 | for j = -3, 3 do |
| 193 | -- domain errors (0^(-n)) are not portable |
| 194 | if not _ENV._port or i ~= 0 or j > 0 then |
| 195 | assert(eq(i^j, 1 / i^(-j))) |
| 196 | end |
| 197 | end |
| 198 | end |
| 199 | end |
| 200 | |
| 201 | -- comparison between floats and integers (border cases) |
| 202 | if floatbits < intbits then |
| 203 | assert(2.0^floatbits == (1 << floatbits)) |
| 204 | assert(2.0^floatbits - 1.0 == (1 << floatbits) - 1.0) |
| 205 | assert(2.0^floatbits - 1.0 ~= (1 << floatbits)) |
| 206 | -- float is rounded, int is not |
| 207 | assert(2.0^floatbits + 1.0 ~= (1 << floatbits) + 1) |
| 208 | else -- floats can express all integers with full accuracy |
| 209 | assert(maxint == maxint + 0.0) |
| 210 | assert(maxint - 1 == maxint - 1.0) |
| 211 | assert(minint + 1 == minint + 1.0) |
| 212 | assert(maxint ~= maxint - 1.0) |
| 213 | end |
| 214 | assert(maxint + 0.0 == 2.0^(intbits - 1) - 1.0) |
| 215 | assert(minint + 0.0 == minint) |
| 216 | assert(minint + 0.0 == -2.0^(intbits - 1)) |
| 217 | |
| 218 | |
| 219 | -- order between floats and integers |
| 220 | assert(1 < 1.1); assert(not (1 < 0.9)) |
| 221 | assert(1 <= 1.1); assert(not (1 <= 0.9)) |
| 222 | assert(-1 < -0.9); assert(not (-1 < -1.1)) |
| 223 | assert(1 <= 1.1); assert(not (-1 <= -1.1)) |
| 224 | assert(-1 < -0.9); assert(not (-1 < -1.1)) |
| 225 | assert(-1 <= -0.9); assert(not (-1 <= -1.1)) |
| 226 | assert(minint <= minint + 0.0) |
| 227 | assert(minint + 0.0 <= minint) |
| 228 | assert(not (minint < minint + 0.0)) |
| 229 | assert(not (minint + 0.0 < minint)) |
| 230 | assert(maxint < minint * -1.0) |
| 231 | assert(maxint <= minint * -1.0) |
| 232 | |
| 233 | do |
| 234 | local fmaxi1 = 2^(intbits - 1) |
| 235 | assert(maxint < fmaxi1) |
| 236 | assert(maxint <= fmaxi1) |
| 237 | assert(not (fmaxi1 <= maxint)) |
| 238 | assert(minint <= -2^(intbits - 1)) |
| 239 | assert(-2^(intbits - 1) <= minint) |
| 240 | end |
| 241 | |
| 242 | if floatbits < intbits then |
| 243 | print("testing order (floats cannot represent all integers)") |
| 244 | local fmax = 2^floatbits |
| 245 | local ifmax = fmax | 0 |
| 246 | assert(fmax < ifmax + 1) |
| 247 | assert(fmax - 1 < ifmax) |
| 248 | assert(-(fmax - 1) > -ifmax) |
| 249 | assert(not (fmax <= ifmax - 1)) |
| 250 | assert(-fmax > -(ifmax + 1)) |
| 251 | assert(not (-fmax >= -(ifmax - 1))) |
| 252 | |
| 253 | assert(fmax/2 - 0.5 < ifmax//2) |
| 254 | assert(-(fmax/2 - 0.5) > -ifmax//2) |
| 255 | |
| 256 | assert(maxint < 2^intbits) |
| 257 | assert(minint > -2^intbits) |
| 258 | assert(maxint <= 2^intbits) |
| 259 | assert(minint >= -2^intbits) |
| 260 | else |
| 261 | print("testing order (floats can represent all integers)") |
| 262 | assert(maxint < maxint + 1.0) |
| 263 | assert(maxint < maxint + 0.5) |
| 264 | assert(maxint - 1.0 < maxint) |
| 265 | assert(maxint - 0.5 < maxint) |
| 266 | assert(not (maxint + 0.0 < maxint)) |
| 267 | assert(maxint + 0.0 <= maxint) |
| 268 | assert(not (maxint < maxint + 0.0)) |
| 269 | assert(maxint + 0.0 <= maxint) |
| 270 | assert(maxint <= maxint + 0.0) |
| 271 | assert(not (maxint + 1.0 <= maxint)) |
| 272 | assert(not (maxint + 0.5 <= maxint)) |
| 273 | assert(not (maxint <= maxint - 1.0)) |
| 274 | assert(not (maxint <= maxint - 0.5)) |
| 275 | |
| 276 | assert(minint < minint + 1.0) |
| 277 | assert(minint < minint + 0.5) |
| 278 | assert(minint <= minint + 0.5) |
| 279 | assert(minint - 1.0 < minint) |
| 280 | assert(minint - 1.0 <= minint) |
| 281 | assert(not (minint + 0.0 < minint)) |
| 282 | assert(not (minint + 0.5 < minint)) |
| 283 | assert(not (minint < minint + 0.0)) |
| 284 | assert(minint + 0.0 <= minint) |
| 285 | assert(minint <= minint + 0.0) |
| 286 | assert(not (minint + 1.0 <= minint)) |
| 287 | assert(not (minint + 0.5 <= minint)) |
| 288 | assert(not (minint <= minint - 1.0)) |
| 289 | end |
| 290 | |
| 291 | do |
| 292 | local NaN <const> = 0/0 |
| 293 | assert(not (NaN < 0)) |
| 294 | assert(not (NaN > minint)) |
| 295 | assert(not (NaN <= -9)) |
| 296 | assert(not (NaN <= maxint)) |
| 297 | assert(not (NaN < maxint)) |
| 298 | assert(not (minint <= NaN)) |
| 299 | assert(not (minint < NaN)) |
| 300 | assert(not (4 <= NaN)) |
| 301 | assert(not (4 < NaN)) |
| 302 | end |
| 303 | |
| 304 | |
| 305 | -- avoiding errors at compile time |
| 306 | local function checkcompt (msg, code) |
| 307 | checkerror(msg, assert(load(code))) |
| 308 | end |
| 309 | checkcompt("divide by zero", "return 2 // 0") |
| 310 | checkcompt(msgf2i, "return 2.3 >> 0") |
| 311 | checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1)) |
| 312 | checkcompt("field 'huge'", "return math.huge << 1") |
| 313 | checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1)) |
| 314 | checkcompt(msgf2i, "return 2.3 ~ 0.0") |
| 315 | |
| 316 | |
| 317 | -- testing overflow errors when converting from float to integer (runtime) |
| 318 | local function f2i (x) return x | x end |
| 319 | checkerror(msgf2i, f2i, math.huge) -- +inf |
| 320 | checkerror(msgf2i, f2i, -math.huge) -- -inf |
| 321 | checkerror(msgf2i, f2i, 0/0) -- NaN |
| 322 | |
| 323 | if floatbits < intbits then |
| 324 | -- conversion tests when float cannot represent all integers |
| 325 | assert(maxint + 1.0 == maxint + 0.0) |
| 326 | assert(minint - 1.0 == minint + 0.0) |
| 327 | checkerror(msgf2i, f2i, maxint + 0.0) |
| 328 | assert(f2i(2.0^(intbits - 2)) == 1 << (intbits - 2)) |
| 329 | assert(f2i(-2.0^(intbits - 2)) == -(1 << (intbits - 2))) |
| 330 | assert((2.0^(floatbits - 1) + 1.0) // 1 == (1 << (floatbits - 1)) + 1) |
| 331 | -- maximum integer representable as a float |
| 332 | local mf = maxint - (1 << (floatbits - intbits)) + 1 |
| 333 | assert(f2i(mf + 0.0) == mf) -- OK up to here |
| 334 | mf = mf + 1 |
| 335 | assert(f2i(mf + 0.0) ~= mf) -- no more representable |
| 336 | else |
| 337 | -- conversion tests when float can represent all integers |
| 338 | assert(maxint + 1.0 > maxint) |
| 339 | assert(minint - 1.0 < minint) |
| 340 | assert(f2i(maxint + 0.0) == maxint) |
| 341 | checkerror("no integer rep", f2i, maxint + 1.0) |
| 342 | checkerror("no integer rep", f2i, minint - 1.0) |
| 343 | end |
| 344 | |
| 345 | -- 'minint' should be representable as a float no matter the precision |
| 346 | assert(f2i(minint + 0.0) == minint) |
| 347 | |
| 348 | |
| 349 | -- testing numeric strings |
| 350 | |
| 351 | assert("2" + 1 == 3) |
| 352 | assert("2 " + 1 == 3) |
| 353 | assert(" -2 " + 1 == -1) |
| 354 | assert(" -0xa " + 1 == -9) |
| 355 | |
| 356 | |
| 357 | -- Literal integer Overflows (new behavior in 5.3.3) |
| 358 | do |
| 359 | -- no overflows |
| 360 | assert(eqT(tonumber(tostring(maxint)), maxint)) |
| 361 | assert(eqT(tonumber(tostring(minint)), minint)) |
| 362 | |
| 363 | -- add 1 to last digit as a string (it cannot be 9...) |
| 364 | local function incd (n) |
| 365 | local s = string.format("%d", n) |
| 366 | s = string.gsub(s, "%d$", function (d) |
| 367 | assert(d ~= '9') |
| 368 | return string.char(string.byte(d) + 1) |
| 369 | end) |
| 370 | return s |
| 371 | end |
| 372 | |
| 373 | -- 'tonumber' with overflow by 1 |
| 374 | assert(eqT(tonumber(incd(maxint)), maxint + 1.0)) |
| 375 | assert(eqT(tonumber(incd(minint)), minint - 1.0)) |
| 376 | |
| 377 | -- large numbers |
| 378 | assert(eqT(tonumber("1"..string.rep("0", 30)), 1e30)) |
| 379 | assert(eqT(tonumber("-1"..string.rep("0", 30)), -1e30)) |
| 380 | |
| 381 | -- hexa format still wraps around |
| 382 | assert(eqT(tonumber("0x1"..string.rep("0", 30)), 0)) |
| 383 | |
| 384 | -- lexer in the limits |
| 385 | assert(minint == load("return " .. minint)()) |
| 386 | assert(eqT(maxint, load("return " .. maxint)())) |
| 387 | |
| 388 | -- limit for integer encoding in binary chunks |
| 389 | -- (~(-1 >> 1) is optimized by the compiler as minint, which is |
| 390 | -- then encoded in a binary chunk as max unsigned) |
| 391 | local f = string.dump(function () return ~(-1 >> 1) end) |
| 392 | assert(load(f)() == minint) |
| 393 | |
| 394 | assert(eqT(10000000000000000000000.0, 10000000000000000000000)) |
| 395 | assert(eqT(-10000000000000000000000.0, -10000000000000000000000)) |
| 396 | end |
| 397 | |
| 398 | |
| 399 | -- testing 'tonumber' |
| 400 | |
| 401 | -- 'tonumber' with numbers |
| 402 | assert(tonumber(3.4) == 3.4) |
| 403 | assert(eqT(tonumber(3), 3)) |
| 404 | assert(eqT(tonumber(maxint), maxint) and eqT(tonumber(minint), minint)) |
| 405 | assert(tonumber(1/0) == 1/0) |
| 406 | |
| 407 | -- 'tonumber' with strings |
| 408 | assert(tonumber("0") == 0) |
| 409 | assert(not tonumber("")) |
| 410 | assert(not tonumber(" ")) |
| 411 | assert(not tonumber("-")) |
| 412 | assert(not tonumber(" -0x ")) |
| 413 | assert(not tonumber{}) |
| 414 | assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and |
| 415 | tonumber'.01' == 0.01 and tonumber'-1.' == -1 and |
| 416 | tonumber'+1.' == 1) |
| 417 | assert(not tonumber'+ 0.01' and not tonumber'+.e1' and |
| 418 | not tonumber'1e' and not tonumber'1.0e+' and |
| 419 | not tonumber'.') |
| 420 | assert(tonumber('-012') == -010-2) |
| 421 | assert(tonumber('-1.2e2') == - - -120) |
| 422 | |
| 423 | assert(tonumber("0xffffffffffff") == (1 << (4*12)) - 1) |
| 424 | assert(tonumber("0x"..string.rep("f", (intbits//4))) == -1) |
| 425 | assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1) |
| 426 | |
| 427 | -- testing 'tonumber' with base |
| 428 | assert(tonumber(' 001010 ', 2) == 10) |
| 429 | assert(tonumber(' 001010 ', 10) == 001010) |
| 430 | assert(tonumber(' -1010 ', 2) == -10) |
| 431 | assert(tonumber('10', 36) == 36) |
| 432 | assert(tonumber(' -10 ', 36) == -36) |
| 433 | assert(tonumber(' +1Z ', 36) == 36 + 35) |
| 434 | assert(tonumber(' -1z ', 36) == -36 + -35) |
| 435 | assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15))))))) |
| 436 | assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2^(intbits - 2)) |
| 437 | assert(tonumber('ffffFFFF', 16)+1 == (1 << 32)) |
| 438 | assert(tonumber('0ffffFFFF', 16)+1 == (1 << 32)) |
| 439 | assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40)) |
| 440 | for i = 2,36 do |
| 441 | local i2 = i * i |
| 442 | local i10 = i2 * i2 * i2 * i2 * i2 -- i^10 |
| 443 | assert(tonumber('\t10000000000\t', i) == i10) |
| 444 | end |
| 445 | |
| 446 | if not _ENV._soft then |
| 447 | -- tests with very long numerals |
| 448 | assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) |
| 449 | assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) |
| 450 | assert(tonumber("0x"..string.rep("f", 300)..".0") == 2.0^(4*300) - 1) |
| 451 | assert(tonumber("0x"..string.rep("f", 500)..".0") == 2.0^(4*500) - 1) |
| 452 | assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3) |
| 453 | assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10) |
| 454 | assert(tonumber('0x0.' .. string.rep('0', 13).."1") == 2.0^(-4*14)) |
| 455 | assert(tonumber('0x0.' .. string.rep('0', 150).."1") == 2.0^(-4*151)) |
| 456 | assert(tonumber('0x0.' .. string.rep('0', 300).."1") == 2.0^(-4*301)) |
| 457 | assert(tonumber('0x0.' .. string.rep('0', 500).."1") == 2.0^(-4*501)) |
| 458 | |
| 459 | assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0) |
| 460 | assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 0x7.4) |
| 461 | end |
| 462 | |
| 463 | -- testing 'tonumber' for invalid formats |
| 464 | |
| 465 | local function f (...) |
| 466 | if select('#', ...) == 1 then |
| 467 | return (...) |
| 468 | else |
| 469 | return "***" |
| 470 | end |
| 471 | end |
| 472 | |
| 473 | assert(not f(tonumber('fFfa', 15))) |
| 474 | assert(not f(tonumber('099', 8))) |
| 475 | assert(not f(tonumber('1\0', 2))) |
| 476 | assert(not f(tonumber('', 8))) |
| 477 | assert(not f(tonumber(' ', 9))) |
| 478 | assert(not f(tonumber(' ', 9))) |
| 479 | assert(not f(tonumber('0xf', 10))) |
| 480 | |
| 481 | assert(not f(tonumber('inf'))) |
| 482 | assert(not f(tonumber(' INF '))) |
| 483 | assert(not f(tonumber('Nan'))) |
| 484 | assert(not f(tonumber('nan'))) |
| 485 | |
| 486 | assert(not f(tonumber(' '))) |
| 487 | assert(not f(tonumber(''))) |
| 488 | assert(not f(tonumber('1 a'))) |
| 489 | assert(not f(tonumber('1 a', 2))) |
| 490 | assert(not f(tonumber('1\0'))) |
| 491 | assert(not f(tonumber('1 \0'))) |
| 492 | assert(not f(tonumber('1\0 '))) |
| 493 | assert(not f(tonumber('e1'))) |
| 494 | assert(not f(tonumber('e 1'))) |
| 495 | assert(not f(tonumber(' 3.4.5 '))) |
| 496 | |
| 497 | |
| 498 | -- testing 'tonumber' for invalid hexadecimal formats |
| 499 | |
| 500 | assert(not tonumber('0x')) |
| 501 | assert(not tonumber('x')) |
| 502 | assert(not tonumber('x3')) |
| 503 | assert(not tonumber('0x3.3.3')) -- two decimal points |
| 504 | assert(not tonumber('00x2')) |
| 505 | assert(not tonumber('0x 2')) |
| 506 | assert(not tonumber('0 x2')) |
| 507 | assert(not tonumber('23x')) |
| 508 | assert(not tonumber('- 0xaa')) |
| 509 | assert(not tonumber('-0xaaP ')) -- no exponent |
| 510 | assert(not tonumber('0x0.51p')) |
| 511 | assert(not tonumber('0x5p+-2')) |
| 512 | |
| 513 | |
| 514 | -- testing hexadecimal numerals |
| 515 | |
| 516 | assert(0x10 == 16 and 0xfff == 2^12 - 1 and 0XFB == 251) |
| 517 | assert(0x0p12 == 0 and 0x.0p-3 == 0) |
| 518 | assert(0xFFFFFFFF == (1 << 32) - 1) |
| 519 | assert(tonumber('+0x2') == 2) |
| 520 | assert(tonumber('-0xaA') == -170) |
| 521 | assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1) |
| 522 | |
| 523 | -- possible confusion with decimal exponent |
| 524 | assert(0E+1 == 0 and 0xE+1 == 15 and 0xe-1 == 13) |
| 525 | |
| 526 | |
| 527 | -- floating hexas |
| 528 | |
| 529 | assert(tonumber(' 0x2.5 ') == 0x25/16) |
| 530 | assert(tonumber(' -0x2.5 ') == -0x25/16) |
| 531 | assert(tonumber(' +0x0.51p+8 ') == 0x51) |
| 532 | assert(0x.FfffFFFF == 1 - '0x.00000001') |
| 533 | assert('0xA.a' + 0 == 10 + 10/16) |
| 534 | assert(0xa.aP4 == 0XAA) |
| 535 | assert(0x4P-2 == 1) |
| 536 | assert(0x1.1 == '0x1.' + '+0x.1') |
| 537 | assert(0Xabcdef.0 == 0x.ABCDEFp+24) |
| 538 | |
| 539 | |
| 540 | assert(1.1 == 1.+.1) |
| 541 | assert(100.0 == 1E2 and .01 == 1e-2) |
| 542 | assert(1111111111 - 1111111110 == 1000.00e-03) |
| 543 | assert(1.1 == '1.'+'.1') |
| 544 | assert(tonumber'1111111111' - tonumber'1111111110' == |
| 545 | tonumber" +0.001e+3 \n\t") |
| 546 | |
| 547 | assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31) |
| 548 | |
| 549 | assert(0.123456 > 0.123455) |
| 550 | |
| 551 | assert(tonumber('+1.23E18') == 1.23*10.0^18) |
| 552 | |
| 553 | -- testing order operators |
| 554 | assert(not(1<1) and (1<2) and not(2<1)) |
| 555 | assert(not('a'<'a') and ('a'<'b') and not('b'<'a')) |
| 556 | assert((1<=1) and (1<=2) and not(2<=1)) |
| 557 | assert(('a'<='a') and ('a'<='b') and not('b'<='a')) |
| 558 | assert(not(1>1) and not(1>2) and (2>1)) |
| 559 | assert(not('a'>'a') and not('a'>'b') and ('b'>'a')) |
| 560 | assert((1>=1) and not(1>=2) and (2>=1)) |
| 561 | assert(('a'>='a') and not('a'>='b') and ('b'>='a')) |
| 562 | assert(1.3 < 1.4 and 1.3 <= 1.4 and not (1.3 < 1.3) and 1.3 <= 1.3) |
| 563 | |
| 564 | -- testing mod operator |
| 565 | assert(eqT(-4 % 3, 2)) |
| 566 | assert(eqT(4 % -3, -2)) |
| 567 | assert(eqT(-4.0 % 3, 2.0)) |
| 568 | assert(eqT(4 % -3.0, -2.0)) |
| 569 | assert(eqT(4 % -5, -1)) |
| 570 | assert(eqT(4 % -5.0, -1.0)) |
| 571 | assert(eqT(4 % 5, 4)) |
| 572 | assert(eqT(4 % 5.0, 4.0)) |
| 573 | assert(eqT(-4 % -5, -4)) |
| 574 | assert(eqT(-4 % -5.0, -4.0)) |
| 575 | assert(eqT(-4 % 5, 1)) |
| 576 | assert(eqT(-4 % 5.0, 1.0)) |
| 577 | assert(eqT(4.25 % 4, 0.25)) |
| 578 | assert(eqT(10.0 % 2, 0.0)) |
| 579 | assert(eqT(-10.0 % 2, 0.0)) |
| 580 | assert(eqT(-10.0 % -2, 0.0)) |
| 581 | assert(math.pi - math.pi % 1 == 3) |
| 582 | assert(math.pi - math.pi % 0.001 == 3.141) |
| 583 | |
| 584 | do -- very small numbers |
| 585 | local i, j = 0, 20000 |
| 586 | while i < j do |
| 587 | local m = (i + j) // 2 |
| 588 | if 10^-m > 0 then |
| 589 | i = m + 1 |
| 590 | else |
| 591 | j = m |
| 592 | end |
| 593 | end |
| 594 | -- 'i' is the smallest possible ten-exponent |
| 595 | local b = 10^-(i - (i // 10)) -- a very small number |
| 596 | assert(b > 0 and b * b == 0) |
| 597 | local delta = b / 1000 |
| 598 | assert(eq((2.1 * b) % (2 * b), (0.1 * b), delta)) |
| 599 | assert(eq((-2.1 * b) % (2 * b), (2 * b) - (0.1 * b), delta)) |
| 600 | assert(eq((2.1 * b) % (-2 * b), (0.1 * b) - (2 * b), delta)) |
| 601 | assert(eq((-2.1 * b) % (-2 * b), (-0.1 * b), delta)) |
| 602 | end |
| 603 | |
| 604 | |
| 605 | -- basic consistency between integer modulo and float modulo |
| 606 | for i = -10, 10 do |
| 607 | for j = -10, 10 do |
| 608 | if j ~= 0 then |
| 609 | assert((i + 0.0) % j == i % j) |
| 610 | end |
| 611 | end |
| 612 | end |
| 613 | |
| 614 | for i = 0, 10 do |
| 615 | for j = -10, 10 do |
| 616 | if j ~= 0 then |
| 617 | assert((2^i) % j == (1 << i) % j) |
| 618 | end |
| 619 | end |
| 620 | end |
| 621 | |
| 622 | do -- precision of module for large numbers |
| 623 | local i = 10 |
| 624 | while (1 << i) > 0 do |
| 625 | assert((1 << i) % 3 == i % 2 + 1) |
| 626 | i = i + 1 |
| 627 | end |
| 628 | |
| 629 | i = 10 |
| 630 | while 2^i < math.huge do |
| 631 | assert(2^i % 3 == i % 2 + 1) |
| 632 | i = i + 1 |
| 633 | end |
| 634 | end |
| 635 | |
| 636 | assert(eqT(minint % minint, 0)) |
| 637 | assert(eqT(maxint % maxint, 0)) |
| 638 | assert((minint + 1) % minint == minint + 1) |
| 639 | assert((maxint - 1) % maxint == maxint - 1) |
| 640 | assert(minint % maxint == maxint - 1) |
| 641 | |
| 642 | assert(minint % -1 == 0) |
| 643 | assert(minint % -2 == 0) |
| 644 | assert(maxint % -2 == -1) |
| 645 | |
| 646 | -- non-portable tests because Windows C library cannot compute |
| 647 | -- fmod(1, huge) correctly |
| 648 | if not _ENV._port then |
| 649 | local function anan (x) assert(isNaN(x)) end -- assert Not a Number |
| 650 | anan(0.0 % 0) |
| 651 | anan(1.3 % 0) |
| 652 | anan(math.huge % 1) |
| 653 | anan(math.huge % 1e30) |
| 654 | anan(-math.huge % 1e30) |
| 655 | anan(-math.huge % -1e30) |
| 656 | assert(1 % math.huge == 1) |
| 657 | assert(1e30 % math.huge == 1e30) |
| 658 | assert(1e30 % -math.huge == -math.huge) |
| 659 | assert(-1 % math.huge == math.huge) |
| 660 | assert(-1 % -math.huge == -1) |
| 661 | end |
| 662 | |
| 663 | |
| 664 | -- testing unsigned comparisons |
| 665 | assert(math.ult(3, 4)) |
| 666 | assert(not math.ult(4, 4)) |
| 667 | assert(math.ult(-2, -1)) |
| 668 | assert(math.ult(2, -1)) |
| 669 | assert(not math.ult(-2, -2)) |
| 670 | assert(math.ult(maxint, minint)) |
| 671 | assert(not math.ult(minint, maxint)) |
| 672 | |
| 673 | |
| 674 | assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1)) |
| 675 | assert(eq(math.tan(math.pi/4), 1)) |
| 676 | assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0)) |
| 677 | assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and |
| 678 | eq(math.asin(1), math.pi/2)) |
| 679 | assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2)) |
| 680 | assert(math.abs(-10.43) == 10.43) |
| 681 | assert(eqT(math.abs(minint), minint)) |
| 682 | assert(eqT(math.abs(maxint), maxint)) |
| 683 | assert(eqT(math.abs(-maxint), maxint)) |
| 684 | assert(eq(math.atan(1,0), math.pi/2)) |
| 685 | assert(math.fmod(10,3) == 1) |
| 686 | assert(eq(math.sqrt(10)^2, 10)) |
| 687 | assert(eq(math.log(2, 10), math.log(2)/math.log(10))) |
| 688 | assert(eq(math.log(2, 2), 1)) |
| 689 | assert(eq(math.log(9, 3), 2)) |
| 690 | assert(eq(math.exp(0), 1)) |
| 691 | assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) |
| 692 | |
| 693 | |
| 694 | do print("testing ldexp/frexp") |
| 695 | global ipairs |
| 696 | for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do |
| 697 | local m, p = math.frexp(x) |
| 698 | assert(math.ldexp(m, p) == x) |
| 699 | local am = math.abs(m) |
| 700 | assert(m == x or (0.5 <= am and am < 1)) |
| 701 | end |
| 702 | |
| 703 | end |
| 704 | |
| 705 | |
| 706 | assert(tonumber(' 1.3e-2 ') == 1.3e-2) |
| 707 | assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) |
| 708 | |
| 709 | -- testing constant limits |
| 710 | -- 2^23 = 8388608 |
| 711 | assert(8388609 + -8388609 == 0) |
| 712 | assert(8388608 + -8388608 == 0) |
| 713 | assert(8388607 + -8388607 == 0) |
| 714 | |
| 715 | |
| 716 | |
| 717 | do -- testing floor & ceil |
| 718 | assert(eqT(math.floor(3.4), 3)) |
| 719 | assert(eqT(math.ceil(3.4), 4)) |
| 720 | assert(eqT(math.floor(-3.4), -4)) |
| 721 | assert(eqT(math.ceil(-3.4), -3)) |
| 722 | assert(eqT(math.floor(maxint), maxint)) |
| 723 | assert(eqT(math.ceil(maxint), maxint)) |
| 724 | assert(eqT(math.floor(minint), minint)) |
| 725 | assert(eqT(math.floor(minint + 0.0), minint)) |
| 726 | assert(eqT(math.ceil(minint), minint)) |
| 727 | assert(eqT(math.ceil(minint + 0.0), minint)) |
| 728 | assert(math.floor(1e50) == 1e50) |
| 729 | assert(math.ceil(1e50) == 1e50) |
| 730 | assert(math.floor(-1e50) == -1e50) |
| 731 | assert(math.ceil(-1e50) == -1e50) |
| 732 | for _, p in pairs{31,32,63,64} do |
| 733 | assert(math.floor(2^p) == 2^p) |
| 734 | assert(math.floor(2^p + 0.5) == 2^p) |
| 735 | assert(math.ceil(2^p) == 2^p) |
| 736 | assert(math.ceil(2^p - 0.5) == 2^p) |
| 737 | end |
| 738 | checkerror("number expected", math.floor, {}) |
| 739 | checkerror("number expected", math.ceil, print) |
| 740 | assert(eqT(math.tointeger(minint), minint)) |
| 741 | assert(eqT(math.tointeger(minint .. ""), minint)) |
| 742 | assert(eqT(math.tointeger(maxint), maxint)) |
| 743 | assert(eqT(math.tointeger(maxint .. ""), maxint)) |
| 744 | assert(eqT(math.tointeger(minint + 0.0), minint)) |
| 745 | assert(not math.tointeger(0.0 - minint)) |
| 746 | assert(not math.tointeger(math.pi)) |
| 747 | assert(not math.tointeger(-math.pi)) |
| 748 | assert(math.floor(math.huge) == math.huge) |
| 749 | assert(math.ceil(math.huge) == math.huge) |
| 750 | assert(not math.tointeger(math.huge)) |
| 751 | assert(math.floor(-math.huge) == -math.huge) |
| 752 | assert(math.ceil(-math.huge) == -math.huge) |
| 753 | assert(not math.tointeger(-math.huge)) |
| 754 | assert(math.tointeger("34.0") == 34) |
| 755 | assert(not math.tointeger("34.3")) |
| 756 | assert(not math.tointeger({})) |
| 757 | assert(not math.tointeger(0/0)) -- NaN |
| 758 | end |
| 759 | |
| 760 | |
| 761 | -- testing fmod for integers |
| 762 | for i = -6, 6 do |
| 763 | for j = -6, 6 do |
| 764 | if j ~= 0 then |
| 765 | local mi = math.fmod(i, j) |
| 766 | local mf = math.fmod(i + 0.0, j) |
| 767 | assert(mi == mf) |
| 768 | assert(math.type(mi) == 'integer' and math.type(mf) == 'float') |
| 769 | if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then |
| 770 | assert(eqT(mi, i % j)) |
| 771 | end |
| 772 | end |
| 773 | end |
| 774 | end |
| 775 | assert(eqT(math.fmod(minint, minint), 0)) |
| 776 | assert(eqT(math.fmod(maxint, maxint), 0)) |
| 777 | assert(eqT(math.fmod(minint + 1, minint), minint + 1)) |
| 778 | assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1)) |
| 779 | |
| 780 | checkerror("zero", math.fmod, 3, 0) |
| 781 | |
| 782 | |
| 783 | do -- testing max/min |
| 784 | checkerror("value expected", math.max) |
| 785 | checkerror("value expected", math.min) |
| 786 | assert(eqT(math.max(3), 3)) |
| 787 | assert(eqT(math.max(3, 5, 9, 1), 9)) |
| 788 | assert(math.max(maxint, 10e60) == 10e60) |
| 789 | assert(eqT(math.max(minint, minint + 1), minint + 1)) |
| 790 | assert(eqT(math.min(3), 3)) |
| 791 | assert(eqT(math.min(3, 5, 9, 1), 1)) |
| 792 | assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2) |
| 793 | assert(math.min(1.9, 1.7, 1.72) == 1.7) |
| 794 | assert(math.min(-10e60, minint) == -10e60) |
| 795 | assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) |
| 796 | assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) |
| 797 | end |
| 798 | -- testing implicit conversions |
| 799 | |
| 800 | local a,b = '10', '20' |
| 801 | assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) |
| 802 | assert(a == '10' and b == '20') |
| 803 | |
| 804 | |
| 805 | do |
| 806 | print("testing -0 and NaN") |
| 807 | global rawset, undef |
| 808 | local mz <const> = -0.0 |
| 809 | local z <const> = 0.0 |
| 810 | assert(mz == z) |
| 811 | assert(1/mz < 0 and 0 < 1/z) |
| 812 | local a = {[mz] = 1} |
| 813 | assert(a[z] == 1 and a[mz] == 1) |
| 814 | a[z] = 2 |
| 815 | assert(a[z] == 2 and a[mz] == 2) |
| 816 | local inf = math.huge * 2 + 1 |
| 817 | local mz <const> = -1/inf |
| 818 | local z <const> = 1/inf |
| 819 | assert(mz == z) |
| 820 | assert(1/mz < 0 and 0 < 1/z) |
| 821 | local NaN <const> = inf - inf |
| 822 | assert(NaN ~= NaN) |
| 823 | assert(not (NaN < NaN)) |
| 824 | assert(not (NaN <= NaN)) |
| 825 | assert(not (NaN > NaN)) |
| 826 | assert(not (NaN >= NaN)) |
| 827 | assert(not (0 < NaN) and not (NaN < 0)) |
| 828 | local NaN1 <const> = 0/0 |
| 829 | assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) |
| 830 | local a = {} |
| 831 | assert(not pcall(rawset, a, NaN, 1)) |
| 832 | assert(a[NaN] == undef) |
| 833 | a[1] = 1 |
| 834 | assert(not pcall(rawset, a, NaN, 1)) |
| 835 | assert(a[NaN] == undef) |
| 836 | -- strings with same binary representation as 0.0 (might create problems |
| 837 | -- for constant manipulation in the pre-compiler) |
| 838 | 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" |
| 839 | assert(a1 == a2 and a2 == a4 and a1 ~= a3) |
| 840 | assert(a3 == a5) |
| 841 | |
| 842 | -- Bug since 5.4: Results differ for constant 0 versus variable 0; |
| 843 | -- (mz - 0) should be -0.0, but it resulted in +0.0 |
| 844 | local zi = 0 |
| 845 | assert(1/(mz - 0) == 1/(mz - zi)) |
| 846 | end |
| 847 | |
| 848 | |
| 849 | -- |
| 850 | -- [[================================================================== |
| 851 | print("testing 'math.random'") |
| 852 | -- -=================================================================== |
| 853 | -- |
| 854 | |
| 855 | local random, max, min = math.random, math.max, math.min |
| 856 | |
| 857 | local function testnear (val, ref, tol) |
| 858 | return (math.abs(val - ref) < ref * tol) |
| 859 | end |
| 860 | |
| 861 | |
| 862 | -- low-level!! For the current implementation of random in Lua, |
| 863 | -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 |
| 864 | do |
| 865 | -- all computations should work with 32-bit integers |
| 866 | local h <const> = 0x7a7040a5 -- higher half |
| 867 | local l <const> = 0xa323c9d6 -- lower half |
| 868 | |
| 869 | math.randomseed(1007) |
| 870 | -- get the low 'intbits' of the 64-bit expected result |
| 871 | local res = (h << 32 | l) & ~(~0 << intbits) |
| 872 | assert(random(0) == res) |
| 873 | |
| 874 | math.randomseed(1007, 0) |
| 875 | -- using higher bits to generate random floats; (the '% 2^32' converts |
| 876 | -- 32-bit integers to floats as unsigned) |
| 877 | local res |
| 878 | if floatbits <= 32 then |
| 879 | -- get all bits from the higher half |
| 880 | res = (h >> (32 - floatbits)) % 2^32 |
| 881 | else |
| 882 | -- get 32 bits from the higher half and the rest from the lower half |
| 883 | res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32) |
| 884 | end |
| 885 | local rand = random() |
| 886 | assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits)) |
| 887 | assert(rand * 2^floatbits == res) |
| 888 | end |
| 889 | |
| 890 | do |
| 891 | -- testing return of 'randomseed' |
| 892 | local x, y = math.randomseed() |
| 893 | local res = math.random(0) |
| 894 | x, y = math.randomseed(x, y) -- should repeat the state |
| 895 | assert(math.random(0) == res) |
| 896 | math.randomseed(x, y) -- again should repeat the state |
| 897 | assert(math.random(0) == res) |
| 898 | -- keep the random seed for following tests |
| 899 | print(string.format("random seeds: %d, %d", x, y)) |
| 900 | end |
| 901 | |
| 902 | do -- test random for floats |
| 903 | local randbits = math.min(floatbits, 64) -- at most 64 random bits |
| 904 | local mult = 2^randbits -- to make random float into an integral |
| 905 | local counts = {} -- counts for bits |
| 906 | for i = 1, randbits do counts[i] = 0 end |
| 907 | local up = -math.huge |
| 908 | local low = math.huge |
| 909 | local rounds = 100 * randbits -- 100 times for each bit |
| 910 | local totalrounds = 0 |
| 911 | ::doagain:: -- will repeat test until we get good statistics |
| 912 | for i = 0, rounds do |
| 913 | local t = random() |
| 914 | assert(0 <= t and t < 1) |
| 915 | up = max(up, t) |
| 916 | low = min(low, t) |
| 917 | assert(t * mult % 1 == 0) -- no extra bits |
| 918 | local bit = i % randbits -- bit to be tested |
| 919 | if (t * 2^bit) % 1 >= 0.5 then -- is bit set? |
| 920 | counts[bit + 1] = counts[bit + 1] + 1 -- increment its count |
| 921 | end |
| 922 | end |
| 923 | totalrounds = totalrounds + rounds |
| 924 | if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then |
| 925 | goto doagain |
| 926 | end |
| 927 | -- all bit counts should be near 50% |
| 928 | local expected = (totalrounds / randbits / 2) |
| 929 | for i = 1, randbits do |
| 930 | if not testnear(counts[i], expected, 0.10) then |
| 931 | goto doagain |
| 932 | end |
| 933 | end |
| 934 | print(string.format("float random range in %d calls: [%f, %f]", |
| 935 | totalrounds, low, up)) |
| 936 | end |
| 937 | |
| 938 | |
| 939 | do -- test random for full integers |
| 940 | local up = 0 |
| 941 | local low = 0 |
| 942 | local counts = {} -- counts for bits |
| 943 | for i = 1, intbits do counts[i] = 0 end |
| 944 | local rounds = 100 * intbits -- 100 times for each bit |
| 945 | local totalrounds = 0 |
| 946 | ::doagain:: -- will repeat test until we get good statistics |
| 947 | for i = 0, rounds do |
| 948 | local t = random(0) |
| 949 | up = max(up, t) |
| 950 | low = min(low, t) |
| 951 | local bit = i % intbits -- bit to be tested |
| 952 | -- increment its count if it is set |
| 953 | counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1) |
| 954 | end |
| 955 | totalrounds = totalrounds + rounds |
| 956 | local lim = maxint >> 10 |
| 957 | if not (maxint - up < lim and low - minint < lim) then |
| 958 | goto doagain |
| 959 | end |
| 960 | -- all bit counts should be near 50% |
| 961 | local expected = (totalrounds / intbits / 2) |
| 962 | for i = 1, intbits do |
| 963 | if not testnear(counts[i], expected, 0.10) then |
| 964 | goto doagain |
| 965 | end |
| 966 | end |
| 967 | print(string.format( |
| 968 | "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]", |
| 969 | totalrounds, (minint - low) / minint * 1e6, |
| 970 | (maxint - up) / maxint * 1e6)) |
| 971 | end |
| 972 | |
| 973 | do |
| 974 | -- test distribution for a dice |
| 975 | local count = {0, 0, 0, 0, 0, 0} |
| 976 | local rep = 200 |
| 977 | local totalrep = 0 |
| 978 | ::doagain:: |
| 979 | for i = 1, rep * 6 do |
| 980 | local r = random(6) |
| 981 | count[r] = count[r] + 1 |
| 982 | end |
| 983 | totalrep = totalrep + rep |
| 984 | for i = 1, 6 do |
| 985 | if not testnear(count[i], totalrep, 0.05) then |
| 986 | goto doagain |
| 987 | end |
| 988 | end |
| 989 | end |
| 990 | |
| 991 | do |
| 992 | local function aux (x1, x2) -- test random for small intervals |
| 993 | local mark = {}; local count = 0 -- to check that all values appeared |
| 994 | while true do |
| 995 | local t = random(x1, x2) |
| 996 | assert(x1 <= t and t <= x2) |
| 997 | if not mark[t] then -- new value |
| 998 | mark[t] = true |
| 999 | count = count + 1 |
| 1000 | if count == x2 - x1 + 1 then -- all values appeared; OK |
| 1001 | goto ok |
| 1002 | end |
| 1003 | end |
| 1004 | end |
| 1005 | ::ok:: |
| 1006 | end |
| 1007 | |
| 1008 | aux(-10,0) |
| 1009 | aux(1, 6) |
| 1010 | aux(1, 2) |
| 1011 | aux(1, 13) |
| 1012 | aux(1, 31) |
| 1013 | aux(1, 32) |
| 1014 | aux(1, 33) |
| 1015 | aux(-10, 10) |
| 1016 | aux(-10,-10) -- unit set |
| 1017 | aux(minint, minint) -- unit set |
| 1018 | aux(maxint, maxint) -- unit set |
| 1019 | aux(minint, minint + 9) |
| 1020 | aux(maxint - 3, maxint) |
| 1021 | end |
| 1022 | |
| 1023 | do |
| 1024 | local function aux(p1, p2) -- test random for large intervals |
| 1025 | local max = minint |
| 1026 | local min = maxint |
| 1027 | local n = 100 |
| 1028 | local mark = {}; local count = 0 -- to count how many different values |
| 1029 | ::doagain:: |
| 1030 | for _ = 1, n do |
| 1031 | local t = random(p1, p2) |
| 1032 | if not mark[t] then -- new value |
| 1033 | assert(p1 <= t and t <= p2) |
| 1034 | max = math.max(max, t) |
| 1035 | min = math.min(min, t) |
| 1036 | mark[t] = true |
| 1037 | count = count + 1 |
| 1038 | end |
| 1039 | end |
| 1040 | -- at least 80% of values are different |
| 1041 | if not (count >= n * 0.8) then |
| 1042 | goto doagain |
| 1043 | end |
| 1044 | -- min and max not too far from formal min and max |
| 1045 | local diff = (p2 - p1) >> 4 |
| 1046 | if not (min < p1 + diff and max > p2 - diff) then |
| 1047 | goto doagain |
| 1048 | end |
| 1049 | end |
| 1050 | aux(0, maxint) |
| 1051 | aux(1, maxint) |
| 1052 | aux(3, maxint // 3) |
| 1053 | aux(minint, -1) |
| 1054 | aux(minint // 2, maxint // 2) |
| 1055 | aux(minint, maxint) |
| 1056 | aux(minint + 1, maxint) |
| 1057 | aux(minint, maxint - 1) |
| 1058 | aux(0, 1 << (intbits - 5)) |
| 1059 | end |
| 1060 | |
| 1061 | |
| 1062 | assert(not pcall(random, 1, 2, 3)) -- too many arguments |
| 1063 | |
| 1064 | -- empty interval |
| 1065 | assert(not pcall(random, minint + 1, minint)) |
| 1066 | assert(not pcall(random, maxint, maxint - 1)) |
| 1067 | assert(not pcall(random, maxint, minint)) |
| 1068 | |
| 1069 | -- ]]================================================================== |
| 1070 | |
| 1071 | |
| 1072 | -- |
| 1073 | -- [[================================================================== |
| 1074 | print("testing precision of 'tostring'") |
| 1075 | -- -=================================================================== |
| 1076 | -- |
| 1077 | |
| 1078 | -- number of decimal digits supported by float precision |
| 1079 | local decdig = math.floor(floatbits * math.log(2, 10)) |
| 1080 | print(string.format(" %d-digit float numbers with full precision", |
| 1081 | decdig)) |
| 1082 | -- number of decimal digits supported by integer precision |
| 1083 | local Idecdig = math.floor(math.log(maxint, 10)) |
| 1084 | print(string.format(" %d-digit integer numbers with full precision", |
| 1085 | Idecdig)) |
| 1086 | |
| 1087 | do |
| 1088 | -- Any number should print so that reading it back gives itself: |
| 1089 | -- tonumber(tostring(x)) == x |
| 1090 | |
| 1091 | -- Mersenne fractions |
| 1092 | local p = 1.0 |
| 1093 | for i = 1, maxexp do |
| 1094 | p = p + p |
| 1095 | local x = 1 / (p - 1) |
| 1096 | assert(x == tonumber(tostring(x))) |
| 1097 | end |
| 1098 | |
| 1099 | -- some random numbers in [0,1) |
| 1100 | for i = 1, 100 do |
| 1101 | local x = math.random() |
| 1102 | assert(x == tonumber(tostring(x))) |
| 1103 | end |
| 1104 | |
| 1105 | -- different numbers should print differently. |
| 1106 | -- check pairs of floats with minimum detectable difference |
| 1107 | local p = floatbits - 1 |
| 1108 | global ipairs |
| 1109 | for i = 1, maxexp - 1 do |
| 1110 | for _, i in ipairs{-i, i} do |
| 1111 | local x = 2^i |
| 1112 | local diff = 2^(i - p) -- least significant bit for 'x' |
| 1113 | local y = x + diff |
| 1114 | local fy = tostring(y) |
| 1115 | assert(x ~= y and tostring(x) ~= fy) |
| 1116 | assert(tonumber(fy) == y) |
| 1117 | end |
| 1118 | end |
| 1119 | |
| 1120 | |
| 1121 | -- "reasonable" numerals should be printed like themselves |
| 1122 | |
| 1123 | -- create random float numerals with 5 digits, with a decimal point |
| 1124 | -- inserted in all places. (With more than 5, things like "0.00001" |
| 1125 | -- reformats like "1e-5".) |
| 1126 | for i = 1, 1000 do |
| 1127 | -- random numeral with 5 digits |
| 1128 | local x = string.format("%.5d", math.random(0, 99999)) |
| 1129 | for i = 2, #x do |
| 1130 | -- insert decimal point at position 'i' |
| 1131 | local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1) |
| 1132 | y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros |
| 1133 | assert(y == tostring(tonumber(y))) |
| 1134 | end |
| 1135 | end |
| 1136 | |
| 1137 | -- all-random floats |
| 1138 | local Fsz = string.packsize("n") -- size of floats in bytes |
| 1139 | |
| 1140 | for i = 1, 400 do |
| 1141 | local s = string.pack("j", math.random(0)) -- a random string of bits |
| 1142 | while #s < Fsz do -- make 's' long enough |
| 1143 | s = s .. string.pack("j", math.random(0)) |
| 1144 | end |
| 1145 | local n = string.unpack("n", s) -- read 's' as a float |
| 1146 | s = tostring(n) |
| 1147 | if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf |
| 1148 | assert(tonumber(s) == n) |
| 1149 | end |
| 1150 | end |
| 1151 | |
| 1152 | end |
| 1153 | -- ]]================================================================== |
| 1154 | |
| 1155 | |
| 1156 | print('OK') |