l@271: vows = require "vows" l@271: assert = require "assert" l@271: binpack = require "../index" l@271: l@271: # do a round trip l@271: okayForOptions = (num, options) -> l@271: return false if options.size? and Math.abs(num) > options.size? l@271: return false if num < 0 and options.unsigned l@271: true l@271: l@271: roundTrip = (type, options) -> l@271: works : (num) -> l@271: return null if not okayForOptions(num, options) l@271: assert.strictEqual (binpack["unpack" + type] binpack["pack" + type] num), num l@271: l@271: "fails plus 1.1" : (num) -> l@271: return null if not okayForOptions(num, options) l@271: assert.notStrictEqual (binpack["unpack" + type] binpack["pack" + type] num + 1.1), num l@271: l@271: "works little endian" : (num) -> l@271: return null if options.onebyte l@271: return null if not okayForOptions(num, options) l@271: assert.strictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "little"), "little"), num l@271: l@271: "works big endian" : (num) -> l@271: return null if options.onebyte l@271: return null if not okayForOptions(num, options) l@271: assert.strictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "big"), "big"), num l@271: l@271: "fails mismatched" : (num) -> l@271: return null if not okayForOptions(num, options) l@271: return null if num is 0 l@271: return null if options.onebyte l@271: assert.notStrictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "little"), "big"), num l@271: l@271: types = l@271: "Float32" : {} l@271: "Float64" : {} l@271: "Int8" : {onebyte : true, size : 128} l@271: "Int16" : {size : 32768} l@271: "Int32" : {} l@271: "Int64" : {} l@271: "UInt8" : {unsigned : true, onebyte : true, size:255} l@271: "UInt16" : {unsigned : true, size : 65535} l@271: "UInt32" : {unsigned : true} l@271: "UInt64" : {unsigned : true} l@271: l@271: # round trip testing makes up the core of the test. l@271: roundTripTests = (num) -> l@271: tests = {topic : num} l@271: for type, options of types l@271: tests[type + "round trip test"] = roundTrip type, options l@271: tests l@271: l@271: vows.describe("binpack").addBatch( l@271: # choose a bunch of random numbers l@271: 'roundTrips for 0' : roundTripTests 0 l@271: 'roundTrips for 12' : roundTripTests 12 l@271: 'roundTrips for -18' : roundTripTests -18 l@271: 'roundTrips for 129' : roundTripTests 129 l@271: 'roundTrips for -400' : roundTripTests -400 l@271: 'roundTrips for 60000' : roundTripTests 60000 l@271: 'roundTrips for 1234567' : roundTripTests 1234567 l@271: ).export module