Skip to content

Instantly share code, notes, and snippets.

View silentbicycle's full-sized avatar

Scott Vokes silentbicycle

  • Fastly (@fastly)
  • Grand Rapids, MI
View GitHub Profile
@silentbicycle
silentbicycle / gist:2251890
Created March 30, 2012 14:26
fixed-width macro lines
/* macro.h */
#define GREATEST_ASSERT_EQm(MSG, EXP, GOT) \
do { \
greatest_info.msg = MSG; \
greatest_info.fail_file = __FILE__; \
greatest_info.fail_line = __LINE__; \
if ((EXP) != (GOT)) { \
GREATEST_CALL_TEARDOWN(); \
return -1; \
} \
@silentbicycle
silentbicycle / gist:2251910
Created March 30, 2012 14:28
macro capture example
#define CONTRIVED_IF_EXAMPLE(F, COUNTER) F(COUNTER); COUNTER++;
if (some_condition) CONTRIVED_IF_EXAMPLE(func, x);
/* This will expand to: */
if (some_condition) func(x); x++;
/* Note that the `x++;` statement will always evaluate - it is outside the `if`. Defining the macro with braces will fix the issue. */
#define FIXED_IF_EXAMPLE(F, COUNTER) { F(COUNTER); COUNTER++; }
/* Similarly, compare */
#define CONTRIVED_EXAMPLE(X, Y) X + Y
@silentbicycle
silentbicycle / gist:3176672
Created July 25, 2012 15:09
tests for known input
function test_basic_encoding_for_known_case()
local ints = {1, 0, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4}
local res = rle.encode(ints)
local expected = {1, 0, 0, 0, 6, 2, 3, 3, 3, 4}
for i=1,#expected do
assert_equal(expected[i], res[i])
end
end
@silentbicycle
silentbicycle / gist:3176678
Created July 25, 2012 15:10
Generate appropriate input for testing run-length encoding
-- Generate a random array of bytes, with some redundancy (via sorting).
function gen_int_array(limit)
limit = limit or 10000
local ri = lunatest.random_int
local length = ri(1, limit)
local ints = {}
for i=1,length do
-- keep them small, to increase duplication
ints[i] = ri(0, 2^8 - 1)
end
@silentbicycle
silentbicycle / gist:3176683
Created July 25, 2012 15:11
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()
@silentbicycle
silentbicycle / gist:3176689
Created July 25, 2012 15:12
run-length encoding
-- Use literal zero for escape before compression data
local esc = 0
-- Run-length-encode an array of ints.
function encode(ints)
local res, i, ct = {}, 1, 1 -- result, index, count
while i <= #ints do
local v = ints[i]
while ints[i+1] == v do
ct = ct + 1
@silentbicycle
silentbicycle / gist:3176702
Created July 25, 2012 15:14
tests for 0-escaping
function test_encoding_two_zeroes()
-- this should be {0, 2, 0}, not {0, 0, 0, 0}, to keep
-- encoding from filling up with too many escapes.
local ints = {0, 0}
local res = rle.encode(ints)
local expected = {0, 2, 0}
for i=1,#expected do
assert_equal(expected[i], res[i])
end
@silentbicycle
silentbicycle / gist:3889656
Created October 14, 2012 20:06
overlapping FIXED mmap clobbering
#include <stdio.h>
#include <sys/mman.h>
#include <err.h>
int main(int argc, char **argv) {
char *slab = NULL, *after = NULL, *overlap = NULL;
int sz = 4096;
#define CHECK(P) if (P == NULL) err(1, #P);
slab = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON,-1,0);
CHECK(slab);
@silentbicycle
silentbicycle / gist:4056292
Created November 11, 2012 21:13
more ruby parser brittleness
def this_is_okay
{
:a => "b"
}
end
def this_is_too
yield ({
:a => "b"
})
@silentbicycle
silentbicycle / rapbot_say
Created February 25, 2013 16:55
glue rapbot to say(1)
#!/bin/sh
VOICE="-v fred"
#VOICE="-v ralph"
#VOICE="-v lee"
curl http://rapbot.jit.su/ 2>/dev/null | awk '
{
for (i=1; i<NF; i++) {
if (match($i, "twitter.com/share")) {