Skip to content

Commit 4af1925

Browse files
Add upper constraint to quicksort (#332)
1 parent fc5a670 commit 4af1925

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

metrics/quantile.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ local sample_constructor = ffi.typeof('sample')
1313

1414
local function quicksort(array, low, high)
1515
assert(low >= 0, 'Low bound must be non-negative')
16+
assert(high < ffi.sizeof(array) / ffi.sizeof('double'),
17+
'Upper bound must be lower than array size')
1618
if high - low < 1 then
1719
return array
1820
end

test/quantile_test.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ g.test_low_bound_negative = function()
109109
)
110110
end
111111

112+
g.test_high_bound_higher_array_size = function()
113+
local empty = ffi.new('double[?]', 2)
114+
t.assert_error_msg_contains(
115+
'Upper bound must be lower than array size',
116+
quantile.quicksort,
117+
empty,
118+
1,
119+
10
120+
)
121+
end
122+
112123
g.test_not_sorted = function()
113124
local array = ffi.new('double[?]', 2)
114125
array[0] = math.huge

0 commit comments

Comments
 (0)