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

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

M
mahaifeng 已提交
12
    this.testuint32();
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 Uint32Array.from
    var array = Uint32Array.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 Uint32Array.of
    var array = Uint32Array.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
  testuint32() {
    let uint32 = new Uint32Array(2);
    uint32[0] = 42;
    expect(uint32[0]).toEqual(42);
    expect(uint32.length).toEqual(2);
M
mahaifeng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    expect(Uint32Array.BYTES_PER_ELEMENT).toEqual(4);

    let x = new Uint32Array([21, 31, 3]);
    expect(x[1]).toEqual(31);

    let y = new Uint32Array(x);
    expect(y[0]).toEqual(21);

    let buffer = new ArrayBuffer(16);
    let z = new Uint32Array(buffer, 2, 4);
    expect(z.byteOffset).toEqual(2);
    expect(z.length).toEqual(4);
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
75 76 77
    let uint32 = new Uint32Array(buffer);
    uint32[1] = 42;
    expect(uint32.toString()).toEqual("0,42,0,0");
M
mahaifeng 已提交
78 79 80
  }

  testSet() {
M
mahaifeng 已提交
81
    // #TEST Uint32Array.set
M
mahaifeng 已提交
82
    let uint32 = new Uint32Array(8);
M
mahaifeng 已提交
83
    var array = [1, 2, 3];
M
mahaifeng 已提交
84
    uint32.set(array, 1);
M
mahaifeng 已提交
85
    console.log(uint32.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
86
    // #END
M
mahaifeng 已提交
87
    expect(uint32.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
88 89 90
  }

  testCopyWith() {
M
mahaifeng 已提交
91
    // #TEST Uint32Array.copyWithin
M
mahaifeng 已提交
92 93 94
    let uint32 = new Uint32Array(8);
    uint32.set([1, 2, 3], 1);
    uint32.copyWithin(3, 0, 3);
95 96 97
    let result2 = uint32.toString();
    console.log(result2); // "0,1,2,0,1,2,0,0"

M
mahaifeng 已提交
98
    // #END
99
    expect(result2).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
100 101 102
  }

  testEvery() {
M
mahaifeng 已提交
103 104
    // #TEST Uint32Array.every
    let result = new Uint32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint32Array) : boolean => value < 40);
M
mahaifeng 已提交
105
    console.log(result); // false
M
mahaifeng 已提交
106
    // #END
M
mahaifeng 已提交
107
    expect(result).toEqual(false);
M
mahaifeng 已提交
108 109
  }
  testFill() {
M
mahaifeng 已提交
110
    // #TEST Uint32Array.fill
M
mahaifeng 已提交
111
    let uint32 = new Uint32Array([1, 2, 3]).fill(4);
112 113 114
    let result1 = uint32.toString();
    console.log(result1); // "4,4,4"

M
mahaifeng 已提交
115
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1);
116 117 118
    let result2 = uint32.toString();
    console.log(result2); // "1,4,4"

M
mahaifeng 已提交
119
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 2);
120 121 122
    let result3 = uint32.toString();
    console.log(result3); // "1,4,3"

M
mahaifeng 已提交
123
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 1);
124 125 126
    let result4 = uint32.toString();
    console.log(result4); // "1,2,3"

M
mahaifeng 已提交
127
    uint32 = new Uint32Array([1, 2, 3]).fill(4, -3, -2);
128 129 130
    let result5 = uint32.toString();
    console.log(result5); // "4,2,3"

M
mahaifeng 已提交
131
    // #END
132 133 134 135 136
    expect(result1).toEqual("4,4,4");
    expect(result2).toEqual("1,4,4");
    expect(result3).toEqual("1,4,3");
    expect(result4).toEqual("1,2,3");
    expect(result5).toEqual("4,2,3");
M
mahaifeng 已提交
137
  }
M
mahaifeng 已提交
138 139


M
mahaifeng 已提交
140
  testFilter() {
M
mahaifeng 已提交
141 142
    // #TEST Uint32Array.filter
    let uint32 = new Uint32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint32Array) : boolean => value >= 10);
M
mahaifeng 已提交
143
    console.log(uint32.toString()); // "12,44"
M
mahaifeng 已提交
144
    // #END
M
mahaifeng 已提交
145
    expect(uint32.toString()).toEqual("12,44");
M
mahaifeng 已提交
146 147 148
  }

  find() {
M
mahaifeng 已提交
149
    // #TEST Uint32Array.find
M
mahaifeng 已提交
150
    let uint32 = new Uint32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
151
    let res = uint32.find((value : number, _ : number, _a : Uint32Array) : boolean => value > 5);
M
mahaifeng 已提交
152
    console.log(res); // 8
M
mahaifeng 已提交
153
    // #END
M
mahaifeng 已提交
154
    expect(res).toEqual(8);
M
mahaifeng 已提交
155 156 157
  }

  findIndex() {
M
mahaifeng 已提交
158
    // #TEST Uint32Array.findIndex
M
mahaifeng 已提交
159
    let uint32 = new Uint32Array([4, 6, 8, 12]);
160 161 162 163 164 165 166

    let res1 = uint32.findIndex((value : number, _ : number, _a : Uint32Array) : boolean => value > 100);
    console.log(res1); // -1

    let res2 = new Uint32Array([4, 6, 7, 120]).findIndex((value : number, _ : number, _a : Uint32Array) : boolean => value > 100);
    console.log(res2); // 3

M
mahaifeng 已提交
167
    // #END
168 169
    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
170 171
  }

