• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2025 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            assertTrue(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.toLong(Double.MAX_SAFE_INTEGER)
43    assertTrue(!eq(maxsafe * 4 + 1, maxsafe * 4))
44    assertTrue(eq(maxsafe * 4 + 1, Long.toDouble(maxsafe * 4)))
45}
46
47function main() {
48    assertTrue(eq(null, null));
49    assertTrue(eq(null, undefined));
50    assertTrue(eq(undefined, null));
51    assertTrue(eq(undefined, undefined));
52    assertTrue(eq("abc", ((a: string, b: string): string => { return a + b })("a", "bc")));
53    assertTrue(eq(123n, ((a: bigint, b: bigint): bigint => { return a + b })(120n, 3n)));
54    assertTrue(eq(true, true));
55    assertTrue(eq(box<int>(123), box<int>(123)));
56    assertTrue(eq('a', 'a'));
57
58    assertTrue(!eq(null, false))
59    assertTrue(!eq(undefined, false))
60    assertTrue(!eq(null, 0))
61    assertTrue(!eq(true, false))
62    assertTrue(!eq("", 0))
63    assertTrue(!eq("", false))
64    assertTrue(!eq("abc", "cde"))
65    assertTrue(!eq("1", 1))
66    assertTrue(!eq(123n, 124n))
67    assertTrue(!eq(-123n, 123n))
68    assertTrue(!eq('a', 'b'))
69    assertTrue(!eq(box<int>(1), box<int>(2)))
70
71    test_numerics();
72    test_numeric_precision();
73}
74