1 | #!/usr/bin/env mocha -R spec
|
2 |
|
3 | var assert = require("assert");
|
4 |
|
5 | var msgpackJS = "../index";
|
6 | var isBrowser = ("undefined" !== typeof window);
|
7 | var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
8 | var TITLE = __filename.replace(/^.*\//, "") + ":";
|
9 |
|
10 | describe(TITLE, function() {
|
11 |
|
12 |
|
13 | it("00-7f: positive fixint", function() {
|
14 | assert.deepEqual(toArray(msgpack.encode(0x00)), [0x00]);
|
15 | assert.deepEqual(toArray(msgpack.encode(0x7F)), [0x7F]);
|
16 | });
|
17 |
|
18 |
|
19 | it("80-8f: fixmap", function() {
|
20 | assert.deepEqual(toArray(msgpack.encode({})), [0x80]);
|
21 |
|
22 | var map = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15};
|
23 | var array = [0x8F];
|
24 | Object.keys(map).forEach(function(key) {
|
25 | array.push(0xa1, key.charCodeAt(0), map[key]);
|
26 | });
|
27 | assert.deepEqual(toArray(msgpack.encode(map)), array);
|
28 | });
|
29 |
|
30 |
|
31 | it("90-9f: fixarray", function() {
|
32 | assert.deepEqual(toArray(msgpack.encode([])), [0x90]);
|
33 |
|
34 | var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
35 | assert.deepEqual(toArray(msgpack.encode(array)), concat([0x9F], array));
|
36 | });
|
37 |
|
38 |
|
39 | it("a0-bf: fixstr", function() {
|
40 | assert.deepEqual(toArray(msgpack.encode("")), [0xa0]);
|
41 |
|
42 | var str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
43 | assert.deepEqual(toArray(msgpack.encode(str)), concat([0xBF], Buffer(str)));
|
44 | });
|
45 |
|
46 |
|
47 | it("c0: nil", function() {
|
48 | assert.deepEqual(toArray(msgpack.encode(null)), [0xc0]);
|
49 | });
|
50 |
|
51 |
|
52 |
|
53 | it("c2-c3: boolean", function() {
|
54 | assert.deepEqual(toArray(msgpack.encode(false)), [0xc2]);
|
55 | assert.deepEqual(toArray(msgpack.encode(true)), [0xc3]);
|
56 | });
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | it("c4-c6: bin 8/16/32", function() {
|
62 | var bin;
|
63 | bin = Buffer(1);
|
64 | bin.fill(0);
|
65 | assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc4, 1], bin));
|
66 |
|
67 | bin = Buffer(256);
|
68 | bin.fill(0);
|
69 | assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc5, 1, 0], bin));
|
70 |
|
71 | bin = Buffer(65536);
|
72 | bin.fill(0);
|
73 | assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc6, 0, 1, 0, 0], bin));
|
74 | });
|
75 |
|
76 |
|
77 |
|
78 | it("ca-cb: float 32/64", function() {
|
79 | assert.deepEqual(toArray(msgpack.encode(0.5)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
|
80 | });
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | it("cc-cf: uint 8/16/32/64", function() {
|
87 | assert.deepEqual(toArray(msgpack.encode(0xFF)), [0xcc, 0xFF]);
|
88 | assert.deepEqual(toArray(msgpack.encode(0xFFFF)), [0xcd, 0xFF, 0xFF]);
|
89 | assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
|
90 | });
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | it("d0-d3: int 8/16/32/64", function() {
|
97 | assert.deepEqual(toArray(msgpack.encode(-0x80)), [0xd0, 0x80]);
|
98 | assert.deepEqual(toArray(msgpack.encode(-0x8000)), [0xd1, 0x80, 0x00]);
|
99 | assert.deepEqual(toArray(msgpack.encode(-0x80000000)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
|
100 | });
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | it("d9-db: str 8/16/32", function() {
|
106 | this.timeout(10000);
|
107 | var str, src = "a";
|
108 | for (var i = 0; i < 17; i++) src += src;
|
109 |
|
110 | str = src.substr(0, 0xFF);
|
111 | assert.deepEqual(toArray(msgpack.encode(str)), concat([0xd9, 0xFF], Buffer(str)));
|
112 |
|
113 | str = src.substr(0, 0x0100);
|
114 | assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0x01, 0x00], Buffer(str)));
|
115 |
|
116 | str = src.substr(0, 0xFFFF);
|
117 | assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0xFF, 0xFF], Buffer(str)));
|
118 |
|
119 | str = src.substr(0, 0x010000);
|
120 | assert.deepEqual(toArray(msgpack.encode(str)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer(str)));
|
121 | });
|
122 |
|
123 |
|
124 | it("e0-ff: negative fixint", function() {
|
125 | assert.deepEqual(toArray(msgpack.encode(-1)), [0xFF]);
|
126 | assert.deepEqual(toArray(msgpack.encode(-32)), [0xE0]);
|
127 | });
|
128 | });
|
129 |
|
130 | function toArray(buffer) {
|
131 | return Array.prototype.slice.call(buffer);
|
132 | }
|
133 |
|
134 | function concat(buf) {
|
135 | return Array.prototype.concat.apply([], Array.prototype.map.call(arguments, toArray));
|
136 | } |
\ | No newline at end of file |