TInt8Array.uts 10.9 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 12
    // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

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 40
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

M
mahaifeng 已提交
41 42 43 44 45 46 47 48
  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 已提交
49
    // #TEST Int8Array.set
M
mahaifeng 已提交
50
    let int8 = new Int8Array(8);
M
mahaifeng 已提交
51
    var array = [1, 2, 3];
M
mahaifeng 已提交
52
    int8.set(array, 1);
M
mahaifeng 已提交
53
    console.log(int8.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
54
    // #END
M
mahaifeng 已提交
55
    expect(int8.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
56 57 58
  }

  testCopyWith() {
M
mahaifeng 已提交
59
    // #TEST Int8Array.copyWithin
M
mahaifeng 已提交
60 61 62
    let int8 = new Int8Array(8);
    int8.set([1, 2, 3], 1);
    int8.copyWithin(3, 0, 3);
M
mahaifeng 已提交
63
    console.log(int8.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
64
    // #END
M
mahaifeng 已提交
65
    expect(int8.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
66 67 68
  }

  testEvery() {
M
mahaifeng 已提交
69
    // #TEST Int8Array.every
M
mahaifeng 已提交
70
    // const isBelowThreshold = (currentValue: number, index: number, array:Int8Array): boolean => currentValue < 40;    
M
mahaifeng 已提交
71
    let result = new Int8Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Int8Array) : boolean =>
M
mahaifeng 已提交
72 73 74
      value < 40
    );
    console.log(result); // false
M
mahaifeng 已提交
75
    // #END
M
mahaifeng 已提交
76
    expect(result).toEqual(false);
M
mahaifeng 已提交
77 78 79
  }

  testFill() {
M
mahaifeng 已提交
80
    // #TEST Int8Array.fill
M
mahaifeng 已提交
81
    let int8 = new Int8Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
82
    console.log(int8.toString()); // "4,4,4"
M
mahaifeng 已提交
83

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

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

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

M
mahaifeng 已提交
93 94
    let int8_t4 = new Int8Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int8_t4.toString()); // "4,2,3"
M
mahaifeng 已提交
95
    // #END
M
mahaifeng 已提交
96 97 98 99 100 101
    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 已提交
102 103 104
  }

  testFilter() {
M
mahaifeng 已提交
105
    // #TEST Int8Array.filter
M
mahaifeng 已提交
106
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;
M
mahaifeng 已提交
107
    let int8 = new Int8Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Int8Array) : boolean =>
M
mahaifeng 已提交
108 109 110
      value >= 10
    );
    console.log(int8.toString()); // "12,44"
M
mahaifeng 已提交
111
    // #END
M
mahaifeng 已提交
112
    expect(int8.toString()).toEqual("12,44");
M
mahaifeng 已提交
113 114
  }

M
mahaifeng 已提交
115

M
mahaifeng 已提交
116
  find() {
M
mahaifeng 已提交
117
    // #TEST Int8Array.find
M
mahaifeng 已提交
118
    let int8 = new Int8Array([4, 5, 8, 12]);
M
mahaifeng 已提交
119
    let res = int8.find((value : number, _ : number, _a : Int8Array) : boolean => value > 5);
M
mahaifeng 已提交
120
    console.log(res); // 8
M
mahaifeng 已提交
121
    // #END
M
mahaifeng 已提交
122
    expect(res).toEqual(8);
M
mahaifeng 已提交
123 124 125
  }

  findIndex() {
M
mahaifeng 已提交
126
    // #TEST Int8Array.findIndex
M
mahaifeng 已提交
127
    let int8 = new Int8Array([4, 6, 8, 12]);
M
mahaifeng 已提交
128
    let res = int8.findIndex((value : number, _ : number, _a : Int8Array) : boolean => value > 100);
M
mahaifeng 已提交
129 130
    console.log(res); // -1

M
mahaifeng 已提交
131

M
mahaifeng 已提交
132
    int8 = new Int8Array([4, 6, 7, 120]);
M
mahaifeng 已提交
133 134
    let res1 = int8.findIndex((value : number, _ : number, _a : Int8Array) : boolean => value > 100);
    console.log(res1); // 3
M
mahaifeng 已提交
135
    // #END
M
mahaifeng 已提交
136 137
    expect(res1).toEqual(3);
    expect(res).toEqual(-1);
M
mahaifeng 已提交
138 139 140
  }

  foreach() {
M
mahaifeng 已提交
141 142
    // #TEST Int8Array.forEach
    new Int8Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Int8Array) => {
M
mahaifeng 已提交
143 144
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
145
    // #END
M
mahaifeng 已提交
146 147
  }
  iterator() {
M
mahaifeng 已提交
148
    // #TEST Int8Array.entries
M
mahaifeng 已提交
149 150
    let arr = new Int8Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
151 152 153 154
    let ret1 = entries.next().value[1]
    let ret2 = entries.next().value[1]
    console.log(ret1); // Output: 10
    console.log(ret2); // Output: 20
M
mahaifeng 已提交
155
    // #END
M
mahaifeng 已提交
156 157
    expect(ret1).toEqual(10);
    expect(ret2).toEqual(20);
M
mahaifeng 已提交
158 159 160
  }

  includes() {
M
mahaifeng 已提交
161
    // #TEST Int8Array.includes
M
mahaifeng 已提交
162 163
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.includes(2);
M
mahaifeng 已提交
164 165 166 167
    console.log(res); // Output: true

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


M
mahaifeng 已提交
170 171 172
    let res2 = int8.includes(3, 3);
    console.log(res2); // Output: false

M
mahaifeng 已提交
173
    // #END
M
mahaifeng 已提交
174 175 176
    expect(res).toEqual(true);
    expect(res1).toEqual(false);
    expect(res2).toEqual(false);
M
mahaifeng 已提交
177 178 179
  }

  indexOf() {
M
mahaifeng 已提交
180
    // #TEST Int8Array.indexOf
M
mahaifeng 已提交
181 182
    let int8 = new Int8Array([2, 5, 9]);
    let res = int8.indexOf(2);
M
mahaifeng 已提交
183
    console.log(res); // Output: 0
M
mahaifeng 已提交
184 185


M
mahaifeng 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199
    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 已提交
200 201


M
mahaifeng 已提交
202
    // #END
M
mahaifeng 已提交
203 204 205 206 207
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
    expect(res4).toEqual(0);
M
mahaifeng 已提交
208 209
  }