172

M
mahaifeng 已提交
173
  foreach() {
M
mahaifeng 已提交
174 175
    // #TEST Uint32Array.forEach
    new Uint32Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Uint32Array) => {
M
mahaifeng 已提交
176 177
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
178
    // #END
M
mahaifeng 已提交
179
  }
M
mahaifeng 已提交
180

M
mahaifeng 已提交
181
  iterator() {
M
mahaifeng 已提交
182
    // #TEST Uint32Array.entries
M
mahaifeng 已提交
183 184
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
185 186
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
187
    // #END
M
mahaifeng 已提交
188
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
189 190 191
  }

  includes() {
M
mahaifeng 已提交
192
    // #TEST Uint32Array.includes
M
mahaifeng 已提交
193
    let uint32 = new Uint32Array([1, 2, 3]);
194 195 196 197 198 199 200 201 202 203

    let res1 = uint32.includes(2);
    console.log(res1); // true

    let res2 = uint32.includes(4);
    console.log(res2); // false

    let res3 = uint32.includes(3, 3);
    console.log(res3); // false

M
mahaifeng 已提交
204
    // #END
205 206 207
    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
208 209
  }

M
mahaifeng 已提交
210

M
mahaifeng 已提交
211
  indexOf() {
M
mahaifeng 已提交
212
    // #TEST Uint32Array.indexOf
M
mahaifeng 已提交
213
    let uint32 = new Uint32Array([2, 5, 9]);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    let res1 = uint32.indexOf(2);
    console.log(res1); // 0

    let res2 = uint32.indexOf(7);
    console.log(res2); // -1

    let res3 = uint32.indexOf(9, 2);
    console.log(res3); // 2

    let res4 = uint32.indexOf(2, -1);
    console.log(res4); // -1

    let res5 = uint32.indexOf(2, -3);
    console.log(res5); // 0

M
mahaifeng 已提交
230
    // #END
231 232 233 234 235
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
236
  }
M
mahaifeng 已提交
237

M
mahaifeng 已提交
238
  join() {
M
mahaifeng 已提交
239
    // #TEST Uint32Array.join
M
mahaifeng 已提交
240
    let uint32 = new Uint32Array([1, 2, 3]);
241 242 243 244 245 246 247 248 249 250

    let res1 = uint32.join();
    console.log(res1); // "1,2,3"

    let res2 = uint32.join(" / ");
    console.log(res2); // "1 / 2 / 3"

    let res3 = uint32.join("");
    console.log(res3); // "123"

M
mahaifeng 已提交
251
    // #END
252 253 254
    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
255 256
  }

257

M
mahaifeng 已提交
258
  keys() {
M
mahaifeng 已提交
259
    // #TEST Uint32Array.keys
M
mahaifeng 已提交
260 261
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
262 263
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
264
    // #END
M
mahaifeng 已提交
265
    expect(value).toEqual(0);
M
mahaifeng 已提交
266 267
  }

M
mahaifeng 已提交
268

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

  reduce() {
M
mahaifeng 已提交
280
    // #TEST Uint32Array.reduce
M
mahaifeng 已提交
281
    let total = new Uint32Array([0, 1, 2, 3]);
282 283 284 285

    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue);
    console.log(res1); // 6

M
mahaifeng 已提交
286
    total = new Uint32Array([0, 1, 2, 3]);
287 288 289
    let res2 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue, 8);
    console.log(res2); // 14

M
mahaifeng 已提交
290
    // #END
291 292
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
293
  }
M
mahaifeng 已提交
294

M
mahaifeng 已提交
295
  reduceRight() {
M
mahaifeng 已提交
296
    // #TEST Uint32Array.reduceRight
M
mahaifeng 已提交
297
    let total = new Uint32Array([0, 1, 2, 3]);
298 299 300 301

    let res1 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue);
    console.log(res1); // 6

M
mahaifeng 已提交
302
    total = new Uint32Array([0, 1, 2, 3]);
303 304 305
    let res2 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue, 8);
    console.log(res2); // 14

M
mahaifeng 已提交
306
    // #END
