From dfb354df5c1226b74e2362e16581192a14022c60 Mon Sep 17 00:00:00 2001 From: lijincheng Date: Tue, 15 Jul 2025 10:57:45 +0800 Subject: [PATCH] Add new constructor for Set/Map 1.Add new constructor for Set/Map Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICLMSR Signed-off-by: lijincheng --- .../plugins/ets/stdlib/escompat/Map.ets | 5 ++ .../plugins/ets/stdlib/escompat/Set.ets | 5 ++ .../escompat/MapSetBucketTest.ets | 66 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 static_core/plugins/ets/tests/ets_func_tests/escompat/MapSetBucketTest.ets diff --git a/static_core/plugins/ets/stdlib/escompat/Map.ets b/static_core/plugins/ets/stdlib/escompat/Map.ets index 859729706c..507760201e 100644 --- a/static_core/plugins/ets/stdlib/escompat/Map.ets +++ b/static_core/plugins/ets/stdlib/escompat/Map.ets @@ -230,6 +230,11 @@ export class Map implements ReadonlyMap { } } + // NOTE: This API may be removed someday due #28030 internal issue + constructor(bucketsCount: int) { + this.buckets = new MapBucket[bucketsCount] + } + override toString(): String { const strBuf = new StringBuilder() diff --git a/static_core/plugins/ets/stdlib/escompat/Set.ets b/static_core/plugins/ets/stdlib/escompat/Set.ets index dee9f29905..ce1f234cb7 100644 --- a/static_core/plugins/ets/stdlib/escompat/Set.ets +++ b/static_core/plugins/ets/stdlib/escompat/Set.ets @@ -103,6 +103,11 @@ export class Set implements ReadonlySet { } } + // NOTE: This API may be removed someday due #28030 internal issue + constructor(bucketsCount: int) { + this.elements = new Map(bucketsCount); + } + private toStringVals(): String { const strBuf = new StringBuilder() diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/MapSetBucketTest.ets b/static_core/plugins/ets/tests/ets_func_tests/escompat/MapSetBucketTest.ets new file mode 100644 index 0000000000..bb9d19a9a8 --- /dev/null +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/MapSetBucketTest.ets @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class TestClassA { +} + +class TestClassB { + b1: number + b2: string + b3: TestClassA = new TestClassA() + b4: boolean + b5: int + constructor(b: int) { + this.b1 = b + 1.1 + this.b2 = "abc" + b + this.b4 = (b === 0) + this.b5 = b + } +} + +function testSetInitialBuckets(): void { + let t1 = new TestClassB(1) + let t2 = new TestClassB(2) + let t3 = new TestClassB(3) + let set1 = new Set(16) + set1.add(t1) + set1.add(t2) + set1.add(t3) + arktest.assertTrue(set1.size === 3) + arktest.assertTrue(set1.has(t1) === true) + arktest.assertTrue(set1.has(t2) === true) + arktest.assertTrue(set1.has(t3) === true) +} + +function testMapInitialBuckets(): void { + let map1: Map = new Map(16) + let m1 = new TestClassB(1) + let m2 = new TestClassB(2) + let m3 = new TestClassB(3) + map1.set("m1", m1) + map1.set("m2", m2) + map1.set("m3", m3) + arktest.assertTrue(map1.size === 3) + arktest.assertTrue(map1.get("m1") === m1) + arktest.assertTrue(map1.get("m2") === m2) + arktest.assertTrue(map1.get("m3") === m3) +} + +function main(): int { + const suite = new arktest.ArkTestsuite("Map Set Bucket") + suite.addTest("Set with initial Buckets", testSetInitialBuckets) + suite.addTest("Map with initial Buckets", testMapInitialBuckets) + return suite.run() +} \ No newline at end of file -- Gitee