aboutsummaryrefslogtreecommitdiffhomepage
path: root/js/binary/utils_test.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/binary/utils_test.js')
-rw-r--r--js/binary/utils_test.js75
1 files changed, 56 insertions, 19 deletions
diff --git a/js/binary/utils_test.js b/js/binary/utils_test.js
index 5c330791..13450644 100644
--- a/js/binary/utils_test.js
+++ b/js/binary/utils_test.js
@@ -36,6 +36,7 @@
* @author aappleby@google.com (Austin Appleby)
*/
+goog.require('goog.crypt');
goog.require('goog.crypt.base64');
goog.require('goog.testing.asserts');
goog.require('jspb.BinaryConstants');
@@ -197,6 +198,41 @@ describe('binaryUtilsTest', function() {
assertEquals('123456789123456789', result[2]);
});
+ /*
+ * Going from decimal strings to hash strings should be lossless.
+ */
+ it('testDecimalToHashConversion', function() {
+ var result;
+ var convert = jspb.utils.decimalStringToHash64;
+
+ result = convert('0');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
+
+ result = convert('-1');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
+
+ result = convert('18446744073709551615');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
+
+ result = convert('9223372036854775808');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
+
+ result = convert('-9223372036854775808');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result);
+
+ result = convert('123456789123456789');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result);
+
+ result = convert('-123456789123456789');
+ assertEquals(goog.crypt.byteArrayToString(
+ [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result);
+ });
/**
* Going from hash strings to hex strings should be lossless.
@@ -224,21 +260,21 @@ describe('binaryUtilsTest', function() {
var convert = jspb.utils.hexStringToHash64;
result = convert('0x0000000000000000');
- assertEquals(String.fromCharCode.apply(null,
+ assertEquals(goog.crypt.byteArrayToString(
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result);
result = convert('0xffffffffffffffff');
- assertEquals(String.fromCharCode.apply(null,
+ assertEquals(goog.crypt.byteArrayToString(
[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result);
// Hex string is big-endian, hash string is little-endian.
result = convert('0x123456789ABCDEF0');
- assertEquals(String.fromCharCode.apply(null,
+ assertEquals(goog.crypt.byteArrayToString(
[0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result);
// Capitalization should not matter.
result = convert('0x0000abcdefABCDEF');
- assertEquals(String.fromCharCode.apply(null,
+ assertEquals(goog.crypt.byteArrayToString(
[0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result);
});
@@ -310,7 +346,7 @@ describe('binaryUtilsTest', function() {
// NaN.
jspb.utils.splitFloat32(NaN);
if (!isNaN(jspb.utils.joinFloat32(jspb.utils.split64Low,
- jspb.utils.split64High))) {
+ jspb.utils.split64High))) {
throw 'fail!';
}
@@ -324,7 +360,7 @@ describe('binaryUtilsTest', function() {
if (opt_bits != jspb.utils.split64Low) throw 'fail!';
}
if (truncate(x) != jspb.utils.joinFloat32(jspb.utils.split64Low,
- jspb.utils.split64High)) {
+ jspb.utils.split64High)) {
throw 'fail!';
}
}
@@ -376,7 +412,7 @@ describe('binaryUtilsTest', function() {
// NaN.
jspb.utils.splitFloat64(NaN);
if (!isNaN(jspb.utils.joinFloat64(jspb.utils.split64Low,
- jspb.utils.split64High))) {
+ jspb.utils.split64High))) {
throw 'fail!';
}
@@ -394,7 +430,7 @@ describe('binaryUtilsTest', function() {
if (opt_lowBits != jspb.utils.split64Low) throw 'fail!';
}
if (x != jspb.utils.joinFloat64(jspb.utils.split64Low,
- jspb.utils.split64High)) {
+ jspb.utils.split64High)) {
throw 'fail!';
}
}
@@ -439,16 +475,20 @@ describe('binaryUtilsTest', function() {
* Tests counting packed varints.
*/
it('testCountVarints', function() {
- var writer = new jspb.BinaryWriter();
-
- var count = 0;
+ var values = [];
for (var i = 1; i < 1000000000; i *= 1.1) {
- writer.rawWriteVarint(Math.floor(i));
- count++;
+ values.push(Math.floor(i));
}
+ var writer = new jspb.BinaryWriter();
+ writer.writePackedUint64(1, values);
+
var buffer = new Uint8Array(writer.getResultBuffer());
- assertEquals(count, jspb.utils.countVarints(buffer, 0, buffer.length));
+
+ // We should have two more varints than we started with - one for the field
+ // tag, one for the packed length.
+ assertEquals(values.length + 2,
+ jspb.utils.countVarints(buffer, 0, buffer.length));
});
@@ -604,7 +644,7 @@ describe('binaryUtilsTest', function() {
var sourceBytes = new Uint8Array(sourceData);
var sourceBuffer = sourceBytes.buffer;
var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData);
- var sourceString = String.fromCharCode.apply(null, sourceData);
+ var sourceString = goog.crypt.byteArrayToString(sourceData);
function check(result) {
assertEquals(Uint8Array, result.constructor);
@@ -617,7 +657,7 @@ describe('binaryUtilsTest', function() {
// Converting Uint8Arrays into Uint8Arrays should be a no-op.
assertEquals(sourceBytes, convert(sourceBytes));
- // Converting Array.<numbers> into Uint8Arrays should work.
+ // Converting Array<numbers> into Uint8Arrays should work.
check(convert(sourceData));
// Converting ArrayBuffers into Uint8Arrays should work.
@@ -625,8 +665,5 @@ describe('binaryUtilsTest', function() {
// Converting base64-encoded strings into Uint8Arrays should work.
check(convert(sourceBase64));
-
- // Converting binary-data strings into Uint8Arrays should work.
- check(convert(sourceString, /* opt_stringIsRawBytes = */ true));
});
});