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 / array.lua
Created May 12, 2013 16:11
quick draft of an array/vector library for lua (mostly as an example of metatables)
local NONE = setmetatable({}, {['__tostring'] = function() return "NONE" end})
local Array = {
['__newindex'] = function(a, k, v)
if type(k) ~= "number" then error("bad index") end
if k < 1 then error("out of bounds") end
if a._lim ~= NONE and k <= a._lim then
a._v[k] = v
elseif a._lim == NONE then
if k > a._len then
@silentbicycle
silentbicycle / Yes.
Created May 7, 2013 17:34
The only limit is yourself.
$ cat ~/tmp/welcome
__ __ _ _
\ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___
\ \ /\ / / _ \ |/ __/ _ \| '_ ' _ \ / _ \ | __/ _ \
\ V V / __/ | (_| (_) | | | | | | __/ | || (_) |
\_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/
_____ _ ____
|__ /___ _ __ ___ | |__ ___ / ___|___ _ __ ___
/ // _ \| '_ ' _ \| '_ \ / _ \| | / _ \| '_ ' _ \
@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")) {
@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 / 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: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: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: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: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: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