Array.uts 23.4 KB
Newer Older
Y
yurj26 已提交
1 2
import { describe, test, expect, Result } from './tests.uts'

3

M
mahaifeng 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

export function testArray() : Result {
  return describe("Array", () => {

    test('constructor', () => {
      // 构造器测试
      // #TEST Array.Constructor
      let a1 = [1, 2, 3]
      let a2 = [1, '2', 3]
      console.log(a1) //[1, 2, 3]
      console.log(a2) // [1, '2', 3]
      // #END
      expect(a1).toEqual([1, 2, 3]);
      expect(a2).toEqual([1, '2', 3]);
      let a3 = new Array(1, 2, 3);

      // swift 中字面量创建数组,仅支持同一类型的元素
      // #ifndef APP-IOS
      expect(a3).toEqual(new Array(1, 2, 3));
      let a4 = new Array<any>(1, '2', 3);
      expect(a4).toEqual(new Array<any>(1, '2', 3));
      let a5 = Array(1, 2, 3);
      expect(a5).toEqual(Array(1, 2, 3));
      let a6 = Array<any>(1, '2', '3')
      expect(a6).toEqual(Array<any>(1, '2', '3'));
      // #endif

    })

    test('equals', () => {
      // 构造器测试
      let a1 = [1, 2, 3]
      let a2 = [1, 2, 3]
      let equalsRet = (a1 == a2)
      console.log(equalsRet)
      // #ifndef APP-IOS
      expect(equalsRet).toEqual(false);
      // #endif
      // #ifdef APP-IOS
      expect(equalsRet).toEqual(true);
      // #endif


    })

    test('convert-native', () => {
M
mahaifeng 已提交
50
      // #TEST Array.toKotlinList
M
mahaifeng 已提交
51 52 53 54 55 56 57
      // #ifdef APP-ANDROID
      let utsArray = ["1", 2, 3.0]
      let javaArray = utsArray.toTypedArray();
      let kotlinArray = utsArray.toKotlinList()

      let convertArrayFromJava = Array.fromNative(javaArray);
      let convertArrayFromKotlin = Array.fromNative(kotlinArray);
M
mahaifeng 已提交
58 59 60 61
      console.log(convertArrayFromJava[0] == convertArrayFromKotlin[0])//true

      console.log(convertArrayFromJava[0])//"1"
      // #END
M
mahaifeng 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      expect(convertArrayFromJava[0] == convertArrayFromKotlin[0]).toEqual(true);
      expect(convertArrayFromJava[0]).toEqual("1");
      // #endif

    })

    test('length', () => {
      // #TEST Array.length
      const arr = ['shoes', 'shirts', 'socks', 'sweaters'];
      console.log(arr.length)//4
      console.log(arr[1])//'shoes'
      console.log(arr[1])//'shirts'
      // #END
      expect(arr.length).toEqual(4);
      expect(arr[0]).toEqual('shoes');
      expect(arr[1]).toEqual('shirts');
      // expect(arr[4]).toEqual(null);
      const numbers : number[] = [1, 2, 3, 4, 5];
      if (numbers.length > 3) {
        numbers.length = 3;
      }
      expect(numbers.length).toEqual(3);
      expect(numbers).toEqual([1, 2, 3]);
      expect([].length).toEqual(0);

      // 1. web: 最大长度 2^32-1
      // 超出边界报错: RangeError: Invalid array length
      // 2. kotlin: 最大长度 2^31-1
      // 超出边界报错: Error: targetMethod error::java.lang.OutOfMemoryError: Failed to allocate a 420546432 byte allocation with 6291456 free bytes and 300MB until OOM, target footprint 295113520, growth limit 603979776
      // 3. swift: 最大长度和内存有关
      // 超出边界没有返回信息
    })
    test("concat", () => {
M
mahaifeng 已提交
95 96 97 98 99 100 101 102
      // #TEST Array.concat,Array.concat_1
      let ret = ['a', 'b', 'c'].concat(['d', 'e', 'f'])
      console.log(ret) //["a", "b", "c", "d", "e", "f"]
      let ret1 = [1, 2, 3].concat([4, 5, 6])
      console.log(ret1)//[1, 2, 3, 4, 5, 6]
      let ret2 = [''].concat([''])//
      console.log(ret2)//["", ""]

M
mahaifeng 已提交
103 104 105 106
      const num1 = [1, 2, 3];
      const num2 = [4, 5, 6];
      const num3 = [7, 8, 9];
      const numbers = num1.concat(num2, num3);
M
mahaifeng 已提交
107 108 109
      console.log(numbers)//[1, 2, 3, 4, 5, 6, 7, 8, 9]
      // #END

M
mahaifeng 已提交
110
      expect(numbers).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
M
mahaifeng 已提交
111 112 113 114
      expect(ret).toEqual(["a", "b", "c", "d", "e", "f"]);
      expect(ret1).toEqual([1, 2, 3, 4, 5, 6]);
      expect(ret2).toEqual(["", ""]);

M
mahaifeng 已提交
115 116
    })
    test("copyWithin", () => {
M
mahaifeng 已提交
117
      // #TEST Array.copyWithin
M
mahaifeng 已提交
118
      const arr = ['a', 'b', 'c', 'd', 'e'];
M
mahaifeng 已提交
119 120 121 122
      let ret1 = arr.copyWithin(0, 3, 4)
      console.log(ret1)//["d", "b", "c", "d", "e"]
      let ret2 = arr.copyWithin(1, 3)
      console.log(ret2)//["d", "d", "e", "d", "e"]
M
mahaifeng 已提交
123
      const arr2 = [1, 2, 3, 4, 5];
M
mahaifeng 已提交
124 125 126 127 128 129 130 131 132
      let ret3 = arr2.copyWithin(-2)
      console.log(ret3) //[1, 2, 3, 1, 2]
      let ret4 = arr2.copyWithin(-2, -3, -1)
      console.log(ret4) //[1, 2, 3, 3, 1]
      // #END
      expect(ret1).toEqual(["d", "b", "c", "d", "e"]);
      expect(ret2).toEqual(["d", "d", "e", "d", "e"]);
      expect(ret3).toEqual([1, 2, 3, 1, 2]);
      expect(ret4).toEqual([1, 2, 3, 3, 1]);
M
mahaifeng 已提交
133 134
    })
    test("every", () => {
M
mahaifeng 已提交
135
      // #TEST Array.every,Array.every_1,Array.every_2,Array.every_3
M
mahaifeng 已提交
136 137
      const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40;
      const array1 : number[] = [1, 30, 39, 29, 10, 13];
M
mahaifeng 已提交
138 139
      console.log(array1.every(isBelowThreshold));// true

M
mahaifeng 已提交
140
      const array2 : number[] = [1, 30, 39, 29, 10, 13, 41];
M
mahaifeng 已提交
141 142
      console.log(array2.every(isBelowThreshold));// false

M
mahaifeng 已提交
143
      const array3 : number[] = [1, 2, 3];
M
mahaifeng 已提交
144 145 146 147 148 149
      array3.every((element : number, index : number, array : number[]) : boolean => {
        console.log(array[index])//1=>2->3
        return true;
      })

      // #END
M
mahaifeng 已提交
150 151 152 153
      array3.every((element : number, index : number, array : number[]) : boolean => {
        expect(array[index]).toEqual(element);
        return true;
      })
M
mahaifeng 已提交
154 155
      expect(array1.every(isBelowThreshold)).toEqual(true);
      expect(array2.every(isBelowThreshold)).toEqual(false);
M
mahaifeng 已提交
156 157
    })
    test("fill", () => {
M
mahaifeng 已提交
158
      // #TEST Array.fill
M
mahaifeng 已提交
159
      const array1 : number[] = [1, 2, 3, 4];
M
mahaifeng 已提交
160 161 162 163 164 165 166 167 168
      console.log(array1.fill(0, 2, 4)); //[1, 2, 0, 0]
      console.log(array1.fill(5, 1)); //[1, 5, 5, 5]
      console.log(array1.fill(6)); //[6, 6, 6, 6]
      const array2 : number[] = [1, 2, 3];
      console.log(array2.fill(4))//[4, 4, 4]
      const array3 : number[] = [0, 0]
      console.log(array3.fill(1, null))//[1, 1]
      console.log(array3.fill(1, 0, 1.5))//([1, 1]);
      // #END
M
mahaifeng 已提交
169 170 171 172 173 174 175 176
      expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
      expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]);
      expect(array1.fill(6)).toEqual([6, 6, 6, 6]);
      expect(array2.fill(4)).toEqual([4, 4, 4]);
      expect(array3.fill(1, null)).toEqual([1, 1]);
      expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
    })
    test("filter", () => {
M
mahaifeng 已提交
177
      // #TEST Array.filter,Array.filter_1,Array.filter_2,Array.filter_3
M
mahaifeng 已提交
178 179
      const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
      const result = words.filter((word : string) : boolean => word.length > 6);
M
mahaifeng 已提交
180 181
      console.log(result);// ["exuberant", "destruction", "present"]

M
mahaifeng 已提交
182 183 184 185 186 187 188 189 190 191
      const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
      const isPrime = array1.filter((num : number) : boolean => {
        for (let i = 2; num > i; i++) {
          // swift里,基础类型暂不支持!==,===对比
          if (num % i == 0) {
            return false;
          }
        }
        return num > 1;
      })
M
mahaifeng 已提交
192 193
      console.log(isPrime)//[2, 3, 5, 7, 11, 13]

M
mahaifeng 已提交
194 195 196
      const array2 : number[] = [1, 2, 3];
      array2.filter((element : number, index : number, array : number[]) : boolean => {
        expect(array[index]).toEqual(element);
M
mahaifeng 已提交
197
        console.log(array[index])//1=>2=>3
M
mahaifeng 已提交
198 199
        return true;
      })
M
mahaifeng 已提交
200 201 202 203 204 205 206
      // #END
      array2.filter((element : number, index : number, array : number[]) : boolean => {
        expect(array[index]).toEqual(element);
        return true;
      })
      expect(result).toEqual(["exuberant", "destruction", "present"]);
      expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]);
M
mahaifeng 已提交
207 208
    })
    test("find", () => {
M
mahaifeng 已提交
209
      // #TEST Array.find_2
M
mahaifeng 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      const array1 : number[] = [5, 12, 8, 130, 44];
      const found1 = array1.find((element : number) : boolean => element > 10);
      console.log(found1) //12
      // #END
      expect(found1).toEqual(12);
      // #TEST Array.find_1
      const array3 : number[] = [5, 12, 8, 130, 44];
      const found2 = array3.find((element : number, index : number) : boolean => element < 5);
      console.log(found2) // null
      // #END
      expect(found2).toEqual(null);
      // #TEST Array.find
      let array2 : number[] = [1, 2, 3];
      array2.find((element : number, index : number, array : number[]) : boolean => {
        console.log(array[index]) //1=>2=>3
        return true;
      })
      // #END
      array2 = [1, 2, 3];
      array2.find((element : number, index : number, array : number[]) : boolean => {
        expect(array[index]).toEqual(element);
        return true;
      })
    })
    test("findIndex", () => {
M
mahaifeng 已提交
235
      // #TEST Array.findIndex_1,Array.findIndex_2,Array.findIndex
M
mahaifeng 已提交
236
      const array1 : number[] = [5, 12, 8, 130, 44];
M
mahaifeng 已提交
237 238 239
      let isLargeNumber = (element : number, index : number) : boolean => element > 13;
      console.log(isLargeNumber)//3

M
mahaifeng 已提交
240
      const array2 : number[] = [10, 11, 12];
M
mahaifeng 已提交
241 242
      console.log(array2.findIndex(isLargeNumber))//3

M
mahaifeng 已提交
243
      const array3 : number[] = [1, 2, 3];
M
mahaifeng 已提交
244 245 246 247 248 249 250
      array3.findIndex((element : number, index : number, array : number[]) : boolean => {
        console.log(array[index]) //1=>2=>3
        return true;
      })
      // #END
      expect(array2.findIndex(isLargeNumber)).toEqual(-1);
      expect(array1.findIndex(isLargeNumber)).toEqual(3);
M
mahaifeng 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264
      array3.findIndex((element : number, index : number, array : number[]) : boolean => {
        expect(array[index]).toEqual(element);
        return true;
      })
    })
    test("flat", () => {
      const arr1 : any[] = [0, 1, 2, [3, 4]];
      expect(arr1.flat()).toEqual([0, 1, 2, 3, 4]);
      const arr2 : any[] = [0, 1, 2, [[[3, 4]]]];
      expect(arr2.flat(2)).toEqual([0, 1, 2, [3, 4]]);
      const arr3 : any[] = [1, 2, [3, 4, [5, 6]]];
      expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]);
    })
    test("forEach", () => {
M
mahaifeng 已提交
265
      // #TEST Array.forEach,Array.forEach_1,Array.forEach_2
M
mahaifeng 已提交
266
      const array1 : string[] = ['a', 'b', 'c'];
M
mahaifeng 已提交
267 268 269 270
      array1.forEach(element => console.log(element));
      // expected output: "a"
      // expected output: "b"
      // expected output: "c"
M
mahaifeng 已提交
271 272 273 274 275
      const items : string[] = ['item1', 'item2', 'item3'];
      const copyItems : string[] = [];
      items.forEach((item : string) => {
        copyItems.push(item);
      });
M
mahaifeng 已提交
276 277 278 279 280
      console.log(copyItems)//['item1', 'item2', 'item3']
      // #END
      array1.forEach((element : string, index : number) => {
        expect(array1[index]).toEqual(element)
      });
M
mahaifeng 已提交
281 282 283
      expect(copyItems).toEqual(items)
    })
    test("includes", () => {
M
mahaifeng 已提交
284
      // #TEST Array.includes
M
mahaifeng 已提交
285
      const array1 : number[] = [1, 2, 3];
M
mahaifeng 已提交
286 287
      console.log(array1.includes(2))//true
      // #END
M
mahaifeng 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      expect(array1.includes(2)).toEqual(true);
      const pets : string[] = ['cat', 'dog', 'bat'];
      expect(pets.includes('cat')).toEqual(true);
      expect(pets.includes('at')).toEqual(false);
      const array2 : string[] = ['a', 'b', 'c'];
      expect(array2.includes('c', 3)).toEqual(false);
      expect(array2.includes('c', 100)).toEqual(false);

      type P = {
        x : number
        y : number
      }

      // #ifndef APP-IOS
      const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])) as P[]
      s[0].x += 0;
      const clearList = s.map((v : P, _, _a) : number => v.x)
      expect(clearList.includes(0)).toEqual(true);
      // #endif
      // #ifdef APP-IOS
      const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])!) as P[]
      s[0].x += 0;
      const clearList = s.map((v : P, index : number, _a) : number => v.x)
      expect(clearList.includes(0)).toEqual(true);
      // #endif

    })
    test("indexOf", () => {

      let raw = {}
      let arr = new Array<UTSJSONObject>()
      arr.push({});
      arr.push({});
      arr.push(raw);
      expect(arr.indexOf(raw)).toEqual(2);

M
mahaifeng 已提交
324
      // #TEST Array.indexOf
M
mahaifeng 已提交
325
      const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
M
mahaifeng 已提交
326 327 328 329 330 331 332 333 334

      console.log(beasts.indexOf('bison')); //  1


      console.log(beasts.indexOf('bison', 2));// 2

      console.log(beasts.indexOf('giraffe'));// -1
      // #END

M
mahaifeng 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
      expect(beasts.indexOf('bison')).toEqual(1);
      expect(beasts.indexOf('bison', 2)).toEqual(4);
      expect(beasts.indexOf('giraffe')).toEqual(-1);

      const indices : number[] = [];
      const array : string[] = ['a', 'b', 'a', 'c', 'a', 'd'];
      const element = 'a';
      let idx = array.indexOf(element);
      // swift里,基础类型暂不支持!==,===对比
      while (idx != -1) {
        indices.push(idx);
        idx = array.indexOf(element, idx + 1);
      }
      expect(indices).toEqual([0, 2, 4]);
    })
    test("join", () => {
M
mahaifeng 已提交
351
      // #TEST Array.join
M
mahaifeng 已提交
352
      const elements : string[] = ['Fire', 'Air', 'Water'];
M
mahaifeng 已提交
353 354 355 356 357 358 359
      let ret1 = elements.join()//Fire,Air,Water
      let ret2 = elements.join('') //FireAirWater
      let ret3 = elements.join('-')//Fire-Air-Water
      expect(ret1).toEqual("Fire,Air,Water");
      expect(ret2).toEqual("FireAirWater");
      expect(ret3).toEqual("Fire-Air-Water");
      // #END
M
mahaifeng 已提交
360 361 362 363 364 365 366 367 368 369
    })
    test("lastIndexOf", () => {

      let raw = {}
      let arr = new Array<UTSJSONObject>()
      arr.push({});
      arr.push({});
      arr.push(raw);
      expect(arr.lastIndexOf(raw)).toEqual(2);

M
mahaifeng 已提交
370
      // #TEST Array.lastIndexOf
M
mahaifeng 已提交
371
      const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
M
mahaifeng 已提交
372 373 374 375
      console.log(animals.lastIndexOf('Dodo'));//3
      console.log(animals.lastIndexOf('Tiger'));//1
      // #END

M
mahaifeng 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
      expect(animals.lastIndexOf('Dodo')).toEqual(3);
      expect(animals.lastIndexOf('Tiger')).toEqual(1);
      const array : number[] = [2, 5, 9, 2];
      let index = array.lastIndexOf(2);
      expect(index).toEqual(3);
      index = array.lastIndexOf(7);
      expect(index).toEqual(-1);
      index = array.lastIndexOf(2, 3);
      expect(index).toEqual(3);
      index = array.lastIndexOf(2, 2);
      expect(index).toEqual(0);




    })
    test("map", () => {
M
mahaifeng 已提交
393
      // #TEST Array.map,Array.map_1,Array.map_2
M
mahaifeng 已提交
394 395
      const array1 : number[] = [1, 4, 9, 16];
      const map1 = array1.map((x : number) : number => x * 2);
M
mahaifeng 已提交
396 397
      console.log(map1);
      // expected output: Array [2, 8, 18, 32]
M
mahaifeng 已提交
398 399
      const numbers : number[] = [1, 4, 9];
      const roots = numbers.map((num : number) : number => num + 1);
M
mahaifeng 已提交
400

M
mahaifeng 已提交
401 402

      const array2 : number[] = [1, 2, 3];
M
mahaifeng 已提交
403 404 405 406
      array2.map((element : number, index : number, array : number[]) => {
        console.log(array[index]) //1=>2=>3
      })
      // #END
M
mahaifeng 已提交
407 408 409
      array2.map((element : number, index : number, array : number[]) => {
        expect(array[index]).toEqual(element);
      })
M
mahaifeng 已提交
410 411 412
      expect(map1).toEqual([2, 8, 18, 32]);
      expect(numbers).toEqual([1, 4, 9]);
      expect(roots).toEqual([2, 5, 10]);
M
mahaifeng 已提交
413 414
    })
    test("pop", () => {
M
mahaifeng 已提交
415
      // #TEST Array.pop
M
mahaifeng 已提交
416
      const plants : string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
M
mahaifeng 已提交
417 418 419 420 421
      let ret1 = plants.pop()
      console.log(ret1)//"tomato"
      console.log(plants)//["broccoli", "cauliflower", "cabbage", "kale"]
      // #END
      expect(ret1).toEqual("tomato");
M
mahaifeng 已提交
422 423 424
      expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]);
      plants.pop();
      expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]);
