1/*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { ListUtil } from '../../../src/utils/ListUtil';
17import { describe, it } from 'mocha';
18import { assert, expect } from 'chai';
19
20describe('unit test for ListUtil.ts', function () {
21  describe('get init list test', function () {
22    it('check init list input value bad', function () {
23      let arr = ListUtil.getInitList(-1);
24      assert.isTrue(arr.length === 0);
25    });
26
27    it('check init list input value zero', function () {
28      let arr = ListUtil.getInitList(0);
29      assert.isTrue(arr.length === 0);
30    });
31
32    it('check init list input value NaN', function () {
33      let arr = ListUtil.getInitList(NaN);
34      assert.isTrue(arr.length === 0);
35    });
36
37    it('check init list input value MAX_INIT_LEN', function () {
38      let arr = ListUtil.getInitList(ListUtil.MAX_INIT_LEN);
39      assert.isTrue(arr.length === ListUtil.MAX_INIT_LEN);
40    });
41
42    it('check init list input value bigger than MAX_INIT_LEN', function () {
43      let arr = ListUtil.getInitList(ListUtil.MAX_INIT_LEN + 1);
44      assert.isTrue(arr.length === 0);
45    });
46
47    it('check init list input normal value', function () {
48      let arr = ListUtil.getInitList(26);
49
50      arr.forEach(((value, index) => {
51        assert.strictEqual(value, index);
52      }));
53    });
54  });
55
56  describe('list shuffle test', function () {
57    it('check shuffle invalid list', function () {
58      let arr = undefined;
59      ListUtil.shuffle(arr);
60
61      assert.isTrue(true);
62    });
63
64    it('check shuffle list', function () {
65      let arr = ListUtil.getInitList(26);
66      ListUtil.shuffle(arr);
67
68      let isShuffled = false;
69      for (let i = 1; i < arr.length; i++) {
70        if (arr[i] !== i) {
71          isShuffled = true;
72        }
73      }
74
75      assert.isTrue(isShuffled);
76    });
77  });
78
79  describe('list unique merge test', function () {
80    it('check unique merge two undefined list', function () {
81      let arr1 = undefined;
82      let arr2 = undefined;
83
84      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2);
85      assert.isTrue(arrUnique.length === 0);
86    });
87
88    it('check unique merge two unique list', function () {
89      let arr1 = ['1', '2', '3'];
90      let arr2 = ['4', '5', '6'];
91
92      const expectedArr = ['1', '2', '3', '4', '5', '6'];
93
94      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2);
95      assert.isTrue(arrUnique.length === expectedArr.length);
96      arrUnique.forEach((value, index) => {
97        assert.strictEqual(value, expectedArr[index]);
98      });
99    });
100
101    it('check unique merge two not unique list', function () {
102      let arr1 = ['1', '2', '3', '4'];
103      let arr2 = ['4', '5', '4', '6'];
104
105      const expectedArr = ['1', '2', '3', '4', '5', '6'];
106
107      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2);
108      assert.isTrue(arrUnique.length === expectedArr.length);
109      arrUnique.forEach((value, index) => {
110        assert.strictEqual(value, expectedArr[index]);
111      });
112    });
113
114    it('check unique merge three undefined list', function () {
115      let arr1 = undefined;
116      let arr2 = undefined;
117      let arr3 = undefined;
118
119      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3);
120      assert.isTrue(arrUnique.length === 0);
121    });
122
123    it('check unique merge three unique list', function () {
124      let arr1 = ['1', '2', '3'];
125      let arr2 = ['4', '5', '6'];
126      let arr3 = ['7', '8', '9'];
127
128      const expectedArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
129
130      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3);
131      assert.isTrue(arrUnique.length === expectedArr.length);
132      arrUnique.forEach((value, index) => {
133        assert.strictEqual(value, expectedArr[index]);
134      });
135    });
136
137    it('check unique merge three not unique list', function () {
138      let arr1 = ['1', '2', '3', '4'];
139      let arr2 = ['4', '5', '4', '6'];
140      let arr3 = ['6', '7', '8', '8'];
141
142      const expectedArr = ['1', '2', '3', '4', '5', '6', '7', '8'];
143
144      const arrUnique = ListUtil.uniqueMergeList(arr1, arr2, arr3);
145      assert.isTrue(arrUnique.length === expectedArr.length);
146      arrUnique.forEach((value, index) => {
147        assert.strictEqual(value, expectedArr[index]);
148      });
149    });
150  });
151});
152
153describe('ListUtil.uniqueMergeList', () => {
154  it('should return an empty list when all lists are empty', () => {
155    const result = ListUtil.uniqueMergeList([], [], []);
156    expect(result).to.deep.equal([]);
157  });
158
159  it('should return unique elements when merging two lists with no common elements', () => {
160    const result = ListUtil.uniqueMergeList(['a', 'b'], ['c', 'd']);
161    expect(result).to.deep.equal(['a', 'b', 'c', 'd']);
162  });
163
164  it('should return unique elements when merging two lists with some common elements', () => {
165    const result = ListUtil.uniqueMergeList(['a', 'b', 'c'], ['b', 'c', 'd']);
166    expect(result).to.deep.equal(['a', 'b', 'c', 'd']);
167  });
168
169  it('should return unique elements when merging three lists with some common elements', () => {
170    const result = ListUtil.uniqueMergeList(['a', 'b'], ['b', 'c'], ['c', 'd']);
171    expect(result).to.deep.equal(['a', 'b', 'c', 'd']);
172  });
173
174  it('should handle null or undefined inputs gracefully', () => {
175    const result = ListUtil.uniqueMergeList(['a', 'b'], undefined, ['b', 'c']);
176    expect(result).to.deep.equal(['a', 'b', 'c']);
177  });
178
179  it('should return a list with unique elements from a single list', () => {
180    const result = ListUtil.uniqueMergeList(['a', 'b', 'a', 'c', 'b'], []);
181    expect(result).to.deep.equal(['a', 'b', 'c']);
182  });
183
184  it('should return a list with undefied third list', () => {
185    const result = ListUtil.uniqueMergeList(['a', 'b'], ['c', 'd'], undefined);
186    expect(result).to.deep.equal(['a', 'b', 'c', 'd']);
187  });
188});