-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbyte.test.js
More file actions
642 lines (571 loc) · 24.6 KB
/
byte.test.js
File metadata and controls
642 lines (571 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*!
* byte - test/byte.test.js
*
* Copyright(c) 2013 - 2014
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
* dead-horse <dead_horse@qq.com> (https://github.com/dead-horse)
*/
"use strict";
/**
* Module dependencies.
*/
var should = require('should');
var Long = require('long');
var ByteBuffer = require('../');
describe('byte.test.js', function () {
describe('new ByteBuffer()', function () {
it('should create ByteBuffer', function () {
var bytes = new ByteBuffer();
bytes.array().should.length(0);
bytes.position().should.equal(0);
});
it('should correct set size when passed array', function () {
var bytes = new ByteBuffer({ array: new Buffer(10) });
bytes.limit().should.equal(10);
});
});
describe('putString(), getString()', function () {
it('should put empty string', function () {
var bytes = new ByteBuffer({size: 1});
bytes.putString('');
bytes.putString('');
bytes.putString(null);
bytes.putString(new Buffer(''));
bytes.putString(0, '');
bytes.position(0);
bytes.getString().should.equal('');
bytes.getString().should.equal('');
bytes.getString().should.equal('');
bytes.getString().should.equal('');
});
it('should put strings', function () {
var bytes = new ByteBuffer({size: 10});
bytes.putString('foo, 中文');
bytes.getString(0).should.equal('foo, 中文');
bytes.putString(0, 'foo, 中国');
bytes.getString(0).should.equal('foo, 中国');
bytes.putString(0, new Buffer('foo, 中国'));
bytes.getString(0).should.equal('foo, 中国');
bytes.putString('foo2');
bytes.getString(new Buffer('foo, 中国').length + 4).should.equal('foo2');
bytes.position(0);
bytes.getString().should.equal('foo, 中国');
bytes = new ByteBuffer({size: 1});
bytes.putCString(0, '');
bytes._size.should.equal(4 * 2);
bytes = new ByteBuffer({size: 10});
bytes.putCString('foo, \u0000中文\u0000');
bytes.getCString(0).should.equal('foo, \u0000中文\u0000');
bytes.putCString(0, 'bar123123, \u0000中文\u0000');
bytes.getCString(0).should.equal('bar123123, \u0000中文\u0000');
var lbadstr = 'bar123123123123123123123123123123123123, \u0000中文\u0000';
bytes.putCString(0, lbadstr);
bytes.getCString(0).should.equal(lbadstr);
bytes.position(0);
bytes.putCString('bar123123, \u0000中文\u0000');
bytes.putCString('foo2foo, \u0000中文\u0000');
bytes.getCString(new Buffer('bar123123, \u0000中文\u0000').length + 4 + 1).should.equal('foo2foo, \u0000中文\u0000');
bytes.position(0);
bytes.getCString().should.equal('bar123123, \u0000中文\u0000');
});
});
describe('putChar(), getChar()', function () {
it('should put a char', function () {
var bytes = ByteBuffer.allocate(2);
bytes.putChar('a');
bytes.toString().should.equal('<ByteBuffer 61>');
bytes.putChar('A');
bytes.toString().should.equal('<ByteBuffer 61 41>');
bytes.putChar(255);
bytes.toString().should.equal('<ByteBuffer 61 41 ff>');
bytes.putChar(2, 'a');
bytes.toString().should.equal('<ByteBuffer 61 41 61>');
bytes.putChar('a');
bytes.toString().should.equal('<ByteBuffer 61 41 61 61>');
bytes.putChar('a');
bytes.toString().should.equal('<ByteBuffer 61 41 61 61 61>');
bytes.putChar('a');
bytes.toString().should.equal('<ByteBuffer 61 41 61 61 61 61>');
bytes.position(0);
bytes.getChar().should.equal('a');
bytes.getChar().should.equal('A');
bytes.getChar().should.equal('a');
bytes.getChar().should.equal('a');
bytes.getChar().should.equal('a');
bytes.getChar().should.equal('a');
bytes.getChar(1).should.equal('A');
bytes.position().should.equal(6);
});
});
describe('putFloat(), getFloat()', function () {
it('should put a float', function () {
var cases = [
// value, big, litter
[-91.99999, '<ByteBuffer c2 b7 ff ff>', '<ByteBuffer ff ff b7 c2>'],
[-100, '<ByteBuffer c2 c8 00 00>', '<ByteBuffer 00 00 c8 c2>'],
[-1, '<ByteBuffer bf 80 00 00>', '<ByteBuffer 00 00 80 bf>'],
[0, '<ByteBuffer 00 00 00 00>', '<ByteBuffer 00 00 00 00>'],
[1, '<ByteBuffer 3f 80 00 00>', '<ByteBuffer 00 00 80 3f>'],
[2, '<ByteBuffer 40 00 00 00>', '<ByteBuffer 00 00 00 40>'],
[100, '<ByteBuffer 42 c8 00 00>', '<ByteBuffer 00 00 c8 42>'],
[999, '<ByteBuffer 44 79 c0 00>', '<ByteBuffer 00 c0 79 44>'],
[99.999, '<ByteBuffer 42 c7 ff 7d>', '<ByteBuffer 7d ff c7 42>'],
[1024, '<ByteBuffer 44 80 00 00>', '<ByteBuffer 00 00 80 44>'],
[123123.125, '<ByteBuffer 47 f0 79 90>', '<ByteBuffer 90 79 f0 47>'],
];
var bytes = ByteBuffer.allocate(4);
bytes.putFloat(0);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00>');
bytes.position(0);
bytes.getFloat().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putFloat(0, item[0]);
bytes.toString().should.equal(item[1]);
String(bytes.getFloat(0)).should.containEql(item[0]);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putFloat(0, item[0]);
bytes.toString().should.equal(item[2]);
String(bytes.getFloat(0)).should.containEql(item[0]);
});
});
});
describe('putInt(), getInt()', function () {
it('should put an int', function () {
var cases = [
// value, big, litter
[-91, '<ByteBuffer ff ff ff a5>', '<ByteBuffer a5 ff ff ff>'],
[-100, '<ByteBuffer ff ff ff 9c>', '<ByteBuffer 9c ff ff ff>'],
[-1, '<ByteBuffer ff ff ff ff>', '<ByteBuffer ff ff ff ff>'],
[0, '<ByteBuffer 00 00 00 00>', '<ByteBuffer 00 00 00 00>'],
[1, '<ByteBuffer 00 00 00 01>', '<ByteBuffer 01 00 00 00>'],
[2, '<ByteBuffer 00 00 00 02>', '<ByteBuffer 02 00 00 00>'],
[100, '<ByteBuffer 00 00 00 64>', '<ByteBuffer 64 00 00 00>'],
[999, '<ByteBuffer 00 00 03 e7>', '<ByteBuffer e7 03 00 00>'],
[99999, '<ByteBuffer 00 01 86 9f>', '<ByteBuffer 9f 86 01 00>'],
[1024, '<ByteBuffer 00 00 04 00>', '<ByteBuffer 00 04 00 00>'],
[-2147483648, '<ByteBuffer 80 00 00 00>', '<ByteBuffer 00 00 00 80>'],
[-2147483647, '<ByteBuffer 80 00 00 01>', '<ByteBuffer 01 00 00 80>'],
// Math.pow(2, 31) - 1
[2147483647, '<ByteBuffer 7f ff ff ff>', '<ByteBuffer ff ff ff 7f>'],
[2147483646, '<ByteBuffer 7f ff ff fe>', '<ByteBuffer fe ff ff 7f>'],
];
var bytes = ByteBuffer.allocate(1);
bytes.putInt(0);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00>');
bytes.position(0);
bytes.getInt().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putInt(0, item[0]);
bytes.toString().should.equal(item[1]);
bytes.getInt(0).should.equal(item[0]);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putInt(0, item[0]);
bytes.toString().should.equal(item[2]);
bytes.getInt(0).should.equal(item[0]);
});
});
});
describe('putInt8(), getInt8(), putUInt8(), getUInt8()', function () {
it('should put and get 8 bits int', function () {
var bytes = ByteBuffer.allocate(1);
bytes.putInt8(-128);
bytes.toString().should.equal('<ByteBuffer 80>');
bytes.putInt8(0, -128);
bytes.toString().should.equal('<ByteBuffer 80>');
bytes.position(0);
bytes.getInt8().should.equal(-128);
bytes.putInt8(0, 0);
bytes.toString().should.equal('<ByteBuffer 00>');
bytes.getInt8(0).should.equal(0);
bytes.putInt8(0, -128);
bytes.toString().should.equal('<ByteBuffer 80>');
bytes.putInt8(0, 127);
bytes.toString().should.equal('<ByteBuffer 7f>');
bytes.position(0);
bytes.putUInt8(255);
bytes.toString().should.equal('<ByteBuffer ff>');
bytes.position(0);
bytes.getUInt8().should.equal(255);
bytes.putUInt8(0, 0).toString().should.equal('<ByteBuffer 00>');
bytes.putUInt8(0, 0).getUInt8(0).should.equal(0);
});
});
describe('putUInt(), getUInt()', function () {
it('should put an int', function () {
var cases = [
// value, big, litter
[0, '<ByteBuffer 00 00 00 00>', '<ByteBuffer 00 00 00 00>'],
[1, '<ByteBuffer 00 00 00 01>', '<ByteBuffer 01 00 00 00>'],
[2, '<ByteBuffer 00 00 00 02>', '<ByteBuffer 02 00 00 00>'],
[100, '<ByteBuffer 00 00 00 64>', '<ByteBuffer 64 00 00 00>'],
[999, '<ByteBuffer 00 00 03 e7>', '<ByteBuffer e7 03 00 00>'],
[99999, '<ByteBuffer 00 01 86 9f>', '<ByteBuffer 9f 86 01 00>'],
[1024, '<ByteBuffer 00 00 04 00>', '<ByteBuffer 00 04 00 00>'],
// Math.pow(2, 31) - 1
[2147483647, '<ByteBuffer 7f ff ff ff>', '<ByteBuffer ff ff ff 7f>'],
[2147483646, '<ByteBuffer 7f ff ff fe>', '<ByteBuffer fe ff ff 7f>'],
[2147483648, '<ByteBuffer 80 00 00 00>', '<ByteBuffer 00 00 00 80>'],
[3249209323, '<ByteBuffer c1 aa ff eb>', '<ByteBuffer eb ff aa c1>'],
// Math.pow(2, 32 - 2)
[4294967294, '<ByteBuffer ff ff ff fe>', '<ByteBuffer fe ff ff ff>'],
// Math.pow(2, 32 - 1)
[4294967295, '<ByteBuffer ff ff ff ff>', '<ByteBuffer ff ff ff ff>'],
];
var bytes = ByteBuffer.allocate(1);
bytes.putUInt(0);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00>');
bytes.position(0);
bytes.getUInt().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putUInt(0, item[0]);
bytes.toString().should.equal(item[1]);
bytes.getUInt(0).should.equal(item[0]);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putUInt(0, item[0]);
bytes.toString().should.equal(item[2]);
bytes.getUInt(0).should.equal(item[0]);
});
});
});
describe('putShort(), getShort()', function () {
it('should put a short', function () {
var cases = [
// value, big, litter
[-91, '<ByteBuffer ff a5>', '<ByteBuffer a5 ff>'],
[-100, '<ByteBuffer ff 9c>', '<ByteBuffer 9c ff>'],
[-1, '<ByteBuffer ff ff>', '<ByteBuffer ff ff>'],
[0, '<ByteBuffer 00 00>', '<ByteBuffer 00 00>'],
[1, '<ByteBuffer 00 01>', '<ByteBuffer 01 00>'],
[2, '<ByteBuffer 00 02>', '<ByteBuffer 02 00>'],
[100, '<ByteBuffer 00 64>', '<ByteBuffer 64 00>'],
[999, '<ByteBuffer 03 e7>', '<ByteBuffer e7 03>'],
[9999, '<ByteBuffer 27 0f>', '<ByteBuffer 0f 27>'],
[1024, '<ByteBuffer 04 00>', '<ByteBuffer 00 04>'],
[-32768, '<ByteBuffer 80 00>', '<ByteBuffer 00 80>'],
[-32767, '<ByteBuffer 80 01>', '<ByteBuffer 01 80>'],
// Math.pow(2, 15) - 1
[32767, '<ByteBuffer 7f ff>', '<ByteBuffer ff 7f>'],
[32766, '<ByteBuffer 7f fe>', '<ByteBuffer fe 7f>'],
];
var bytes = ByteBuffer.allocate(1);
bytes.putShort(0);
bytes.toString().should.equal('<ByteBuffer 00 00>');
bytes.position(0);
bytes.getShort().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putShort(0, item[0]);
bytes.toString().should.equal(item[1]);
bytes.getShort(0).should.equal(item[0]);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putShort(0, item[0]);
bytes.toString().should.equal(item[2]);
bytes.getShort(0).should.equal(item[0]);
});
});
});
describe('putUInt16(), getUInt16()', function () {
it('should put a uint16', function () {
var cases = [
// value, big, litter
[0, '<ByteBuffer 00 00>', '<ByteBuffer 00 00>'],
[1, '<ByteBuffer 00 01>', '<ByteBuffer 01 00>'],
[2, '<ByteBuffer 00 02>', '<ByteBuffer 02 00>'],
[100, '<ByteBuffer 00 64>', '<ByteBuffer 64 00>'],
[999, '<ByteBuffer 03 e7>', '<ByteBuffer e7 03>'],
[9999, '<ByteBuffer 27 0f>', '<ByteBuffer 0f 27>'],
[1024, '<ByteBuffer 04 00>', '<ByteBuffer 00 04>'],
// Math.pow(2, 15) - 1
[32768, '<ByteBuffer 80 00>', '<ByteBuffer 00 80>'],
[40000, '<ByteBuffer 9c 40>', '<ByteBuffer 40 9c>'],
// Math.pow(2, 16) - 1
[65534, '<ByteBuffer ff fe>', '<ByteBuffer fe ff>'],
[65535, '<ByteBuffer ff ff>', '<ByteBuffer ff ff>'],
];
var bytes = ByteBuffer.allocate(1);
bytes.putUInt16(0);
bytes.toString().should.equal('<ByteBuffer 00 00>');
bytes.position(0);
bytes.getUInt16().should.equal(0);
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putUInt16(0, item[0]);
bytes.toString().should.equal(item[1]);
bytes.getUInt16(0).should.equal(item[0]);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putUInt16(0, item[0]);
bytes.toString().should.equal(item[2]);
bytes.getUInt16(0).should.equal(item[0]);
});
});
});
describe('putLong(), getLong()', function () {
it('should put a long', function () {
var cases = [
// value, big, litter
[-91, '<ByteBuffer ff ff ff ff ff ff ff a5>', '<ByteBuffer a5 ff ff ff ff ff ff ff>'],
[-100, '<ByteBuffer ff ff ff ff ff ff ff 9c>', '<ByteBuffer 9c ff ff ff ff ff ff ff>'],
[-1, '<ByteBuffer ff ff ff ff ff ff ff ff>', '<ByteBuffer ff ff ff ff ff ff ff ff>'],
[0, '<ByteBuffer 00 00 00 00 00 00 00 00>', '<ByteBuffer 00 00 00 00 00 00 00 00>'],
[1, '<ByteBuffer 00 00 00 00 00 00 00 01>', '<ByteBuffer 01 00 00 00 00 00 00 00>'],
[2, '<ByteBuffer 00 00 00 00 00 00 00 02>', '<ByteBuffer 02 00 00 00 00 00 00 00>'],
[100, '<ByteBuffer 00 00 00 00 00 00 00 64>', '<ByteBuffer 64 00 00 00 00 00 00 00>'],
[999, '<ByteBuffer 00 00 00 00 00 00 03 e7>', '<ByteBuffer e7 03 00 00 00 00 00 00>'],
[99999, '<ByteBuffer 00 00 00 00 00 01 86 9f>', '<ByteBuffer 9f 86 01 00 00 00 00 00>'],
[1024, '<ByteBuffer 00 00 00 00 00 00 04 00>', '<ByteBuffer 00 04 00 00 00 00 00 00>'],
[-2147483648, '<ByteBuffer ff ff ff ff 80 00 00 00>', '<ByteBuffer 00 00 00 80 ff ff ff ff>'],
[-2147483647, '<ByteBuffer ff ff ff ff 80 00 00 01>', '<ByteBuffer 01 00 00 80 ff ff ff ff>'],
// Math.pow(2, 31) - 1
[2147483647, '<ByteBuffer 00 00 00 00 7f ff ff ff>', '<ByteBuffer ff ff ff 7f 00 00 00 00>'],
[2147483646, '<ByteBuffer 00 00 00 00 7f ff ff fe>', '<ByteBuffer fe ff ff 7f 00 00 00 00>'],
// Math.pow(2, 31)
[2147483648, '<ByteBuffer 00 00 00 00 80 00 00 00>', '<ByteBuffer 00 00 00 80 00 00 00 00>'],
['99999', '<ByteBuffer 00 00 00 00 00 01 86 9f>', '<ByteBuffer 9f 86 01 00 00 00 00 00>'],
['2147483648', '<ByteBuffer 00 00 00 00 80 00 00 00>', '<ByteBuffer 00 00 00 80 00 00 00 00>'],
['-2147483647', '<ByteBuffer ff ff ff ff 80 00 00 01>', '<ByteBuffer 01 00 00 80 ff ff ff ff>'],
// 2, 63 - 1
['-9223372036854775808', '<ByteBuffer 80 00 00 00 00 00 00 00>', '<ByteBuffer 00 00 00 00 00 00 00 80>'],
['-9223372036854775807', '<ByteBuffer 80 00 00 00 00 00 00 01>', '<ByteBuffer 01 00 00 00 00 00 00 80>'],
['9223372036854775807', '<ByteBuffer 7f ff ff ff ff ff ff ff>', '<ByteBuffer ff ff ff ff ff ff ff 7f>'],
['9223372036854775806', '<ByteBuffer 7f ff ff ff ff ff ff fe>', '<ByteBuffer fe ff ff ff ff ff ff 7f>'],
[Long.fromString('9223372036854775806'),
'<ByteBuffer 7f ff ff ff ff ff ff fe>', '<ByteBuffer fe ff ff ff ff ff ff 7f>'],
];
var bytes = ByteBuffer.allocate(1);
bytes.putLong(0);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00 00 00 00 00>');
bytes.position(0);
bytes.getLong().toString().should.equal('0');
cases.forEach(function (item) {
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putLong(0, item[0]);
bytes.toString().should.equal(item[1]);
bytes.getLong(0).toString().should.equal(String(item[0]));
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putLong(0, item[0]);
bytes.toString().should.equal(item[2]);
bytes.getLong(0).toString().should.equal(String(item[0]));
});
});
});
describe('putDouble(), getDouble()', function () {
it('should put a double', function () {
var bytes = ByteBuffer.allocate(2);
bytes.putDouble(1);
bytes.toString().should.equal('<ByteBuffer 3f f0 00 00 00 00 00 00>');
bytes.position(0);
bytes.getDouble().should.equal(1);
bytes.putDouble(0, 0);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00 00 00 00 00>');
bytes.getDouble(0).should.equal(0);
bytes.putDouble(0, 2);
bytes.toString().should.equal('<ByteBuffer 40 00 00 00 00 00 00 00>');
bytes.getDouble(0).should.equal(2);
bytes.putDouble(0, 1024);
bytes.toString().should.equal('<ByteBuffer 40 90 00 00 00 00 00 00>');
bytes.getDouble(0).should.equal(1024);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putDouble(0, 1);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00 00 00 f0 3f>');
bytes.getDouble(0).should.equal(1);
bytes.putDouble(0, 1024);
bytes.toString().should.equal('<ByteBuffer 00 00 00 00 00 00 90 40>');
bytes.getDouble(0).should.equal(1024);
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putDouble(0, 1123123.123123);
bytes.toString().should.equal('<ByteBuffer 41 31 23 33 1f 84 fd 2a>');
bytes.getDouble(0).should.equal(1123123.123123);
bytes.order(ByteBuffer.LITTLE_ENDIAN);
bytes.putDouble(0, 1123123.123123);
bytes.toString().should.equal('<ByteBuffer 2a fd 84 1f 33 23 31 41>');
bytes.getDouble(0).should.equal(1123123.123123);
bytes.order(ByteBuffer.BIG_ENDIAN);
bytes.putDouble(1123123.123123);
bytes.toString().should.equal('<ByteBuffer 2a fd 84 1f 33 23 31 41 41 31 23 33 1f 84 fd 2a>');
bytes.getDouble(8).should.equal(1123123.123123);
ByteBuffer.wrap(bytes.array()).getDouble(8).should.equal(1123123.123123);
ByteBuffer.wrap(bytes.array(), 8, 8).getDouble(0).should.equal(1123123.123123);
});
});
describe('put()', function () {
it('should put byte', function () {
var bytes = ByteBuffer.allocate(1);
bytes.put(1);
bytes.toString().should.equal('<ByteBuffer 01>');
bytes.get(0).should.equal(1);
bytes.put(0, 2);
bytes.toString().should.equal('<ByteBuffer 02>');
bytes.get(0).should.equal(2);
bytes.put(1);
bytes.toString().should.equal('<ByteBuffer 02 01>');
bytes.get(1).should.equal(1);
bytes.put(new Buffer([255, 255, 255]));
bytes.toString().should.equal('<ByteBuffer 02 01 ff ff ff>');
bytes.get(2, 3).should.eql(new Buffer([255, 255, 255]));
bytes.put(new Buffer([255, 254, 1]), 1, 2);
bytes.toString().should.equal('<ByteBuffer 02 01 ff ff ff fe 01>');
bytes.get(5, 2).should.eql(new Buffer([254, 1]));
bytes.position(0);
bytes.read(7).should.eql(new Buffer([2, 1, 255, 255, 255, 254, 1]));
bytes.position().should.equal(7);
bytes.position(0);
bytes.skip(5);
bytes.read(2).should.eql(new Buffer([254, 1]));
bytes.position().should.equal(7);
});
});
describe('putRawString()', function () {
it('should put raw string', function () {
var bytes = ByteBuffer.allocate(1);
bytes.putRawString('hello');
bytes.putRawString(' world');
bytes.toString().should.equal('<ByteBuffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>');
bytes.getRawString(0, 11).should.equal('hello world');
bytes.position(0);
bytes.getRawString().should.equal('h');
bytes = ByteBuffer.allocate(1);
bytes.putRawString('你好');
bytes.toString().should.equal('<ByteBuffer e4 bd a0 e5 a5 bd>');
bytes.position(0).readRawString(6).should.equal('你好');
bytes.putRawString(0, '我们');
bytes.toString().should.equal('<ByteBuffer e6 88 91 e4 bb ac>');
bytes.getRawString(0, 6).should.equal('我们');
bytes.readRawString(0, 6).should.equal('我们');
bytes = ByteBuffer.allocate(1);
bytes.putRawString('');
bytes.toString().should.equal('<ByteBuffer>');
});
it('should put emoji', function () {
// utf8
var bytes = ByteBuffer.allocate(1);
var str = 'hello\u9983\u5c32';
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer 68 65 6c 6c 6f e9 a6 83 e5 b0 b2>');
bytes.getRawString(0, 11).should.eql(str);
// gbk
var bytes = ByteBuffer.allocate(1);
var str = 'hello\ud83c\udf3c';
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer 68 65 6c 6c 6f ed a0 bc ed bc bc>');
bytes.getRawString(0, 11).should.eql(str);
var bytes = ByteBuffer.allocate(1);
// java encode bytes: [-19, -96, -67, -19, -72, -128, 87, 119, 119, -23, -126, -93]
var str = '\ud83d\ude00Www那';
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer ed a0 bd ed b8 80 57 77 77 e9 82 a3>');
bytes.getRawString(0, 12).should.eql(str);
// Construction of a special test case which triggers the bug
// of allocating insufficient space via _checkSize
var bytes = ByteBuffer.allocate(4);
var str = '\ud83d\ude00';
bytes.putRawString(str);
bytes.toString().should.eql('<ByteBuffer ed a0 bd ed b8 80>');
});
});
describe('array(), copy()', function () {
it('should copy(start)', function () {
var bytes = ByteBuffer.allocate(8);
bytes.putInt(0);
bytes.putInt(1);
bytes.copy(4).should.eql(new Buffer([0, 0, 0, 1]));
});
it('should copy(start, end)', function () {
var bytes = ByteBuffer.allocate(9);
bytes.putInt(0);
bytes.putInt(1);
bytes.copy(0, 8).should.eql(new Buffer([0, 0, 0, 0, 0, 0, 0, 1]));
bytes.copy(0, 9).should.eql(new Buffer([0, 0, 0, 0, 0, 0, 0, 1]));
bytes.copy(0, 4).should.eql(new Buffer([0, 0, 0, 0]));
bytes.copy(4, 8).should.eql(new Buffer([0, 0, 0, 1]));
});
});
describe('_copy()', function () {
it('should splice < 2048 ok', function () {
var bytes = ByteBuffer.allocate(4096);
bytes.putRawString('hello');
bytes.putRawString('world');
bytes._copy(0, 5).toString().should.equal('hello');
});
it('should splice > 2048 ok', function () {
var bytes = ByteBuffer.allocate(4096);
for (var i = 0; i < 800; i++) {
bytes.putRawString('hello');
}
bytes._copy(1000, 4000).toString().should.have.length(3000);
});
});
describe('capacity()', function () {
it('should capacity()', function () {
var bytes = ByteBuffer.allocate(4);
bytes.putInt(0);
bytes.capacity().should.eql(4);
bytes.putInt(1);
bytes.capacity().should.eql(16);
bytes.flip();
bytes.capacity().should.eql(16);
});
});
describe('remaining(), hasRemaining()', function () {
it('should remaining(), hasRemaining()', function () {
var bytes = ByteBuffer.allocate(8);
bytes.remaining().should.eql(8);
bytes.hasRemaining().should.true;
bytes.put(1);
bytes.remaining().should.eql(7);
bytes.hasRemaining().should.true;
bytes.putShort(2);
bytes.remaining().should.eql(5);
bytes.hasRemaining().should.true;
bytes.put(1);
bytes.putInt(1);
bytes.remaining().should.eql(0);
bytes.hasRemaining().should.false;
});
});
describe('flip(), limit()', function () {
it('should flip(), limit()', function () {
var bytes = ByteBuffer.allocate(8);
bytes.limit().should.eql(8);
bytes.putInt(1);
bytes.limit().should.eql(8);
bytes.flip();
bytes.limit().should.eql(4);
});
it('should limit(newLimit)', function () {
var bytes = ByteBuffer.allocate(8);
bytes.limit().should.eql(8);
bytes.limit(4).limit().should.eql(4);
});
});
describe('get(dst, offset, length)', function () {
it('should get(dst, offset, length)', function () {
var bytes = ByteBuffer.allocate(4);
bytes.putInt(1);
bytes.flip(); // switch to read mode
var buf = new Buffer(4);
bytes.get(buf);
buf.should.eql(new Buffer([0, 0, 0, 1]));
var buf2 = new Buffer(1);
(function() {
bytes.get(buf2);
}).should.throw('BufferOverflowException');
});
it('should get(dst, offset, length) again', function () {
var bytes = ByteBuffer.allocate(8);
bytes.putInt(1);
bytes.putInt(5);
bytes.flip(); // switch to read mode
var buf = new Buffer(4);
bytes.get(buf);
buf.should.eql(new Buffer([0, 0, 0, 1]));
bytes.get(buf);
buf.should.eql(new Buffer([0, 0, 0, 5]));
});
});
});