M
mahaifeng 已提交
210

M
mahaifeng 已提交
211
  join() {
M
mahaifeng 已提交
212
    // #TEST Int8Array.join
M
mahaifeng 已提交
213 214
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.join();
M
mahaifeng 已提交
215
    console.log(res); // Output: "1,2,3"
M
mahaifeng 已提交
216

M
mahaifeng 已提交
217 218
    let res1 = int8.join(" / ");
    console.log(res1); // Output: "1 / 2 / 3"
M
mahaifeng 已提交
219

M
mahaifeng 已提交
220 221 222

    let res2 = int8.join("");
    console.log(res2); // Output: "123"
M
mahaifeng 已提交
223
    // #END
M
mahaifeng 已提交
224 225 226
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
227 228 229
  }

  keys() {
M
mahaifeng 已提交
230
    // #TEST Int8Array.keys
M
mahaifeng 已提交
231 232
    let arr = new Int8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
233 234 235 236 237 238 239 240 241 242
    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 已提交
243
    // #END
M
mahaifeng 已提交
244 245 246 247 248
    expect(value1).toEqual(0);
    expect(value2).toEqual(1);
    expect(value3).toEqual(2);
    expect(value4).toEqual(3);
    expect(value5).toEqual(4);
M
mahaifeng 已提交
249 250 251
  }

  map() {
M
mahaifeng 已提交
252
    // #TEST Int8Array.map
M
mahaifeng 已提交
253
    let numbers = new Int8Array([1, 4, 9]);
M
mahaifeng 已提交
254
    let doubles = numbers.map((value : number, _ : number, _a : Int8Array) : number => value * 2);
M
mahaifeng 已提交
255 256 257
    console.log(numbers.toString()); // Output: "1,4,9"
    console.log(doubles.toString()); // Output: "2,8,18"
    // #END
M
mahaifeng 已提交
258 259 260 261 262
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
M
mahaifeng 已提交
263
    // #TEST Int8Array.reduce
M
mahaifeng 已提交
264
    let total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
265
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Int8Array) :
M
mahaifeng 已提交
266
      number => accumulator + currentValue);
M
mahaifeng 已提交
267 268
    console.log(res); // Output: 6

M
mahaifeng 已提交
269 270

    total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
271
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Int8Array) : number =>
M
mahaifeng 已提交
272
      accumulator + currentValue, 8);
M
mahaifeng 已提交
273
    console.log(res1); // Output: 14
M
mahaifeng 已提交
274
    // #END
M
mahaifeng 已提交
275 276
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
277 278 279
  }

  reduceRight() {
M
mahaifeng 已提交
280
    // #TEST Int8Array.reduceRight
M
mahaifeng 已提交
281
    let total = new Int8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
282
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int8Array) :
M
mahaifeng 已提交
283
      number => accumulator + currentValue);
M
mahaifeng 已提交
284
    console.log(res); // Output: 6
