diff --git a/src/base/table.lua b/src/base/table.lua index 03d7fe68..d534b5dc 100644 --- a/src/base/table.lua +++ b/src/base/table.lua @@ -270,7 +270,7 @@ local minindex = 1 local maxindex = #tbl + 1 while minindex < maxindex do - local index = minindex + bit32.arshift(maxindex - minindex, 1) + local index = minindex + math.floor((maxindex - minindex) / 2) local test = tbl[index] if fn(value, test) then maxindex = index @@ -427,7 +427,7 @@ return formatting .. '(nil)' elseif type(v) == "table" then if recurse and recurse > 0 then - return formatting .. '\n' .. table.tostring(v, recurse-1, i+1) + return formatting .. '\n' .. table.tostring(v, recurse-1, i+1) else return formatting .. "" end diff --git a/tests/base/test_table.lua b/tests/base/test_table.lua index 01e5b5e5..801e5ad5 100644 --- a/tests/base/test_table.lua +++ b/tests/base/test_table.lua @@ -74,3 +74,24 @@ function suite.isempty_ReturnsFalseOnNotEmptyMapWithFalseKey() test.isfalse(table.isempty({ [false] = 0 })) end + + +-- +-- table.insertsorted() tests +-- + + function suite.insertsorted() + local t = {} + table.insertsorted(t, 5) + table.insertsorted(t, 2) + table.insertsorted(t, 8) + table.insertsorted(t, 4) + table.insertsorted(t, 1) + + test.istrue(#t == 5) + test.istrue(t[1] == 1) + test.istrue(t[2] == 2) + test.istrue(t[3] == 4) + test.istrue(t[4] == 5) + test.istrue(t[5] == 8) + end