Skip to content

Instantly share code, notes, and snippets.

@silentbicycle
Created July 25, 2012 15:11
Show Gist options
  • Save silentbicycle/3176683 to your computer and use it in GitHub Desktop.
Save silentbicycle/3176683 to your computer and use it in GitHub Desktop.
property tests
function test_encoding_and_decoding_should_be_lossless()
assert_random({ name="encode_decode",
always={ -- regressions: always test these seeds
-- found test_encoding_N_zeroes's edge case
3735485075354, 2721442120304, 3956609256407,
-- found test_encoding_two_zeroes's edge case
175063139413, 3771894109384, 1972533966070},
},
function ()
local ints = gen_int_array()
local packed = rle.encode(ints)
local unpacked = rle.decode(packed)
local max = #ints > #unpacked and #ints or #unpacked
for i=1,#ints do
assert_true(unpacked[i] == ints[i])
end
end)
end
function test_encoded_data_should_usually_decrease_in_size_or_stay_the_same()
assert_random({ name="decrease" },
function ()
local ints = gen_int_array()
local packed = rle.encode(ints)
assert_true(#packed <= #ints)
end)
end
function test_encoding_multiple_times_quickly_reach_a_fixpoint()
assert_random({ name="diminshing_returns" },
function ()
local prev = gen_int_array()
for count=1,5 do
local cur = rle.encode(prev)
if #cur >= #prev then return end
prev = cur
end
fail("still shrinking after 5 cycles")
end)
end
function test_encoded_values_should_not_have_long_runs_of_duplicate_values()
assert_random({ name="decrease" },
function ()
local ints = gen_int_array()
local packed = rle.encode(ints)
local longest, cur_run, last = 0, 0, nil
for i=1,#packed do
if last and last == packed[i] then
cur_run = cur_run + 1
else
if cur_run > longest then
longest = cur_run
end
cur_run = 0
end
last = packed[i]
end
assert_true(longest < 3)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment