diff --git a/compileruntime/util_lib_standard/src/main/js/test/ArrayList.test.js b/compileruntime/util_lib_standard/src/main/js/test/ArrayList.test.js new file mode 100644 index 0000000000000000000000000000000000000000..212cb42b43f565a1e06be32d1ccf5d499016a0af --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/ArrayList.test.js @@ -0,0 +1,676 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {ArrayList} from '@ohos.arraylist' + +describe('ArraylistTest', function () { + it('SR000GGR44_testConstructor001', 0, function () { + try{ + let arraylist = new ArrayList(); + expect(arraylist != undefined).assertEqual(true) + }catch(err){ + expect(err).assertEqual("Error: Cannot create new arraylist") + } + }) + it('SR000GGR44_testAdd002', 0, function () { + let arraylist = new ArrayList(); + arraylist.add("四"); + let res = arraylist.has("四") + expect(res).assertEqual(true) + }) + it('SR000GGR44_testAdd003', 0, function () { + let arraylist = new ArrayList(); + arraylist.add(8); + let res = arraylist.has(8) + expect(res).assertEqual(true) + }) + it('SR000GGR44_testAdd004 ', 0, function () { + let arraylist = new ArrayList(); + let a={name:'lala',age:'13岁'} + arraylist.add(a); + let res = arraylist.has(a) + expect(res).assertEqual(true) + }) + it('SR000GGR44_testAdd005 ', 0, function () { + let arraylist = new ArrayList(); + let a=[1,2,3,4] + arraylist.add(a); + let res = arraylist.has(a) + expect(res).assertEqual(true) + }) + it('SR000GGR44_testAdd006 ', 0, function () { + let arraylist = new ArrayList(); + let a = true + arraylist.add(a); + let res = arraylist.has(a) + expect(res).assertEqual(true) + }) + it('SR000GGR44_testInsert007', 0, function () { + let arraylist = new ArrayList(); + arraylist.add("四"); + arraylist.add("三"); + arraylist.add(1); + arraylist.add('a') + arraylist.insert(8,2) + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a=["四","三",8,1,"a"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["四","三","a"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["四","a",1,"b","c",1]; + for(let i=0;i { + return (item = 2 * item); + }); + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a=[8,6,2,4,28]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + for(let i=0;i{ + arr.push(item); + }) + let a=[1,2]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + for(let i=0;i a-b); + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a=[1,2,3,4,14]; + for(let i=0;i{ + if(item == 1){ + arraylist.remove(item); + } + }) + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a = [0,2,3]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + for(let i=0;i a-b); + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a=["刘","张三",1,2,"李四"]; + for(let i=0;i a-b); + let arr=[]; + arraylist.forEach((item,index)=>{ + arr.push(item); + }) + let a=["c","a","b","e","d"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","a","b"] + for(let i=0;i{ + arr.push(item); + }) + let a=["b","c","a","b"]; + for(let i=0;i { + return (item = 2 * item); + }); + let arr=[]; + list.forEach((item,index)=>{ + arr.push(item); + }) + let a=[2,4,8,10]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","c","d","a","b"]; + for(let i=0;i a-b); + let arr=[]; + list.forEach((item,index)=>{ + arr.push(item); + }) + let a=[1,2,3,4,14]; + for(let i=0;i{ + arr.push(item); + }) + let a=[1,2]; + for(let i=0;i{ + arr.push(item); + }) + let a = [8,"一","二",5,c,6,"三","四"] + for(let i=0;i a-b); + let arr=[]; + list.forEach((item,index)=>{ + arr.push(item); + }) + let a=[";", "一","a","1","2","14"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["1","2"]; + for(let i=0;i{ + arr.push(item); + }) + let a = ['.','*','|']; + for(let i=0;i{ + }) + }) + + it('SR000GGR4A_testAdd043', 0, function () { + let list = new List(); + for (let i = 0; i < 100; i++) { + list.add(i); + } + let res = list.get(99); + let res1 = list.length; + expect(res).assertEqual(99); + expect(res1).assertEqual(100); + }) + + it('SR000GGR4A_testAdd044', 0, function () { + let list = new List(); + list.add(null); + let res = list.get(0); + expect(res).assertEqual(null); + }) + + it('SR000GGR4A_testAdd045', 0, function () { + let list = new List(); + list.add(0.1111); + let res = list.get(0); + expect(res).assertEqual(0.1111); + }) + + it('SR000GGR4A_testAdd046', 0, function () { + let list = new List(); + list.add(-1); + let res = list.get(0); + expect(res).assertEqual(-1); + }) + + it('SR000GGR4A_testAdd047', 0, function () { + let list = new List(); + const obj = {}; + list.add(obj); + let res = list.get(0); + expect(res).assertEqual(obj); + }) + + it('SR000GGR4A_testSort048', 0, function () { + let list= new List(); + list.sort((a,b) => a-b); + let res1 = list.length; + expect(res1).assertEqual(0); + }) + + it('SR000GGR4A_testRemoveByIndex049', 0, function () { + let list= new List(); + try { + let res = list.removeByIndex(1); + } catch (err) { + expect(err).assertEqual("Error: removeByIndex is out-of-bounds") + } + }) + + it('SR000GGR4A_testEqual050', 0, function () { + let list = new List(); + let list1 = new List(); + try { + let res = list.equal(list1); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR4A_testGetIndexOf051', 0, function () { + let list= new List(); + let res = list.getIndexOf(1); + expect(res).assertEqual(-1); + }) + + it('SR000GGR4A_testForEach052', 0, function () { + let list= new List(); + let num = 0 + try { + list.forEach((item, index)=>{ + num++; + }) + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + expect(num).assertEqual(0); + }) + + it('SR000GGR4A_testIsEmpty053', 0, function () { + let list= new List(); + try { + let res = list.IsEmpty(); + } catch (err) { + expect(err).assertEqual("TypeError: is not callable") + } + }) + + it('SR000GGR4A_testHas054', 0, function () { + let list= new List(); + try { + let res = list.has(1); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR4A_testGet055', 0, function () { + let list= new List(); + try { + let res = list.get(1); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR4A_testClear056', 0, function () { + let list= new List(); + list.clear(); + let res1 = list.length; + expect(res1).assertEqual(0); + }) + + it('SR000GGR4A_testGetLast057', 0, function () { + let list= new List(); + try { + let res = list.getLast(); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR4A_testReplaceAllElements058', 0, function () { + let list= new List(); + let num = 0 + try { + list.replaceAllElements((item, index)=>{ + num++; + }) + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + expect(num).assertEqual(0); + }) + + it('SR000GGR4A_testConvertToArray059', 0, function () { + let list= new List(); + const res = list.convertToArray() + expect(res.length).assertEqual(0); + }) + + it('SR000GGR4A_testConvertToArray060', 0, function () { + let list= new List(); + const res = list.convertToArray() + expect(res.length).assertEqual(0); + }) + + it('SR000GGR4A_testlterator061', 0, function () { + let list= new List(); + for (let index = 0; index < 10; index++) { + list.add(index); + } + let num = 0; + for (const iterator of list) { + expect(iterator).assertEqual(num); + num++; + } + }) + + it('SR000GGR4A_testGetFirst062', 0, function () { + let list= new List(); + try { + let res = list.getFirst(); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/Deque.test.js b/compileruntime/util_lib_standard/src/main/js/test/Deque.test.js new file mode 100644 index 0000000000000000000000000000000000000000..261fefb7096e91410a4193942ddeedc0b5ac33ca --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/Deque.test.js @@ -0,0 +1,331 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {Deque} from '@ohos.deque' + +describe('DequeTest', function () { + it('SR000GGR3K_testConstructor001', 0, function () { + try{ + let deque = new Deque(); + }catch(err){ + expect(err).assertEqual("Error:Cannot create new deque") + } + }) + it('SR000GGR3K_testInsertFront001', 0, function () { + let deque = new Deque(); + deque.insertFront("一"); + let res = deque.getFirst(); + expect(res).assertEqual("一"); + }) + + it('SR000GGR3K_testInsertFront002', 0, function () { + let deque = new Deque(); + deque.insertFront(8); + let res = deque.getFirst(); + expect(res).assertEqual(8) + }) + + it('SR000GGR3K_testInsertFront003', 0, function () { + let deque = new Deque(); + let a ={name:'lala',age:'15'} + deque.insertFront(a); + let res = deque.getFirst(); + expect(res).assertEqual(a) + }) + + it('SR000GGR3K_testInsertFront004', 0, function () { + let deque = new Deque(); + let a =[1,2,3,4] + deque.insertFront(a); + let res = deque.getFirst(); + expect(res).assertEqual(a) + }) + + it('SR000GGR3K_testInsertEnd005', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + let res = deque.getLast() + expect(res).assertEqual(8) + }) + + it('SR000GGR3K_testInsertEnd006', 0, function () { + let deque = new Deque(); + let a = ['a','b','c'] + deque.insertEnd(a); + let res = deque.getLast() + expect(res).assertEqual(a) + }) + + it('SR000GGR3K_testInsertEnd007', 0, function () { + let deque = new Deque(); + let a = { class:'6班',say:'we'} + deque.insertEnd(a); + let res = deque.getLast() + expect(res).assertEqual(a) + }) + + it('SR000GGR3K_testGetFirst007', 0, function () { + let deque = new Deque(); + deque.insertEnd("四"); + deque.insertEnd("三"); + let res = deque.getFirst() + expect(res).assertEqual("四") + }) + + it('SR000GGR3K_testGetLast008', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + deque.insertEnd("三"); + let res = deque.getLast() + expect(res).assertEqual("三") + }) + + it('SR000GGR3K_testHas009', 0, function () { + let deque = new Deque(); + deque.insertEnd(6); + let res = deque.has(6) + expect(res).assertEqual(true) + }) + + it('SR000GGR3K_testHas010', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + let res = deque.has(6) + expect(res).assertEqual(false) + }) + + it('SR000GGR3K_testPopFirst011', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + deque.insertFront("一") + let res = deque.popFirst() + expect(res).assertEqual("一") + }) + + it('SR000GGR3K_testPopLast012', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + deque.insertFront("一") + deque.insertFront("二") + let res = deque.popLast() + expect(res).assertEqual(8) + }) + + it('SR000GGR3K_testForEach013', 0, function () { + let deque = new Deque(); + deque.insertEnd(8); + deque.insertFront("一") + deque.insertFront("二") + deque.insertEnd(1); + deque.insertEnd(2); + deque.insertEnd(3); + deque.insertEnd(4); + deque.insertEnd(5); + deque.insertEnd(6); + deque.insertFront("三") + deque.insertFront("四") + let arr = [] + deque.forEach((item,index)=>{ + arr.push(item); + }) + let a=["四","三","二","一",8,1,2,3,4,5,6] + for(let i=0;i{ + arr.push(item); + }) + let a=[8,1,2,3,3,4,5,6] + for(let i=0;i{ + keys.push(key); + vals.push(value); + }) + for(let i = 0; i < keys.length; i++){ + let has = hashmap.hasKey(keys[i]); + expect(has).assertEqual(true); + } + for(let i = 0; i < vals.length; i++){ + let has = hashmap.hasValue(vals[i]); + expect(has).assertEqual(true); + } + }) + it('SR000GGR4B_testIterator018', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1,"A"); + hashmap.set(2,"B"); + hashmap.set(3,"C"); + hashmap.set(4,"D"); + hashmap.set(5,"E"); + let arr = []; + for(let [key,value] of hashmap) { + arr.push([key,value]); + } + arr.sort(function(a, b){return a[0] - b[0]}); + + for(let i = 0; i < arr.length; i++){ + expect(arr[i][0]).assertEqual(i + 1); + expect(arr[i][1]).assertEqual(hashmap.get(i + 1)); + } + }) + it('SR000GGR4B_testEntries019', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1,"A"); + hashmap.set(2,"B"); + hashmap.set(3,"C"); + hashmap.set(4,"D"); + hashmap.set(5,"E"); + let res = hashmap.entries(); + let arr = []; + for(let i = 0; i < hashmap.length; i++){ + let obj = res.next(); + arr.push([obj.value[0], obj.value[1]]); + } + arr.sort(function(a, b){return a[0] - b[0]}); + for(let i = 0; i < arr.length; i++){ + expect(arr[i][0]).assertEqual(i + 1); + expect(arr[i][1]).assertEqual(hashmap.get(i + 1)); + } + }) + it('SR000GGR4B_testSet020', 0, function () { + let hashmap = new HashMap(); + hashmap.set('',"A"); + let res = hashmap.get(''); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet021', 0, function () { + let hashmap = new HashMap(); + hashmap.set('$',"A"); + let res = hashmap.get('$'); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet022', 0, function () { + let hashmap = new HashMap(); + hashmap.set(3.14,"A"); + let res = hashmap.get(3.14); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet007', 0, function () { + let hashmap = new HashMap(); + hashmap.set(0.3,"A");//key值为浮点数 + let res = hashmap.get(0.3); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet023', 0, function () { + let hashmap = new HashMap(); + hashmap.set(-1,"A"); + let res = hashmap.get(-1); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet024', 0, function () { + let hashmap = new HashMap(); + let a = {}; + hashmap.set(a,"A"); + let res = hashmap.get(a); + expect(res).assertEqual("A"); + }) + it('SR000GGR4B_testSet025', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1,null); + try{ + let res = hashmap.get(1); + expect(res).assertEqual(null); + }catch(err){ + expect(err).assertEqual("Error: this hashmap don't find the key") + } + }) + it('SR000GGR4B_testSet026', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1, '$'); + let res = hashmap.get(1); + expect(res).assertEqual('$'); + }) + it('SR000GGR4B_testSet027', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1, 3.14); + let res = hashmap.get(1); + expect(res).assertEqual(3.14); + }) + it('SR000GGR4B_testSet028', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1, -1); + let res = hashmap.get(1); + expect(res).assertEqual(-1); + }) + it('SR000GGR4B_testSet029', 0, function () { + let hashmap = new HashMap(); + let a = {}; + hashmap.set(1,a); + let res = hashmap.get(1); + expect(res).assertEqual(a); + }) + it('SR000GGR4B_testSet030', 0, function () { + let hashmap = new HashMap(); + let i = 0; + for (; i < 100; i++) { + hashmap.set(1, i); + } + let res = hashmap.get(1); + let res1 = hashmap.length; + expect(res).assertEqual(99); + expect(res1).assertEqual(1); + }) + it('SR000GGR4B_testSet031', 0, function () { + let hashmap = new HashMap(); + let i = 0; + for (; i < 100; i++) { + hashmap.set(i, 1); + } + let res = hashmap.get(i - 1); + let res1 = hashmap.length; + expect(res).assertEqual(1); + expect(res1).assertEqual(100); + }) + it('SR000GGR4B_testIterator032', 0, function () { + let hashmap = new HashMap(); + let arr = []; + try { + for (let item of hashmap) { + arr.push(item); + } + } catch (err) { + expect(err).assertEqual("Error: Cannot create new HashMap") + } + expect(arr.length).assertEqual(0); + }) + it('SR000GGR4B_testForEach033', 0, function () { + let hashmap = new HashMap(); + let arr = []; + try { + hashmap.forEach((item, index) => { + arr.push(item); + }) + } catch (err) { + expect(err).assertEqual("Error:Cannot create new stack") + } + expect(arr.length).assertEqual(0); + }) + it('SR000GGR4B_testIsEmpty034', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1,"A"); + hashmap.set(2,"B"); + hashmap.set(3,"C"); + hashmap.set(4,"D"); + hashmap.set(5,"E"); + let res = hashmap.length; + let res1 = hashmap.isEmpty(); + expect(res).assertEqual(5); + expect(res1).assertEqual(false); + }) + it('SR000GGR4B_testIsEmpty035', 0, function () { + let hashmap = new HashMap(); + hashmap.set(1,"A"); + hashmap.set(2,"B"); + hashmap.set(3,"C"); + hashmap.set(4,"D"); + hashmap.set(5,"E"); + hashmap.clear(); + let res = hashmap.length; + let res1 = hashmap.isEmpty(); + expect(res).assertEqual(0); + expect(res1).assertEqual(true); + }) + it('SR000GGR4B_testHasKey036', 0, function () { + let hashmap = new HashMap(); + let res = hashmap.hasKey(8); + expect(res).assertEqual(false); + }) + it('SR000GGR4B_testHasValue037', 0, function () { + let hashmap = new HashMap(); + let res = hashmap.hasValue(8); + expect(res).assertEqual(false); + }) + it('SR000GGR4B_testRemove038', 0, function () { + let hashmap = new HashMap(); + try{ + let res= hashmap.remove(3); + }catch(err){ + expect(err).assertEqual("Error: The removed element does not exist in this container") + } + }) + it('SR000GGR4B_testReplace039', 0, function () { + let hashmap = new HashMap(); + let res = hashmap.replace(2,"G"); + expect(res).assertEqual(false); + }) + it('SR000GGR4B_testGet040', 0, function () { + let hashmap = new HashMap(); + try{ + let res = hashmap.get(3); + }catch(err){ + expect(err).assertEqual("Error: this hashmap don't find the key") + } + }) + it('SR000GGR4B_testSetAll041', 0, function () { + let hashmap = new HashMap(); + let hashmap1 = new HashMap(); + hashmap1.set(1,1); + hashmap1.set(2,2); + hashmap.setAll(hashmap1); + let arr = []; + for(let [key,value] of hashmap) { + arr.push([key,value]); + } + arr.sort(function(a, b){return a[0] - b[0]}); + + for(let i = 0; i < arr.length; i++){ + expect(arr[i]).assertEqual([i + 1,hashmap.get(i + 1)]); + } + }) + it('SR000GGR4B_testClear042', 0, function () { + let hashmap = new HashMap(); + hashmap.clear(); + let res = hashmap.length; + expect(res).assertEqual(0); + }) + it('SR000GGR4B_testEntries043', 0, function () { + let hashmap = new HashMap(); + let res = hashmap.entries(); + expect(undefined).assertEqual(res.next().value); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/HashSet.test.js b/compileruntime/util_lib_standard/src/main/js/test/HashSet.test.js new file mode 100644 index 0000000000000000000000000000000000000000..7af5abc02c39c6f9dd8ef51600ec2cc4ef34dea4 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/HashSet.test.js @@ -0,0 +1,330 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {HashSet} from '@ohos.hashset' +describe('HashSetTest', function () { + it('SR000GGR3J_testNew001', 0, function () { + try{ + let hashset = new HashSet(); + }catch(err){ + expect(err).assertEqual("Error: Cannot create new HashSet") + } + }) + it('SR000GGR3J_testAdd001', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + let res = hashset.has(1); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd002', 0, function () { + let hashset = new HashSet(); + hashset.add("b"); + hashset.add("c"); + hashset.add("d"); + hashset.add("a"); + hashset.add("g"); + let res = hashset.has("a"); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd004',0, function () { + let hashset = new HashSet(); + let c = [1,2,3] + hashset.add(c); + let res = hashset.has(c); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd005', 0, function () { + let hashset = new HashSet(); + let c = { name: 'lili', age: '13' } + hashset.add(c); + let res = hashset.has(c); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testLength006', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + hashset.add(2); + hashset.add(3); + hashset.add(4); + hashset.add(5); + let res = hashset.length; + expect(res).assertEqual(5); + }) + it('SR000GGR3J_testHas007', 0, function () { + let hashset = new HashSet(); + hashset.add(4); + hashset.add(1); + hashset.add(3); + hashset.add(2); + hashset.add(5); + let res = hashset.has(1); + expect(res).assertEqual(true); + let res1 = hashset.has("A"); + expect(res1).assertEqual(false); + }) + it('SR000GGR3J_testIsEmpty008', 0, function () { + let hashset = new HashSet(); + hashset.add(4); + hashset.add(1); + hashset.add(3); + hashset.add(2); + hashset.add(5); + let res = hashset.isEmpty(); + expect(res).assertEqual(false); + }) + it('SR000GGR3J_testRemove009', 0, function () { + let hashset = new HashSet(); + hashset.add(4); + hashset.add(1); + hashset.add(3); + hashset.add(2); + hashset.add(5); + let res = hashset.remove(1); + expect(res).assertEqual(true); + let arr = [] + hashset.forEach((value,index)=>{ + arr.push(value); + }) + + let i = 0; + for(; i < arr.length; i++){ + let has = hashset.has(arr[i]); + expect(has).assertEqual(true); + } + }) + it('SR000GGR3J_testClear010', 0, function () { + let hashset = new HashSet(); + hashset.add(4); + hashset.add(1); + hashset.add(3); + hashset.add(2); + hashset.add(5); + let res = hashset.length; + expect(res).assertEqual(5); + hashset.clear(); + let res1 = hashset.length; + expect(res1).assertEqual(0); + }) + it('SR000GGR3J_testValues011', 0, function () { + let hashset = new HashSet(); + hashset.add("A"); + hashset.add("B"); + hashset.add("C"); + hashset.add("D"); + hashset.add("E"); + let res = hashset.values(); + let i = 0; + for(; i < hashset.length; i++){ + let has = hashset.has(res.next().value); + expect(has).assertEqual(true); + } + }) + it('SR000GGR3J_testForEach012', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + hashset.add(2); + hashset.add(3); + hashset.add(4); + hashset.add(5); + let arr = [] + hashset.forEach((value,index)=>{ + arr.push(value); + }) + for(let i = 0; i < arr.length; i++){ + let has = hashset.has(arr[i]); + expect(has).assertEqual(true); + } + + }) + it('SR000GGR3J_testIterator013', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + hashset.add(2); + hashset.add(3); + hashset.add(4); + hashset.add(5); + let arr=[] + for(let item of hashset){ + arr.push(item) + } + for(let i = 0; i < arr.length; i++){ + let has = hashset.has(arr[i]); + expect(has).assertEqual(true); + } + }) + it('SR000GGR3J_testIterator014', 0, function () { + let hashset = new HashSet(); + hashset.add("A"); + hashset.add("B"); + hashset.add("C"); + hashset.add("D"); + hashset.add("E"); + let arr=[] + for(let item of hashset){ + arr.push(item) + } + for(let i = 0; i < arr.length; i++){ + let has = hashset.has(arr[i]); + expect(has).assertEqual(true); + } + }) + it('SR000GGR3J_testEntries015', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + hashset.add(2); + hashset.add(3); + hashset.add(4); + hashset.add(5); + + let res = hashset.entries(); + let i = 0; + for(; i < hashset.length; i++){ + let has = hashset.has(res.next().value[1]); + expect(has).assertEqual(true); + } + }) + it('SR000GGR3J_testAdd016',0, function () { + let hashset = new HashSet(); + hashset.add(''); + let res = hashset.has(''); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd017',0, function () { + let hashset = new HashSet(); + hashset.add('$'); + let res = hashset.has('$'); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd017',0, function () { + let hashset = new HashSet(); + hashset.add(1.34); + let res = hashset.has(1.34); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd018',0, function () { + let hashset = new HashSet(); + hashset.add(-1); + let res = hashset.has(-1); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd019',0, function () { + let hashset = new HashSet(); + let a = {}; + hashset.add(a); + let res = hashset.has(a); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testAdd020',0, function () { + let hashset = new HashSet(); + let i = 0; + for (; i < 100; i++) { + hashset.add(i); + } + let res = hashset.has(i - 1); + let res1 = hashset.length; + expect(res).assertEqual(true); + expect(res1).assertEqual(100); + }) + it('SR000GGR3J_testIterator021', 0, function () { + let hashset = new HashSet(); + let i = 0; + for(let item of hashset){ + expect(item).assertEqual(i); + i++; + } + }) + it('SR000GGR3J_testForEach022', 0, function () { + let hashset = new HashSet(); + let arr = []; + hashset.forEach((value,index)=>{ + arr.push(value); + }) + expect(arr.length).assertEqual(0); + }) + it('SR000GGR3J_testIsEmpty023', 0, function () { + let hashset = new HashSet(); + hashset.add(4); + hashset.add(1); + hashset.add(3); + hashset.add(2); + hashset.add(5); + hashset.clear(); + let res = hashset.isEmpty(); + expect(res).assertEqual(true); + }) + it('SR000GGR3J_testHas024', 0, function () { + let hashset = new HashSet(); + let res = hashset.has(1); + expect(res).assertEqual(false); + }) + it('SR000GGR3J_testRemove025', 0, function () { + let hashset = new HashSet(); + let res = hashset.remove(1); + expect(res).assertEqual(false); + }) + it('SR000GGR3J_testClear026', 0, function () { + let hashset = new HashSet(); + let res = hashset.clear(); + expect(res).assertEqual(undefined); + + }) + it('SR000GGR3J_testEntries027', 0, function () { + let hashset = new HashSet(); + let res = hashset.entries(); + expect(res.next().value).assertEqual(undefined); + + }) + it('SR000GGR3J_testIterator028', 0, function () { + let hashset = new HashSet(); + let i = 0; + for (; i < 100; i++) { + let text = hashset.add(i); + } + let arr = []; + for(let item of hashset){ + arr.push(Number.parseInt(item)); + } + for (i = 0; i < 100; i++) { + let a = arr[i]; + let res = hashset.has(a); + expect(res).assertEqual(true); + } + }) + it('SR000GGR3J_testForEach029', 0, function () { + let hashset = new HashSet(); + let i = 0; + for (; i < 100; i++) { + hashset.add(i); + } + let arr = []; + hashset.forEach((value,index)=>{ + arr.push(value); + }) + for (i = 0; i < 100; i++) { + let a = arr[i]; + let res = hashset.has(a); + expect(res).assertEqual(true); + } + }) + it('SR000GGR3J_testAdd030', 0, function () { + let hashset = new HashSet(); + hashset.add(1); + hashset.add(1); + let has = hashset.has(1); + let size = hashset.length; + expect(has).assertEqual(true); + expect(size).assertEqual(1); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/LightWeightMap.test.js b/compileruntime/util_lib_standard/src/main/js/test/LightWeightMap.test.js new file mode 100644 index 0000000000000000000000000000000000000000..db062d43962886a764863d4568ef173770dc7f21 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/LightWeightMap.test.js @@ -0,0 +1,604 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {LightWeightMap} from '@ohos.lightweightmap' + +describe('SR00GGR3L_LightWeightMapTest', function () { + it('SR00GGR3L_testConstructor001', 0, function () { + try{ + let lightweightmap = new LightWeightMap(); + expect(lightweightmap != undefined).assertEqual(true); + }catch(err){ + expect(err).assertEqual("Error:Cannot create new TreeMap") + } + }) + it('SR00GGR3L_testSet002', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1,"A"); + let res = lightweightmap.hasValue("A"); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(1); + expect(res1).assertEqual(true); + }) + it('SR00GGR3L_testSet003', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a","A"); + let res = lightweightmap.hasValue("A"); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey("a"); + expect(res1).assertEqual(true); + }) + it('SR00GGR3L_testSet004', 0, function () { + let lightweightmap = new LightWeightMap(); + let a =[1,2,3,4] + lightweightmap.set(1,a); + let res = lightweightmap.hasValue(a); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(1); + expect(res1).assertEqual(true); + }) + it('SR00GGR3L_testSet005', 0, function () { + let lightweightmap = new LightWeightMap(); + let c = { name: 'lili', age: '13' } + lightweightmap.set(1,c); + let res = lightweightmap.hasValue(c); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(1); + expect(res1).assertEqual(true); + }) + it('SR00GGR3L_testGet006', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1,"A"); + lightweightmap.set(2,"B"); + lightweightmap.set(3,"C"); + lightweightmap.set(4,"D"); + lightweightmap.set(5,"E"); + let res = lightweightmap.get(4); + expect(res).assertEqual("D"); + }) + it('SR00GGR3L_testLength007', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1,"A"); + lightweightmap.set(2,"B"); + lightweightmap.set(3,"C"); + lightweightmap.set(4,"D"); + lightweightmap.set(5,"E"); + let res = lightweightmap.length; + expect(res).assertEqual(5); + }) + it('SR00GGR3L_testHasAll008', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a","A"); + lightweightmap.set("b","B"); + lightweightmap.set("c","C"); + lightweightmap.set("d","D"); + lightweightmap.set("e","E"); + let lightweightmap1 = new LightWeightMap(); + lightweightmap1.set("a","A"); + lightweightmap1.set("d","D"); + let res = lightweightmap.hasAll(lightweightmap1); + expect(res).assertEqual(true); + }) + it('SR00GGR3L_testHasKey009', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a", "A"); + lightweightmap.set("b", "B"); + lightweightmap.set("c", "C"); + lightweightmap.set("d", "D"); + lightweightmap.set("e", "E"); + let res = lightweightmap.hasKey("a"); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(1); + expect(res1).assertEqual(false); + }) + it('SR00GGR3L_testHasValue010', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a", "A"); + lightweightmap.set("b", "B"); + lightweightmap.set("c", "C"); + lightweightmap.set("d", "D"); + lightweightmap.set("e", "E"); + let res = lightweightmap.hasValue("A"); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasValue(1); + expect(res1).assertEqual(false); + }) + it('SR00GGR3L_testIncreaseCapacityTo011', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a", "A"); + lightweightmap.set("b", "B"); + lightweightmap.set("c", "C"); + lightweightmap.set("d", "D"); + lightweightmap.set("e", "E"); + let res = lightweightmap.increaseCapacityTo(3); + expect(res).assertEqual(undefined); + }) + it('SR00GGR3L_testEntries012', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.entries(); + expect(JSON.stringify(res.next().value)).assertEqual("[1,\"A\"]"); + expect(JSON.stringify(res.next().value)).assertEqual("[2,\"B\"]"); + expect(JSON.stringify(res.next().value)).assertEqual("[3,\"C\"]"); + expect(JSON.stringify(res.next().value)).assertEqual("[4,\"D\"]"); + expect(JSON.stringify(res.next().value)).assertEqual("[5,\"E\"]"); + }) + it('SR00GGR3L_testGetIndexOfKey013', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.getIndexOfKey(2); + expect(res).assertEqual(1); + }) + it('SR00GGR3L_testGetIndexOfValue014', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + lightweightmap.setValueAt(4, "F"); + let res = lightweightmap.getIndexOfValue("F"); + expect(res).assertEqual(4); + }) + it('SR00GGR3L_testIsEmpty015', 0, function () { + let lightweightmap = new LightWeightMap(); + let res1 = lightweightmap.isEmpty(); + expect(res1).assertEqual(true); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res2 = lightweightmap.isEmpty(); + expect(res2).assertEqual(false); + }) + it('SR00GGR3L_testGetKeyAt016', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.getKeyAt(1); + expect(res).assertEqual(2); + }) + it('SR00GGR3L_testKeys017', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.keys(); + expect(res.next().value).assertEqual(1); + expect(res.next().value).assertEqual(2); + expect(res.next().value).assertEqual(3); + expect(res.next().value).assertEqual(4); + expect(res.next().value).assertEqual(5); + }) + it('SR00GGR3L_testSetAll018', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let lightweightmap1 = new LightWeightMap(); + lightweightmap1.set(6, "A"); + lightweightmap1.set(7, "B"); + lightweightmap.setAll(lightweightmap1); + for(let i=1; i < 8; i++) { + expect(lightweightmap1.hasKey(i)).assertEqual(true); + } + }) + + it('SR00GGR3L_testRemove019', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.remove(3); + expect(res).assertEqual("C"); + let res1 = lightweightmap.hasValue("C"); + expect(res1).assertEqual(false); + }) + + it('SR00GGR3L_testRemoveAt020', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let res = lightweightmap.removeAt(1); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasValue("A"); + expect(res1).assertEqual(true); + let res2 = lightweightmap.hasValue("B"); + expect(res2).assertEqual(false); + let res3 = lightweightmap.removeAt(10); + expect(res3).assertEqual(false); + }) + + it('SR00GGR3L_testClear021', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + lightweightmap.clear(); + let res = lightweightmap.length; + expect(res).assertEqual(0); + let isEmpty = lightweightmap.isEmpty(); + expect(isEmpty).assertEqual(true); + }) + it('SR00GGR3L_testSetValueAt022', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + lightweightmap.setValueAt(0,"a"); + let res = lightweightmap.get(1); + expect(res).assertEqual("a"); + }) + it('SR00GGR3L_testForEach023', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1, "A"); + lightweightmap.set(2, "B"); + lightweightmap.set(3, "C"); + lightweightmap.set(4, "D"); + lightweightmap.set(5, "E"); + let arr = [] + lightweightmap.forEach((value,index)=>{ + arr.push(value); + }) + let arr1= ["A","B","C","D","E"]; + for(let i=0;i -5001; i--) { + lightweightmap.set(i, i); + } + + let res1 = lightweightmap.length; + expect(res1).assertEqual(10000); + + for (let index = 0; index < 5000; index++) { + let resKey = lightweightmap.hasKey(index); + expect(resKey).assertEqual(true); + let resValue = lightweightmap.hasValue(index); + expect(resValue).assertEqual(true); + } + + for (let i = -1; i > -5001; i--) { + let resKey = lightweightmap.hasKey(i); + expect(resKey).assertEqual(true); + let resValue = lightweightmap.hasValue(i); + expect(resValue).assertEqual(true); + } + }) + + it('SR00GGR3L_testSet047', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(true, 0.001); + let res = lightweightmap.hasValue(0.001); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(true); + expect(res1).assertEqual(true); + }) + + it('SR00GGR3L_testSet048', 0, function () { + let lightweightmap = new LightWeightMap(); + let a =[1,2,3,4] + lightweightmap.set(a, 1); + let res = lightweightmap.hasValue(1); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(a); + expect(res1).assertEqual(true); + }) + + it('SR00GGR3L_testSet049', 0, function () { + let lightweightmap = new LightWeightMap(); + let a = { abc: 1 } + lightweightmap.set(a, ''); + let res = lightweightmap.hasValue(''); + expect(res).assertEqual(true); + let res1 = lightweightmap.hasKey(a); + expect(res1).assertEqual(true); + }) + + it('SR00GGR3L_testGet050', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set(1,"A"); + lightweightmap.set(2,"B"); + lightweightmap.set(3,"C"); + lightweightmap.set(4,"D"); + lightweightmap.set(5,"E"); + let res = lightweightmap.get(10); + expect(res).assertEqual(null); + }) + + it('SR00GGR3L_testHasAll051', 0, function () { + let lightweightmap = new LightWeightMap(); + lightweightmap.set("a","A"); + lightweightmap.set("b","B"); + lightweightmap.set("c","C"); + lightweightmap.set("d","D"); + lightweightmap.set("e","E"); + let lightweightmap1 = new LightWeightMap(); + lightweightmap1.set("a","A"); + lightweightmap1.set("d","D1"); + let res = lightweightmap.hasAll(lightweightmap1); + expect(res).assertEqual(false); + }) + +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/LightWeightSet.test.js b/compileruntime/util_lib_standard/src/main/js/test/LightWeightSet.test.js new file mode 100644 index 0000000000000000000000000000000000000000..c6c476e37b6b1d9622419b3b95e21008a2f016b0 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/LightWeightSet.test.js @@ -0,0 +1,475 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {LightWeightSet} from '@ohos.lightweightset' +import {Struct} from "@ohos.struct" + +describe('LightWeightSetTest', function () { + it('SR000GGR43_testNew001', 0, function () { + try{ + let lightweightset = new LightWeightSet(); + expect(lightweightset != undefined).assertEqual(true); + }catch(err){ + expect(err).assertEqual("Error:Cannot create new TreeMap"); + } + }) + it('SR000GGR43_testAdd002', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + let res = lightweightset.has(1); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testAdd003', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add("a"); + let res = lightweightset.has("a"); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testAdd004', 0, function () { + let lightweightset = new LightWeightSet(); + let a =[1,2,3,4] + lightweightset.add(a); + let res = lightweightset.has(a); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testAdd005', 0, function () { + let lightweightset = new LightWeightSet(); + let c = { name: 'lili', age: '13' } + lightweightset.add(c); + let res = lightweightset.has(c); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testAdd006', 0, function () { + let lightweightset = new LightWeightSet(); + let c = false; + lightweightset.add(c); + let res = lightweightset.has(c); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testLength007', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let res = lightweightset.length; + expect(res).assertEqual(5); + }) + + it('SR000GGR43_testAddAll008', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let lightweightset1 = new LightWeightSet(); + lightweightset1.add(6); + lightweightset1.add(7); + let res1 = lightweightset.addAll(lightweightset1); + for(let item of lightweightset) { + } + for(let item of lightweightset1) { + } + for(let i=1; i < 8; i++) { + expect(lightweightset.has(i)).assertEqual(true); + } + expect(res1).assertEqual(true); + }) + it('SR000GGR43_testHasAll009', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add("a"); + lightweightset.add("b"); + lightweightset.add("c"); + lightweightset.add("d"); + lightweightset.add("e"); + let lightweightset1 = new LightWeightSet(); + lightweightset1.add("a"); + lightweightset1.add("d"); + let res = lightweightset.hasAll(lightweightset1); + expect(res).assertEqual(true); + }) + it('SR000GGR43_testHasAllr010', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add("a"); + lightweightset.add("b"); + lightweightset.add("c"); + lightweightset.add("e"); + let lightweightset1 = new LightWeightSet(); + lightweightset1.add("a"); + lightweightset1.add("d"); + let res = lightweightset.hasAll(lightweightset1); + expect(res).assertEqual(false); + }) + + it('SR000GGR43_testHas011', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add("a"); + lightweightset.add("b"); + lightweightset.add("c"); + lightweightset.add("d"); + lightweightset.add("e"); + let res = lightweightset.has("a"); + expect(res).assertEqual(true); + let res1 = lightweightset.has(1); + expect(res1).assertEqual(false); + }) + + it('SR000GGR43_testEntries012', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let res = lightweightset.entries(); + expect(JSON.stringify(res.next().value)).assertEqual("[1,1]"); + expect(JSON.stringify(res.next().value)).assertEqual("[2,2]"); + expect(JSON.stringify(res.next().value)).assertEqual("[3,3]"); + expect(JSON.stringify(res.next().value)).assertEqual("[4,4]"); + expect(JSON.stringify(res.next().value)).assertEqual("[5,5]"); + }) + it('SR000GGR43_testGetIndexOf013', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let res = lightweightset.getIndexOf(2); + expect(res).assertEqual(1); + }) + it('SR000GGR43_testIsEmpty014', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let res = lightweightset.isEmpty(); + expect(res).assertEqual(false); + }) + it('SR000GGR43_testRemove015', 0, function () { + let lightweightset = new LightWeightSet(); + lightweightset.add(1); + lightweightset.add(2); + lightweightset.add(3); + lightweightset.add(4); + lightweightset.add(5); + let res = lightweightset.remove(1); + expect(res).assertEqual(1); + let arr=[]; + lightweightset.forEach((value,index)=>{ + arr.push(value); + }) + let arr1 =[2,3,4,5]; + for(let i=0;i{ + arr.push(value); + }) + let arr1 =[1,3,4,5]; + for(let i=0;i{ + arr.push(value); + }) + let arr1= [1,2,3,4,5]; + for(let i=0;i{ + arr.push(item); + }) + let a=["e","a","b"] + for(let i=0;i{ + arr.push(item); + }) + let a=["b","c"] + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","a","b"] + for(let i=0;i{ + arr.push(item); + }) + let a=["b","c","a","b"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","c","a","b"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","c","a"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","c","d","a","b"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["d","a","b","c","a","b"]; + for(let i=0;i{ + arr.push(item); + }) + let a=["a","b","c","a","b","d"]; + for(let i=0;i{ + arr.push(item); + }) + let a = [8,"一","二",5,c,6,"三","四"] + for(let i=0;i{ + arr.push(item); + }) + let a=["e","a","b"] + for(let i=0;i{ + }) + expect(res).assertEqual("b") + }) + it('SR000GGR46_testListSet042', 0, function () { + let list= new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("a"); + list.add("b"); + list[1] = "f"; + let res = list[1]; + list.forEach((item,index)=>{ + }) + expect(res).assertEqual("f") + }) + it('SR000GGR46_testAdd043', 0, function () { + let linkedlist = new LinkedList(); + for (let i = 0; i < 100; i++) { + linkedlist .add(i); + } + let res = linkedlist.get(99); + expect(res).assertEqual(99); + }) + + it('SR000GGR46_testRemoveByIndex044', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.removeByIndex(1); + } catch (err) { + expect(err).assertEqual("Error: removeByIndex is out-of-bounds"); + } + }) + + it('SR000GGR46_testClone045', 0, function () { + let linkedlist= new LinkedList(); + try { + let newlist = linkedlist.clone(); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR46_testGetIndexOf046', 0, function () { + let linkedlist= new LinkedList(); + let res = linkedlist.getIndexOf(1); + expect(res).assertEqual(-1); + }) + + it('SR000GGR46_testForEach047', 0, function () { + let linkedlist= new LinkedList(); + let num = 0 + try { + linkedlist.forEach((item, index)=>{ + num++; + }) + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + expect(num).assertEqual(0); + }) + + it('SR000GGR46_testIsEmpty048', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.IsEmpty(); + } catch (err) { + expect(err).assertEqual("TypeError: is not callable") + } + }) + + it('SR000GGR46_testHas049', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.has(1); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR46_testGet050', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.get(1); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR46_testClear051', 0, function () { + let linkedlist= new LinkedList(); + linkedlist.clear(); + const len = linkedlist.length; + expect(len).assertEqual(0); + }) + + it('SR000GGR46_testGetLast052', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.getLast(); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR46_testGetFirst053', 0, function () { + let linkedlist= new LinkedList(); + try { + let res = linkedlist.getFirst(); + } catch (err) { + expect(err).assertEqual("TypeError: Obj is not a valid object") + } + }) + + it('SR000GGR46_testAdd054', 0, function () { + let linkedlist= new LinkedList(); + linkedlist.add(null); + let res = linkedlist.get(0) + expect(res).assertEqual(null); + }) + + it('SR000GGR46_testAdd055', 0, function () { + let linkedlist= new LinkedList(); + linkedlist.add(0.1); + let res = linkedlist.get(0) + expect(res).assertEqual(0.1); + }) + + it('SR000GGR46_testAdd056', 0, function () { + let linkedlist= new LinkedList(); + linkedlist.add(-1); + let res = linkedlist.get(0) + expect(res).assertEqual(-1); + }) + + it('SR000GGR46_testAdd057', 0, function () { + let linkedlist= new LinkedList(); + const obj = {} + linkedlist.add(obj); + let res = linkedlist.get(0) + expect(res).assertEqual(obj); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/List.test.js b/compileruntime/util_lib_standard/src/main/js/test/List.test.js index 628610ebda7f9b3421bf1704417fed5010c6a921..a63d10aaf24b391f3ec7c6c4d3ccb5e9b19edb92 100644 --- a/compileruntime/util_lib_standard/src/main/js/test/List.test.js +++ b/compileruntime/util_lib_standard/src/main/js/test/List.test.js @@ -14,4 +14,18 @@ */ require('./ExampleJsunit.test.js') -require('./util.test.js') \ No newline at end of file +require('./util.test.js') +require('./Deque.test.js') +require('./ArrayList.test.js') +require('./Queue.test.js') +require('./Stack.test.js') +require('./ContainerList.test.js') +require('./LinkedList.test.js') +require('./Vector.test.js') +require('./LightWeightMap.test.js') +require('./LightWeightSet.test.js') +require('./HashMap.test.js') +require('./HashSet.test.js') +require('./PlainArray.test.js') +require('./TreeMap.test.js') +require('./TreeSet.test.js') diff --git a/compileruntime/util_lib_standard/src/main/js/test/PlainArray.test.js b/compileruntime/util_lib_standard/src/main/js/test/PlainArray.test.js new file mode 100644 index 0000000000000000000000000000000000000000..95e699ac1f2edb30597ef04ec00cba2e16e518a7 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/PlainArray.test.js @@ -0,0 +1,532 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {PlainArray} from '@ohos.plainarray' + +describe('SR000GGR45_PlainArrayTest', function () { + it('SR000GGR45_testConstructor001', 0, function () { + try{ + let plainarray = new PlainArray(); + }catch(err){ + expect(err).assertEqual("Error:Cannot create new PlainArray") + } + }) + it('SR000GGR45_testAdd002', 0, function () { + let plainarray = new PlainArray(); + plainarray.add(1,"A"); + let res = plainarray.get(1); + expect(res).assertEqual("A"); + }) + it('SR000GGR45_testAdd003', 0, function () { + let plainarray = new PlainArray(); + let a=[1,2,3,4] + plainarray.add(2,a); + let res = plainarray.get(2); + expect(res).assertEqual(a); + }) + it('SR000GGR45_testAdd004', 0, function () { + let plainarray = new PlainArray(); + let c = { name: 'lili', age: '13' } + plainarray.add(1,c); + let res = plainarray.get(1); + expect(res).assertEqual(c); + }) + it('SR000GGR45_testAdd005', 0, function () { + let plainarray = new PlainArray(); + let c = false + plainarray.add(1,c); + let res = plainarray.get(1); + expect(res).assertEqual(false); + }) + it('SR000GGR45_testAdd006', 0, function () { + let plainarray = new PlainArray(); + plainarray.add(1,3); + let res = plainarray.get(1); + expect(res).assertEqual(3); + }) + it('SR000GGR45_testGet007', 0, function () { + let plainarray = new PlainArray(); + plainarray.add(1,"A"); + plainarray.add(2,"B"); + plainarray.add(3,"C"); + plainarray.add(4,"D"); + plainarray.add(5,"E"); + let res = plainarray.get(4); + expect(res).assertEqual("D"); + try{ + res = plainarray.get(10); + }catch(err){ + expect(err).assertEqual("Error: Key error found"); + } + }) + it('SR000GGR45_testLength008', 0, function () { + let plainarray = new PlainArray(); + let res = plainarray.length; + expect(res).assertEqual(0); + plainarray.add(1,"A"); + plainarray.add(2,"B"); + plainarray.add(3,"C"); + plainarray.add(4,"D"); + plainarray.add(5,"E"); + res = plainarray.length; + expect(res).assertEqual(5); + }) + it('SR000GGR45_testClone009', 0, function () { + let plainarray = new PlainArray(); + let clonePlainarray = plainarray.clone(); + let cloneLength = clonePlainarray.length; // 克隆空集 + expect(cloneLength).assertEqual(0); + plainarray.add(1,"A"); + plainarray.add(2,"B"); + plainarray.add(3,"C"); + plainarray.add(4,"D"); + plainarray.add(5,"E"); + let res = plainarray.clone(); + let arr=[]; + res.forEach((value,index)=>{ + arr.push(value); + }) + let arr1=["A","B","C","D","E"]; + for(let i=0;i{ + keyArr.push(key); + }) + plainarray.forEach((value,index)=>{ + arr.push(value); + }) + let arr1= ["A","B","C","D","E"]; + for(let i=0;i:;\|{}[]"); + plainarray.add(3,null); + plainarray.add(4,undefined); + plainarray.add(5,[]); + plainarray.add(6,{}); + plainarray.add(7,true); + plainarray.add(8,1234); + plainarray.add(9,1.234); + plainarray.add(10,-1234); + let res = plainarray.get(2); + expect(res).assertEqual("~!@#$^&*()_+-*/=.?<>:;\|{}[]"); + }) + it('SR000GGR45_testAdd039', 0, function () { + let plainarray = new PlainArray(); + plainarray.add(1.23,"a"); + plainarray.add(-2,"b"); + try{ + plainarray.add("a","c"); + }catch(err){ + expect(err).assertEqual("Error: PlainArray's only number is allowed") + } + let res = plainarray.get(-2); + expect(res).assertEqual("b"); + }) + it('SR000GGR45_testAdd040', 0, function () { + let plainarray = new PlainArray(); + plainarray.add(1,1); + plainarray.add(2,"b"); + plainarray.add(3,true); + plainarray.add(4,[1,2,3,4]); + plainarray.add(5,{ name: 'lili', age: '13' }); + plainarray.add(6,undefined); + plainarray.add(7,null); + let res = plainarray.get(2); + expect(res).assertEqual("b"); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/Queue.test.js b/compileruntime/util_lib_standard/src/main/js/test/Queue.test.js new file mode 100644 index 0000000000000000000000000000000000000000..0f3b6cefe6ba44bf5da141b1800a0958b481c797 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/Queue.test.js @@ -0,0 +1,195 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {Queue} from '@ohos.queue' + +describe('QueueTest', function () { + it('SR000GGR48_test_Constructor001', 0, function () { + try{ + let queue = new Queue(); + }catch(err){ + expect(err).assertEqual("Error:Cannot create new queue") + } + }) + + it('SR000GGR48_test_add002', 0, function () { + let queue = new Queue(); + queue.add("四"); + let res = queue.getFirst() + expect(res).assertEqual("四") + }) + it('SR000GGR48_test_add003', 0, function () { + let queue = new Queue(); + queue.add(8); + let res = queue.getFirst() + expect(res).assertEqual(8) + }) + it('SR000GGR48_test_add004', 0, function () { + let queue = new Queue(); + let a = ['a','b','c'] + queue.add(a); + let res = queue.getFirst() + expect(res).assertEqual(a) + }) + + it('SR000GGR48_test_getFirst005', 0, function () { + let queue = new Queue(); + queue.add(6); + queue.add('四') + let res = queue.getFirst() + expect(res).assertEqual(6) + }) + it('SR000GGR48_test_pop006', 0, function () { + let queue = new Queue(); + queue.add(6); + queue.add('四') + let res = queue.pop(); + let res1 = queue.getFirst(); + expect(res).assertEqual(6) + expect(res1).assertEqual('四') + }) + + it('SR000GGR48_test_forEach007', 0, function () { + let queue = new Queue(); + queue.add(8); + queue.add("一") + queue.add("二") + queue.add(1); + queue.add(2); + queue.add(3); + queue.add(4); + queue.add(5); + queue.add(6); + let c =['a','b','c','d']; + queue.add(c) + let arr = [] + queue.forEach((item,index)=>{ + arr.push(item); + }) + let a=[8,"一","二",1,2,3,4,5,6,c] + for(let i=0;i { + arr.push(item); + }) + } catch (err) { + expect(err).assertEqual("Error: Queue: get out-of-bounds") + } + expect(arr.length).assertEqual(1); + }) + + it('SR000GGR48_test_iterator018', 0, function () { + let queue = new Queue(); + let arr = []; + try { + for(let item of queue) { + arr.push(item); + } + } catch (err) { + expect(err).assertEqual("Error:Cannot create new stack") + } + expect(arr.length).assertEqual(0); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/Stack.test.js b/compileruntime/util_lib_standard/src/main/js/test/Stack.test.js new file mode 100644 index 0000000000000000000000000000000000000000..880d59e3e03c6cd8e390e913fe42ac1ee686a4fd --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/Stack.test.js @@ -0,0 +1,330 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {Stack} from '@ohos.stack' + +describe('StackTest', function () { + it('SR000GGR49_test_Constructor001', 0, function () { + try { + let stack = new Stack(); + expect(stack != undefined).assertEqual(true) + } catch (err) { + expect(err).assertEqual("Error: Cannot create new stack") + } + }) + + it('SR000GGR49_test_push002', 0, function () { + let stack = new Stack(); + stack.push("四"); + let res = stack.peek() + expect(res).assertEqual("四") + }) + + it('SR000GGR49_test_push003', 0, function () { + let stack = new Stack(); + stack.push(8); + let res = stack.peek() + expect(res).assertEqual(8) + }) + + it('SR000GGR49_test_push004', 0, function () { + let stack = new Stack(); + let a = { name: 'lala', age: '13岁' } + stack.push(a); + let res = stack.peek() + expect(res).assertEqual(a) + }) + + it('SR000GGR49_test_push005', 0, function () { + let stack = new Stack(); + let a = [1, 2, 3, 4] + stack.push(a); + let res = stack.peek() + expect(res).assertEqual(a) + }) + + it('SR000GGR49_test_peek006', 0, function () { + let stack = new Stack(); + let a = [1, 2, 3, 4] + stack.push(a); + stack.push("A"); + stack.push("B"); + stack.push(1); + let res = stack.peek() + expect(res).assertEqual(1) + }) + + it('SR000GGR49_test_locate007', 0, function () { + let stack = new Stack(); + let a = [1, 2, 3, 4]; + stack.push(8); + stack.push(a); + stack.push("A"); + stack.push("B"); + stack.push(1); + let res = stack.locate("A"); + let res1 = stack.locate("C"); + expect(res).assertEqual(2); + expect(res1).assertEqual(-1); + }) + + it('SR000GGR49_test_pop008', 0, function () { + let stack = new Stack(); + stack.push("B"); + stack.push(1); + let res = stack.pop(); + let res1 = stack.peek(); + expect(res).assertEqual(1); + expect(res1).assertEqual("B"); + }) + + it('SR000GGR49_test_forEach009', 0, function () { + let stack = new Stack(); + stack.push(8); + stack.push("三") + stack.push(5); + let c = { name: 'lili', age: '13' }; + stack.push(c); + stack.push(6); + stack.push("四"); + let arr = []; + stack.forEach((item, index) => { + arr.push(item); + }) + let a = [8, "三", 5,c, 6, "四"] + for (let i = 0;i < a.length; i++) { + expect(arr[i]).assertEqual(a[i]) + } + }) + + it('SR000GGR49_test_iterator010', 0, function () { + let stack = new Stack(); + stack.push(8); + stack.push("一") + stack.push("二") + stack.push(5); + let c = [1, 2, 3, 4]; + stack.push(c); + stack.push(6); + stack.push("三") + stack.push("四") + let arr = [] + for (let item of stack) { + arr.push(item); + } + let a = [8, "一", "二", 5, c,6, "三", "四"] + for (let i = 0;i < a.length; i++) { + expect(arr[i]).assertEqual(a[i]) + } + }) + + it('SR000GGR49_test_length011_1', 0, function () { + let stack = new Stack(); + stack.push(8); + stack.push("一") + stack.push("二") + stack.push(5); + let res = stack.length; + expect(4).assertEqual(res) + }) + + it('SR000GGR49_test_length011_2', 0, function () { + let stack = new Stack(); + let res = stack.length; + expect(0).assertEqual(res) + }) + + it('SR000GGR49_test_isEmpty012_1', 0, function () { + let stack = new Stack(); + stack.push(8); + stack.push("一") + stack.push("二") + stack.push(5); + let res = stack.isEmpty(); + expect(res).assertEqual(false) + }) + + it('SR000GGR49_test_isEmpty012_2', 0, function () { + let stack = new Stack(); + let res = stack.isEmpty(); + expect(res).assertEqual(true) + }) + + it('SR000GGR49_test_push013', 0, function () { + let stack = new Stack(); + stack.push("你好世界"); + let res = stack.peek() + expect(res).assertEqual("你好世界") + }) + + it('SR000GGR49_test_push014', 0, function () { + let stack = new Stack(); + stack.push(1234); + let res = stack.peek() + expect(res).assertEqual(1234) + }) + + it('SR000GGR49_test_push015', 0, function () { + let stack = new Stack(); + stack.push(1.234); + let res = stack.peek() + expect(res).assertEqual(1.234) + }) + + it('SR000GGR49_test_push016', 0, function () { + let stack = new Stack(); + stack.push(-1234); + let res = stack.peek() + expect(res).assertEqual(-1234) + }) + + it('SR000GGR49_test_push017', 0, function () { + let stack = new Stack(); + stack.push(0); + let res = stack.peek() + expect(res).assertEqual(0) + }) + + it('SR000GGR49_test_push018', 0, function () { + let stack = new Stack(); + stack.push(10); + stack.push(3.14); + stack.push(-2); + stack.push("AB"); + stack.push("*"); + let res = stack.peek() + expect(res).assertEqual("*") + }) + + it('SR000GGR49_test_push019', 0, function () { + let stack = new Stack(); + let arr = {}; + stack.push(arr); + let res = stack.peek() + expect(res).assertEqual(arr) + }) + + it('SR000GGR49_test_push020', 0, function () { + let stack = new Stack(); + let arr = []; + stack.push(arr); + let res = stack.peek() + expect(res).assertEqual(arr) + }) + + it('SR000GGR49_test_push021', 0, function () { + let stack = new Stack(); + stack.push("hello world"); + let res = stack.peek() + expect(res).assertEqual("hello world") + }) + + it('SR000GGR49_test_push022', 0, function () { + let stack = new Stack(); + stack.push("~!@#$^&*()_+-*/=.?<>:;\|{}[]"); + let res = stack.peek(); + expect(res).assertEqual("~!@#$^&*()_+-*/=.?<>:;\|{}[]"); + }) + + it('SR000GGR49_test_push023', 0, function () { + let stack = new Stack(); + stack.push(""); + let res = stack.peek(); + expect(res).assertEqual(""); + }) + + it('SR000GGR49_test_push024', 0, function () { + let stack = new Stack(); + stack.push(true); + let res = stack.peek(); + expect(res).assertEqual(true); + }) + + it('SR000GGR49_test_push025', 0, function () { + let stack = new Stack(); + stack.push(false); + let res = stack.peek(); + expect(res).assertEqual(false); + }) + + it('SR000GGR49_test_push026', 0, function () { + let stack = new Stack(); + stack.push(null); + let res = stack.peek(); + expect(res).assertEqual(null); + }) + + it('SR000GGR49_test_push027', 0, function () { + let stack = new Stack(); + stack.push(undefined); + let res = stack.peek(); + expect(res).assertEqual(undefined); + }) + + it('SR000GGR49_test_peek028', 0, function () { + let stack = new Stack(); + try { + let res = stack.peek(); + } catch (err) { + expect(err).assertEqual("Error: Stack: get out-of-bounds") + } + }) + + it('SR000GGR49_test_pop029', 0, function () { + let stack = new Stack(); + try { + let res = stack.pop(); + } catch (err) { + expect(err).assertEqual("Error: Stack: get out-of-bounds") + } + }) + + it('SR000GGR49_test_forEach030', 0, function () { + let stack = new Stack(); + let arr = []; + try { + stack.forEach((item, index) => { + arr.push(item); + }) + } catch (err) { + expect(err).assertEqual("Error:Cannot create new stack") + } + expect(arr.length).assertEqual(0); + }) + + it('SR000GGR49_test_iterator031', 0, function () { + let stack = new Stack(); + let arr = []; + try { + for (let item of stack) { + arr.push(item); + } + } catch (err) { + expect(err).assertEqual("Error:Cannot create new stack") + } + expect(arr.length).assertEqual(0); + }) + + it('SR000GGR49_test_push032', 0, function () { + let stack = new Stack(); + for (let i = 0; i < 1000; i++) { + stack.push(i); + } + let res = stack.length; + expect(res).assertEqual(1000); + let res1 = stack.peek(); + expect(res1).assertEqual(999); + }) +}) \ No newline at end of file diff --git a/compileruntime/util_lib_standard/src/main/js/test/TreeMap.test.js b/compileruntime/util_lib_standard/src/main/js/test/TreeMap.test.js new file mode 100644 index 0000000000000000000000000000000000000000..1507aa74c070aec3e3c4913532fb182f817551f9 --- /dev/null +++ b/compileruntime/util_lib_standard/src/main/js/test/TreeMap.test.js @@ -0,0 +1,404 @@ +/* + * Copyright (C) 2021 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. + */ +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' +import {TreeMap} from '@ohos.treemap' + +describe('TreeMapTest', function () { + it('SR000GGR3H_testConstructor001', 0, function () { + try{ + let treemap = new TreeMap(); + expect(treemap != undefined).assertEqual(true) + }catch(err){ + expect(err).assertEqual("Error: Cannot create new TreeMap") + } + }) + + it('SR000GGR3H_testSet002_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"四"); + let res = treemap.get(0); + expect(res).assertEqual("四") + }) + + it('SR000GGR3H_testSet002_2', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,8); + let res = treemap.get(0) + expect(res).assertEqual(8) + }) + + it('SR000GGR3H_testSet002_3', 0, function () { + let treemap = new TreeMap(); + let a=[1,2,3,4] + treemap.set(0,a); + let res = treemap.get(0) + expect(res).assertEqual(a) + }) + + it('SR000GGR3H_testSet002_4', 0, function () { + let treemap = new TreeMap(); + let a={name:"lala",age:"13"} + treemap.set(0,a); + let res = treemap.get(0) + expect(res).assertEqual(a) + }) + + it('SR000GGR3H_testSet002_5', 0, function () { + let treemap = new TreeMap(); + treemap.set(2,"*"); + let res = treemap.get(2) + expect(res).assertEqual("*") + }) + + it('SR000GGR3H_testSet002_6', 0, function () { + let treemap = new TreeMap(); + treemap.set(-2,3.14); + let res = treemap.get(-2) + expect(res).assertEqual(3.14) + }) + + it('SR000GGR3H_testSet002_7', 0, function () { + let treemap = new TreeMap(); + treemap.set(100,true); + let res = treemap.get(100) + expect(res).assertEqual(true) + }) + + it('SR000GGR3H_testSet002_8', 0, function () { + let treemap = new TreeMap(); + treemap.set(100,""); + let res = treemap.get(100) + expect(res).assertEqual("") + }) + + it('SR000GGR3H_testSet002_9', 0, function () { + let treemap = new TreeMap(); + treemap.set("a","四"); + let res = treemap.get("a") + expect(res).assertEqual("四") + }) + + it('SR000GGR3H_testSet002_10', 0, function () { + let treemap = new TreeMap(); + for(let i = 0; i < 10000; i++){ + treemap.set(0,8); + } + let res = treemap.get(0) + expect(res).assertEqual(8) + expect(treemap.length).assertEqual(1) + }) + + it('SR000GGR3H_testHasKey003', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + let res = treemap.hasKey(1); + expect(res).assertEqual(true); + let res1 = treemap.hasKey(6); + expect(res1).assertEqual(false); + }) + + it('SR000GGR3H_testHasValue004', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let res = treemap.hasValue("a"); + expect(res).assertEqual(true); + let res1 = treemap.hasValue("d"); + expect(res1).assertEqual(false); + }) + + it('SR000GGR3H_testGet005_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let res = treemap.get(1); + expect(res).assertEqual("b"); + }) + + it('SR000GGR3H_testGet005_2', 0, function () { + let treemap = new TreeMap(); + let res = treemap.get(1); + expect(res).assertEqual(null); + }) + + it('SR000GGR3H_testGet005_3', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.clear(); + expect(treemap.length).assertEqual(0) + let res = treemap.get(0); + expect(res).assertEqual(null); + }) + + it('SR000GGR3H_testGetFirstKey006_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let res = treemap.getFirstKey(); + expect(res).assertEqual(0); + }) + + it('SR000GGR3H_testGetFirstKey006_2', 0, function () { + let treemap = new TreeMap(); + try{ + let res = treemap.getFirstKey(); + }catch(err){ + expect(err).assertEqual("Error: don't find this key,this tree is null"); + } + }) + + it('SR000GGR3H_testGetLastKey007_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let res = treemap.getLastKey(); + expect(res).assertEqual(2); + }) + + it('SR000GGR3H_testGetLastKey007_2', 0, function () { + let treemap = new TreeMap(); + try{ + let res = treemap.getLastKey(); + }catch(err){ + expect(err).assertEqual("Error: don't find this key,this tree is null"); + } + }) + + it('SR000GGR3H_testSetAll008', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let treemap1 = new TreeMap(); + treemap1.set(0,"a"); + treemap1.set(1,"b"); + treemap.setAll(treemap1); + let res=treemap1.get(0); + expect(res).assertEqual("a"); + let res1=treemap1.get(1); + expect(res1).assertEqual("b"); + let res2=treemap1.get(2); + expect(res2).assertEqual("c"); + }) + + it('SR000GGR3H_testRemove009_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + let res = treemap.remove(1); + expect(res).assertEqual("b"); + }) + + it('SR000GGR3H_testRemove009_2', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + let res = treemap.remove(1); + expect(res).assertEqual(null); + }) + + it('SR000GGR3H_testClear010_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.clear(); + let res = treemap.length; + expect(res).assertEqual(0) + }) + + it('SR000GGR3H_testClear010_2', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.clear(); + let res = treemap.length; + expect(res).assertEqual(0) + treemap.set(3,"d"); + let va = treemap.get(3); + expect(va).assertEqual("d") + }) + + it('SR000GGR3H_testGetLowerKey011_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.getLowerKey(2); + expect(res).assertEqual(1) + }) + + it('SR000GGR3H_testGetLowerKey011_2', 0, function () { + let treemap = new TreeMap(); + try{ + let res = treemap.getLowerKey(2); + }catch(err){ + expect(err).assertEqual("Error: don't find this key,this node is undefine") + } + }) + + it('SR000GGR3H_testGetHigherKey012_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.getHigherKey(2); + expect(res).assertEqual(3) + }) + + it('SR000GGR3H_testGetHigherKey012_2', 0, function () { + let treemap = new TreeMap(); + try{ + let res = treemap.getHigherKey(2); + }catch(err){ + expect(err).assertEqual("Error: don't find this key,this node is undefine") + } + }) + + it('SR000GGR3H_testKeys013', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.keys(); + expect(res.next().value).assertEqual(0); + expect(res.next().value).assertEqual(1); + expect(res.next().value).assertEqual(2); + expect(res.next().value).assertEqual(3); + expect(res.next().value).assertEqual(4); + }) + + it('SR000GGR3H_testValues014', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.values(); + expect(res.next().value).assertEqual("a"); + expect(res.next().value).assertEqual("b"); + expect(res.next().value).assertEqual("c"); + expect(res.next().value).assertEqual("d"); + expect(res.next().value).assertEqual("g"); + }) + + it('SR000GGR3H_testReplace015_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.replace(2,"B"); + let res1 = treemap.get(2); + expect(res1).assertEqual("B"); + }) + + it('SR000GGR3H_testReplace015_2', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + let res = treemap.replace(2,"B"); + expect(res).assertEqual(false); + let res1 = treemap.get(2); + expect(res1).assertEqual(null); + }) + + it('SR000GGR3H_testLength016_1', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let res = treemap.length; + expect(res).assertEqual(5) + }) + + it('SR000GGR3H_testLength016_2', 0, function () { + let treemap = new TreeMap(); + let res = treemap.length; + expect(res).assertEqual(0) + + }) + + it('SR000GGR3H_testForEach017', 0, function () { + let treemap = new TreeMap(); + treemap.set(0,"a"); + treemap.set(1,"b"); + treemap.set(2,"c"); + treemap.set(3,"d"); + treemap.set(4,"g"); + let arr1=[]; + let arr2 = []; + treemap.forEach((value,key)=>{ + arr1.push(value); + }) + treemap.forEach((value,key)=>{ + arr2.push(key); + }) + let arr=["a","b","c","d","g"]; + for(let i=0;i{ + arr1.push(value); + }) + let arr=[0,1,2,3,4]; + for(let i=0;i{ + arr.push(item); + + }) + let a=["四","a",1,"b","c",1]; + for(let i=0;i{ + arr.push(item); + }) + expect(arr.length).assertEqual(1) + }) + it('SR000GGR47_testRemoveByRange032', 0, function () { + let vector = new Vector(); + vector.add("四"); + vector.add("三"); + vector.add(1); + vector.add('a'); + vector.add(1); + vector.add("b"); + vector.add("c"); + vector.add(1); + let capacity = vector.getCapacity(); + try{ + vector.removeByRange(-1,capacity); + }catch(err){ + expect(err).assertEqual("Error: Vector: set out-of-bounds") + } + }) + it('SR000GGR47_testRemoveByRange033', 0, function () { + let vector = new Vector(); + vector.add("四"); + vector.add("三"); + vector.add(1); + vector.add('a'); + vector.add(1); + vector.add("b"); + vector.add("c"); + vector.add(1); + let capacity = vector.getCapacity(); + vector.removeByRange(0,capacity+1); + let length = vector.length; + expect(length).assertEqual(1) + }) + it('SR000GGR47_testSetLength034', 0, function () { + let vector = new Vector(); + vector.add("四"); + vector.add("三"); + vector.add(1); + vector.add('a'); + vector.add(1); + vector.add("b"); + vector.add("c"); + vector.add(1); + vector.setLength(5); + let res1 = vector.length + expect(res1).assertEqual(5) + }) + it('SR000GGR47_testReplaceAllElements035', 0, function () { + let vector = new Vector(); + vector.add(4); + vector.add(3); + vector.add(1); + vector.add(2); + vector.add(14); + vector.replaceAllElements((item, index) => { + return (item = 2 * item); + }); + let arr=[]; + vector.forEach((item,index)=>{ + arr.push(item); + }) + let a=[8,6,2,4,28]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + expect(arr.length).assertEqual(0) + }) + it('SR000GGR47_testConstructor038', 0, function () { + let vector = new Vector(); + vector.add(4); + vector.add(3); + vector.add(1); + vector.add(2); + vector.add(14); + let res = vector.subVector(2,4); + let arr=[]; + res.forEach((item,index)=>{ + arr.push(item); + }) + let a=[1,2]; + for(let i=0;i{ + arr.push(item); + }) + let a=[4,3,1,2,14]; + for(let i=0;i a-b); + let arr=[]; + vector.forEach((item,index)=>{ + arr.push(item); + }) + let a=[1,2,3,4,14]; + for(let i=0;i