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