M
mahaifeng 已提交
425

M
mahaifeng 已提交
426 427
    })
    test("push", () => {
M
mahaifeng 已提交
428
      // #TEST Array.push
M
mahaifeng 已提交
429 430
      const animals : string[] = ['pigs', 'goats', 'sheep'];
      const count = animals.push('cows');
M
mahaifeng 已提交
431 432 433
      console.log(count)//4
      console.log(animals) //['pigs', 'goats', 'sheep', 'cows']
      // #END
M
mahaifeng 已提交
434 435 436 437
      expect(count).toEqual(4);
      expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']);
      animals.push('chickens', 'cats', 'dogs');
      expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]);
M
mahaifeng 已提交
438

M
mahaifeng 已提交
439 440
    })
    test("reduce", () => {
M
mahaifeng 已提交
441
      // #TEST Array.reduce,Array.reduce_1,Array.reduce_2,Array.reduce_3,Array.reduce_4,Array.reduce_5
M
mahaifeng 已提交
442 443 444 445 446 447
      const array1 : number[] = [1, 2, 3, 4];
      const initialValue : number = 0;
      const sumWithInitial = array1.reduce(
        (previousValue : number, currentValue : number) : number => previousValue + currentValue,
        initialValue
      );
M
mahaifeng 已提交
448 449
      console.log(sumWithInitial)//10
      // #END
M
mahaifeng 已提交
450 451 452
      expect(sumWithInitial).toEqual(10);
    })
    test("shift", () => {
M
mahaifeng 已提交
453 454 455
      // #TEST Array.shift
      const array1 = [1, 2, 3];

M
mahaifeng 已提交
456
      const firstElement = array1.shift();
M
mahaifeng 已提交
457 458 459 460 461

      console.log(array1); // [2, 3]     

      console.log(firstElement); //1
      // #END
M
mahaifeng 已提交
462 463 464 465
      expect(firstElement).toEqual(1);
      expect(array1).toEqual([2, 3]);
    })
    test("slice", () => {
M
mahaifeng 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      // #TEST Array.slice
      const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

      console.log(animals.slice(2));
      //  ["camel", "duck", "elephant"]

      console.log(animals.slice(2, 4));
      //["camel", "duck"]

      console.log(animals.slice(1, 5));
      //  ["bison", "camel", "duck", "elephant"]

      console.log(animals.slice(-2));
      // ["duck", "elephant"]

      console.log(animals.slice(2, -1));
      // ["camel", "duck"]

      console.log(animals.slice());
      //["ant", "bison", "camel", "duck", "elephant"]
      // #END
M
mahaifeng 已提交
487 488 489 490 491 492 493 494
      expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]);
      expect(animals.slice(2, 4)).toEqual(["camel", "duck"]);
      expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]);
      expect(animals.slice(-2)).toEqual(["duck", "elephant"]);
      expect(animals.slice(2, -1)).toEqual(["camel", "duck"]);
      expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]);
    })
    test("some", () => {
M
mahaifeng 已提交
495
      // #TEST Array.some
M
mahaifeng 已提交
496 497
      const array : number[] = [1, 2, 3, 4, 5];
      const even = (element : number) : boolean => element % 2 == 0;
M
mahaifeng 已提交
498 499 500
      console.log(array.some(even));//true
      // #END

M
mahaifeng 已提交
501 502 503 504 505 506
      expect(array.some(even)).toEqual(true);
      const isBiggerThan10 = (element : number) : boolean => element > 10;
      expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false);
      expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true);
    })
    test("splice", () => {
M
mahaifeng 已提交
507
      // #TEST Array.splice
M
mahaifeng 已提交
508 509
      const months : string[] = ['Jan', 'March', 'April', 'June'];
      months.splice(1, 0, 'Feb');
M
mahaifeng 已提交
510 511 512
      console.log(months)//["Jan", "Feb", "March", "April", "June"]
      // #END

M
mahaifeng 已提交
513 514 515 516 517
      expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]);
      months.splice(4, 1, 'May');
      expect(months).toEqual(["Jan", "Feb", "March", "April", "May"]);
    })
    test('sort', () => {
M
mahaifeng 已提交
518
      // #TEST Array.slice
M
mahaifeng 已提交
519 520
      const months = ['March', 'Jan', 'Feb', 'Dec'];
      months.sort();
M
mahaifeng 已提交
521 522
      console.log(months)//["Dec", "Feb", "Jan", "March"]
      // #END
M
mahaifeng 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
      expect(months).toEqual(["Dec", "Feb", "Jan", "March"]);

      const array1 = [1, 30, 4, 21, 100000];
      array1.sort();
      expect(array1).toEqual([1, 100000, 21, 30, 4]);

      const array2 = [5, 1, 4, 2, 3];
      array2.sort((a, b) : number => a - b);
      expect(array2).toEqual([1, 2, 3, 4, 5]);

      // const array3 = [5, "banana", 4, "apple", 3, "cherry", 2, "date", 1];
      // array3.sort();
      // expect(array3).toEqual([1, 2, 3, 4, 5, "apple", "banana", "cherry", "date"]);

      const array4 = [
        { name: "John", age: 24 },
        { name: "Sarah", age: 19 },
        { name: "Bob", age: 27 },
        { name: "Alice", age: 21 }
      ];
      // 先强转类型,解决编译报错
      array4.sort((a, b) : number => (a['age'] as number) - (b['age'] as number));

      // #ifndef APP-IOS
      expect(array4).toEqual([{ name: "Sarah", age: 19 }, { name: "Alice", age: 21 }, { name: "John", age: 24 }, { name: "Bob", age: 27 }]);
      // #endif

      // #ifdef APP-IOS
      const arr = array4.map((value : UTSJSONObject) : number => { return value["age"] as number })
      expect(arr).toEqual([19, 21, 24, 27])
      // #endif




    })
    test("unshift", () => {
M
mahaifeng 已提交
560 561 562 563 564 565 566 567 568
      // #TEST Array.unshift
      const array1 = [1, 2, 3];

      console.log(array1.unshift(4, 5));
      //  5

      console.log(array1);
      // [4, 5, 1, 2, 3]
      // #END
M
mahaifeng 已提交
569 570 571 572
      expect(array1.unshift(4, 5)).toEqual(5);
      expect(array1).toEqual([4, 5, 1, 2, 3]);
    })
    test("toString", () => {
M
mahaifeng 已提交
573
      // #TEST Array.toString
M
mahaifeng 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
      const array1 : number[] = [1, 2, 3];
      console.log(array1.toString()) //"1,2,3"
      // #END
      expect(array1.toString()).toEqual("1,2,3");
      const array2 = new Array<string>()
      array2.push("a")
      array2.push("b")
      array2.push("c")
      expect(array2.toString()).toEqual("a,b,c");
    })
    test('reverse', () => {
      // const array1: string[] = ['one', 'two', 'three'];
      // const reversed1: string[] = array1.reverse();
      // expect(reversed1).toEqual(["three", "two", "one"]);
      // expect(array1).toEqual(["three", "·two", "one"]);

      // const array2 = [1, 2, 3, 4, 5];
      // const reversed2 = array2.reverse();
      // expect(reversed2).toEqual([5, 4, 3, 2, 1]);
      // expect(array2).toEqual([5, 4, 3, 2, 1]);
Y
yurj26 已提交
594
    })
