THRIFT-4675: Generate Int64 constants for js
diff --git a/lib/js/test/test.js b/lib/js/test/test.js
index a86a509..35ed6ff 100755
--- a/lib/js/test/test.js
+++ b/lib/js/test/test.js
@@ -51,6 +51,9 @@
const protocol = new Thrift.Protocol(transport);
const client = new ThriftTest.ThriftTestClient(protocol);
+const int64_2_pow_60 = new Int64('1000000000000000');
+const int64_minus_2_pow_60 = new Int64('f000000000000000');
+
// Work around for old API used by QUnitAdapter of jsTestDriver
if (typeof QUnit.log == 'function') {
// When using real QUnit (fron PhantomJS) log failures to console
@@ -132,9 +135,12 @@
});
QUnit.test('I64', function(assert) {
assert.equal(client.testI64(0), 0);
- //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
- assert.equal(client.testI64(Math.pow(2, 52)), Math.pow(2, 52));
- assert.equal(client.testI64(-Math.pow(2, 52)), -Math.pow(2, 52));
+
+ let int64_2_pow_60_result = client.testI64(int64_2_pow_60);
+ assert.ok(int64_2_pow_60.equals(int64_2_pow_60_result));
+
+ let int64_minus_2_pow_60_result = client.testI64(int64_minus_2_pow_60);
+ assert.ok(int64_minus_2_pow_60.equals(int64_minus_2_pow_60_result));
});
@@ -145,15 +151,14 @@
structTestInput.string_thing = 'worked';
structTestInput.byte_thing = 0x01;
structTestInput.i32_thing = Math.pow(2, 30);
- //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
- structTestInput.i64_thing = Math.pow(2, 52);
+ structTestInput.i64_thing = int64_2_pow_60;
const structTestOutput = client.testStruct(structTestInput);
assert.equal(structTestOutput.string_thing, structTestInput.string_thing);
assert.equal(structTestOutput.byte_thing, structTestInput.byte_thing);
assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing);
- assert.equal(structTestOutput.i64_thing, structTestInput.i64_thing);
+ assert.ok(structTestOutput.i64_thing.equals(structTestInput.i64_thing));
assert.equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
});
@@ -163,8 +168,7 @@
xtrTestInput.string_thing = 'worked';
xtrTestInput.byte_thing = 0x01;
xtrTestInput.i32_thing = Math.pow(2, 30);
- //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
- xtrTestInput.i64_thing = Math.pow(2, 52);
+ xtrTestInput.i64_thing = int64_2_pow_60;
const nestTestInput = new ThriftTest.Xtruct2();
nestTestInput.byte_thing = 0x02;
@@ -177,7 +181,7 @@
assert.equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
assert.equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
assert.equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
- assert.equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
+ assert.ok(nestTestOutput.struct_thing.i64_thing.equals(nestTestInput.struct_thing.i64_thing));
assert.equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
assert.equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
@@ -252,7 +256,7 @@
structTestInput.string_thing = 'worked';
structTestInput.byte_thing = 0x01;
structTestInput.i32_thing = Math.pow(2, 30);
- structTestInput.i64_thing = Math.pow(2, 52);
+ structTestInput.i64_thing = int64_2_pow_60;
const structTestOutput = modifiedClient.testStruct(structTestInput);
@@ -260,7 +264,7 @@
assert.equal(structTestOutput.string_thing, structTestInput.string_thing);
assert.equal(structTestOutput.changed, null);
assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing);
- assert.equal(structTestOutput.i64_thing, structTestInput.i64_thing);
+ assert.ok(structTestOutput.i64_thing.equals(structTestInput.i64_thing));
});
@@ -332,13 +336,13 @@
'string_thing': 'Goodbye4',
'byte_thing': 4,
'i32_thing': 4,
- 'i64_thing': 4
+ 'i64_thing': new Int64(4)
},
{
'string_thing': 'Hello2',
'byte_thing': 2,
'i32_thing': 2,
- 'i64_thing': 2
+ 'i64_thing': new Int64(2)
}]
};
QUnit.test('testInsanity', function(assert) {
@@ -401,15 +405,13 @@
assert.expect(2);
const done = assert.async(2);
- //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
- client.testI64(Math.pow(2, 52), function(result) {
- assert.equal(result, Math.pow(2, 52));
+ client.testI64(int64_2_pow_60, function(result) {
+ assert.ok(int64_2_pow_60.equals(result));
done();
});
- //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
- client.testI64(Math.pow(-2, 52), function(result) {
- assert.equal(result, Math.pow(-2, 52));
+ client.testI64(int64_minus_2_pow_60, function(result) {
+ assert.ok(int64_minus_2_pow_60.equals(result));
done();
});
});