307 308
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
309 310 311
  }

  reverse() {
M
mahaifeng 已提交
312
    // #TEST Uint32Array.reverse
M
mahaifeng 已提交
313 314
    let uint32 = new Uint32Array([1, 2, 3]);
    uint32.reverse();
M
mahaifeng 已提交
315
    console.log(uint32.toString()); // "3,2,1"
M
mahaifeng 已提交
316
    // #END
M
mahaifeng 已提交
317
    expect(uint32.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
318 319
  }

M
mahaifeng 已提交
320

M
mahaifeng 已提交
321
  slice() {
M
mahaifeng 已提交
322
    // #TEST Uint32Array.slice
M
mahaifeng 已提交
323
    let uint32 = new Uint32Array([1, 2, 3]);
M
mahaifeng 已提交
324

325 326 327 328 329 330 331
    let res1 = uint32.slice(1);
    let res1String = res1.toString();
    console.log(res1String); // "2,3"

    let res2 = uint32.slice(2);
    let res2String = res2.toString();
    console.log(res2String); // "3"
M
mahaifeng 已提交
332

333 334 335 336 337 338 339
    let res3 = uint32.slice(-2);
    let res3String = res3.toString();
    console.log(res3String); // "2,3"

    let res4 = uint32.slice(0, 1);
    let res4String = res4.toString();
    console.log(res4String); // "1"
M
mahaifeng 已提交
340

M
mahaifeng 已提交
341
    // #END
342 343 344 345
    expect(res1String).toEqual("2,3");
    expect(res2String).toEqual("3");
    expect(res3String).toEqual("2,3");
    expect(res4String).toEqual("1");
M
mahaifeng 已提交
346 347 348
  }

  sort() {
M
mahaifeng 已提交
349
    // #TEST Uint32Array.sort
M
mahaifeng 已提交
350
    let numbers = new Uint32Array([40, 1, 5]);
351 352
    let numbersString = numbers.toString();
    console.log(numbersString); // "1,5,40"
M
mahaifeng 已提交
353

M
mahaifeng 已提交
354
    numbers.sort((a, b) : number => a - b);
355 356
    let sortedString = numbers.toString();
    console.log(sortedString); // "1,5,40"
M
mahaifeng 已提交
357
    // #END
358 359
    expect(numbersString).toEqual("40,1,5");
    expect(sortedString).toEqual("1,5,40");
M
mahaifeng 已提交
360
  }
M
mahaifeng 已提交
361

M
mahaifeng 已提交
362
  subarray() {
M
mahaifeng 已提交
363
    // #TEST Uint32Array.subarray
M
mahaifeng 已提交
364
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
365 366
    let uint32 = new Uint32Array(buffer);
    uint32.set([1, 2, 3]);
367 368
    let uint32String = uint32.toString();
    console.log(uint32String); // "1,2,3,0"
M
mahaifeng 已提交
369

M
mahaifeng 已提交
370
    let sub = uint32.subarray(0, 4);
371 372
    let subString = sub.toString();
    console.log(subString); // "1,2,3,0"
M
mahaifeng 已提交
373
    // #END
374 375
    expect(uint32String).toEqual("1,2,3,0");
    expect(subString).toEqual("1,2,3,0");
M
mahaifeng 已提交
376 377
  }

M
mahaifeng 已提交
378

379

M
mahaifeng 已提交
380
  values() {
M
mahaifeng 已提交
381
    // #TEST Uint32Array.values
M
mahaifeng 已提交
382 383
    let arr = new Uint32Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
384 385
    let value = values.next().value;
    console.log(value); // 1
M
mahaifeng 已提交
386
    // #END
M
mahaifeng 已提交
387
    expect(value).toEqual(1);
M
mahaifeng 已提交
388 389
  }
  arrayBufferSlice() {
M
mahaifeng 已提交
390
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
391
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
392 393
    let uint32 = new Uint32Array(buffer);
    uint32[3] = 42;
M
mahaifeng 已提交
394
    console.log(uint32.toString()); // "0,0,0,42"
M
mahaifeng 已提交
395 396 397

    let res = buffer.slice(8);
    let sliced = new Uint32Array(res);
M
mahaifeng 已提交
398
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
399
    // #END
400
    expect(uint32.toString()).toEqual("0,0,0,42");
M
mahaifeng 已提交
401
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
402
  }
M
mahaifeng 已提交
403 404


M
mahaifeng 已提交
405 406 407 408 409
  testSome() {
    // #TEST Uint32Array.some
    const uint32 = new Uint32Array([8, 20, 30, 40, 50]);
    const positives = new Uint32Array([10, 20, 30, 40, 50]);

410
    let result1 = uint32.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
411
      element < 10
M
mahaifeng 已提交
412
    );
413
    console.log(result1); // true
M
mahaifeng 已提交
414

415
    let result2 = positives.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
416
      element < 0
M
mahaifeng 已提交
417
    );
418
    console.log(result2); // false
M
mahaifeng 已提交
419
    // #END
420 421
    expect(result1).toEqual(true);
    expect(result2).toEqual(false);
M
mahaifeng 已提交
422
  }
M
mahaifeng 已提交
423

M
mahaifeng 已提交
424 425 426



M
mahaifeng 已提交
427
  // #endif
M
mahaifeng 已提交
428
}