Skip to content

Instantly share code, notes, and snippets.

@hdon
Created May 27, 2009 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdon/118801 to your computer and use it in GitHub Desktop.
Save hdon/118801 to your computer and use it in GitHub Desktop.
#! /usr/bin/gsr -zzdd
const ByteString = require("binary").ByteString;
const Binary = require("binary").Binary;
/* Provide some scaffolding for individual tests */
var testUtils = {
/* eq compares two or more values and expects that they will pass an equality test without type coercion (===) */
'eq': function() {
if (arguments.length < 2) throw 'too few arguments to testUtils.eq()';
for(var i=1, l=arguments.length; i<l; i++)
if (arguments[0] !== arguments[i]) {
print('VALUE MISMATCH');
print('V1', arguments[0]);
print('V2', arguments[i]);
return false;
}
return true;
},
/* 'startswith' string function for gpsee exception handling */
'sw': function(part) {
return function(full) {
if (full.indexOf(part) == 0) return true;
print('EXCEPTION MISMATCH');
print('V1', full);
print('V2', part);
return false;
}
}
};
/* A little scaffolding for dispatching individual tests */
function runtest(test) {
/* The test can set testUtils.ex to an expected exception, if you're into that sort of thing */
delete testUtils.ex;
try { return test(testUtils) }
catch (ex) {
/* Is an exception expected? */
if (testUtils.ex) {
/* Is it to be tested against a function? */
if ('function' == typeof testUtils.ex)
return testUtils.ex(ex);
/* Otherwise just compare */
return testUtils.ex == ex;
}
/* This will all end in tears */
print('Exception thrown:', ex);
}
}
/* Test material */
var gobbledygook = '2~`E 6_BV JGrK 4*I<f 0^B,g Ctks ;~Sn dA:Z )#h/qp4 I3p^C';
var EX_OVERFLOW = 'gpsee.module.ca.page.binary.ByteString.substr.range.overflow';
var tests = [
/* Decode/extract tests */
function(){return new ByteString("hello world").decodeToString("utf-8") === 'hello world'},
function(){return new ByteString("hello").toByteString().decodeToString("ascii") === 'hello'},
function(){return new ByteString("hello")[0].decodeToString("ascii") == 'h'},
function(){return new ByteString("hello").charAt(1).decodeToString("US-ASCII") === 'e'},
function(){return new ByteString("hello").byteAt(4) === 111},
function(){return new ByteString("hello").length === 5},
function(){return new ByteString("hello", "utf-8").toByteString("utf-8", "utf-16").length === 12},
function(){return (new ByteString("hello") instanceof Binary) === true},
/* toString tests */
/* decodeToString tests */
/* indexOf tests */
/* lastIndexOf tests */
/* charAt tests */
/* byteAt tests */
/* charCodeAt tests */
/* split tests */
/* slice tests */
/* substr tests */
function(t) { return t.eq(new ByteString('1234').substr(0,1).decodeToString('US-ASCII'), '1') },
function(t) { return t.eq(new ByteString('1234').substr(1,1).decodeToString('US-ASCII'), '2') },
function(t) { return t.eq(new ByteString('1234').substr(2,2).decodeToString('US-ASCII'), '34') },
function(t) { t.ex = t.sw(EX_OVERFLOW); new ByteString('1234').substr(2,3).decodeToString('US-ASCII'); return false },
// /*CRASH reports out of memory!!!*/ function(t) { return t.eq(new ByteString('Institutionalized education is a test of obedience!').substring(17,14), ' education is ') },
/* substring tests */
function(t) { return t.eq(new ByteString('1234').substring(0,1).decodeToString('US-ASCII'), '1') },
function(t) { return t.eq(new ByteString('Institutionalized education is a test of obedience!').substring(17,31).decodeToString('US-ASCII'), ' education is ') },
/* toSource tests */
function(t) { return t.eq(new ByteString('Bush rigged the elections!').toSource(), '(new ByteString([66,117,115,104,32,114,105,103,103,101,100,32,116,104,101,32,101,108,101,99,116,105,111,110,115,33]))') },
];
/* Run all the tests! */
var passed = 0;
var failed = 0;
for each(var test in tests) {
if (runtest(test)) {
passed++;
} else {
failed++;
print("test failed:");
print(test);
}
}
/* Report test results */
print('passed '+passed+' tests');
print('failed '+failed+' tests');
if (!failed)
print('ALL TESTS PASSED');
else
print('FAIL');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment