blob: bb0345851e1b21cfb9fc88159c39dd21e7ca8b36 [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
20import Int64 = require("node-int64");
Cameron Martincaef0ed2025-01-15 11:58:39 +010021import JSONInt64 = require("json-int64");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030022import i64types = require("./gen-nodejs/Int64Test_types");
23import 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: number = 42;
28 const EXPECTED_SMALL_INT64: Int64 = new Int64(42);
Cameron Martincaef0ed2025-01-15 11:58:39 +010029 const EXPECTED_MAX_JS_SAFE_INT64: Int64 = new Int64(
30 Number.MAX_SAFE_INTEGER,
31 );
32 const EXPECTED_MIN_JS_SAFE_INT64: Int64 = new Int64(
33 Number.MIN_SAFE_INTEGER,
34 );
35 const EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64: Int64 = new Int64(
36 "0020000000000000",
37 ); // hex-encoded
38 const EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64: Int64 = new Int64(
39 "ffe0000000000000",
40 ); // hex-encoded 2's complement
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030041 const EXPECTED_MAX_SIGNED_INT64: Int64 = new Int64("7fffffffffffffff"); // hex-encoded
42 const EXPECTED_MIN_SIGNED_INT64: Int64 = new Int64("8000000000000000"); // hex-encoded 2's complement
43 const EXPECTED_INT64_LIST: Int64[] = [
44 EXPECTED_SMALL_INT64,
45 EXPECTED_MAX_JS_SAFE_INT64,
46 EXPECTED_MIN_JS_SAFE_INT64,
47 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64,
48 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64,
49 EXPECTED_MAX_SIGNED_INT64,
Cameron Martincaef0ed2025-01-15 11:58:39 +010050 EXPECTED_MIN_SIGNED_INT64,
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030051 ];
52
53 assert.ok(EXPECTED_SMALL_INT64.equals(i64types.SMALL_INT64));
54 assert.ok(EXPECTED_MAX_JS_SAFE_INT64.equals(i64types.MAX_JS_SAFE_INT64));
55 assert.ok(EXPECTED_MIN_JS_SAFE_INT64.equals(i64types.MIN_JS_SAFE_INT64));
56 assert.ok(
57 EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64.equals(
Cameron Martincaef0ed2025-01-15 11:58:39 +010058 i64types.MAX_JS_SAFE_PLUS_ONE_INT64,
59 ),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030060 );
61 assert.ok(
62 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64.equals(
Cameron Martincaef0ed2025-01-15 11:58:39 +010063 i64types.MIN_JS_SAFE_MINUS_ONE_INT64,
64 ),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030065 );
66 assert.ok(EXPECTED_MAX_SIGNED_INT64.equals(i64types.MAX_SIGNED_INT64));
67 assert.ok(EXPECTED_MIN_SIGNED_INT64.equals(i64types.MIN_SIGNED_INT64));
68 assert.equal(
69 EXPECTED_SMALL_INT64_AS_NUMBER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010070 i64types.SMALL_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030071 );
72 assert.equal(
73 Number.MAX_SAFE_INTEGER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010074 i64types.MAX_JS_SAFE_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030075 );
76 assert.equal(
77 Number.MIN_SAFE_INTEGER,
Cameron Martincaef0ed2025-01-15 11:58:39 +010078 i64types.MIN_JS_SAFE_INT64.toNumber(),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030079 );
80
81 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
82 assert.ok(EXPECTED_INT64_LIST[i].equals(i64types.INT64_LIST[i]));
83 }
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030084
Cameron Martincaef0ed2025-01-15 11:58:39 +010085 for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030086 let int64Object = EXPECTED_INT64_LIST[i];
Cameron Martincaef0ed2025-01-15 11:58:39 +010087 assert.ok(
88 i64types.INT64_2_INT64_MAP[
89 JSONInt64.toDecimalString(int64Object)
90 ].equals(int64Object),
91 );
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030092 }
93
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030094 assert.end();
Cameron Martincaef0ed2025-01-15 11:58:39 +010095 },
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030096};
97
Cameron Martincaef0ed2025-01-15 11:58:39 +010098Object.keys(cases).forEach(function (caseName) {
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030099 test(caseName, cases[caseName]);
100});