summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-16 17:05:22 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-16 17:05:22 -0300
commitf8c4c4fcf2b2fed00b3c5b71c19cd64e539dee51 (patch)
treeeaa1409a4f92e90a1f971a57f68b94d72ac9d651
parent71bc69c2afaf49ab5f54f3443af9ae70dd1fed06 (diff)
downloadlua-github-f8c4c4fcf2b2fed00b3c5b71c19cd64e539dee51.tar.gz
New test for table rehash
-rw-r--r--testes/nextvar.lua38
1 files changed, 29 insertions, 9 deletions
diff --git a/testes/nextvar.lua b/testes/nextvar.lua
index 9e23e572..0874e5bb 100644
--- a/testes/nextvar.lua
+++ b/testes/nextvar.lua
@@ -9,6 +9,16 @@ local function checkerror (msg, f, ...)
end
+local function check (t, na, nh)
+ if not T then return end
+ local a, h = T.querytab(t)
+ if a ~= na or h ~= nh then
+ print(na, nh, a, h)
+ assert(nil)
+ end
+end
+
+
local a = {}
-- make sure table has lots of space in hash part
@@ -20,6 +30,25 @@ for i=1,100 do
assert(#a == i)
end
+
+do -- rehash moving elements from array to hash
+ local a = {}
+ for i = 1, 100 do a[i] = i end
+ check(a, 128, 0)
+
+ for i = 5, 95 do a[i] = nil end
+ check(a, 128, 0)
+
+ a.x = 1 -- force a re-hash
+ check(a, 4, 8)
+
+ for i = 1, 4 do assert(a[i] == i) end
+ for i = 5, 95 do assert(a[i] == nil) end
+ for i = 96, 100 do assert(a[i] == i) end
+ assert(a.x == 1)
+end
+
+
-- testing ipairs
local x = 0
for k,v in ipairs{10,20,30;x=12} do
@@ -65,15 +94,6 @@ local function mp2 (n) -- minimum power of 2 >= n
end
-local function check (t, na, nh)
- local a, h = T.querytab(t)
- if a ~= na or h ~= nh then
- print(na, nh, a, h)
- assert(nil)
- end
-end
-
-
-- testing C library sizes
do
local s = 0