comparison resources/osc/node_modules/binpack/tests/test-binpack.coffee @ 271:fb9c28a4676b prerelease

Added osc example project and node script for testing
author Liam Donovan <l.b.donovan@qmul.ac.uk>
date Tue, 17 May 2016 16:01:06 +0100
parents
children
comparison
equal deleted inserted replaced
270:de37582ce6f3 271:fb9c28a4676b
1 vows = require "vows"
2 assert = require "assert"
3 binpack = require "../index"
4
5 # do a round trip
6 okayForOptions = (num, options) ->
7 return false if options.size? and Math.abs(num) > options.size?
8 return false if num < 0 and options.unsigned
9 true
10
11 roundTrip = (type, options) ->
12 works : (num) ->
13 return null if not okayForOptions(num, options)
14 assert.strictEqual (binpack["unpack" + type] binpack["pack" + type] num), num
15
16 "fails plus 1.1" : (num) ->
17 return null if not okayForOptions(num, options)
18 assert.notStrictEqual (binpack["unpack" + type] binpack["pack" + type] num + 1.1), num
19
20 "works little endian" : (num) ->
21 return null if options.onebyte
22 return null if not okayForOptions(num, options)
23 assert.strictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "little"), "little"), num
24
25 "works big endian" : (num) ->
26 return null if options.onebyte
27 return null if not okayForOptions(num, options)
28 assert.strictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "big"), "big"), num
29
30 "fails mismatched" : (num) ->
31 return null if not okayForOptions(num, options)
32 return null if num is 0
33 return null if options.onebyte
34 assert.notStrictEqual (binpack["unpack" + type] (binpack["pack" + type] num, "little"), "big"), num
35
36 types =
37 "Float32" : {}
38 "Float64" : {}
39 "Int8" : {onebyte : true, size : 128}
40 "Int16" : {size : 32768}
41 "Int32" : {}
42 "Int64" : {}
43 "UInt8" : {unsigned : true, onebyte : true, size:255}
44 "UInt16" : {unsigned : true, size : 65535}
45 "UInt32" : {unsigned : true}
46 "UInt64" : {unsigned : true}
47
48 # round trip testing makes up the core of the test.
49 roundTripTests = (num) ->
50 tests = {topic : num}
51 for type, options of types
52 tests[type + "round trip test"] = roundTrip type, options
53 tests
54
55 vows.describe("binpack").addBatch(
56 # choose a bunch of random numbers
57 'roundTrips for 0' : roundTripTests 0
58 'roundTrips for 12' : roundTripTests 12
59 'roundTrips for -18' : roundTripTests -18
60 'roundTrips for 129' : roundTripTests 129
61 'roundTrips for -400' : roundTripTests -400
62 'roundTrips for 60000' : roundTripTests 60000
63 'roundTrips for 1234567' : roundTripTests 1234567
64 ).export module