From f8c4c4fcf2b2fed00b3c5b71c19cd64e539dee51 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Sep 2022 17:05:22 -0300 Subject: New test for table rehash --- testes/nextvar.lua | 38 +++++++++++++++++++++++++++++--------- 1 file 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 -- cgit v1.2.1