From 2e8a26df5c475b3b6934c01a1b3f67ffedaaf4f0 Mon Sep 17 00:00:00 2001 From: Tom van Dijck Date: Thu, 9 Jun 2016 18:12:06 -0700 Subject: [PATCH] Fix usage of bit32.arshift added unit-test --- src/base/table.lua | 4 ++-- tests/base/test_table.lua | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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