M
mahaifeng 已提交
595
    test("reduceRight", () => {
M
mahaifeng 已提交
596
      // #TEST Array.reduceRight,Array.reduceRight_1,Array.reduceRight_2,Array.reduceRight_3,Array.reduceRight_4,Array.reduceRight_5,Array.reduceRight_6
M
mahaifeng 已提交
597 598
      const array1 : number[][] = [[0, 1], [2, 3], [4, 5]];
      const result1 = array1.reduceRight((accumulator : number[], currentValue : number[]) : number[] => accumulator.concat(currentValue));
M
mahaifeng 已提交
599 600
      console.log(result1) //[4, 5, 2, 3, 0, 1]

M
mahaifeng 已提交
601 602

      const array2 : number[] = [1, 2, 3, 4];
M
mahaifeng 已提交
603
      let result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => {
M
mahaifeng 已提交
604 605
        return acc + cur;
      });
M
mahaifeng 已提交
606 607 608

      console.log(result2) //10

M
mahaifeng 已提交
609 610

      const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5);
M
mahaifeng 已提交
611 612 613
      console.log(result3) //15
      // #END

M
mahaifeng 已提交
614
      expect(result3).toEqual(15);
M
mahaifeng 已提交
615 616 617 618 619 620
      expect(result1).toEqual([4, 5, 2, 3, 0, 1]);
      result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => {
        expect(array[index]).toEqual(cur);
        return acc + cur;
      });
      expect(result2).toEqual(10);
