From 0abcf44d6b1a31250272eb4a4e24192eeba53db1 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 15 Jun 2015 22:02:23 -0700 Subject: [PATCH] test: add Buffer slice UTF-8 test PR-URL: https://github.com/nodejs/io.js/pull/1989 Reviewed-By: Trevor Norris --- test/parallel/test-buffer.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 502dcf43a70f91..02d902b199e23d 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -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; @@ -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);