blob: 21d4d58a6a8d97c328354486f7f2e3ba7de4b5ff [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 */
19
20const Int64 = require("node-int64");
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030021const JSONInt64 = require("json-int64");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030022const i64types = require("./gen-nodejs-es6/Int64Test_types.js");
23const test = require("tape");
24
25const cases = {
Cameron Martincaef0ed2025-01-15 11:58:39 +010026 "should correctly generate Int64 constants": function (assert) {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030027 const EXPECTED_SMALL_INT64_AS_NUMBER = 42;
28 const EXPECTED_SMALL_INT64 = new Int64(42);
29 const EXPECTED_MAX_JS_SAFE_INT64 = new Int64(Number.MAX_SAFE_INTEGER);
30 const EXPECTED_MIN_JS_SAFE_INT64 = new Int64(Number.MIN_SAFE_INTEGER);
31 const EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64 = new Int64("0020000000000000"); // hex-encoded
32 const EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64 = new Int64("ffe0000000000000"); // hex-encoded 2's complement
33 const EXPECTED_MAX_SIGNED_INT64 = new Int64("7fffffffffffffff"); // hex-encoded
34 const EXPECTED_MIN_SIGNED_INT64 = new Int64("8000000000000000"); // hex-encoded 2's complement
35 const EXPECTED_INT64_LIST = [
36 EXPECTED_SMALL_INT64,
37 EXPECTED_MAX_JS_SAFE_INT64,
38 EXPECTED_MIN_JS_SAFE_INT64,
39 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64,
40 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64,
41 EXPECTED_MAX_SIGNED_INT64,
Cameron Martincaef0ed2025-01-15 11:58:39 +010042 EXPECTED_MIN_SIGNED_INT64,
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030043 ];
44
45 assert.ok(EXPECTED_SMALL_INT64.equals(i64types.SMALL_INT64));
46 assert.ok(EXPECTED_MAX_JS_SAFE_INT64.equals(i64types.MAX_JS_SAFE_INT64));
47 assert.ok(EXPECTED_MIN_JS_SAFE_INT64.equals(i64types.MIN_JS_SAFE_INT64));
48 assert.ok(
49 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64.equals(
Cameron Martincaef0ed2025-01-15 11:58:39 +010050 i64types.MAX_JS_SAFE_PLUS_ONE_INT64,
51 ),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030052 );
53 assert.ok(
54 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64.equals(
Cameron Martincaef0ed2025-01-15 11:58:39 +010055 i64types.MIN_JS_SAFE_MINUS_ONE_INT64,
56 ),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030057 );
58 assert.ok(EXPECTED_MAX_SIGNED_INT64.equals(i64types.MAX_SIGNED_INT64));
59 assert.ok(EXPECTED_MIN_SIGNED_INT64.equals(i64types.MIN_SIGNED_INT64));
60 assert.equal(
61 EXPECTED_SMALL_INT64_AS_NUMBER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010062 i64types.SMALL_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030063 );
64 assert.equal(
65 Number.MAX_SAFE_INTEGER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010066 i64types.MAX_JS_SAFE_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030067 );
68 assert.equal(
69 Number.MIN_SAFE_INTEGER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010070 i64types.MIN_JS_SAFE_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030071 );
72
73 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
74 assert.ok(EXPECTED_INT64_LIST[i].equals(i64types.INT64_LIST[i]));
75 }
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030076
77 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
78 const int64Object = EXPECTED_INT64_LIST[i];
79 assert.ok(
80 i64types.INT64_2_INT64_MAP[
81 JSONInt64.toDecimalString(int64Object)
Cameron Martincaef0ed2025-01-15 11:58:39 +010082 ].equals(int64Object),
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030083 );
84 }
85
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030086 assert.end();
Cameron Martincaef0ed2025-01-15 11:58:39 +010087 },
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030088};
89
Cameron Martincaef0ed2025-01-15 11:58:39 +010090Object.keys(cases).forEach(function (caseName) {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030091 test(caseName, cases[caseName]);
92});