TUint32Array.uts 10.5 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
  testuint32() {
    let uint32 = new Uint32Array(2);
    uint32[0] = 42;
    expect(uint32[0]).toEqual(42);
    expect(uint32.length).toEqual(2);
M
mahaifeng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    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 已提交
61 62 63
    let uint32 = new Uint32Array(buffer);
    uint32[1] = 42;
    expect(uint32.toString()).toEqual("0,42,0,0");
M
mahaifeng 已提交
64 65 66
  }

  testSet() {
M
mahaifeng 已提交
67
    // #TEST Uint32Array.set
M
mahaifeng 已提交
68
    let uint32 = new Uint32Array(8);
M
mahaifeng 已提交
69
    var array = [1, 2, 3];
M
mahaifeng 已提交
70
    uint32.set(array, 1);
M
mahaifeng 已提交
71
    console.log(uint32.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
72
    // #END
M
mahaifeng 已提交
73
    expect(uint32.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
74 75 76
  }

  testCopyWith() {
M
mahaifeng 已提交
77
    // #TEST Uint32Array.copyWithin
M
mahaifeng 已提交
78 79 80
    let uint32 = new Uint32Array(8);
    uint32.set([1, 2, 3], 1);
    uint32.copyWithin(3, 0, 3);
81 82 83
    let result2 = uint32.toString();
    console.log(result2); // "0,1,2,0,1,2,0,0"

M
mahaifeng 已提交
84
    // #END
85
    expect(result2).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
86 87 88
  }

  testEvery() {
M
mahaifeng 已提交
89 90
    // #TEST Uint32Array.every
    let result = new Uint32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint32Array) : boolean => value < 40);
M
mahaifeng 已提交
91
    console.log(result); // false
M
mahaifeng 已提交
92
    // #END
M
mahaifeng 已提交
93
    expect(result).toEqual(false);
M
mahaifeng 已提交
94 95
  }
  testFill() {
M
mahaifeng 已提交
96
    // #TEST Uint32Array.fill
M
mahaifeng 已提交
97
    let uint32 = new Uint32Array([1, 2, 3]).fill(4);
98 99 100
    let result1 = uint32.toString();
    console.log(result1); // "4,4,4"

M
mahaifeng 已提交
101
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1);
102 103 104
    let result2 = uint32.toString();
    console.log(result2); // "1,4,4"

M
mahaifeng 已提交
105
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 2);
106 107 108
    let result3 = uint32.toString();
    console.log(result3); // "1,4,3"

M
mahaifeng 已提交
109
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 1);
110 111 112
    let result4 = uint32.toString();
    console.log(result4); // "1,2,3"

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

M
mahaifeng 已提交
117
    // #END
118 119 120 121 122
    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 已提交
123
  }
M
mahaifeng 已提交
124 125


M
mahaifeng 已提交
126
  testFilter() {
M
mahaifeng 已提交
127 128
    // #TEST Uint32Array.filter
    let uint32 = new Uint32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint32Array) : boolean => value >= 10);
M
mahaifeng 已提交
129
    console.log(uint32.toString()); // "12,44"
M
mahaifeng 已提交
130
    // #END
M
mahaifeng 已提交
131
    expect(uint32.toString()).toEqual("12,44");
M
mahaifeng 已提交
132 133 134
  }

  find() {
M
mahaifeng 已提交
135
    // #TEST Uint32Array.find
M
mahaifeng 已提交
136
    let uint32 = new Uint32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
137
    let res = uint32.find((value : number, _ : number, _a : Uint32Array) : boolean => value > 5);
M
mahaifeng 已提交
138
    console.log(res); // 8
M
mahaifeng 已提交
139
    // #END
M
mahaifeng 已提交
140
    expect(res).toEqual(8);
M
mahaifeng 已提交
141 142 143
  }

  findIndex() {
M
mahaifeng 已提交
144
    // #TEST Uint32Array.findIndex
M
mahaifeng 已提交
145
    let uint32 = new Uint32Array([4, 6, 8, 12]);
146 147 148 149 150 151 152

    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 已提交
153
    // #END
154 155
    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
156 157
  }

158

M
mahaifeng 已提交
159
  foreach() {
M
mahaifeng 已提交
160 161
    // #TEST Uint32Array.forEach
    new Uint32Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Uint32Array) => {
M
mahaifeng 已提交
162 163
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
164
    // #END
M
mahaifeng 已提交
165
  }
M
mahaifeng 已提交
166

M
mahaifeng 已提交
167
  iterator() {
M
mahaifeng 已提交
168
    // #TEST Uint32Array.entries
M
mahaifeng 已提交
169 170
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
171 172
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
173
    // #END
M
mahaifeng 已提交
174
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
175 176 177
  }

  includes() {
M
mahaifeng 已提交
178
    // #TEST Uint32Array.includes
M
mahaifeng 已提交
179
    let uint32 = new Uint32Array([1, 2, 3]);
180 181 182 183 184 185 186 187 188 189

    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 已提交
190
    // #END
191 192 193
    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
194 195
  }

M
mahaifeng 已提交
196

M
mahaifeng 已提交
197
  indexOf() {
M
mahaifeng 已提交
198
    // #TEST Uint32Array.indexOf
M
mahaifeng 已提交
199
    let uint32 = new Uint32Array([2, 5, 9]);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

    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 已提交
216
    // #END
217 218 219 220 221
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
222
  }
M
mahaifeng 已提交
223

