Files
node/test/parallel/test-stringbytes-external.js
Trevor Norris 54cc7212df buffer: introduce latin1 encoding term
When node began using the OneByte API (f150d56) it also switched to
officially supporting ISO-8859-1. Though at the time no new encoding
string was introduced.

Introduce the new encoding string 'latin1' to be more explicit. The
previous 'binary' and documented as an alias to 'latin1'.  While many
tests have switched to use 'latin1', there are still plenty that do both
'binary' and 'latin1' checks side-by-side to ensure there is no
regression.

PR-URL: https://github.com/nodejs/node/pull/7111
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-06-07 13:51:14 -06:00

123 lines
3.5 KiB
JavaScript

'use strict';
require('../common');
var assert = require('assert');
// minimum string size to overflow into external string space
var EXTERN_APEX = 0xFBEE9;
// manually controlled string for checking binary output
var ucs2_control = 'a\u0000';
var write_str = 'a';
// first do basic checks
var b = Buffer.from(write_str, 'ucs2');
// first check latin1
var c = b.toString('latin1');
assert.equal(b[0], 0x61);
assert.equal(b[1], 0);
assert.equal(ucs2_control, c);
// now check binary
c = b.toString('binary');
assert.equal(b[0], 0x61);
assert.equal(b[1], 0);
assert.equal(ucs2_control, c);
// now create big strings
var size = 1 + (1 << 20);
write_str = Array(size).join(write_str);
ucs2_control = Array(size).join(ucs2_control);
// check resultant buffer and output string
b = Buffer.from(write_str, 'ucs2');
// check fist Buffer created from write string
for (let i = 0; i < b.length; i += 2) {
assert.equal(b[i], 0x61);
assert.equal(b[i + 1], 0);
}
// create another string to create an external string
var b_ucs = b.toString('ucs2');
// check control against external binary string
var l_bin = b.toString('latin1');
assert.equal(ucs2_control, l_bin);
// check control against external binary string
var b_bin = b.toString('binary');
assert.equal(ucs2_control, b_bin);
// create buffer copy from external
var c_bin = Buffer.from(l_bin, 'latin1');
var c_ucs = Buffer.from(b_ucs, 'ucs2');
// make sure they're the same length
assert.equal(c_bin.length, c_ucs.length);
// make sure Buffers from externals are the same
for (let i = 0; i < c_bin.length; i++) {
assert.equal(c_bin[i], c_ucs[i]);
}
// check resultant strings
assert.equal(c_bin.toString('ucs2'), c_ucs.toString('ucs2'));
assert.equal(c_bin.toString('latin1'), ucs2_control);
assert.equal(c_ucs.toString('latin1'), ucs2_control);
// now let's test BASE64 and HEX ecoding/decoding
var RADIOS = 2;
var PRE_HALF_APEX = Math.ceil(EXTERN_APEX / 2) - RADIOS;
var PRE_3OF4_APEX = Math.ceil((EXTERN_APEX / 4) * 3) - RADIOS;
(function() {
for (var j = 0; j < RADIOS * 2; j += 1) {
var datum = b;
var slice = datum.slice(0, PRE_HALF_APEX + j);
var slice2 = datum.slice(0, PRE_HALF_APEX + j + 2);
var pumped_string = slice.toString('hex');
var pumped_string2 = slice2.toString('hex');
var decoded = Buffer.from(pumped_string, 'hex');
// the string are the same?
for (var k = 0; k < pumped_string.length; ++k) {
assert.equal(pumped_string[k], pumped_string2[k]);
}
// the recoded buffer is the same?
for (var i = 0; i < decoded.length; ++i) {
assert.equal(datum[i], decoded[i]);
}
}
})();
(function() {
for (var j = 0; j < RADIOS * 2; j += 1) {
var datum = b;
var slice = datum.slice(0, PRE_3OF4_APEX + j);
var slice2 = datum.slice(0, PRE_3OF4_APEX + j + 2);
var pumped_string = slice.toString('base64');
var pumped_string2 = slice2.toString('base64');
var decoded = Buffer.from(pumped_string, 'base64');
// the string are the same?
for (var k = 0; k < pumped_string.length - 3; ++k) {
assert.equal(pumped_string[k], pumped_string2[k]);
}
// the recoded buffer is the same?
for (var i = 0; i < decoded.length; ++i) {
assert.equal(datum[i], decoded[i]);
}
}
})();
// https://github.com/nodejs/node/issues/1024
(function() {
var a = Array(1 << 20).join('x');
var b = Buffer.from(a, 'ucs2').toString('ucs2');
var c = Buffer.from(b, 'utf8').toString('utf8');
assert.equal(a.length, b.length);
assert.equal(b.length, c.length);
assert.equal(a, b);
assert.equal(b, c);
})();