TInt8Array.uts 11.3 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4
import {
  describe,
  test,
  expect,
M
mahaifeng 已提交
5
  expectNumber,
M
mahaifeng 已提交
6 7 8 9 10
  Result
} from './tests.uts'

export class TInt8Array {
  test() {
M
mahaifeng 已提交
11
    // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
12

M
mahaifeng 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    this.testConstructor();
    this.testSet();
    this.testCopyWith();
    this.testEvery();
    this.testFill();
    this.testFilter();
    this.find();
    this.findIndex();
    this.foreach();
    this.iterator();
    this.includes();
    this.indexOf();
    this.join();
    this.keys();
    this.map();
    this.reduce();
    this.reduceRight();
    this.reverse();
    this.slice();
    this.sort();
    this.subarray();
    this.values();
    this.arrayBufferSlice();
M
mahaifeng 已提交
36
    //#endif
M
mahaifeng 已提交
37 38
  }

M
mahaifeng 已提交
39
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
  from() {
    // #TEST Int8Array.from
    var array = Int8Array.from([1, 2, 3], (v : number, _ : number) : number => v + v);
    console.log(array.toString()); // '2,4,6'
    // #END
    expect(array.toString()).toEqual('2,4,6');
  }
  of() {
    // #TEST Int8Array.of
    var array = Int8Array.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
54 55 56 57 58 59 60 61
  testConstructor() {
    let buffer = new ArrayBuffer(16);
    let int8View = new Int8Array(buffer);
    int8View[5] = 42;
    expect(int8View.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0");
  }

  testSet() {
M
mahaifeng 已提交
62
    // #TEST Int8Array.set
M
mahaifeng 已提交
63
    let int8 = new Int8Array(8);
M
mahaifeng 已提交
64
    var array = [1, 2, 3];
M
mahaifeng 已提交
65
    int8.set(array, 1);
M
mahaifeng 已提交
66
    console.log(int8.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
67
    // #END
M
mahaifeng 已提交
68
    expect(int8.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
69 70 71
  }

  testCopyWith() {
M
mahaifeng 已提交
72
    // #TEST Int8Array.copyWithin
M
mahaifeng 已提交
73 74 75
    let int8 = new Int8Array(8);
    int8.set([1, 2, 3], 1);
    int8.copyWithin(3, 0, 3);
M
mahaifeng 已提交
76
    console.log(int8.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
77
    // #END
M
mahaifeng 已提交
78
    expect(int8.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
79 80 81
  }

  testEvery() {
M
mahaifeng 已提交
82
    // #TEST Int8Array.every
M
mahaifeng 已提交
83
    // const isBelowThreshold = (currentValue: number, index: number, array:Int8Array): boolean => currentValue < 40;    
M
mahaifeng 已提交
84
    let result = new Int8Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Int8Array) : boolean =>
M
mahaifeng 已提交
85 86 87
      value < 40
    );
    console.log(result); // false
M
mahaifeng 已提交
88
    // #END
M
mahaifeng 已提交
89
    expect(result).toEqual(false);
M
mahaifeng 已提交
90 91 92
  }

  testFill() {
M
mahaifeng 已提交
93
    // #TEST Int8Array.fill
M
mahaifeng 已提交
94
    let int8 = new Int8Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
95
    console.log(int8.toString()); // "4,4,4"
M
mahaifeng 已提交
96

M
mahaifeng 已提交
97 98
    let int8_t1 = new Int8Array([1, 2, 3]).fill(4, 1);
    console.log(int8_t1.toString()); // "1,4,4"
M
mahaifeng 已提交
99

M
mahaifeng 已提交
100 101
    let int8_t2 = new Int8Array([1, 2, 3]).fill(4, 1, 2);
    console.log(int8_t2.toString()); // "1,4,3"
M
mahaifeng 已提交
102

M
mahaifeng 已提交
103 104
    let int8_t3 = new Int8Array([1, 2, 3]).fill(4, 1, 1);
    console.log(int8_t3.toString()); // "1,2,3"
M
mahaifeng 已提交
105

M
mahaifeng 已提交
106 107
    let int8_t4 = new Int8Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int8_t4.toString()); // "4,2,3"
M
mahaifeng 已提交
108
    // #END
M
mahaifeng 已提交
109 110 111 112 113 114
    expect(int8_t4.toString()).toEqual("4,2,3");
    expect(int8.toString()).toEqual("4,4,4");
    expect(int8_t1.toString()).toEqual("1,4,4");
    expect(int8_t2.toString()).toEqual("1,4,3");
    expect(int8_t3.toString()).toEqual("1,2,3");

M
mahaifeng 已提交
115 116 117
  }

  testFilter() {
M
mahaifeng 已提交
118
    // #TEST Int8Array.filter
M
mahaifeng 已提交
119
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;
M
mahaifeng 已提交
120
    let int8 = new Int8Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Int8Array) : boolean =>
M
mahaifeng 已提交
121 122 123
      value >= 10
    );
    console.log(int8.toString()); // "12,44"
M
mahaifeng 已提交
124
    // #END
M
mahaifeng 已提交
125
    expect(int8.toString()).toEqual("12,44");
M
mahaifeng 已提交
126 127
  }

M
mahaifeng 已提交
128

M
mahaifeng 已提交
129
  find() {
M
mahaifeng 已提交
130
    // #TEST Int8Array.find
M
mahaifeng 已提交
131
    let int8 = new Int8Array([4, 5, 8, 12]);
M
mahaifeng 已提交
132
    let res = int8.find((value : number, _ : number, _a : Int8Array) : boolean => value > 5);
M
mahaifeng 已提交
133
    console.log(res); // 8
M
mahaifeng 已提交
134
    // #END
M
mahaifeng 已提交
135
    expect(res).toEqual(8);
M
mahaifeng 已提交
136 137 138
  }

  findIndex() {
M
mahaifeng 已提交
139
    // #TEST Int8Array.findIndex
M
mahaifeng 已提交
140
    let int8 = new Int8Array([4, 6, 8, 12]);
M
mahaifeng 已提交
141
    let res = int8.findIndex((value : number, _ : number, _a : Int8Array) : boolean => value > 100);
M
mahaifeng 已提交
142 143
    console.log(res); // -1

M
mahaifeng 已提交
144

M
mahaifeng 已提交
145
    int8 = new Int8Array([4, 6, 7, 120]);
M
mahaifeng 已提交
146 147
    let res1 = int8.findIndex((value : number, _ : number, _a : Int8Array) : boolean => value > 100);
    console.log(res1); // 3
M
mahaifeng 已提交
148
    // #END
M
mahaifeng 已提交
149 150
    expect(res1).toEqual(3);
    expect(res).toEqual(-1);
M
mahaifeng 已提交
151 152 153
  }

  foreach() {
M
mahaifeng 已提交
154 155
    // #TEST Int8Array.forEach
    new Int8Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Int8Array) => {
M
mahaifeng 已提交
156 157
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
158
    // #END
M
mahaifeng 已提交
159 160
  }
  iterator() {
M
mahaifeng 已提交
161
    // #TEST Int8Array.entries
M
mahaifeng 已提交
162 163
    let arr = new Int8Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
164 165 166 167
    let ret1 = entries.next().value[1]
    let ret2 = entries.next().value[1]
    console.log(ret1); // Output: 10
    console.log(ret2); // Output: 20
M
mahaifeng 已提交
168
    // #END
M
mahaifeng 已提交
169 170
    expect(ret1).toEqual(10);
    expect(ret2).toEqual(20);
M
mahaifeng 已提交
171 172 173
  }

  includes() {
M
mahaifeng 已提交
174
    // #TEST Int8Array.includes
M
mahaifeng 已提交
175 176
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.includes(2);
M
mahaifeng 已提交
177 178 179 180
    console.log(res); // Output: true

    let res1 = int8.includes(4);
    console.log(res1); // Output: false
M
mahaifeng 已提交
181 182


M
mahaifeng 已提交
183 184 185
    let res2 = int8.includes(3, 3);
    console.log(res2); // Output: false

M
mahaifeng 已提交
186
    // #END
M
mahaifeng 已提交
187 188 189
    expect(res).toEqual(true);
    expect(res1).toEqual(false);
    expect(res2).toEqual(false);
M
mahaifeng 已提交
190 191 192
  }

  indexOf() {
M
mahaifeng 已提交
193
    // #TEST Int8Array.indexOf
M
mahaifeng 已提交
194 195
    let int8 = new Int8Array([2, 5, 9]);
    let res = int8.indexOf(2);
M
mahaifeng 已提交
196
    console.log(res); // Output: 0
M
mahaifeng 已提交
197 198


M
mahaifeng 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    let res1 = int8.indexOf(7);
    console.log(res1); // Output: -1


    let res2 = int8.indexOf(9, 2);
    console.log(res2); // Output: 2


    let res3 = int8.indexOf(2, -1);
    console.log(res3); // Output: -1


    let res4 = int8.indexOf(2, -3);
    console.log(res4); // Output: 0
M
mahaifeng 已提交
213 214


M
mahaifeng 已提交
215
    // #END
M
mahaifeng 已提交
216 217 218 219 220
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
    expect(res4).toEqual(0);
M
mahaifeng 已提交
221 222
  }

M
mahaifeng 已提交
223

M
mahaifeng 已提交
224
  join() {
M
mahaifeng 已提交
225
    // #TEST Int8Array.join
M
mahaifeng 已提交
226 227
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.join();
M
mahaifeng 已提交
228
    console.log(res); // Output: "1,2,3"
M
mahaifeng 已提交
229

M
mahaifeng 已提交
230 231
    let res1 = int8.join(" / ");
    console.log(res1); // Output: "1 / 2 / 3"
M
mahaifeng 已提交
232

M
mahaifeng 已提交
233 234 235

    let res2 = int8.join("");
    console.log(res2); // Output: "123"
M
mahaifeng 已提交
236
    // #END
M
mahaifeng 已提交
237 238 239
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
240 241 242
  }

  keys() {
M
mahaifeng 已提交
243
    // #TEST Int8Array.keys
M
mahaifeng 已提交
244 245
    let arr = new Int8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
246 247 248 249 250 251 252 253 254 255
    let value1 = keys.next().value
    let value2 = keys.next().value
    let value3 = keys.next().value
    let value4 = keys.next().value
    let value5 = keys.next().value
    console.log(value1); // Output: 0
    console.log(value2); // Output: 1
    console.log(value3); // Output: 2
    console.log(value4); // Output: 3
    console.log(value5); // Output: 4
M
mahaifeng 已提交
256
    // #END
M
mahaifeng 已提交
257 258 259 260 261
    expect(value1).toEqual(0);
    expect(value2).toEqual(1);
    expect(value3).toEqual(2);
    expect(value4).toEqual(3);
    expect(value5).toEqual(4);
M
mahaifeng 已提交
262 263 264
  }

  map() {
M
mahaifeng 已提交
265
    // #TEST Int8Array.map
M
mahaifeng 已提交
266
    let numbers = new Int8Array([1, 4, 9]);
M
mahaifeng 已提交
267
    let doubles = numbers.map((value : number, _ : number, _a : Int8Array) : number => value * 2);
M
mahaifeng 已提交
268 269 270
    console.log(numbers.toString()); // Output: "1,4,9"
    console.log(doubles.toString()); // Output: "2,8,18"
    // #END
M
mahaifeng 已提交
271 272 273 274 275
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
M
mahaifeng 已提交
276
    // #TEST Int8Array.reduce
M
mahaifeng 已提交
277
    let total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
278
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Int8Array) :
M
mahaifeng 已提交
279
      number => accumulator + currentValue);
M
mahaifeng 已提交
280 281
    console.log(res); // Output: 6

M
mahaifeng 已提交
282 283

