1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16function eq(x: Object | null | undefined, y: Object | null | undefined) {
17    return x == y;
18}
19
20function box<T extends Object>(v: T): Object { return v; }
21
22// NOTE(vpukhov): char included?
23function test_numerics() {
24    let v: byte = 42;
25    const create_set = () =>
26        [box<byte>(v), box<short>(v), box<int>(v), box<long>(v),
27        box<float>(v), box<double>(v)];
28
29    let arr1 = create_set();
30    let arr2 = create_set();
31
32    for (const e1 of arr1) {
33        for (const e2 of arr2) {
34            assert(eq(e1, e2));
35        }
36    }
37}
38
39// NOTE(vpukhov): the same is applied to float?
40// NOTE(vpukhov): char == int overlow?
41function test_numeric_precision() {
42    const maxsafe = Double.MAX_SAFE_INTEGER as long;
43    assert(!eq(maxsafe * 4 + 1, maxsafe * 4));
44    assert(eq(maxsafe * 4 + 1, (maxsafe * 4) as double));
45}
46
47function main() {
48    assert(eq(null, null));
49    assert(eq(null, undefined));
50    assert(eq(undefined, null));
51    assert(eq(undefined, undefined));
52    assert(eq("abc", ((a: string, b: string): string => { return a + b })("a", "bc")));
53    assert(eq(123n, ((a: bigint, b: bigint): bigint => { return a + b })(120n, 3n)));
54    assert(eq(true, true));
55    assert(eq(box<int>(123), box<int>(123)));
56    assert(eq('a', 'a'));
57
58    assert(!eq(null, false));
59    assert(!eq(undefined, false));
60    assert(!eq(null, 0));
61    assert(!eq(true, false));
62    assert(!eq("", 0));
63    assert(!eq("", false));
64    assert(!eq("abc", "cde"));
65    assert(!eq("1", 1));
66    assert(!eq(123n, 124n));
67    assert(!eq(-123n, 123n));
68    assert(!eq('a', 'b'));
69    assert(!eq(box<int>(1), box<int>(2)));
70
71    test_numerics();
72    test_numeric_precision();
73}
74