M
mahaifeng 已提交
224
  join() {
M
mahaifeng 已提交
225
    // #TEST Uint32Array.join
M
mahaifeng 已提交
226
    let uint32 = new Uint32Array([1, 2, 3]);
227 228 229 230 231 232 233 234 235 236

    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 已提交
237
    // #END
238 239 240
    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
241 242
  }

243

M
mahaifeng 已提交
244
  keys() {
M
mahaifeng 已提交
245
    // #TEST Uint32Array.keys
M
mahaifeng 已提交
246 247
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
248 249
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
250
    // #END
M
mahaifeng 已提交
251
    expect(value).toEqual(0);
M
mahaifeng 已提交
252 253
  }

M
mahaifeng 已提交
254

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

  reduce() {
M
mahaifeng 已提交
266
    // #TEST Uint32Array.reduce
M
mahaifeng 已提交
267
    let total = new Uint32Array([0, 1, 2, 3]);
268 269 270 271

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

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

M
mahaifeng 已提交
276
    // #END
277 278
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
279
  }
M
mahaifeng 已提交
280

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

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

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

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

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

M
mahaifeng 已提交
306

M
mahaifeng 已提交
307
  slice() {
M
mahaifeng 已提交
308
    // #TEST Uint32Array.slice
M
mahaifeng 已提交
309
    let uint32 = new Uint32Array([1, 2, 3]);
M
mahaifeng 已提交
310

311 312 313 314 315 316 317
    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 已提交
318

319 320 321 322 323 324 325
    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 已提交
326

M
mahaifeng 已提交
327
    // #END
328 329 330 331
    expect(res1String).toEqual("2,3");
    expect(res2String).toEqual("3");
    expect(res3String).toEqual("2,3");
    expect(res4String).toEqual("1");
M
mahaifeng 已提交
332 333 334
  }

  sort() {
M
mahaifeng 已提交
335
    // #TEST Uint32Array.sort
M
mahaifeng 已提交
336
    let numbers = new Uint32Array([40, 1, 5]);
337 338
    let numbersString = numbers.toString();
    console.log(numbersString); // "1,5,40"
M
mahaifeng 已提交
339

M
mahaifeng 已提交
340
    numbers.sort((a, b) : number => a - b);
341 342
    let sortedString = numbers.toString();
    console.log(sortedString); // "1,5,40"
M
mahaifeng 已提交
343
    // #END
344 345
    expect(numbersString).toEqual("40,1,5");
    expect(sortedString).toEqual("1,5,40");
M
mahaifeng 已提交
346
  }
M
mahaifeng 已提交
347

M
mahaifeng 已提交
348
  subarray() {
M
mahaifeng 已提交
349
    // #TEST Uint32Array.subarray
M
mahaifeng 已提交
350
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
351 352
    let uint32 = new Uint32Array(buffer);
    uint32.set([1, 2, 3]);
353 354
    let uint32String = uint32.toString();
    console.log(uint32String); // "1,2,3,0"
M
mahaifeng 已提交
355

M
mahaifeng 已提交
356
    let sub = uint32.subarray(0, 4);
357 358
    let subString = sub.toString();
    console.log(subString); // "1,2,3,0"
M
mahaifeng 已提交
359
    // #END
360 361
    expect(uint32String).toEqual("1,2,3,0");
    expect(subString).toEqual("1,2,3,0");
M
mahaifeng 已提交
362 363
  }

M
mahaifeng 已提交
364

365

M
mahaifeng 已提交
366
  values() {
M
mahaifeng 已提交
367
    // #TEST Uint32Array.values
M
mahaifeng 已提交
368 369
    let arr = new Uint32Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
370 371
    let value = values.next().value;
    console.log(value); // 1
M
mahaifeng 已提交
372
    // #END
M
mahaifeng 已提交
373
    expect(value).toEqual(1);
M
mahaifeng 已提交
374 375
  }
  arrayBufferSlice() {
M
mahaifeng 已提交
376
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
377
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
378 379
    let uint32 = new Uint32Array(buffer);
    uint32[3] = 42;
M
mahaifeng 已提交
380
    console.log(uint32.toString()); // "0,0,0,42"
M
mahaifeng 已提交
381 382 383

    let res = buffer.slice(8);
    let sliced = new Uint32Array(res);
M
mahaifeng 已提交
384
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
385
    // #END
386
    expect(uint32.toString()).toEqual("0,0,0,42");
M
mahaifeng 已提交
387
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
388
  }
M
mahaifeng 已提交
389 390


M
mahaifeng 已提交
391 392 393 394 395
  testSome() {
    // #TEST Uint32Array.some
    const uint32 = new Uint32Array([8, 20, 30, 40, 50]);
    const positives = new Uint32Array([10, 20, 30, 40, 50]);

396
    let result1 = uint32.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
397
      element < 10
M
mahaifeng 已提交
398
    );
399
    console.log(result1); // true
M
mahaifeng 已提交
400

401
    let result2 = positives.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
402
      element < 0
M
mahaifeng 已提交
403
    );
404
    console.log(result2); // false
M
mahaifeng 已提交
405
    // #END
406 407
    expect(result1).toEqual(true);
    expect(result2).toEqual(false);
M
mahaifeng 已提交
408
  }
M
mahaifeng 已提交
409

M
mahaifeng 已提交
410 411 412



M
mahaifeng 已提交
413
  // #endif
M
mahaifeng 已提交
414
}