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