Skip to content

Commit

Permalink
test: add Buffer slice UTF-8 test
Browse files Browse the repository at this point in the history
PR-URL: nodejs#1989
Reviewed-By: Trevor Norris <[email protected]>
  • Loading branch information
Trott authored and trevnorris committed Jun 17, 2015
1 parent e56758a commit 0abcf44
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ writeTest.write('e', 3, 'ascii');
writeTest.write('j', 'ascii', 4);
assert.equal(writeTest.toString(), 'nodejs');

// ASCII slice test

var asciiString = 'hello world';
var offset = 100;

Expand All @@ -289,8 +291,25 @@ for (var i = 0; i < asciiString.length; i++) {
assert.equal(sliceA[i], sliceB[i]);
}

// TODO utf8 slice tests
// UTF-8 slice test

var utf8String = '¡hέlló wôrld!';
var offset = 100;

b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8');
var utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String));
assert.equal(utf8String, utf8Slice);

var written = b.write(utf8String, offset, 'utf8');
assert.equal(Buffer.byteLength(utf8String), written);
utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String));
assert.equal(utf8String, utf8Slice);

var sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String));
var sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String));
for (var i = 0; i < Buffer.byteLength(utf8String); i++) {
assert.equal(sliceA[i], sliceB[i]);
}

var slice = b.slice(100, 150);
assert.equal(50, slice.length);
Expand Down

0 comments on commit 0abcf44

Please sign in to comment.