diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
| commit | be8120906304a8658fab998587b969e0e42f5650 (patch) | |
| tree | 81cf2d38522b10468d09763fc25d261486008197 /testes | |
| parent | e05590591410a5e007a1e3f1691f6c1cf9d8fe45 (diff) | |
| download | lua-be8120906304a8658fab998587b969e0e42f5650.tar.gz lua-be8120906304a8658fab998587b969e0e42f5650.tar.bz2 lua-be8120906304a8658fab998587b969e0e42f5650.zip | |
First implementation of global declarations
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/db.lua | 1 | ||||
| -rw-r--r-- | testes/goto.lua | 44 | ||||
| -rw-r--r-- | testes/math.lua | 16 |
3 files changed, 57 insertions, 4 deletions
diff --git a/testes/db.lua b/testes/db.lua index e4982c20..ae204c41 100644 --- a/testes/db.lua +++ b/testes/db.lua | |||
| @@ -349,6 +349,7 @@ end, "crl") | |||
| 349 | 349 | ||
| 350 | 350 | ||
| 351 | function f(a,b) | 351 | function f(a,b) |
| 352 | global collectgarbage, assert, g, string | ||
| 352 | collectgarbage() | 353 | collectgarbage() |
| 353 | local _, x = debug.getlocal(1, 1) | 354 | local _, x = debug.getlocal(1, 1) |
| 354 | local _, y = debug.getlocal(1, 2) | 355 | local _, y = debug.getlocal(1, 2) |
diff --git a/testes/goto.lua b/testes/goto.lua index eca68516..fdfddb85 100644 --- a/testes/goto.lua +++ b/testes/goto.lua | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | -- $Id: testes/goto.lua $ | 1 | -- $Id: testes/goto.lua $ |
| 2 | -- See Copyright Notice in file lua.h | 2 | -- See Copyright Notice in file lua.h |
| 3 | 3 | ||
| 4 | print("testing goto and global declarations") | ||
| 5 | |||
| 4 | collectgarbage() | 6 | collectgarbage() |
| 5 | 7 | ||
| 6 | local function errmsg (code, m) | 8 | local function errmsg (code, m) |
| @@ -280,7 +282,47 @@ end | |||
| 280 | 282 | ||
| 281 | 283 | ||
| 282 | foo() | 284 | foo() |
| 283 | -------------------------------------------------------------------------------- | 285 | -------------------------------------------------------------------------- |
| 284 | 286 | ||
| 287 | do | ||
| 288 | global print, load, T<const>; global assert<const> | ||
| 289 | global string | ||
| 290 | |||
| 291 | local function checkerr (code, err) | ||
| 292 | local st, msg = load(code) | ||
| 293 | assert(not st and string.find(msg, err)) | ||
| 294 | end | ||
| 295 | |||
| 296 | -- globals must be declared after a global declaration | ||
| 297 | checkerr("global none; X = 1", "variable 'X'") | ||
| 298 | |||
| 299 | -- global variables cannot be to-be-closed | ||
| 300 | checkerr("global X<close>", "cannot be") | ||
| 301 | |||
| 302 | do | ||
| 303 | local X = 10 | ||
| 304 | do global X; X = 20 end | ||
| 305 | assert(X == 10) -- local X | ||
| 306 | end | ||
| 307 | assert(_ENV.X == 20) -- global X | ||
| 308 | |||
| 309 | -- '_ENV' cannot be global | ||
| 310 | checkerr("global _ENV, a; a = 10", "variable 'a'") | ||
| 311 | |||
| 312 | -- global declarations inside functions | ||
| 313 | checkerr([[ | ||
| 314 | global none | ||
| 315 | local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") | ||
| 316 | |||
| 317 | if not T then -- when not in "test mode", "global" isn't reserved | ||
| 318 | assert(load("global = 1; return global")() == 1) | ||
| 319 | print " ('global' is not a reserved word)" | ||
| 320 | else | ||
| 321 | -- "global" reserved, cannot be used as a variable | ||
| 322 | assert(not load("global = 1; return global")) | ||
| 323 | end | ||
| 324 | |||
| 325 | end | ||
| 285 | 326 | ||
| 286 | print'OK' | 327 | print'OK' |
| 328 | |||
diff --git a/testes/math.lua b/testes/math.lua index 88a57ce7..242579b1 100644 --- a/testes/math.lua +++ b/testes/math.lua | |||
| @@ -3,6 +3,14 @@ | |||
| 3 | 3 | ||
| 4 | print("testing numbers and math lib") | 4 | print("testing numbers and math lib") |
| 5 | 5 | ||
| 6 | local math = require "math" | ||
| 7 | local string = require "string" | ||
| 8 | |||
| 9 | global none | ||
| 10 | |||
| 11 | global print, assert, pcall, type, pairs, load | ||
| 12 | global tonumber, tostring, select | ||
| 13 | |||
| 6 | local minint <const> = math.mininteger | 14 | local minint <const> = math.mininteger |
| 7 | local maxint <const> = math.maxinteger | 15 | local maxint <const> = math.maxinteger |
| 8 | 16 | ||
| @@ -184,7 +192,7 @@ do | |||
| 184 | for i = -3, 3 do -- variables avoid constant folding | 192 | for i = -3, 3 do -- variables avoid constant folding |
| 185 | for j = -3, 3 do | 193 | for j = -3, 3 do |
| 186 | -- domain errors (0^(-n)) are not portable | 194 | -- domain errors (0^(-n)) are not portable |
| 187 | if not _port or i ~= 0 or j > 0 then | 195 | if not _ENV._port or i ~= 0 or j > 0 then |
| 188 | assert(eq(i^j, 1 / i^(-j))) | 196 | assert(eq(i^j, 1 / i^(-j))) |
| 189 | end | 197 | end |
| 190 | end | 198 | end |
| @@ -430,7 +438,7 @@ for i = 2,36 do | |||
| 430 | assert(tonumber('\t10000000000\t', i) == i10) | 438 | assert(tonumber('\t10000000000\t', i) == i10) |
| 431 | end | 439 | end |
| 432 | 440 | ||
| 433 | if not _soft then | 441 | if not _ENV._soft then |
| 434 | -- tests with very long numerals | 442 | -- tests with very long numerals |
| 435 | assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) | 443 | assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) |
| 436 | assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) | 444 | assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) |
| @@ -632,7 +640,7 @@ assert(maxint % -2 == -1) | |||
| 632 | 640 | ||
| 633 | -- non-portable tests because Windows C library cannot compute | 641 | -- non-portable tests because Windows C library cannot compute |
| 634 | -- fmod(1, huge) correctly | 642 | -- fmod(1, huge) correctly |
| 635 | if not _port then | 643 | if not _ENV._port then |
| 636 | local function anan (x) assert(isNaN(x)) end -- assert Not a Number | 644 | local function anan (x) assert(isNaN(x)) end -- assert Not a Number |
| 637 | anan(0.0 % 0) | 645 | anan(0.0 % 0) |
| 638 | anan(1.3 % 0) | 646 | anan(1.3 % 0) |
| @@ -779,6 +787,7 @@ assert(a == '10' and b == '20') | |||
| 779 | 787 | ||
| 780 | do | 788 | do |
| 781 | print("testing -0 and NaN") | 789 | print("testing -0 and NaN") |
| 790 | global rawset, undef | ||
| 782 | local mz <const> = -0.0 | 791 | local mz <const> = -0.0 |
| 783 | local z <const> = 0.0 | 792 | local z <const> = 0.0 |
| 784 | assert(mz == z) | 793 | assert(mz == z) |
| @@ -1074,6 +1083,7 @@ do | |||
| 1074 | -- different numbers should print differently. | 1083 | -- different numbers should print differently. |
| 1075 | -- check pairs of floats with minimum detectable difference | 1084 | -- check pairs of floats with minimum detectable difference |
| 1076 | local p = floatbits - 1 | 1085 | local p = floatbits - 1 |
| 1086 | global ipairs | ||
| 1077 | for i = 1, maxexp - 1 do | 1087 | for i = 1, maxexp - 1 do |
| 1078 | for _, i in ipairs{-i, i} do | 1088 | for _, i in ipairs{-i, i} do |
| 1079 | local x = 2^i | 1089 | local x = 2^i |
