TUint16Array.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 TUint16Array {
  test() {
M
mahaifeng 已提交
10
    // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
11
    this.testuint16();
M
mahaifeng 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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 已提交
35
    // #endif
M
mahaifeng 已提交
36
  }
M
mahaifeng 已提交
37

M
mahaifeng 已提交
38
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
  from() {
    // #TEST Uint16Array.from
    var array = Uint16Array.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 Uint16Array.of
    var array = Uint16Array.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
53 54 55 56 57
  testuint16() {
    let uint16 = new Uint16Array(2);
    uint16[0] = 42;
    expect(uint16[0]).toEqual(42);
    expect(uint16.length).toEqual(2);
M
mahaifeng 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    expect(Uint16Array.BYTES_PER_ELEMENT).toEqual(4);

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

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

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

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

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

  testCopyWith() {
M
mahaifeng 已提交
90 91
    // #TEST Uint16Array.copyWithin
    console.log("testCopyWith 1");
M
mahaifeng 已提交
92 93
    let uint16 = new Uint16Array(8);
    uint16.set([1, 2, 3], 1);
M
mahaifeng 已提交
94
    console.log("testCopyWith 2");
M
mahaifeng 已提交
95
    uint16.copyWithin(3, 0, 3);
M
mahaifeng 已提交
96 97
    console.log("testCopyWith 3");
    console.log(uint16.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
98
    // #END
M
mahaifeng 已提交
99
    expect(uint16.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
100
  }
M
mahaifeng 已提交
101

M
mahaifeng 已提交
102
  testEvery() {
M
mahaifeng 已提交
103 104 105
    // #TEST Uint16Array.every
    let result = new Uint16Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint16Array) :
      boolean => value < 40);
M
mahaifeng 已提交
106
    console.log(result); // false
M
mahaifeng 已提交
107
    // #END
M
mahaifeng 已提交
108
    expect(result).toEqual(false);
M
mahaifeng 已提交
109 110 111
  }

  testFill() {
M
mahaifeng 已提交
112
    // #TEST Uint16Array.fill
M
mahaifeng 已提交
113
    let uint16 = new Uint16Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
114
    console.log(uint16.toString()); // "4,4,4"
M
mahaifeng 已提交
115

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

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

122 123
    let uint16_t3 = new Uint16Array([1, 2, 3]).fill(4, 1, 1);
    console.log(uint16_t3.toString()); // "1,2,3"
M
mahaifeng 已提交
124

125 126
    let uint16_t4 = new Uint16Array([1, 2, 3]).fill(4, -3, -2);
    console.log(uint16_t4.toString()); // "4,2,3"
M
mahaifeng 已提交
127
    // #END
128 129 130 131 132
    expect(uint16_t4.toString()).toEqual("4,2,3");
    expect(uint16.toString()).toEqual("4,4,4");
    expect(uint16_t1.toString()).toEqual("1,4,4");
    expect(uint16_t2.toString()).toEqual("1,4,3");
    expect(uint16_t3.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
133 134 135
  }

  testFilter() {
M
mahaifeng 已提交
136 137
    // #TEST Uint16Array.filter
    let uint16 = new Uint16Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint16Array) :
M
mahaifeng 已提交
138
      boolean => value >= 10);
M
mahaifeng 已提交
139
    console.log(uint16.toString()); // "12,44"
M
mahaifeng 已提交
140
    // #END
M
mahaifeng 已提交
141
    expect(uint16.toString()).toEqual("12,44");
M
mahaifeng 已提交
142 143 144
  }

  find() {
M
mahaifeng 已提交
145
    // #TEST Uint16Array.find
M
mahaifeng 已提交
146
    let uint16 = new Uint16Array([4, 5, 8, 12]);
M
mahaifeng 已提交
147
    let res = uint16.find((value : number, _ : number, _a : Uint16Array) : boolean => value > 5);
M
mahaifeng 已提交
148
    console.log(res); // 8
M
mahaifeng 已提交
149
    // #END
M
mahaifeng 已提交
150
    expect(res).toEqual(8);
M
mahaifeng 已提交
151 152 153
  }

  findIndex() {
M
mahaifeng 已提交
154
    // #TEST Uint16Array.findIndex
M
mahaifeng 已提交
155
    let uint16 = new Uint16Array([4, 6, 8, 12]);
M
mahaifeng 已提交
156
    let res = uint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100);
M
mahaifeng 已提交
157
    console.log(res); // -1
158

M
mahaifeng 已提交
159

M
mahaifeng 已提交
160
    let uuint16 = new Uint16Array([4, 6, 7, 120]);
161 162
    let res1 = uuint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100);
    console.log(res1); // 3
M
mahaifeng 已提交
163
    // #END
164 165 166
    expect(res1).toEqual(3);

    expect(res).toEqual(-1);
M
mahaifeng 已提交
167 168 169
  }

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

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

  includes() {
M
mahaifeng 已提交
188
    // #TEST Uint16Array.includes
M
mahaifeng 已提交
189 190
    let uint16 = new Uint16Array([1, 2, 3]);
    let res = uint16.includes(2);
M
mahaifeng 已提交
191
    console.log(res); // true
M
mahaifeng 已提交
192

193 194
    let res1 = uint16.includes(4);
    console.log(res1); // false
M
mahaifeng 已提交
195

196 197
    let res2 = uint16.includes(3, 3);
    console.log(res2); // false
M
mahaifeng 已提交
198
    // #END
199 200 201
    expect(res2).toEqual(false);
    expect(res1).toEqual(false);
    expect(res).toEqual(true);
M
mahaifeng 已提交
202
  }
