14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci/*
174514f5e3Sopenharmony_ci * @tc.name:sendableset
184514f5e3Sopenharmony_ci * @tc.desc:test sendableset
194514f5e3Sopenharmony_ci * @tc.type: FUNC
204514f5e3Sopenharmony_ci * @tc.require: issue#I93TZC
214514f5e3Sopenharmony_ci */
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_ci// @ts-nocheck
244514f5e3Sopenharmony_cideclare function print(str: any): string;
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cifunction FillSet(set: SendableSet): void {
274514f5e3Sopenharmony_ci  for (let i = 0; i < 5; i++) {
284514f5e3Sopenharmony_ci    set.add(i);
294514f5e3Sopenharmony_ci  }
304514f5e3Sopenharmony_ci}
314514f5e3Sopenharmony_cilet sharedSet: SendableSet = new SendableSet<number>();
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci// Basic tests
344514f5e3Sopenharmony_ciprint("===Basic test begin===")
354514f5e3Sopenharmony_ciFillSet(sharedSet);
364514f5e3Sopenharmony_ciprint("set size is " + sharedSet.size);
374514f5e3Sopenharmony_ciprint(SendableSet[Symbol.species] == SendableSet);
384514f5e3Sopenharmony_ciprint(SendableSet.name == 'SendableSet');
394514f5e3Sopenharmony_ciprint(SendableSet[Symbol.species] == Set);
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ciconst keyIter = sharedSet.keys();
424514f5e3Sopenharmony_cilet nextEntry = keyIter.next();
434514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
444514f5e3Sopenharmony_cinextEntry = keyIter.next();
454514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
464514f5e3Sopenharmony_cinextEntry = keyIter.next();
474514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
484514f5e3Sopenharmony_cinextEntry = keyIter.next();
494514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
504514f5e3Sopenharmony_cinextEntry = keyIter.next();
514514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
524514f5e3Sopenharmony_cinextEntry = keyIter.next();
534514f5e3Sopenharmony_ciprint("keys next:" + nextEntry.value + ", done: " + nextEntry.done);
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ciconst valueIter = sharedSet.keys();
564514f5e3Sopenharmony_cinextEntry = valueIter.next();
574514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
584514f5e3Sopenharmony_cinextEntry = valueIter.next();
594514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
604514f5e3Sopenharmony_cinextEntry = valueIter.next();
614514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
624514f5e3Sopenharmony_cinextEntry = valueIter.next();
634514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
644514f5e3Sopenharmony_cinextEntry = valueIter.next();
654514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
664514f5e3Sopenharmony_cinextEntry = valueIter.next();
674514f5e3Sopenharmony_ciprint("values next:" + nextEntry.value + ", done: " + nextEntry.done);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_cisharedSet.forEach((key: number, value: number, set: SendableSet) => {
704514f5e3Sopenharmony_ci  print('set key[forEach]:' + 'key:' + key + ', value:' + value);
714514f5e3Sopenharmony_ci});
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ciprint(sharedSet[Symbol.toStringTag] == 'SendableSet');
744514f5e3Sopenharmony_cifor (let iter of sharedSet[Symbol.iterator]()) {
754514f5e3Sopenharmony_ci  print("set key[Symbol.iterator]:" + iter);
764514f5e3Sopenharmony_ci}
774514f5e3Sopenharmony_ciprint(sharedSet[Symbol.iterator] == sharedSet.values);
784514f5e3Sopenharmony_ciprint(sharedSet[Symbol.iterator] == sharedSet.keys);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ciprint(sharedSet.has(4));
814514f5e3Sopenharmony_cisharedSet.add(4);
824514f5e3Sopenharmony_ciprint(sharedSet.size == 5);
834514f5e3Sopenharmony_ciprint(sharedSet.has(10));
844514f5e3Sopenharmony_cisharedSet.add(10);
854514f5e3Sopenharmony_ciprint(sharedSet.size == 6);
864514f5e3Sopenharmony_ciprint(sharedSet.has(10));
874514f5e3Sopenharmony_cisharedSet.delete(10);
884514f5e3Sopenharmony_ciprint(sharedSet.has(10));
894514f5e3Sopenharmony_ciprint(sharedSet.size == 5);
904514f5e3Sopenharmony_cisharedSet.clear();
914514f5e3Sopenharmony_ciprint(sharedSet.size == 0);
924514f5e3Sopenharmony_citry {
934514f5e3Sopenharmony_ci  sharedSet["extension"] = "value";
944514f5e3Sopenharmony_ci} catch(e) {
954514f5e3Sopenharmony_ci  print("add extension(.): " + e);
964514f5e3Sopenharmony_ci}
974514f5e3Sopenharmony_citry {
984514f5e3Sopenharmony_ci  sharedSet.extension = "value";
994514f5e3Sopenharmony_ci} catch(e) {
1004514f5e3Sopenharmony_ci  print("add extension([]): " + e);
1014514f5e3Sopenharmony_ci}
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ciprint("===Basic test end===");
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ci// No Expected Concurrent modification exception while iterating using iterators
1064514f5e3Sopenharmony_ciprint("===Concurrent modification during iteration Test(iterator) begin===")
1074514f5e3Sopenharmony_cisharedSet.clear();
1084514f5e3Sopenharmony_ciFillSet(sharedSet);
1094514f5e3Sopenharmony_ciprint("set size is " + sharedSet.size);
1104514f5e3Sopenharmony_ci
1114514f5e3Sopenharmony_ciconst iterator = sharedSet.entries();
1124514f5e3Sopenharmony_cifor (const [key, _] of iterator) {
1134514f5e3Sopenharmony_ci  print("set key[for-of]: " + key);
1144514f5e3Sopenharmony_ci}
1154514f5e3Sopenharmony_citry {
1164514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1174514f5e3Sopenharmony_ci  for (const [key, _] of iterator) {
1184514f5e3Sopenharmony_ci    if (key == 1) {
1194514f5e3Sopenharmony_ci      sharedSet.add(key + 5);
1204514f5e3Sopenharmony_ci    }
1214514f5e3Sopenharmony_ci  }
1224514f5e3Sopenharmony_ci  print("Add Scenario[for-of] updated size: " + sharedSet.size);
1234514f5e3Sopenharmony_ci} catch (e) {
1244514f5e3Sopenharmony_ci  print("Add Scenario[for-of]: " + e);
1254514f5e3Sopenharmony_ci}
1264514f5e3Sopenharmony_citry {
1274514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1284514f5e3Sopenharmony_ci  for (const [key, _] of iterator) {
1294514f5e3Sopenharmony_ci    if (key % 2 == 0) {
1304514f5e3Sopenharmony_ci      sharedSet.delete(key);
1314514f5e3Sopenharmony_ci    }
1324514f5e3Sopenharmony_ci  }
1334514f5e3Sopenharmony_ci  print("Delete Scenario[for-of] updated size: " + sharedSet.size);
1344514f5e3Sopenharmony_ci} catch (e) {
1354514f5e3Sopenharmony_ci  print("Delete Scenario[for-of]: " + e);
1364514f5e3Sopenharmony_ci}
1374514f5e3Sopenharmony_citry {
1384514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1394514f5e3Sopenharmony_ci  for (const [key, _] of iterator) {
1404514f5e3Sopenharmony_ci    sharedSet.clear();
1414514f5e3Sopenharmony_ci  }
1424514f5e3Sopenharmony_ci  print("Clear Scenario[for-of] updated size: " + sharedSet.size);
1434514f5e3Sopenharmony_ci} catch (e) {
1444514f5e3Sopenharmony_ci  print("Clear Scenario[for-of]: " + e);
1454514f5e3Sopenharmony_ci}
1464514f5e3Sopenharmony_ci
1474514f5e3Sopenharmony_cisharedSet.clear();
1484514f5e3Sopenharmony_ciFillSet(sharedSet);
1494514f5e3Sopenharmony_ciprint("set size is " + sharedSet.size);
1504514f5e3Sopenharmony_citry {
1514514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1524514f5e3Sopenharmony_ci  sharedSet.add(6);
1534514f5e3Sopenharmony_ci  iterator.next();
1544514f5e3Sopenharmony_ci  print("Add Scenario[next()] updated size: " + sharedSet.size);
1554514f5e3Sopenharmony_ci} catch (e) {
1564514f5e3Sopenharmony_ci  print("Add Scenario[next()]: " + e);
1574514f5e3Sopenharmony_ci}
1584514f5e3Sopenharmony_citry {
1594514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1604514f5e3Sopenharmony_ci  sharedSet.delete(6);
1614514f5e3Sopenharmony_ci  iterator.next();
1624514f5e3Sopenharmony_ci  print("Delete Scenario[next()] updated size: " + sharedSet.size);
1634514f5e3Sopenharmony_ci} catch (e) {
1644514f5e3Sopenharmony_ci  print("Delete Scenario[next()]: " + e);
1654514f5e3Sopenharmony_ci}
1664514f5e3Sopenharmony_citry {
1674514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
1684514f5e3Sopenharmony_ci  sharedSet.clear();
1694514f5e3Sopenharmony_ci  iterator.next();
1704514f5e3Sopenharmony_ci  print("Clear Scenario[next()] updated size: " + sharedSet.size);
1714514f5e3Sopenharmony_ci} catch (e) {
1724514f5e3Sopenharmony_ci  print("Clear Scenario[next()]: " + e);
1734514f5e3Sopenharmony_ci}
1744514f5e3Sopenharmony_ciprint("===Concurrent modification during iteration Test(iterator) end===")
1754514f5e3Sopenharmony_ci
1764514f5e3Sopenharmony_ci// Expected Concurrent modification exception while iterating using forEach
1774514f5e3Sopenharmony_ciprint("===Concurrent modification during iteration Test(forEach) begin===")
1784514f5e3Sopenharmony_cisharedSet.clear();
1794514f5e3Sopenharmony_ciFillSet(sharedSet);
1804514f5e3Sopenharmony_ciprint("set size is " + sharedSet.size);
1814514f5e3Sopenharmony_cisharedSet.forEach((key: number, _: number, set: SendableSet) => {
1824514f5e3Sopenharmony_ci  print('set key[forEach]: ' + key);
1834514f5e3Sopenharmony_ci});
1844514f5e3Sopenharmony_citry {
1854514f5e3Sopenharmony_ci  sharedSet.forEach((key: number, _: number, set: SendableSet) => {
1864514f5e3Sopenharmony_ci    set.add(key + 5);
1874514f5e3Sopenharmony_ci  });
1884514f5e3Sopenharmony_ci} catch (e) {
1894514f5e3Sopenharmony_ci  print("Add Scenario[forEach]: " + e + ", errCode: " + e.code);
1904514f5e3Sopenharmony_ci}
1914514f5e3Sopenharmony_citry {
1924514f5e3Sopenharmony_ci  sharedSet.forEach((key: number, _: number, set: SendableSet) => {
1934514f5e3Sopenharmony_ci    if (key % 2 == 0) {
1944514f5e3Sopenharmony_ci      set.delete(key);
1954514f5e3Sopenharmony_ci    }
1964514f5e3Sopenharmony_ci  });
1974514f5e3Sopenharmony_ci} catch (e) {
1984514f5e3Sopenharmony_ci  print("Delete Scenario[forEach]: " + e + ", errCode: " + e.code);
1994514f5e3Sopenharmony_ci}
2004514f5e3Sopenharmony_citry {
2014514f5e3Sopenharmony_ci  sharedSet.forEach((key: number, _: number, set: SendableSet) => {
2024514f5e3Sopenharmony_ci    set.clear();
2034514f5e3Sopenharmony_ci  });
2044514f5e3Sopenharmony_ci} catch (e) {
2054514f5e3Sopenharmony_ci  print("Clear Scenario[forEach]: " + e + ", errCode: " + e.code);
2064514f5e3Sopenharmony_ci}
2074514f5e3Sopenharmony_ciprint("===Concurrent modification during iteration Test(forEach) end===");
2084514f5e3Sopenharmony_ci
2094514f5e3Sopenharmony_ciprint("===Type check begin===");
2104514f5e3Sopenharmony_ciclass SObject {
2114514f5e3Sopenharmony_ci  constructor() {
2124514f5e3Sopenharmony_ci    "use sendable"
2134514f5e3Sopenharmony_ci  }
2144514f5e3Sopenharmony_ci};
2154514f5e3Sopenharmony_ci
2164514f5e3Sopenharmony_citry {
2174514f5e3Sopenharmony_ci  let sObj = new SObject();
2184514f5e3Sopenharmony_ci  sharedSet = new SendableSet(['str', 1, sObj, undefined, true, null]);
2194514f5e3Sopenharmony_ci  print("sharedSet add[shared] element success");
2204514f5e3Sopenharmony_ci} catch (e) {
2214514f5e3Sopenharmony_ci  print("sharedSet add[unshared]: " + e + ", errCode: " + e.code);
2224514f5e3Sopenharmony_ci}
2234514f5e3Sopenharmony_ci
2244514f5e3Sopenharmony_citry {
2254514f5e3Sopenharmony_ci  let obj = {}
2264514f5e3Sopenharmony_ci  sharedSet = new SendableSet([obj]);
2274514f5e3Sopenharmony_ci} catch (e) {
2284514f5e3Sopenharmony_ci  print("sharedSet add[unshared]: " + e + ", errCode: " + e.code);
2294514f5e3Sopenharmony_ci}
2304514f5e3Sopenharmony_ci
2314514f5e3Sopenharmony_citry {
2324514f5e3Sopenharmony_ci  let sym = Symbol("testSymbol")
2334514f5e3Sopenharmony_ci  sharedSet = new SendableSet([sym, 2]);
2344514f5e3Sopenharmony_ci} catch (e) {
2354514f5e3Sopenharmony_ci  print("sharedSet add[unshared]: " + e + ", errCode: " + e.code);
2364514f5e3Sopenharmony_ci}
2374514f5e3Sopenharmony_ciprint("===Type check end===");
2384514f5e3Sopenharmony_ci
2394514f5e3Sopenharmony_ciprint("===Class inheritance test begin ===");
2404514f5e3Sopenharmony_ciclass SubSendableSet<T> extends SendableSet {
2414514f5e3Sopenharmony_ci  desc: string = "I'am SubSendableSet";
2424514f5e3Sopenharmony_ci  constructor(entries?: T[] | null) {
2434514f5e3Sopenharmony_ci    'use sendable';
2444514f5e3Sopenharmony_ci    super(entries);
2454514f5e3Sopenharmony_ci  }
2464514f5e3Sopenharmony_ci}
2474514f5e3Sopenharmony_ci
2484514f5e3Sopenharmony_cilet subSharedset = new SubSendableSet<number>();
2494514f5e3Sopenharmony_cisubSharedset.add(1);
2504514f5e3Sopenharmony_ciprint(subSharedset.has(1));
2514514f5e3Sopenharmony_ciprint(subSharedset.size);
2524514f5e3Sopenharmony_ci
2534514f5e3Sopenharmony_cisubSharedset = new SubSendableSet<number>([1, 2, 3]);
2544514f5e3Sopenharmony_ciprint(subSharedset.has(1));
2554514f5e3Sopenharmony_ciprint(subSharedset.has(2));
2564514f5e3Sopenharmony_ciprint(subSharedset.has(3));
2574514f5e3Sopenharmony_ciprint(subSharedset.size);
2584514f5e3Sopenharmony_ci
2594514f5e3Sopenharmony_citry {
2604514f5e3Sopenharmony_ci  let obj = {};
2614514f5e3Sopenharmony_ci  subSharedset = new SubSendableSet<Object>([obj]);
2624514f5e3Sopenharmony_ci  print(subSharedset.size);
2634514f5e3Sopenharmony_ci} catch (e) {
2644514f5e3Sopenharmony_ci  print('SubSendableSet add[unshared]: ' + e + ', errCode: ' + e.code);
2654514f5e3Sopenharmony_ci}
2664514f5e3Sopenharmony_ci
2674514f5e3Sopenharmony_cisubSharedset = new SubSendableSet<string>(['one', 'two', 'three']);
2684514f5e3Sopenharmony_cifor (const [key, _] of subSharedset.entries()) {
2694514f5e3Sopenharmony_ci  print('SubSendableSet key[for-of]: ' + key);
2704514f5e3Sopenharmony_ci}
2714514f5e3Sopenharmony_ci
2724514f5e3Sopenharmony_citry {
2734514f5e3Sopenharmony_ci  subSharedset = new SubSendableSet<number>([1, 2, 3, 4]);
2744514f5e3Sopenharmony_ci  print(subSharedset.size);
2754514f5e3Sopenharmony_ci  subSharedset.forEach((key: number, _: number, set: SubSendableSet) => {
2764514f5e3Sopenharmony_ci    if (key % 2 == 0) {
2774514f5e3Sopenharmony_ci      set.delete(key);
2784514f5e3Sopenharmony_ci    }
2794514f5e3Sopenharmony_ci  });
2804514f5e3Sopenharmony_ci} catch (e) {
2814514f5e3Sopenharmony_ci  print('SubSendableSet Delete Scenario[forEach]: ' + e + ', errCode: ' + e.code);
2824514f5e3Sopenharmony_ci}
2834514f5e3Sopenharmony_ci
2844514f5e3Sopenharmony_ciclass SubSubSendableSet<T> extends SubSendableSet {
2854514f5e3Sopenharmony_ci  constructor(entries?: T[] | null) {
2864514f5e3Sopenharmony_ci    'use sendable';
2874514f5e3Sopenharmony_ci    super(entries);
2884514f5e3Sopenharmony_ci  }
2894514f5e3Sopenharmony_ci}
2904514f5e3Sopenharmony_ci
2914514f5e3Sopenharmony_cilet subSubSendableSet = new SubSubSendableSet<number>([1, 2, 3]);
2924514f5e3Sopenharmony_ciprint(subSubSendableSet.has(1));
2934514f5e3Sopenharmony_ciprint(subSubSendableSet.has(2));
2944514f5e3Sopenharmony_ciprint(subSubSendableSet.has(3));
2954514f5e3Sopenharmony_ciprint(subSubSendableSet.size);
2964514f5e3Sopenharmony_ci
2974514f5e3Sopenharmony_citry {
2984514f5e3Sopenharmony_ci  subSubSendableSet['extension'] = 'value';
2994514f5e3Sopenharmony_ci} catch(e) {
3004514f5e3Sopenharmony_ci  print("add extension(.): " + e);
3014514f5e3Sopenharmony_ci}
3024514f5e3Sopenharmony_citry {
3034514f5e3Sopenharmony_ci  subSubSendableSet.extension = 'value';
3044514f5e3Sopenharmony_ci} catch(e) {
3054514f5e3Sopenharmony_ci  print("add extension([]): " + e);
3064514f5e3Sopenharmony_ci}
3074514f5e3Sopenharmony_ci
3084514f5e3Sopenharmony_citry {
3094514f5e3Sopenharmony_ci  let obj = {};
3104514f5e3Sopenharmony_ci  subSubSendableSet = new SubSubSendableSet<Object>([obj]);
3114514f5e3Sopenharmony_ci  print(subSubSendableSet.size);
3124514f5e3Sopenharmony_ci} catch (e) {
3134514f5e3Sopenharmony_ci  print('SubSubSendableSet add[unshared]: ' + e + ', errCode: ' + e.code);
3144514f5e3Sopenharmony_ci}
3154514f5e3Sopenharmony_ci
3164514f5e3Sopenharmony_citry {
3174514f5e3Sopenharmony_ci  subSubSendableSet = new SubSubSendableSet<number>([1, 2, 3, 4]);
3184514f5e3Sopenharmony_ci  subSubSendableSet.forEach((key: number, _: number, set: SubSubSendableSet) => {
3194514f5e3Sopenharmony_ci    if (key % 2 == 0) {
3204514f5e3Sopenharmony_ci      set.delete(key);
3214514f5e3Sopenharmony_ci    }
3224514f5e3Sopenharmony_ci  });
3234514f5e3Sopenharmony_ci} catch (e) {
3244514f5e3Sopenharmony_ci  print('SubSubSendableSet Delete Scenario[forEach]: ' + e + ', errCode: ' + e.code);
3254514f5e3Sopenharmony_ci}
3264514f5e3Sopenharmony_ci
3274514f5e3Sopenharmony_ciprint("=== An iterable object to convert to an ArkTS Set  begin===")
3284514f5e3Sopenharmony_cisharedSet.clear();
3294514f5e3Sopenharmony_ciFillSet(sharedSet);
3304514f5e3Sopenharmony_citry {
3314514f5e3Sopenharmony_ci  
3324514f5e3Sopenharmony_ci  const iterator = sharedSet.entries();
3334514f5e3Sopenharmony_ci  let sharedSet1: SendableSet = new SendableSet<>(iterator);
3344514f5e3Sopenharmony_ci  sharedSet1.forEach((key: number, _: number, set: SendableSet) => {
3354514f5e3Sopenharmony_ci    print("set key[forEach]: " + key);
3364514f5e3Sopenharmony_ci  })
3374514f5e3Sopenharmony_ci} catch (e) {
3384514f5e3Sopenharmony_ci  print("SendableSetConstructor Scenario[next()]: " + e);
3394514f5e3Sopenharmony_ci}
3404514f5e3Sopenharmony_ci
3414514f5e3Sopenharmony_ciprint("===Class inheritance test end ===");
342