M
mahaifeng 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    })
    test("flatMap", () => {
      const arr : number[] = [1, 2, 3];
      const result = arr.flatMap((x : number) : number[] => [x, x * 2]);
      expect(result).toEqual([1, 2, 2, 4, 3, 6]);

      const arr1 : number[] = [1, 2, 3, 4];
      const result1 = arr1.flatMap((num : number, index : number, array : number[]) : number[] => {
        expect(array[index]).toEqual(num);
        if (num % 2 == 0) {
          return [num * 2];
        }
        return [];
      });
      expect(result1).toEqual([4, 8]);
    })
    test("entries", () => {
      // const array1 = ['a', 'b', 'c'];
      // const iterator1 = array1.entries();
      // expect(iterator1.next().value).toEqual([0, "a"]);
      // expect(iterator1.next().value).toEqual([1, "b"]);
      // expect(iterator1.next().value).toEqual([2, "c"]);
      // expect(iterator1.next().done).toEqual(true);

      // const array2: any[] = [1, 2, 'hello', true, { name: 'john', age: 30 }, [4, 5]];
      // let count = 0;
      // for (const [index, element] of array2.entries()) {
      //     count++;
      //     expect(element).toEqual(array2[index]);
      // }
      // expect(count).toEqual(array2.length);
    })
    test("keys", () => {
      // const array1 = ['a', 'b', 'c'];
      // const iterator1 = array1.keys();
      // expect(iterator1.next().value).toEqual(0);
      // expect(iterator1.next().value).toEqual(1);
      // expect(iterator1.next().value).toEqual(2);
      // expect(iterator1.next().done).toEqual(true);
    })
M
mahaifeng 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    test("isArray", () => {
      // #TEST Array.isArray

      console.log(Array.isArray([1, 3, 5]));
      // Expected output: true

      console.log(Array.isArray('[]'));
      // Expected output: false

      console.log(Array.isArray(new Array(5)));
      // Expected output: true

      console.log(Array.isArray(new Int16Array([15, 33])));
      // Expected output: false
      // #END
    })
M
mahaifeng 已提交
677 678

  })
Y
yurj26 已提交
679
}