diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-25 17:38:58 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-06-25 17:38:58 -0300 |
commit | 4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5 (patch) | |
tree | ddbbcea4caa8b8e8cfe1cbc578ac162033a2425d | |
parent | 05ba2880491fa1ecfe78d8a3542edcd6220e4022 (diff) | |
download | lua-4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5.tar.gz lua-4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5.tar.bz2 lua-4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5.zip |
A few more tests for table access in the API
Added tests where the table being accessed is also the index or
value in the operation.
-rw-r--r-- | ltests.c | 16 | ||||
-rw-r--r-- | testes/api.lua | 48 |
2 files changed, 63 insertions, 1 deletions
@@ -1488,6 +1488,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1488 | else if EQ("pushfstringP") { | 1488 | else if EQ("pushfstringP") { |
1489 | lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); | 1489 | lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); |
1490 | } | 1490 | } |
1491 | else if EQ("rawget") { | ||
1492 | int t = getindex; | ||
1493 | lua_rawget(L1, t); | ||
1494 | } | ||
1491 | else if EQ("rawgeti") { | 1495 | else if EQ("rawgeti") { |
1492 | int t = getindex; | 1496 | int t = getindex; |
1493 | lua_rawgeti(L1, t, getnum); | 1497 | lua_rawgeti(L1, t, getnum); |
@@ -1496,6 +1500,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1496 | int t = getindex; | 1500 | int t = getindex; |
1497 | lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); | 1501 | lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); |
1498 | } | 1502 | } |
1503 | else if EQ("rawset") { | ||
1504 | int t = getindex; | ||
1505 | lua_rawset(L1, t); | ||
1506 | } | ||
1507 | else if EQ("rawseti") { | ||
1508 | int t = getindex; | ||
1509 | lua_rawseti(L1, t, getnum); | ||
1510 | } | ||
1499 | else if EQ("rawsetp") { | 1511 | else if EQ("rawsetp") { |
1500 | int t = getindex; | 1512 | int t = getindex; |
1501 | lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); | 1513 | lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); |
@@ -1538,6 +1550,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1538 | const char *s = getstring; | 1550 | const char *s = getstring; |
1539 | lua_setfield(L1, t, s); | 1551 | lua_setfield(L1, t, s); |
1540 | } | 1552 | } |
1553 | else if EQ("seti") { | ||
1554 | int t = getindex; | ||
1555 | lua_seti(L1, t, getnum); | ||
1556 | } | ||
1541 | else if EQ("setglobal") { | 1557 | else if EQ("setglobal") { |
1542 | const char *s = getstring; | 1558 | const char *s = getstring; |
1543 | lua_setglobal(L1, s); | 1559 | lua_setglobal(L1, s); |
diff --git a/testes/api.lua b/testes/api.lua index bcc04dac..8f4e89ac 100644 --- a/testes/api.lua +++ b/testes/api.lua | |||
@@ -498,7 +498,53 @@ do -- getp/setp | |||
498 | local a = {} | 498 | local a = {} |
499 | T.testC("rawsetp 2 1", a, 20) | 499 | T.testC("rawsetp 2 1", a, 20) |
500 | assert(a[T.pushuserdata(1)] == 20) | 500 | assert(a[T.pushuserdata(1)] == 20) |
501 | assert(T.testC("rawgetp 2 1; return 1", a) == 20) | 501 | assert(T.testC("rawgetp -1 1; return 1", a) == 20) |
502 | end | ||
503 | |||
504 | |||
505 | do -- using the table itself as index | ||
506 | local a = {} | ||
507 | a[a] = 10 | ||
508 | local prog = "gettable -1; return *" | ||
509 | local res = {T.testC(prog, a)} | ||
510 | assert(#res == 2 and res[1] == prog and res[2] == 10) | ||
511 | |||
512 | local prog = "settable -2; return *" | ||
513 | local res = {T.testC(prog, a, 20)} | ||
514 | assert(a[a] == 20) | ||
515 | assert(#res == 1 and res[1] == prog) | ||
516 | |||
517 | -- raw | ||
518 | a[a] = 10 | ||
519 | local prog = "rawget -1; return *" | ||
520 | local res = {T.testC(prog, a)} | ||
521 | assert(#res == 2 and res[1] == prog and res[2] == 10) | ||
522 | |||
523 | local prog = "rawset -2; return *" | ||
524 | local res = {T.testC(prog, a, 20)} | ||
525 | assert(a[a] == 20) | ||
526 | assert(#res == 1 and res[1] == prog) | ||
527 | |||
528 | -- using the table as the value to set | ||
529 | local prog = "rawset -1; return *" | ||
530 | local res = {T.testC(prog, 30, a)} | ||
531 | assert(a[30] == a) | ||
532 | assert(#res == 1 and res[1] == prog) | ||
533 | |||
534 | local prog = "settable -1; return *" | ||
535 | local res = {T.testC(prog, 40, a)} | ||
536 | assert(a[40] == a) | ||
537 | assert(#res == 1 and res[1] == prog) | ||
538 | |||
539 | local prog = "rawseti -1 100; return *" | ||
540 | local res = {T.testC(prog, a)} | ||
541 | assert(a[100] == a) | ||
542 | assert(#res == 1 and res[1] == prog) | ||
543 | |||
544 | local prog = "seti -1 200; return *" | ||
545 | local res = {T.testC(prog, a)} | ||
546 | assert(a[200] == a) | ||
547 | assert(#res == 1 and res[1] == prog) | ||
502 | end | 548 | end |
503 | 549 | ||
504 | a = {x=0, y=12} | 550 | a = {x=0, y=12} |