UNPKG

3.56 kBJavaScriptView Raw
1'use strict';
2
3var BufferShim = require('../buffer'),
4 test = require('tap').test;
5
6/*eslint comma-spacing: 0*/
7
8function toArray(buf) {
9 var arr = [];
10 for (var i = 0; i < buf.length; i++) {
11 arr.push(buf[i]);
12 }
13 return arr;
14}
15
16test('empty buffer', function(t) {
17 var buf = new BufferShim();
18 t.equal(buf.length, 0);
19 t.end();
20});
21
22test('isBuffer', function(t) {
23 var buf = new BufferShim();
24 t.equal(BufferShim.isBuffer(buf), true);
25 t.equal(BufferShim.isBuffer([1, 2, 3]), false);
26 t.equal(BufferShim.isBuffer(new BufferShim([1, 2, 3])), true);
27 t.end();
28});
29
30test('writeUInt32LE', function(t) {
31 var shim = new BufferShim(8);
32 shim.writeUInt32LE(12562, 0);
33 shim.writeUInt32LE(555, 4);
34
35 t.same(toArray(shim), [18,49,0,0,43,2,0,0]);
36 t.end();
37});
38
39test('readUInt32LE', function(t) {
40 var shim = new BufferShim(8);
41 shim.writeUInt32LE(12562, 0);
42 shim.writeUInt32LE(555, 4);
43
44 t.same([shim.readUInt32LE(0), shim.readUInt32LE(4)], [12562, 555]);
45 t.end();
46});
47
48test('readInt32LE', function(t) {
49 var shim = new BufferShim(8);
50 shim.writeInt32LE(12562, 0);
51 shim.writeInt32LE(-555, 4);
52
53 t.same([shim.readInt32LE(0), shim.readInt32LE(4)], [12562, -555]);
54 t.end();
55});
56
57test('writeFloatLE', function(t) {
58 var shim = new BufferShim(4);
59 shim.writeFloatLE(123.456, 0);
60
61 t.same(toArray(shim), [121,233,246,66]);
62 t.end();
63});
64
65test('readFloatLE', function(t) {
66 var shim = new BufferShim(4);
67 shim.writeFloatLE(123.456, 0);
68
69 t.ok(Math.round(shim.readFloatLE(0) * 1000) / 1000, 123.456);
70 t.end();
71});
72
73test('writeDoubleLE', function(t) {
74 var shim = new BufferShim(8);
75 shim.writeDoubleLE(123.4567890123456789, 0);
76
77 t.same(toArray(shim), [153,76,251,7,60,221,94,64]);
78 t.end();
79});
80
81test('readDoubleLE', function(t) {
82 var shim = new BufferShim(8);
83 shim.writeDoubleLE(123.4567890123456789, 0);
84
85 t.ok(Math.round(shim.readDoubleLE(0) * 1e16) / 1e16, 123.4567890123456789);
86 t.end();
87});
88
89var testStr = 'Привет 李小龙',
90 testBytes = [208,159,209,128,208,184,208,178,208,181,209,130,32,230,157,142,229,176,143,233,190,153];
91
92test('write', function(t) {
93 var shim = new BufferShim(22);
94 shim.write(testStr, 0);
95
96 t.same(toArray(shim), testBytes);
97 t.end();
98});
99
100test('toString', function(t) {
101 var shim = new BufferShim(22);
102 shim.write(testStr, 0);
103
104 t.same(shim.toString(), testStr);
105 t.end();
106});
107
108test('more complicated utf8', function(t) {
109 var shim = new BufferShim(100);
110 // crazy test from github.com/mathiasbynens/utf8.js
111 var str = '\uDC00\uDC00\uDC00\uDC00A\uDC00\uD834\uDF06\uDC00\uDEEE\uDFFF\uD800\uDC00\uD800\uD800\uD800\uD800A' +
112 '\uD800\uD834\uDF06';
113 var len = BufferShim.byteLength(str);
114 shim.write(str, 0);
115 var str2 = shim.toString('utf8', 0, len);
116 t.same(new Buffer(str2), new Buffer(str));
117 t.end();
118});
119
120test('wrap', function(t) {
121 var arr = new Uint8Array(testBytes);
122 var shim = new BufferShim(arr);
123
124 t.same(shim.toString(), testStr);
125 t.end();
126});
127
128test('byteLength', function(t) {
129 t.same(BufferShim.byteLength(testStr), 22);
130 t.end();
131});
132
133test('copy', function(t) {
134 var shim = new BufferShim(new Uint8Array(testBytes));
135 var shim2 = new BufferShim(22);
136
137 shim.copy(shim2);
138
139 t.same(toArray(shim), toArray(shim2));
140 t.end();
141});
142
143test('slice', function(t) {
144 var shim = new BufferShim(new Uint8Array(testBytes));
145 t.same(toArray(shim.slice(0, 2)), [208, 159]);
146 t.end();
147});