UNPKG

5.89 kBJavaScriptView Raw
1#!/usr/bin/env mocha -R spec
2
3var assert = require("assert");
4var msgpackJS = "../index";
5var isBrowser = ("undefined" !== typeof window);
6var msgpack = isBrowser && window.msgpack || require(msgpackJS);
7var TITLE = __filename.replace(/^.*\//, "");
8
9describe(TITLE, function() {
10
11 // positive fixint -- 0x00 - 0x7f
12 it("00-7f: positive fixint", function() {
13 for (var i = 0; i <= 0x7F; i++) {
14 assert.deepEqual(toArray(msgpack.encode(i)), [i]);
15 }
16 });
17
18 // fixmap -- 0x80 - 0x8f
19 it("80-8f: fixmap", function() {
20 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, p: 16};
21 var src = {};
22 var exp = [0x80];
23 Object.keys(map).forEach(function(key) {
24 assert.deepEqual(toArray(msgpack.encode(src)), exp);
25 src[key] = map[key];
26 exp[0]++;
27 exp.push(0xa1);
28 exp.push(key.charCodeAt(0));
29 exp.push(map[key]);
30 });
31 });
32
33 // fixarray -- 0x90 - 0x9f
34 it("90-9f: fixarray", function() {
35 var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
36 var src = [];
37 var exp = [0x90];
38 for (var i = 0; i < 16; i++) {
39 assert.deepEqual(toArray(msgpack.encode(src)), exp);
40 src.push(array[i]);
41 exp[0]++;
42 exp.push(array[i]);
43 }
44 });
45
46 // fixstr -- 0xa0 - 0xbf
47 it("a0-bf: fixstr", function() {
48 assert.deepEqual(toArray(msgpack.encode("")), [0xa0]);
49
50 var str = "0123456789abcdefghijklmnopqrstu";
51 var exp = [0xa0];
52 for (var i = 0; i < 32; i++) {
53 var src = str.substr(0, i);
54 assert.deepEqual(toArray(msgpack.encode(src)), exp);
55 exp[0]++;
56 exp.push(str.charCodeAt(i));
57 }
58 });
59
60 // nil -- 0xc0
61 it("c0: nil (null)", function() {
62 assert.deepEqual(toArray(msgpack.encode(null)), [0xc0]);
63 });
64 it("c0: nil (undefined)", function() {
65 assert.deepEqual(toArray(msgpack.encode(undefined)), [0xc0]);
66 });
67 it("c0: nil (Function)", function() {
68 assert.deepEqual(toArray(msgpack.encode(NOP)), [0xc0]);
69 });
70
71 // false -- 0xc2
72 // true -- 0xc3
73 it("c2-c3: boolean", function() {
74 assert.deepEqual(toArray(msgpack.encode(false)), [0xc2]);
75 assert.deepEqual(toArray(msgpack.encode(true)), [0xc3]);
76 });
77
78 // bin 8 -- 0xc4
79 // bin 16 -- 0xc5
80 // bin 32 -- 0xc6
81 it("c4-c6: bin 8/16/32", function() {
82 this.timeout(30000);
83 var bin;
84 bin = Buffer(1);
85 bin.fill(0);
86 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc4, 1], bin));
87
88 bin = Buffer(256);
89 bin.fill(0);
90 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc5, 1, 0], bin));
91
92 bin = Buffer(65536);
93 bin.fill(0);
94 assert.deepEqual(toArray(msgpack.encode(bin)), concat([0xc6, 0, 1, 0, 0], bin));
95 });
96
97 // float 32 -- 0xca -- NOT SUPPORTED
98 // float 64 -- 0xcb
99 it("ca-cb: float 32/64", function() {
100 assert.deepEqual(toArray(msgpack.encode(0.5)), [0xcb, 63, 224, 0, 0, 0, 0, 0, 0]);
101 });
102
103 // uint 8 -- 0xcc
104 // uint 16 -- 0xcd
105 // uint 32 -- 0xce
106 // uint 64 -- 0xcf -- NOT SUPPORTED
107 it("cc-cf: uint 8/16/32/64", function() {
108 assert.deepEqual(toArray(msgpack.encode(0xFF)), [0xcc, 0xFF]);
109 assert.deepEqual(toArray(msgpack.encode(0xFFFF)), [0xcd, 0xFF, 0xFF]);
110 assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
111 });
112
113 // int 8 -- 0xd0
114 // int 16 -- 0xd1
115 // int 32 -- 0xd2
116 // int 64 -- 0xd3 -- NOT SUPPORTED
117 it("d0-d3: int 8/16/32/64", function() {
118 assert.deepEqual(toArray(msgpack.encode(-0x80)), [0xd0, 0x80]);
119 assert.deepEqual(toArray(msgpack.encode(-0x8000)), [0xd1, 0x80, 0x00]);
120 assert.deepEqual(toArray(msgpack.encode(-0x80000000)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
121 });
122
123 // str 8 -- 0xd9
124 // str 16 -- 0xda
125 // str 32 -- 0xdb
126 it("d9-db: str 8/16/32", function() {
127 this.timeout(30000);
128 var str, src = "a";
129 for (var i = 0; i < 17; i++) src += src;
130
131 str = src.substr(0, 0xFF);
132 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xd9, 0xFF], Buffer(str)));
133
134 str = src.substr(0, 0x0100);
135 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0x01, 0x00], Buffer(str)));
136
137 str = src.substr(0, 0xFFFF);
138 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xda, 0xFF, 0xFF], Buffer(str)));
139
140 str = src.substr(0, 0x010000);
141 assert.deepEqual(toArray(msgpack.encode(str)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer(str)));
142 });
143
144 // array 16 -- 0xdc
145 // array 32 -- 0xdd
146 it("dc-dd: array 16/32", function() {
147 this.timeout(30000);
148 var i, exp;
149 var src = new Array(256);
150 for (i = 0; i < 256; i++) src[i] = i & 0x7F;
151 exp = [0xdc, 0x01, 0x00].concat(src);
152 assert.deepEqual(toArray(msgpack.encode(src)), exp);
153
154 for (i = 0; i < 8; i++) src = src.concat(src);
155 exp = [0xdd, 0x00, 0x01, 0x00, 0x00].concat(src);
156 assert.deepEqual(toArray(msgpack.encode(src)), exp);
157 });
158
159 // map 16 -- 0xde
160 // map 32 -- 0xdf
161 it("de-df: map 16/32", function() {
162 this.timeout(30000);
163 var i, actual;
164 var map = {};
165 for (i = 0; i < 256; i++) map[i] = i;
166 actual = msgpack.encode(map);
167 // check only headers because order may vary
168 assert.equal(actual[0], 0xde);
169 assert.equal(actual[1], 1);
170 assert.equal(actual[2], 0);
171
172 for (i = 256; i < 65536; i++) map[i] = i;
173 actual = msgpack.encode(map);
174 assert.equal(actual[0], 0xdf);
175 assert.equal(actual[1], 0);
176 assert.equal(actual[2], 1);
177 assert.equal(actual[3], 0);
178 assert.equal(actual[4], 0);
179 });
180
181 // negative fixint -- 0xe0 - 0xff
182 it("e0-ff: negative fixint", function() {
183 for (var i = -32; i <= -1; i++) {
184 assert.deepEqual(toArray(msgpack.encode(i)), [i & 0xFF]);
185 }
186 });
187});
188
189function toArray(buffer) {
190 return Array.prototype.slice.call(buffer);
191}
192
193function concat(buf) {
194 return Array.prototype.concat.apply([], Array.prototype.map.call(arguments, toArray));
195}
196
197function NOP() {
198}
\No newline at end of file