1/*
2 * Copyright (c) 2023 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
16declare function print(arg:any) : string;
17declare interface ArkTools {
18    timeInUs(arg:any):number
19}
20
21function testAdd() {
22    let key = "key";
23    let mySet = new Set();
24    let start = ArkTools.timeInUs();
25    for (let i = 0; i < 1_000_000; i++) {
26        mySet.add(key);
27    }
28    let end = ArkTools.timeInUs();
29    let time = (end - start) / 1000
30    print("Set Add:\t" + String(time) + "\tms");
31}
32
33function testDelete() {
34    const mySet = new Set();
35    let key = "key";
36    mySet.add(key);
37    let start = ArkTools.timeInUs();
38    for (let i = 0; i < 1_000_000; i++) {
39        mySet.delete(key);
40    }
41    let end = ArkTools.timeInUs();
42    let time = (end - start) / 1000
43    print("Set Delete:\t" + String(time) + "\tms");
44}
45
46function testHas() {
47    const mySet = new Set();
48    let key = "key";
49    mySet.add(key);
50    let start = ArkTools.timeInUs();
51    for (let i = 0; i < 1_000_000; i++) {
52        mySet.has(key);
53    }
54    let end = ArkTools.timeInUs();
55    let time = (end - start) / 1000
56    print("Set Has:\t" + String(time) + "\tms");
57}
58
59function setElements(value1, value2, set) {}
60function testForEach() {
61    const mySet = new Set();
62    let key = "key";
63    mySet.add(key);
64    let start = ArkTools.timeInUs();
65    for (let i = 0; i < 1_000_000; i++) {
66        mySet.forEach(setElements);
67    }
68    let end = ArkTools.timeInUs();
69    let time = (end - start) / 1000
70    print("Set ForEach:\t" + String(time) + "\tms");
71}
72
73function testClear() {
74    const mySet = new Set();
75    let key = "key";
76    mySet.add(key);
77    let start = ArkTools.timeInUs();
78    for (let i = 0; i < 1_000_000; i++) {
79        mySet.clear();
80    }
81    let end = ArkTools.timeInUs();
82    let time = (end - start) / 1000
83    print("Set Clear:\t" + String(time) + "\tms");
84}
85
86function testValues() {
87    const mySet = new Set();
88    let start = ArkTools.timeInUs();
89    for (let i = 0; i < 1_000_000; i++) {
90        mySet.values();
91    }
92    let end = ArkTools.timeInUs();
93    let time = (end - start) / 1000
94    print("Set Values:\t" + String(time) + "\tms");
95}
96
97function testEntries() {
98    const mySet = new Set();
99    let start = ArkTools.timeInUs();
100    for (let i = 0; i < 1_000_000; i++) {
101        mySet.entries();
102    }
103    let end = ArkTools.timeInUs();
104    let time = (end - start) / 1000
105    print("Set Entries:\t" + String(time) + "\tms");
106}
107
108testAdd();
109testDelete();
110testHas();
111testForEach();
112testClear();
113testValues();
114testEntries();
115