From 9099f2eb2e800acc8f79a3dd32fbb9349526d4a2 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 10 Jun 2015 19:35:51 -0600 Subject: [PATCH] add some tests --- index.html | 8 +++++++ node-test.js | 30 ++++++++++++++++++++++++ test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 index.html create mode 100644 node-test.js create mode 100644 test.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..ea40a01 --- /dev/null +++ b/index.html @@ -0,0 +1,8 @@ + + + + + + This is for testing. Look in the console. + + diff --git a/node-test.js b/node-test.js new file mode 100644 index 0000000..878dd44 --- /dev/null +++ b/node-test.js @@ -0,0 +1,30 @@ +var str = "I ½ ♥ 𩶘"; +var buf = new Buffer(str); +var base64 = buf.toString('base64'); +var hex = buf.toString('hex'); +var bytes = Array.prototype.map.call(buf, function (byte) { + return byte; +}); + +console.log(''); +console.log('UTF-8'); +console.log(str); +console.log(base64); +console.log(hex); +console.log('[ ' + bytes.join(', ') + ' ]'); +console.log(''); + +// Array.prototype.map.call(crypto.randomBytes(8), function (b) { return b; }); +var bytes = [ 255, 226, 26, 243, 134, 206, 147, 107 ]; +buf = new Buffer(bytes); +str = buf.toString('binary'); +base64 = buf.toString('base64'); +hex = buf.toString('hex'); + +console.log(''); +console.log('binary'); +console.log(str); +console.log(base64); +console.log(hex); +console.log(bytes); +console.log(''); diff --git a/test.js b/test.js new file mode 100644 index 0000000..acb0317 --- /dev/null +++ b/test.js @@ -0,0 +1,66 @@ +(function () { +'use strict'; + +var Unibabel = window.Unibabel; + +//UTF-8 +var pass = true; +var str = "I ½ ♥ 𩶘"; +var buf = Unibabel.utf8ToBuffer(str); +var base64 = Unibabel.bufferToBase64(buf); +var hex = Unibabel.bufferToHex(buf); + +function buffersAreEqual(buf1, buf2) { + if (buf1.length !== buf2.length) { + return false; + } + return Array.prototype.every.call(buf1, function (byte, i) { + if (byte === buf2[i]) { + return true; + } + }); +} + +console.log('buffer', buf); +// TODO compare buffer +var reference = [ 73, 32, 194, 189, 32, 226, 153, 165, 32, 240, 169, 182, 152 ]; +if (!buffersAreEqual(buf, reference)) { + pass = false; + console.log('[FAIL] utf8 -> buffer', buf); +} +if (base64 !== "SSDCvSDimaUg8Km2mA==") { + pass = false; + console.log('[FAIL] utf8 -> base64', base64); +} +if (hex !== "4920c2bd20e299a520f0a9b698") { + pass = false; + console.log('[FAIL] utf8 -> hex', hex); +} + + +// binary +var bytes = [ 255, 226, 26, 243, 134, 206, 147, 107 ]; +buf = new Uint8Array(bytes); +str = Unibabel.bufferToBinaryString(buf); +base64 = Unibabel.bufferToBase64(buf); +hex = Unibabel.bufferToHex(buf); + +// This can't be properly tested because binary strings can't be parsed +// if (str !== "ÿâó†Î“k") { +// pass = false; +// console.log('[FAIL] binary -> str', str); +// } +if ("/+Ia84bOk2s=" !== base64) { + pass = false; + console.log('[FAIL] binary -> base64', base64); +} +if (hex !== "ffe21af386ce936b") { + pass = false; + console.log('[FAIL] binary -> hex', hex); +} + +if (pass) { + console.log('[PASS] :-D'); +} + +}());