1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
require "bloom_filter"
assert(bloom_filter.version() == "1.0.0", bloom_filter.version())
local errors = {
function() local bf = bloom_filter.new(2) end, -- new() incorrect # args
function() local bf = bloom_filter.new(nil, 0.01) end, -- new() non numeric item
function() local bf = bloom_filter.new(0, 0.01) end, -- invalid items
function() local bf = bloom_filter.new(2, nil) end, -- nil probability
function() local bf = bloom_filter.new(2, 0) end, -- invalid probability
function() local bf = bloom_filter.new(2, 1) end, -- invalid probability
function()
local bf = bloom_filter.new(20, 0.01)
bf:add() --incorrect # args
end,
function()
local bf = bloom_filter.new(20, 0.01)
bf:add({}) --incorrect argument type
end,
function()
local bf = bloom_filter.new(20, 0.01)
bf:query() --incorrect # args
end,
function()
local bf = bloom_filter.new(20, 0.01)
bf:query({}) --incorrect argument type
end,
function()
local bf = bloom_filter.new(20, 0.01)
bf:clear(1) --incorrect # args
end,
}
for i, v in ipairs(errors) do
local ok = pcall(v)
if ok then error(string.format("error test %d failed\n", i)) end
end
bf = bloom_filter.new(1000, 0.01)
local test_items = 950
-- test numbers
assert(bf:count() == 0, "bloom filter should be empty")
assert(not bf:query(1), "bloom filter should be empty")
for i=1, test_items do
assert(bf:add(i), "insert failed")
end
for i=1, test_items do
assert(bf:query(i), "query failed")
end
assert(bf:count() == test_items, "count=" .. bf:count())
bf:clear()
assert(bf:count() == 0, "bloom filter should be empty")
assert(not bf:query(1), "bloom filter should be empty")
-- test strings
for i=1, test_items do
assert(bf:add(tostring(i)), "insert failed")
end
for i=1, test_items do
assert(bf:query(tostring(i)), "query failed")
end
assert(bf:count() == test_items, "count=" .. bf:count())
bf:clear()
assert(bf:count() == 0, "bloom filter should be empty")
assert(not bf:query("1"), "bloom filter should be empty")
|