deno.land / std@0.166.0 / node / _tools / test / parallel / test-buffer-bytelength.js

test-buffer-bytelength.js
نووسراو ببینە
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
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 16.13.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');const SlowBuffer = require('buffer').SlowBuffer;const vm = require('vm');
[ [32, 'latin1'], [NaN, 'utf8'], [{}, 'latin1'], [],].forEach((args) => { assert.throws( () => Buffer.byteLength(...args), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', message: 'The "string" argument must be of type string or an instance ' + 'of Buffer or ArrayBuffer.' + common.invalidArgTypeHelper(args[0]) } );});
assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
assert(ArrayBuffer.isView(new Buffer(10)));assert(ArrayBuffer.isView(new SlowBuffer(10)));assert(ArrayBuffer.isView(Buffer.alloc(10)));assert(ArrayBuffer.isView(Buffer.allocUnsafe(10)));assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10)));assert(ArrayBuffer.isView(Buffer.from('')));
// bufferconst incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);assert.strictEqual(Buffer.byteLength(incomplete), 5);const ascii = Buffer.from('abc');assert.strictEqual(Buffer.byteLength(ascii), 3);
// ArrayBufferconst buffer = new ArrayBuffer(8);assert.strictEqual(Buffer.byteLength(buffer), 8);
// TypedArrayconst int8 = new Int8Array(8);assert.strictEqual(Buffer.byteLength(int8), 8);const uint8 = new Uint8Array(8);assert.strictEqual(Buffer.byteLength(uint8), 8);const uintc8 = new Uint8ClampedArray(2);assert.strictEqual(Buffer.byteLength(uintc8), 2);const int16 = new Int16Array(8);assert.strictEqual(Buffer.byteLength(int16), 16);const uint16 = new Uint16Array(8);assert.strictEqual(Buffer.byteLength(uint16), 16);const int32 = new Int32Array(8);assert.strictEqual(Buffer.byteLength(int32), 32);const uint32 = new Uint32Array(8);assert.strictEqual(Buffer.byteLength(uint32), 32);const float32 = new Float32Array(8);assert.strictEqual(Buffer.byteLength(float32), 32);const float64 = new Float64Array(8);assert.strictEqual(Buffer.byteLength(float64), 64);
// DataViewconst dv = new DataView(new ArrayBuffer(2));assert.strictEqual(Buffer.byteLength(dv), 2);
// Special case: zero length stringassert.strictEqual(Buffer.byteLength('', 'ascii'), 0);assert.strictEqual(Buffer.byteLength('', 'HeX'), 0);
// utf8assert.strictEqual(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19);assert.strictEqual(Buffer.byteLength('κλμνξο', 'utf8'), 12);assert.strictEqual(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15);assert.strictEqual(Buffer.byteLength('𠝹𠱓𠱸', 'UTF8'), 12);// Without an encoding, utf8 should be assumedassert.strictEqual(Buffer.byteLength('hey there'), 9);assert.strictEqual(Buffer.byteLength('𠱸挶νξ#xx :)'), 17);assert.strictEqual(Buffer.byteLength('hello world', ''), 11);// It should also be assumed with unrecognized encodingassert.strictEqual(Buffer.byteLength('hello world', 'abc'), 11);assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
// base64assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'BASE64'), 11);assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3);assert.strictEqual( Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==', 'base64'), 25);assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ', 'base64url'), 11);assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ', 'BASE64URL'), 11);assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE', 'base64url'), 14);assert.strictEqual(Buffer.byteLength('aGkk', 'base64url'), 3);assert.strictEqual( Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw', 'base64url'), 25);// special paddingassert.strictEqual(Buffer.byteLength('aaa=', 'base64'), 2);assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3);assert.strictEqual(Buffer.byteLength('aaa=', 'base64url'), 2);assert.strictEqual(Buffer.byteLength('aaaa==', 'base64url'), 3);
assert.strictEqual(Buffer.byteLength('Il était tué'), 14);assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
['ascii', 'latin1', 'binary'] .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); });
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'] .reduce((es, e) => es.concat(e, e.toUpperCase()), []) .forEach((encoding) => { assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); });
// TODO(Soremwar)// Enable once vm module is available// // Test that ArrayBuffer from a different context is detected correctly// const arrayBuf = vm.runInNewContext('new ArrayBuffer()');// assert.strictEqual(Buffer.byteLength(arrayBuf), 0);
// Verify that invalid encodings are treated as utf8for (let i = 1; i < 10; i++) { const encoding = String(i).repeat(i);
assert.ok(!Buffer.isEncoding(encoding)); assert.strictEqual(Buffer.byteLength('foo', encoding), Buffer.byteLength('foo', 'utf8'));}
std

Version Info

Tagged at
a year ago