blob: d209234b50c2e0f44779cf4b346b52b9b625feef [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");
Mustafa Senol Cosar129bb3e2019-01-22 17:33:09 +030021import 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 = {
26 "should correctly generate Int64 constants": function(assert) {
27 const EXPECTED_SMALL_INT64_AS_NUMBER: number = 42;
28 const EXPECTED_SMALL_INT64: Int64 = new Int64(42);
29 const EXPECTED_MAX_JS_SAFE_INT64: Int64 = new Int64(Number.MAX_SAFE_INTEGER);
30 const EXPECTED_MIN_JS_SAFE_INT64: Int64 = new Int64(Number.MIN_SAFE_INTEGER);
31 const EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64: Int64 = new Int64("0020000000000000"); // hex-encoded
32 const EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64: Int64 = new Int64("ffe0000000000000"); // hex-encoded 2's complement
33 const EXPECTED_MAX_SIGNED_INT64: Int64 = new Int64("7fffffffffffffff"); // hex-encoded
34 const EXPECTED_MIN_SIGNED_INT64: Int64 = new Int64("8000000000000000"); // hex-encoded 2's complement
35 const EXPECTED_INT64_LIST: Int64[] = [
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,
42 EXPECTED_MIN_SIGNED_INT64
43 ];
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(
50 i64types.MAX_JS_SAFE_PLUS_ONE_INT64
51 )
52 );
53 assert.ok(
54 EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64.equals(
55 i64types.MIN_JS_SAFE_MINUS_ONE_INT64
56 )
57 );
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,
62 i64types.SMALL_INT64.toNumber()
63 );
64 assert.equal(
65 Number.MAX_SAFE_INTEGER,
66 i64types.MAX_JS_SAFE_INT64.toNumber()
67 );
68 assert.equal(
69 Number.MIN_SAFE_INTEGER,
70 i64types.MIN_JS_SAFE_INT64.toNumber()
71 );
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 let int64Object = EXPECTED_INT64_LIST[i];
79 assert.ok(i64types.INT64_2_INT64_MAP[JSONInt64.toDecimalString(int64Object)].equals(int64Object));
80 }
81
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030082 assert.end();
83 }
84};
85
86Object.keys(cases).forEach(function(caseName) {
87 test(caseName, cases[caseName]);
88});