Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
"""\
looks for non-utf8, windows-style encodings in filenames and fixes them.
"""
__version__ = "0.0"
import os, sys
@thejoshwolfe
thejoshwolfe / diff_json.py
Created January 25, 2013 05:08
json object diff
#!/usr/bin/env python
import sys
import json
def diff_objects(left, right):
if not (type(left) == type(right) == dict):
return right
result = {}
keys = set()
...................................................................................................#
...............................................................................................#####
..........................................................................................##########
.....................................................................................###############
................................................................................####################
..........................................................................##########################
......................................................................##############################
.................................................................###################################
...........................................................#########################################
.......................................................#############################################
pub struct AllocatorVtable {
allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
freeFn: fn (self: &Allocator, mem: []u8),
}
pub struct Allocator {
vtable: &AllocatorVtable,
}
@thejoshwolfe
thejoshwolfe / a.js
Created April 17, 2017 19:59
testing if abandoning a zlib inflate stream causes a memory leak
var zlib = require("zlib");
var Transform = require("stream").Transform;
var BufferList = require("bl");
function createDeflatedBuffer(size, cb) {
var inputBuffer = new Buffer(size);
for (var i = 0; i < size; i++) {
inputBuffer[i] = Math.floor(Math.random() * 0x100);
}
var deflater = zlib.createDeflateRaw();
@thejoshwolfe
thejoshwolfe / harness_verifyParameter.js
Created August 23, 2017 21:30
Idea for generic parameter type coercion checking
function verifyParameterToIndex(testCases) {
// TODO: Something similar to verifyParameterToInteger, possibly begin by calling it first.
// TODO: Then also test that outside [0, 2**53-1] throws a RangeError.
}
function verifyParameterToInteger(testCases) {
function MyError() {}
var simpleValues = [
[
@thejoshwolfe
thejoshwolfe / main.c
Created August 25, 2017 23:28
given 3 ints, return the median
int median3(int a, int b, int c) {
int index =
((a < b) << 0) |
((b < c) << 1) |
((c < a) << 2);
switch (index) {
case 0: unreachable();
case 1: return c;
case 2: return a;
case 3: return b;
function assertSameValue(result, expected, resultStr) {
if (result === expected) return;
$ERROR("Expected (" + resultStr + ") to be " + expected + " but it was " + result);
}
function compare(a, aStr, b, bStr, cmp) {
assertSameValue(a < b, cmp < 0, aStr + " < " + bStr);
assertSameValue(a <= b, cmp <= 0, aStr + " <= " + bStr);
assertSameValue(a > b, cmp > 0, aStr + " > " + bStr);
assertSameValue(a >= b, cmp >= 0, aStr + " >= " + bStr);
function generateCases(a, b, cmp) {
function generateCase(a, op, b, result) {
print("assert.sameValue(" + a + " " + op + " " + b + ", " + result + ");");
}
generateCase(a, "<", b, cmp < 0);
generateCase(a, "<=", b, cmp <= 0);
generateCase(a, ">", b, cmp > 0);
generateCase(a, ">=", b, cmp >= 0);
generateCase(b, "<", a, 0 < cmp);
generateCase(b, "<=", a, 0 <= cmp);
op_list = [
("<", lambda a, b: a < b, "less-than"),
("<=", lambda a, b: a <= b, "less-than-or-equal"),
(">", lambda a, b: a > b, "greater-than"),
(">=", lambda a, b: a >= b, "greater-than-or-equal"),
]
# This is the hex representation of Number.MAX_VALUE.
max_value_digits = "0xfffffffffffff8" + "0" * 242