M
mahaifeng 已提交
285 286

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

  reverse() {
M
mahaifeng 已提交
296
    // #TEST Int8Array.reverse
M
mahaifeng 已提交
297 298
    let int8 = new Int8Array([1, 2, 3]);
    int8.reverse();
M
mahaifeng 已提交
299
    console.log(int8.toString()); // Output: "3,2,1"
M
mahaifeng 已提交
300
    // #END
M
mahaifeng 已提交
301
    expect(int8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
302 303 304
  }

  slice() {
M
mahaifeng 已提交
305
    // #TEST Int8Array.slice
M
mahaifeng 已提交
306 307
    let int8 = new Int8Array([1, 2, 3]);
    let res = int8.slice(1);
M
mahaifeng 已提交
308
    console.log(res.toString()); // Output: "2,3"
M
mahaifeng 已提交
309

M
mahaifeng 已提交
310 311
    let res1 = int8.slice(2);
    console.log(res1.toString()); // Output: "3"
M
mahaifeng 已提交
312

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

M
mahaifeng 已提交
316 317
    let res3 = int8.slice(0, 1);
    console.log(res3.toString()); // Output: "1"
M
mahaifeng 已提交
318
    // #END
M
mahaifeng 已提交
319 320 321 322
    expect(res3.toString()).toEqual("1");
    expect(res.toString()).toEqual("2,3");
    expect(res1.toString()).toEqual("3");
    expect(res2.toString()).toEqual("2,3");
M
mahaifeng 已提交
323 324 325
  }

  sort() {
M
mahaifeng 已提交
326
    // #TEST Int8Array.sort
M
mahaifeng 已提交
327 328
    let numbers = new Int8Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
329 330
    console.log(numbers.toString()); // Output: "1,5,40"
    let res = numbers.toString()
M
mahaifeng 已提交
331

M
mahaifeng 已提交
332
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
333
    console.log(numbers.toString()); // Output: "1,5,40"
M
mahaifeng 已提交
334
    // #END
M
mahaifeng 已提交
335 336
    expect(numbers.toString()).toEqual("1,5,40");
    expect(res).toEqual("1,5,40");
M
mahaifeng 已提交
337 338 339
  }

  subarray() {
M
mahaifeng 已提交
340
    // #TEST Int8Array.subarray
M
mahaifeng 已提交
341
    let buffer = new ArrayBuffer(8);
M
mahaifeng 已提交
342 343
    let int8 = new Int8Array(buffer);
    int8.set([1, 2, 3]);
M
mahaifeng 已提交
344 345
    console.log(int8.toString()); // Output: "1,2,3,0,0,0,0,0"

M
mahaifeng 已提交
346

M
mahaifeng 已提交
347
    let sub = int8.subarray(0, 4);
M
mahaifeng 已提交
348
    console.log(sub.toString()); // Output: "1,2,3,0"
M
mahaifeng 已提交
349
    // #END
M
mahaifeng 已提交
350 351
    expect(sub.toString()).toEqual("1,2,3,0");
    expect(int8.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
352 353 354
  }

  values() {
M
mahaifeng 已提交
355
    // #TEST Int8Array.values
M
mahaifeng 已提交
356 357
    let arr = new Int8Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
358 359 360 361 362 363
    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 已提交
364
    // #END
M
mahaifeng 已提交
365 366 367
    expect(value1).toEqual(1);
    expect(value2).toEqual(2);
    expect(value3).toEqual(3);
M
mahaifeng 已提交
368 369 370
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
371
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
372
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
373 374
    let int8 = new Int8Array(buffer);
    int8[4] = 42;
M
mahaifeng 已提交
375
    console.log(int8.toString()); // Output: "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
376

M
mahaifeng 已提交
377
    let res = buffer.slice(4, 5);
M
mahaifeng 已提交
378
    let sliced = new Int8Array(res);
M
mahaifeng 已提交
379
    console.log(sliced[0]); // Output: 42
M
mahaifeng 已提交
380
    // #END
M
mahaifeng 已提交
381 382
    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 已提交
383
  }
M
mahaifeng 已提交
384

M
mahaifeng 已提交
385 386 387 388 389
  testSome() {
    // #TEST Int8Array.some
    const int8 = new Int8Array([-10, 20, -30, 40, -50]);
    const positives = new Int8Array([10, 20, 30, 40, 50]);

M
mahaifeng 已提交
390
    let res = int8.some((element : number, index : number, array : Int8Array) : boolean =>
M
mahaifeng 已提交
391
      element < 0
M
mahaifeng 已提交
392 393
    );
    console.log(res); // Output: true
M
mahaifeng 已提交
394 395


M
mahaifeng 已提交
396
    let res1 = positives.some((element : number, index : number, array : Int8Array) : boolean =>
M
mahaifeng 已提交
397
      element < 0
M
mahaifeng 已提交
398 399
    );
    console.log(res1); // Output: false
M
mahaifeng 已提交
400
    // #END
M
mahaifeng 已提交
401 402
    expect(res1).toEqual(false);
    expect(res).toEqual(true);
M
mahaifeng 已提交
403
  }
M
mahaifeng 已提交
404
  // #endif
M
mahaifeng 已提交
405
}