M
mahaifeng 已提交
203

M
mahaifeng 已提交
204
  indexOf() {
M
mahaifeng 已提交
205
    // #TEST Uint16Array.indexOf
M
mahaifeng 已提交
206
    let uint16 = new Uint16Array([2, 5, 9]);
M
mahaifeng 已提交
207

208 209
    let res1 = uint16.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
210

211 212
    let res2 = uint16.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
213

214 215 216 217 218
    let res3 = uint16.indexOf(9, 2);
    console.log(res3); // 2

    let res4 = uint16.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
219

220 221
    let res5 = uint16.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
222
    // #END
M
mahaifeng 已提交
223

224 225 226 227 228 229
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
  }
M
mahaifeng 已提交
230
  join() {
M
mahaifeng 已提交
231
    // #TEST Uint16Array.join
M
mahaifeng 已提交
232
    let uint16 = new Uint16Array([1, 2, 3]);
233 234
    let res1 = uint16.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
235

236 237
    let res2 = uint16.join(" / ");
    console.log(res2); // "1 / 2 / 3"
M
mahaifeng 已提交
238

239 240
    let res3 = uint16.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
241
    // #END
242 243 244 245

    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
246 247 248
  }

  keys() {
M
mahaifeng 已提交
249
    // #TEST Uint16Array.keys
M
mahaifeng 已提交
250 251
    let arr = new Uint16Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
252 253
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
254
    // #END
M
mahaifeng 已提交
255
    expect(value).toEqual(0);
M
mahaifeng 已提交
256 257 258
  }

  map() {
M
mahaifeng 已提交
259
    // #TEST Uint16Array.map
M
mahaifeng 已提交
260
    let numbers = new Uint16Array([1, 4, 9]);
M
mahaifeng 已提交
261
    let doubles = numbers.map((value : number, _ : number, _a : Uint16Array) : number => value * 2);
M
mahaifeng 已提交
262 263 264
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
265 266 267 268
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }
  reduce() {
M
mahaifeng 已提交
269
    // #TEST Uint16Array.reduce
M
mahaifeng 已提交
270
    let total = new Uint16Array([0, 1, 2, 3]);
271 272 273 274
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue
    );
    console.log(res1); // 6
M
mahaifeng 已提交
275 276

    total = new Uint16Array([0, 1, 2, 3]);
277 278 279 280
    let res2 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
281
    // #END
