mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 15:23:48 +01:00
Fix binary search test
This commit is contained in:
parent
a5e4696cd1
commit
258d9d956d
6
test.lua
6
test.lua
@ -114,7 +114,7 @@ do
|
|||||||
end
|
end
|
||||||
return -#list-1
|
return -#list-1
|
||||||
end
|
end
|
||||||
|
|
||||||
for k = 0, 100 do
|
for k = 0, 100 do
|
||||||
local sorted = {}
|
local sorted = {}
|
||||||
for i = 1, k do
|
for i = 1, k do
|
||||||
@ -123,7 +123,9 @@ do
|
|||||||
_G.table.sort(sorted)
|
_G.table.sort(sorted)
|
||||||
for i = 1, 10 do
|
for i = 1, 10 do
|
||||||
local pick = _G.math.random(-100, 1100)
|
local pick = _G.math.random(-100, 1100)
|
||||||
assert(linear_search(sorted, pick) == table.binary_search(sorted, pick))
|
local linear, binary = linear_search(sorted, pick), table.binary_search(sorted, pick)
|
||||||
|
-- If numbers appear twice (or more often), the indices may differ, as long as the number is the same.
|
||||||
|
assert(linear == binary or (linear > 0 and sorted[linear] == sorted[binary]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user