1/*
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 /* jshint -W100 */
20
21var Int64 = require("node-int64");
22var JSONInt64 = require("json-int64");
23import { Int64Test } from "./gen-js/Int64Test_types";
24
25
26// Work around for old API used by QUnitAdapter of jsTestDriver
27if (typeof QUnit.log == 'function') {
28  // When using real QUnit (fron PhantomJS) log failures to console
29  QUnit.log(function(details) {
30    if (!details.result) {
31      console.log('======== FAIL ========');
32      console.log('TestName: ' + details.name);
33      if (details.message) console.log(details.message);
34      console.log('Expected: ' + details.expected);
35      console.log('Actual  : ' + details.actual);
36      console.log('======================');
37    }
38  });
39}
40
41QUnit.module('Int64');
42
43  QUnit.test('Int64', function(assert) {
44    console.log('Int64 test -- starts');
45    const EXPECTED_SMALL_INT64_AS_NUMBER: number = 42;
46    const EXPECTED_SMALL_INT64: typeof Int64 = new Int64(42);
47    const EXPECTED_MAX_JS_SAFE_INT64: typeof Int64 = new Int64(Number.MAX_SAFE_INTEGER);
48    const EXPECTED_MIN_JS_SAFE_INT64: typeof Int64 = new Int64(Number.MIN_SAFE_INTEGER);
49    const EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64: typeof Int64 = new Int64("0020000000000000"); // hex-encoded
50    const EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64: typeof Int64 = new Int64("ffe0000000000000"); // hex-encoded 2's complement
51    const EXPECTED_MAX_SIGNED_INT64: typeof Int64 = new Int64("7fffffffffffffff"); // hex-encoded
52    const EXPECTED_MIN_SIGNED_INT64: typeof Int64 = new Int64("8000000000000000"); // hex-encoded 2's complement
53    const EXPECTED_INT64_LIST = [
54      EXPECTED_SMALL_INT64,
55      EXPECTED_MAX_JS_SAFE_INT64,
56      EXPECTED_MIN_JS_SAFE_INT64,
57      EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64,
58      EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64,
59      EXPECTED_MAX_SIGNED_INT64,
60      EXPECTED_MIN_SIGNED_INT64
61    ];
62    assert.ok(EXPECTED_SMALL_INT64.equals(Int64Test.SMALL_INT64));
63    assert.ok(EXPECTED_MAX_JS_SAFE_INT64.equals(Int64Test.MAX_JS_SAFE_INT64));
64    assert.ok(EXPECTED_MIN_JS_SAFE_INT64.equals(Int64Test.MIN_JS_SAFE_INT64));
65    assert.ok(
66      EXPECTED_MAX_JS_SAFE_PLUS_ONE_INT64.equals(
67        Int64Test.MAX_JS_SAFE_PLUS_ONE_INT64
68      )
69    );
70    assert.ok(
71      EXPECTED_MIN_JS_SAFE_MINUS_ONE_INT64.equals(
72        Int64Test.MIN_JS_SAFE_MINUS_ONE_INT64
73      )
74    );
75    assert.ok(EXPECTED_MAX_SIGNED_INT64.equals(Int64Test.MAX_SIGNED_INT64));
76    assert.ok(EXPECTED_MIN_SIGNED_INT64.equals(Int64Test.MIN_SIGNED_INT64));
77    assert.equal(
78      EXPECTED_SMALL_INT64_AS_NUMBER,
79      Int64Test.SMALL_INT64.toNumber()
80    );
81    assert.equal(
82      Number.MAX_SAFE_INTEGER,
83      Int64Test.MAX_JS_SAFE_INT64.toNumber()
84    );
85    assert.equal(
86      Number.MIN_SAFE_INTEGER,
87      Int64Test.MIN_JS_SAFE_INT64.toNumber()
88    );
89
90    for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i) {
91      assert.ok(EXPECTED_INT64_LIST[i].equals(Int64Test.INT64_LIST[i]));
92    }
93
94    for (let i = 0; i < EXPECTED_INT64_LIST.length; ++i){
95      let int64Object = EXPECTED_INT64_LIST[i];
96      assert.ok(Int64Test.INT64_2_INT64_MAP[JSONInt64.toDecimalString(int64Object)].equals(int64Object));
97    }
98
99    console.log('Int64 test -- ends');
100  });
101
102