282 283 284

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
285 286 287
  }

  reduceRight() {
M
mahaifeng 已提交
288
    // #TEST Uint16Array.reduceRight
M
mahaifeng 已提交
289
    let total = new Uint16Array([0, 1, 2, 3]);
290 291 292 293
    let res1 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue
    );
    console.log(res1); // 6
M
mahaifeng 已提交
294 295

    total = new Uint16Array([0, 1, 2, 3]);
296 297 298 299
    let res2 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
300
    // #END
301 302 303

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
304 305 306
  }

  reverse() {
M
mahaifeng 已提交
307
    // #TEST Uint16Array.reverse
M
mahaifeng 已提交
308 309
    let uint16 = new Uint16Array([1, 2, 3]);
    uint16.reverse();
M
mahaifeng 已提交
310
    console.log(uint16.toString()); // "3,2,1"
M
mahaifeng 已提交
311
    // #END
M
mahaifeng 已提交
312
    expect(uint16.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
313 314 315
  }

  slice() {
M
mahaifeng 已提交
316
    // #TEST Uint16Array.slice
M
mahaifeng 已提交
317
    let uint16 = new Uint16Array([1, 2, 3]);
318 319
    let res1 = uint16.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
320

321 322
    let res2 = uint16.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
323

324 325
    let res3 = uint16.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
326

327 328
    let res4 = uint16.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
329
    // #END
330 331 332 333 334

    expect(res1.toString()).toEqual("2,3");
    expect(res2.toString()).toEqual("3");
    expect(res3.toString()).toEqual("2,3");
    expect(res4.toString()).toEqual("1");
M
mahaifeng 已提交
335
  }
M
mahaifeng 已提交
336

337

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

M
mahaifeng 已提交
344
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
345
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
346
    // #END
347 348

    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
349
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
350 351
  }

352

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

M
mahaifeng 已提交
360
    let sub = uint16.subarray(0, 4);
M
mahaifeng 已提交
361
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
362
    // #END
363 364

    expect(uint16.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
365
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
366 367
  }

368

M
mahaifeng 已提交
369
  values() {
M
mahaifeng 已提交
370
    // #TEST Uint16Array.values
M
mahaifeng 已提交
371 372
    let arr = new Uint16Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
373 374
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
375
    // #END
M
mahaifeng 已提交
376
    expect(value).toEqual(1);
M
mahaifeng 已提交
377 378 379
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
380
    // #TEST Uint16Array.arrayBufferSlice
M
mahaifeng 已提交
381
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
382 383
    let uint16 = new Uint16Array(buffer);
    uint16[4] = 42;
M
mahaifeng 已提交
384
    console.log(uint16.toString()); // "0,0,0,0,42,0,0,0"
M
mahaifeng 已提交
385 386 387

    let res = buffer.slice(8);
    let sliced = new Uint16Array(res);
M
mahaifeng 已提交
388
    console.log(sliced[0]); // 42
389

M
mahaifeng 已提交
390
    // #END
391
    expect(uint16.toString()).toEqual("0,0,0,0,42,0,0,0");
M
mahaifeng 已提交
392
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
393
  }
M
mahaifeng 已提交
394

395

M
mahaifeng 已提交
396 397 398 399
  testSome() {
    // #TEST Uint16Array.some
    const uint16 = new Uint16Array([8, 20, 30, 40, 50]);
    const positives = new Uint16Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
400

401
    const res1 = uint16.some((element : number, index : number, array : Uint16Array) : boolean =>
M
mahaifeng 已提交
402
      element < 10
403 404
    );
    console.log('uint16', res1); // true
M
mahaifeng 已提交
405

406
    const res2 = positives.some((element : number, index : number, array : Uint16Array) : boolean =>
M
mahaifeng 已提交
407
      element < 0
408 409
    );
    console.log('positives', res2); // false
M
mahaifeng 已提交
410
    // #END
411 412
    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
M
mahaifeng 已提交
413
  }
M
mahaifeng 已提交
414

415

M
mahaifeng 已提交
416
  // #endif
M
mahaifeng 已提交
417
}