    total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
284
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Int8Array) : number =>
M
mahaifeng 已提交
285
      accumulator + currentValue, 8);
M
mahaifeng 已提交
286
    console.log(res1); // Output: 14
M
mahaifeng 已提交
287
    // #END
M
mahaifeng 已提交
288 289
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
290 291 292
  }

  reduceRight() {
M
mahaifeng 已提交
293
    // #TEST Int8Array.reduceRight
M
mahaifeng 已提交
294
    let total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
295
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int8Array) :
M
mahaifeng 已提交
296
      number => accumulator + currentValue);
M
mahaifeng 已提交
297
    console.log(res); // Output: 6
M
mahaifeng 已提交
298 299

    total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
300
    let res1 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int8Array) :
M
mahaifeng 已提交
301
      number => accumulator + currentValue, 8);
M
mahaifeng 已提交
302
    console.log(res1); // Output: 14
M
mahaifeng 已提交
303
    // #END
M
mahaifeng 已提交
304 305
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
306 307 308
  }

  reverse() {
M
mahaifeng 已提交
309
    // #TEST Int8Array.reverse
M
mahaifeng 已提交
310 311
    let int8 = new Int8Array([1, 2, 3]);
    int8.reverse();
M
mahaifeng 已提交
312
    console.log(int8.toString()); // Output: "3,2,1"
M
mahaifeng 已提交
313
    // #END
M
mahaifeng 已提交
314
    expect(int8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
315 316 317
  }

  slice() {
M
mahaifeng 已提交
318
    // #TEST Int8Array.slice
M
mahaifeng 已提交
319 320
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.slice(1);
M
mahaifeng 已提交
321
    console.log(res.toString()); // Output: "2,3"
M
mahaifeng 已提交
322

M
mahaifeng 已提交
323 324
    let res1 = int8.slice(2);
    console.log(res1.toString()); // Output: "3"
M
mahaifeng 已提交
325

M
mahaifeng 已提交
326 327
    let res2 = int8.slice(-2);
    console.log(res2.toString()); // Output: "2,3"
M
mahaifeng 已提交
328

M
mahaifeng 已提交
329 330
    let res3 = int8.slice(0, 1);
    console.log(res3.toString()); // Output: "1"
M
mahaifeng 已提交
331
    // #END
M
mahaifeng 已提交
332 333 334 335
    expect(res3.toString()).toEqual("1");
    expect(res.toString()).toEqual("2,3");
    expect(res1.toString()).toEqual("3");
    expect(res2.toString()).toEqual("2,3");
M
mahaifeng 已提交
336 337 338
  }

  sort() {
M
mahaifeng 已提交
339
    // #TEST Int8Array.sort
M
mahaifeng 已提交
340 341
    let numbers = new Int8Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
342 343
    console.log(numbers.toString()); // Output: "1,5,40"
    let res = numbers.toString()
M
mahaifeng 已提交
344

M
mahaifeng 已提交
345
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
346
    console.log(numbers.toString()); // Output: "1,5,40"
M
mahaifeng 已提交
347
    // #END
M
mahaifeng 已提交
348 349
    expect(numbers.toString()).toEqual("1,5,40");
    expect(res).toEqual("1,5,40");
M
mahaifeng 已提交
350 351 352
  }

  subarray() {
M
mahaifeng 已提交
353
    // #TEST Int8Array.subarray
M
mahaifeng 已提交
354
    let buffer = new ArrayBuffer(8);
M
mahaifeng 已提交
355 356
    let int8 = new Int8Array(buffer);
    int8.set([1, 2, 3]);
M
mahaifeng 已提交
357 358
    console.log(int8.toString()); // Output: "1,2,3,0,0,0,0,0"

M
mahaifeng 已提交
359

M
mahaifeng 已提交
360
    let sub = int8.subarray(0, 4);
M
mahaifeng 已提交
361
    console.log(sub.toString()); // Output: "1,2,3,0"
M
mahaifeng 已提交
362
    // #END
M
mahaifeng 已提交
363 364
    expect(sub.toString()).toEqual("1,2,3,0");
    expect(int8.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
365 366 367
  }

  values() {
M
mahaifeng 已提交
368
    // #TEST Int8Array.values
M
mahaifeng 已提交
369 370
    let arr = new Int8Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
371 372 373 374 375 376
    let value1 = values.next().value
    let value2 = values.next().value
    let value3 = values.next().value
    console.log(value1); // Output: 1
    console.log(value2); // Output: 2
    console.log(value3); // Output: 3
M
mahaifeng 已提交
377
    // #END
M
mahaifeng 已提交
378 379 380
    expect(value1).toEqual(1);
    expect(value2).toEqual(2);
    expect(value3).toEqual(3);
M
mahaifeng 已提交
381 382 383
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
384
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
385
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
386 387
    let int8 = new Int8Array(buffer);
    int8[4] = 42;
M
mahaifeng 已提交
388
    console.log(int8.toString()); // Output: "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
389

M
mahaifeng 已提交
390
    let res = buffer.slice(4, 5);
M
mahaifeng 已提交
391
    let sliced = new Int8Array(res);
M
mahaifeng 已提交
392
    console.log(sliced[0]); // Output: 42
M
mahaifeng 已提交
393
    // #END
M
mahaifeng 已提交
394 395
    expect(sliced[0]).toEqual(42);
    expect(int8.toString()).toEqual("0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
396
  }
M
mahaifeng 已提交
397

M
mahaifeng 已提交
398 399 400 401 402
  testSome() {
    // #TEST Int8Array.some
    const int8 = new Int8Array([-10, 20, -30, 40, -50]);
    const positives = new Int8Array([10, 20, 30, 40, 50]);

M
mahaifeng 已提交
403
    let res = int8.some((element : number, index : number, array : Int8Array) : boolean =>
M
mahaifeng 已提交
404
      element < 0
M
mahaifeng 已提交
405 406
    );
    console.log(res); // Output: true
M
mahaifeng 已提交
407 408


M
mahaifeng 已提交
409
    let res1 = positives.some((element : number, index : number, array : Int8Array) : boolean =>
M
mahaifeng 已提交
410
      element < 0
M
mahaifeng 已提交
411 412
    );
    console.log(res1); // Output: false
M
mahaifeng 已提交
413
    // #END
M
mahaifeng 已提交
414 415
    expect(res1).toEqual(false);
    expect(res).toEqual(true);
M
mahaifeng 已提交
416
  }
M
mahaifeng 已提交
417
  // #endif
M
mahaifeng 已提交
418
}