blob: 10e5946fc9699541fa5c20c9701fcccfcb3c8ee0 [file] [log] [blame]
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +03001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Cameron Martincaef0ed2025-01-15 11:58:39 +010019/* jshint -W100 */
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030020
21var Int64 = require("node-int64");
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030022var JSONInt64 = require("json-int64");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030023import { Int64Test } from "./gen-js/Int64Test_types";
24
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030025// Work around for old API used by QUnitAdapter of jsTestDriver
Cameron Martincaef0ed2025-01-15 11:58:39 +010026if (typeof QUnit.log == "function") {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030027 // When using real QUnit (fron PhantomJS) log failures to console
Cameron Martincaef0ed2025-01-15 11:58:39 +010028 QUnit.log(function (details) {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030029 if (!details.result) {
Cameron Martincaef0ed2025-01-15 11:58:39 +010030 console.log("======== FAIL ========");
31 console.log("TestName: " + details.name);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030032 if (details.message) console.log(details.message);
Cameron Martincaef0ed2025-01-15 11:58:39 +010033 console.log("Expected: " + details.expected);
34 console.log("Actual : " + details.actual);
35 console.log("======================");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030036 }
37 });
38}
39
Cameron Martincaef0ed2025-01-15 11:58:39 +010040QUnit.module("Int64");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030041
Cameron Martincaef0ed2025-01-15 11:58:39 +010042QUnit.test("Int64", function (assert) {
43 console.log("Int64 test -- starts");
44 const EXPECTED_SMALL_INT64_AS_NUMBER: number = 42;
45 const EXPECTED_SMALL_INT64: typeof Int64 = new Int64(42);
46 const EXPECTED_MAX_JS_SAFE_INT64: typeof Int64 = new Int64(
47 Number.MAX_SAFE_INTEGER,
48 );
49 const EXPECTED_MIN_JS_SAFE_INT64: typeof Int64 = new Int64(
50 Number.MIN_SAFE_INTEGER,
51 );
52 const EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64: typeof Int64 = new Int64(
53 "0020000000000000",
54 ); // hex-encoded
55 const EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64: typeof Int64 = new Int64(
56 "ffe0000000000000",
57 ); // hex-encoded 2's complement
58 const EXPECTED_MAX_SIGNED_INT64: typeof Int64 = new Int64("7fffffffffffffff"); // hex-encoded
59 const EXPECTED_MIN_SIGNED_INT64: typeof Int64 = new Int64("8000000000000000"); // hex-encoded 2's complement
60 const EXPECTED_INT64_LIST = [
61 EXPECTED_SMALL_INT64,
62 EXPECTED_MAX_JS_SAFE_INT64,
63 EXPECTED_MIN_JS_SAFE_INT64,
64 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64,
65 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64,
66 EXPECTED_MAX_SIGNED_INT64,
67 EXPECTED_MIN_SIGNED_INT64,
68 ];
69 assert.ok(EXPECTED_SMALL_INT64.equals(Int64Test.SMALL_INT64));
70 assert.ok(EXPECTED_MAX_JS_SAFE_INT64.equals(Int64Test.MAX_JS_SAFE_INT64));
71 assert.ok(EXPECTED_MIN_JS_SAFE_INT64.equals(Int64Test.MIN_JS_SAFE_INT64));
72 assert.ok(
73 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64.equals(
74 Int64Test.MAX_JS_SAFE_PLUS_ONE_INT64,
75 ),
76 );
77 assert.ok(
78 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64.equals(
79 Int64Test.MIN_JS_SAFE_MINUS_ONE_INT64,
80 ),
81 );
82 assert.ok(EXPECTED_MAX_SIGNED_INT64.equals(Int64Test.MAX_SIGNED_INT64));
83 assert.ok(EXPECTED_MIN_SIGNED_INT64.equals(Int64Test.MIN_SIGNED_INT64));
84 assert.equal(
85 EXPECTED_SMALL_INT64_AS_NUMBER,
86 Int64Test.SMALL_INT64.toNumber(),
87 );
88 assert.equal(Number.MAX_SAFE_INTEGER, Int64Test.MAX_JS_SAFE_INT64.toNumber());
89 assert.equal(Number.MIN_SAFE_INTEGER, Int64Test.MIN_JS_SAFE_INT64.toNumber());
90
91 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
92 assert.ok(EXPECTED_INT64_LIST[i].equals(Int64Test.INT64_LIST[i]));
93 }
94
95 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
96 let int64Object = EXPECTED_INT64_LIST[i];
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030097 assert.ok(
Cameron Martincaef0ed2025-01-15 11:58:39 +010098 Int64Test.INT64_2_INT64_MAP[
99 JSONInt64.toDecimalString(int64Object)
100 ].equals(int64Object),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300101 );
Cameron Martincaef0ed2025-01-15 11:58:39 +0100102 }
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300103
Cameron Martincaef0ed2025-01-15 11:58:39 +0100104 console.log("Int64 test -- ends");
105});