13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciimport { ListUtil } from '../../../src/utils/ListUtil'; 173af6ab5fSopenharmony_ciimport { describe, it } from 'mocha'; 183af6ab5fSopenharmony_ciimport { assert, expect } from 'chai'; 193af6ab5fSopenharmony_ci 203af6ab5fSopenharmony_cidescribe('unit test for ListUtil.ts', function () { 213af6ab5fSopenharmony_ci describe('get init list test', function () { 223af6ab5fSopenharmony_ci it('check init list input value bad', function () { 233af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(-1); 243af6ab5fSopenharmony_ci assert.isTrue(arr.length === 0); 253af6ab5fSopenharmony_ci }); 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_ci it('check init list input value zero', function () { 283af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(0); 293af6ab5fSopenharmony_ci assert.isTrue(arr.length === 0); 303af6ab5fSopenharmony_ci }); 313af6ab5fSopenharmony_ci 323af6ab5fSopenharmony_ci it('check init list input value NaN', function () { 333af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(NaN); 343af6ab5fSopenharmony_ci assert.isTrue(arr.length === 0); 353af6ab5fSopenharmony_ci }); 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci it('check init list input value MAX_INIT_LEN', function () { 383af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(ListUtil.MAX_INIT_LEN); 393af6ab5fSopenharmony_ci assert.isTrue(arr.length === ListUtil.MAX_INIT_LEN); 403af6ab5fSopenharmony_ci }); 413af6ab5fSopenharmony_ci 423af6ab5fSopenharmony_ci it('check init list input value bigger than MAX_INIT_LEN', function () { 433af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(ListUtil.MAX_INIT_LEN + 1); 443af6ab5fSopenharmony_ci assert.isTrue(arr.length === 0); 453af6ab5fSopenharmony_ci }); 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_ci it('check init list input normal value', function () { 483af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(26); 493af6ab5fSopenharmony_ci 503af6ab5fSopenharmony_ci arr.forEach(((value, index) => { 513af6ab5fSopenharmony_ci assert.strictEqual(value, index); 523af6ab5fSopenharmony_ci })); 533af6ab5fSopenharmony_ci }); 543af6ab5fSopenharmony_ci }); 553af6ab5fSopenharmony_ci 563af6ab5fSopenharmony_ci describe('list shuffle test', function () { 573af6ab5fSopenharmony_ci it('check shuffle invalid list', function () { 583af6ab5fSopenharmony_ci let arr = undefined; 593af6ab5fSopenharmony_ci ListUtil.shuffle(arr); 603af6ab5fSopenharmony_ci 613af6ab5fSopenharmony_ci assert.isTrue(true); 623af6ab5fSopenharmony_ci }); 633af6ab5fSopenharmony_ci 643af6ab5fSopenharmony_ci it('check shuffle list', function () { 653af6ab5fSopenharmony_ci let arr = ListUtil.getInitList(26); 663af6ab5fSopenharmony_ci ListUtil.shuffle(arr); 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ci let isShuffled = false; 693af6ab5fSopenharmony_ci for (let i = 1; i < arr.length; i++) { 703af6ab5fSopenharmony_ci if (arr[i] !== i) { 713af6ab5fSopenharmony_ci isShuffled = true; 723af6ab5fSopenharmony_ci } 733af6ab5fSopenharmony_ci } 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ci assert.isTrue(isShuffled); 763af6ab5fSopenharmony_ci }); 773af6ab5fSopenharmony_ci }); 783af6ab5fSopenharmony_ci 793af6ab5fSopenharmony_ci describe('list unique merge test', function () { 803af6ab5fSopenharmony_ci it('check unique merge two undefined list', function () { 813af6ab5fSopenharmony_ci let arr1 = undefined; 823af6ab5fSopenharmony_ci let arr2 = undefined; 833af6ab5fSopenharmony_ci 843af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2); 853af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === 0); 863af6ab5fSopenharmony_ci }); 873af6ab5fSopenharmony_ci 883af6ab5fSopenharmony_ci it('check unique merge two unique list', function () { 893af6ab5fSopenharmony_ci let arr1 = ['1', '2', '3']; 903af6ab5fSopenharmony_ci let arr2 = ['4', '5', '6']; 913af6ab5fSopenharmony_ci 923af6ab5fSopenharmony_ci const expectedArr = ['1', '2', '3', '4', '5', '6']; 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2); 953af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === expectedArr.length); 963af6ab5fSopenharmony_ci arrUnique.forEach((value, index) => { 973af6ab5fSopenharmony_ci assert.strictEqual(value, expectedArr[index]); 983af6ab5fSopenharmony_ci }); 993af6ab5fSopenharmony_ci }); 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ci it('check unique merge two not unique list', function () { 1023af6ab5fSopenharmony_ci let arr1 = ['1', '2', '3', '4']; 1033af6ab5fSopenharmony_ci let arr2 = ['4', '5', '4', '6']; 1043af6ab5fSopenharmony_ci 1053af6ab5fSopenharmony_ci const expectedArr = ['1', '2', '3', '4', '5', '6']; 1063af6ab5fSopenharmony_ci 1073af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2); 1083af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === expectedArr.length); 1093af6ab5fSopenharmony_ci arrUnique.forEach((value, index) => { 1103af6ab5fSopenharmony_ci assert.strictEqual(value, expectedArr[index]); 1113af6ab5fSopenharmony_ci }); 1123af6ab5fSopenharmony_ci }); 1133af6ab5fSopenharmony_ci 1143af6ab5fSopenharmony_ci it('check unique merge three undefined list', function () { 1153af6ab5fSopenharmony_ci let arr1 = undefined; 1163af6ab5fSopenharmony_ci let arr2 = undefined; 1173af6ab5fSopenharmony_ci let arr3 = undefined; 1183af6ab5fSopenharmony_ci 1193af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3); 1203af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === 0); 1213af6ab5fSopenharmony_ci }); 1223af6ab5fSopenharmony_ci 1233af6ab5fSopenharmony_ci it('check unique merge three unique list', function () { 1243af6ab5fSopenharmony_ci let arr1 = ['1', '2', '3']; 1253af6ab5fSopenharmony_ci let arr2 = ['4', '5', '6']; 1263af6ab5fSopenharmony_ci let arr3 = ['7', '8', '9']; 1273af6ab5fSopenharmony_ci 1283af6ab5fSopenharmony_ci const expectedArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9']; 1293af6ab5fSopenharmony_ci 1303af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3); 1313af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === expectedArr.length); 1323af6ab5fSopenharmony_ci arrUnique.forEach((value, index) => { 1333af6ab5fSopenharmony_ci assert.strictEqual(value, expectedArr[index]); 1343af6ab5fSopenharmony_ci }); 1353af6ab5fSopenharmony_ci }); 1363af6ab5fSopenharmony_ci 1373af6ab5fSopenharmony_ci it('check unique merge three not unique list', function () { 1383af6ab5fSopenharmony_ci let arr1 = ['1', '2', '3', '4']; 1393af6ab5fSopenharmony_ci let arr2 = ['4', '5', '4', '6']; 1403af6ab5fSopenharmony_ci let arr3 = ['6', '7', '8', '8']; 1413af6ab5fSopenharmony_ci 1423af6ab5fSopenharmony_ci const expectedArr = ['1', '2', '3', '4', '5', '6', '7', '8']; 1433af6ab5fSopenharmony_ci 1443af6ab5fSopenharmony_ci const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3); 1453af6ab5fSopenharmony_ci assert.isTrue(arrUnique.length === expectedArr.length); 1463af6ab5fSopenharmony_ci arrUnique.forEach((value, index) => { 1473af6ab5fSopenharmony_ci assert.strictEqual(value, expectedArr[index]); 1483af6ab5fSopenharmony_ci }); 1493af6ab5fSopenharmony_ci }); 1503af6ab5fSopenharmony_ci }); 1513af6ab5fSopenharmony_ci}); 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_cidescribe('ListUtil.uniqueMergeList', () => { 1543af6ab5fSopenharmony_ci it('should return an empty list when all lists are empty', () => { 1553af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList([], [], []); 1563af6ab5fSopenharmony_ci expect(result).to.deep.equal([]); 1573af6ab5fSopenharmony_ci }); 1583af6ab5fSopenharmony_ci 1593af6ab5fSopenharmony_ci it('should return unique elements when merging two lists with no common elements', () => { 1603af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b'], ['c', 'd']); 1613af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c', 'd']); 1623af6ab5fSopenharmony_ci }); 1633af6ab5fSopenharmony_ci 1643af6ab5fSopenharmony_ci it('should return unique elements when merging two lists with some common elements', () => { 1653af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b', 'c'], ['b', 'c', 'd']); 1663af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c', 'd']); 1673af6ab5fSopenharmony_ci }); 1683af6ab5fSopenharmony_ci 1693af6ab5fSopenharmony_ci it('should return unique elements when merging three lists with some common elements', () => { 1703af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b'], ['b', 'c'], ['c', 'd']); 1713af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c', 'd']); 1723af6ab5fSopenharmony_ci }); 1733af6ab5fSopenharmony_ci 1743af6ab5fSopenharmony_ci it('should handle null or undefined inputs gracefully', () => { 1753af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b'], undefined, ['b', 'c']); 1763af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c']); 1773af6ab5fSopenharmony_ci }); 1783af6ab5fSopenharmony_ci 1793af6ab5fSopenharmony_ci it('should return a list with unique elements from a single list', () => { 1803af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b', 'a', 'c', 'b'], []); 1813af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c']); 1823af6ab5fSopenharmony_ci }); 1833af6ab5fSopenharmony_ci 1843af6ab5fSopenharmony_ci it('should return a list with undefied third list', () => { 1853af6ab5fSopenharmony_ci const result = ListUtil.uniqueMergeList(['a', 'b'], ['c', 'd'], undefined); 1863af6ab5fSopenharmony_ci expect(result).to.deep.equal(['a', 'b', 'c', 'd']); 1873af6ab5fSopenharmony_ci }); 1883af6ab5fSopenharmony_ci});