1 | #!/usr/bin/env mocha -R spec
|
2 |
|
3 | var assert = require("assert");
|
4 | var msgpackJS = "../index";
|
5 | var isBrowser = ("undefined" !== typeof window);
|
6 | var msgpack = isBrowser && window.msgpack || require(msgpackJS);
|
7 | var TITLE = __filename.replace(/^.*\//, "");
|
8 |
|
9 | var STRING_ASCII = "a";
|
10 | var STRING_GREEK = "α";
|
11 | var STRING_ASIAN = "亜";
|
12 |
|
13 |
|
14 | for (var i = 0; i < 17; i++) {
|
15 | STRING_ASCII = STRING_ASCII + STRING_ASCII;
|
16 | STRING_GREEK = STRING_GREEK + STRING_GREEK;
|
17 | STRING_ASIAN = STRING_ASIAN + STRING_ASIAN;
|
18 | }
|
19 |
|
20 | function pattern(min, max, offset) {
|
21 | var array = [];
|
22 | var check = {};
|
23 | var val = min - 1;
|
24 | while (val <= max) {
|
25 | if (min <= val && !check[val]) array.push(val);
|
26 | check[val++] = 1;
|
27 | if (val <= max && !check[val]) array.push(val);
|
28 | check[val++] = 1;
|
29 | if (val <= max && !check[val]) array.push(val);
|
30 | check[val--] = 1;
|
31 | val = val ? val * 2 - 1 : 1;
|
32 | }
|
33 | return array;
|
34 | }
|
35 |
|
36 | describe(TITLE, function() {
|
37 |
|
38 | it("null", function() {
|
39 | [null, undefined].forEach(function(value) {
|
40 | var encoded = msgpack.encode(value);
|
41 | var decoded = msgpack.decode(encoded);
|
42 | assert.equal(decoded, value);
|
43 | });
|
44 | });
|
45 |
|
46 | it("boolean", function() {
|
47 | [true, false].forEach(function(value) {
|
48 | var encoded = msgpack.encode(value);
|
49 | var decoded = msgpack.decode(encoded);
|
50 | assert.equal(decoded, value);
|
51 | });
|
52 | });
|
53 |
|
54 | it("positive int (small)", function() {
|
55 | pattern(0, 0x40000000).forEach(function(value) {
|
56 | value = value | 0;
|
57 | var encoded = msgpack.encode(value);
|
58 | var decoded = msgpack.decode(encoded);
|
59 | assert.equal(decoded, value);
|
60 | });
|
61 | });
|
62 |
|
63 | it("positive int (large)", function() {
|
64 | pattern(0x40000000, 0xFFFFFFFF).forEach(function(value) {
|
65 | var encoded = msgpack.encode(value);
|
66 | var decoded = msgpack.decode(encoded);
|
67 | assert.equal(decoded, value);
|
68 | });
|
69 | });
|
70 |
|
71 | it("negative int (small)", function() {
|
72 | pattern(0, 0x40000000).forEach(function(value) {
|
73 | value = -value | 0;
|
74 | var encoded = msgpack.encode(value);
|
75 | var decoded = msgpack.decode(encoded);
|
76 | assert.equal(decoded, value);
|
77 | });
|
78 | });
|
79 |
|
80 | it("negative int (large)", function() {
|
81 | pattern(0x40000000, 0xFFFFFFFF).forEach(function(value) {
|
82 | value = -value;
|
83 | var encoded = msgpack.encode(value);
|
84 | var decoded = msgpack.decode(encoded);
|
85 | assert.equal(decoded, value);
|
86 | });
|
87 | });
|
88 |
|
89 | it("float", function() {
|
90 | [1.1, 10.01, 100.001, 1000.0001, 10000.00001, 100000.000001, 1000000.0000001].forEach(function(value) {
|
91 | var encoded = msgpack.encode(value);
|
92 | var decoded = msgpack.decode(encoded);
|
93 | assert.equal(decoded, value);
|
94 | });
|
95 | });
|
96 |
|
97 | it("string (ASCII)", function() {
|
98 | this.timeout(30000);
|
99 | pattern(0, 65537).forEach(function(length) {
|
100 | var value = STRING_ASCII.substr(0, length);
|
101 | var encoded = msgpack.encode(value);
|
102 | var decoded = msgpack.decode(encoded);
|
103 | assert.equal(decoded, value);
|
104 | });
|
105 | });
|
106 |
|
107 | it("string (GREEK)", function() {
|
108 | this.timeout(30000);
|
109 | pattern(0, 65537).forEach(function(length) {
|
110 | var value = STRING_GREEK.substr(0, length);
|
111 | var encoded = msgpack.encode(value);
|
112 | var decoded = msgpack.decode(encoded);
|
113 | assert.equal(decoded, value);
|
114 | });
|
115 | });
|
116 |
|
117 | it("string (ASIAN)", function() {
|
118 | this.timeout(30000);
|
119 | pattern(0, 65537).forEach(function(length) {
|
120 | var value = STRING_ASIAN.substr(0, length);
|
121 | var encoded = msgpack.encode(value);
|
122 | var decoded = msgpack.decode(encoded);
|
123 | assert.equal(decoded, value);
|
124 | });
|
125 | });
|
126 |
|
127 | it("array (small)", function() {
|
128 | pattern(0, 257).forEach(function(length, idx) {
|
129 | var value = new Array(length);
|
130 | for (var i = 0; i < length; i++) {
|
131 | value[i] = String.fromCharCode(i);
|
132 | }
|
133 | assert.equal(value.length, length);
|
134 | var encoded = msgpack.encode(value);
|
135 | var decoded = msgpack.decode(encoded);
|
136 | assert.equal(decoded.length, length);
|
137 | assert.equal(decoded[0], value[0]);
|
138 | assert.equal(decoded[length - 1], value[length - 1]);
|
139 | });
|
140 | });
|
141 |
|
142 | it("array (large)", function() {
|
143 | this.timeout(30000);
|
144 | pattern(0, 65537).forEach(function(length) {
|
145 | var value = new Array(length);
|
146 | assert.equal(value.length, length);
|
147 | var encoded = msgpack.encode(value);
|
148 | var decoded = msgpack.decode(encoded);
|
149 | assert.equal(decoded.length, length);
|
150 | assert.equal(decoded[0], value[0]);
|
151 | assert.equal(decoded[length - 1], value[length - 1]);
|
152 | });
|
153 | });
|
154 |
|
155 | it("map (small)", function() {
|
156 | pattern(0, 257).forEach(function(length) {
|
157 | var value = {};
|
158 | for (var i = 0; i < length; i++) {
|
159 | var key = String.fromCharCode(i);
|
160 | value[key] = length;
|
161 | }
|
162 | assert.equal(Object.keys(value).length, length);
|
163 | var encoded = msgpack.encode(value);
|
164 | var decoded = msgpack.decode(encoded);
|
165 | assert.equal(Object.keys(decoded).length, length);
|
166 | assert.equal(decoded[0], value[0]);
|
167 | assert.equal(decoded[length - 1], value[length - 1]);
|
168 | });
|
169 | });
|
170 |
|
171 | it("map (large)", function() {
|
172 | this.timeout(30000);
|
173 | pattern(65536, 65537).forEach(function(length) {
|
174 | var value = {};
|
175 | for (var i = 0; i < length; i++) {
|
176 | value[i] = length;
|
177 | }
|
178 | assert.equal(Object.keys(value).length, length);
|
179 | var encoded = msgpack.encode(value);
|
180 | var decoded = msgpack.decode(encoded);
|
181 | assert.equal(Object.keys(decoded).length, length);
|
182 | assert.equal(decoded[0], value[0]);
|
183 | assert.equal(decoded[length - 1], value[length - 1]);
|
184 | });
|
185 | });
|
186 |
|
187 | it("buffer", function() {
|
188 | this.timeout(30000);
|
189 | pattern(2, 65537).forEach(function(length, idx) {
|
190 | var value = new Buffer(length);
|
191 | value.fill(idx);
|
192 | assert.equal(value.length, length);
|
193 | var encoded = msgpack.encode(value);
|
194 | var decoded = msgpack.decode(encoded);
|
195 | assert.equal(decoded.length, length);
|
196 | assert.equal(decoded[0], value[0]);
|
197 | assert.equal(decoded[length - 1], value[length - 1]);
|
198 | });
|
199 | });
|
200 | }); |
\ | No newline at end of file |