TUint16Array.uts 10.4 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 (UNI-APP-X && 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();
35
    // #endif
M
mahaifeng 已提交
36
  }
M
mahaifeng 已提交
37

M
mahaifeng 已提交
38
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB
M
mahaifeng 已提交
39 40 41 42 43
  testuint16() {
    let uint16 = new Uint16Array(2);
    uint16[0] = 42;
    expect(uint16[0]).toEqual(42);
    expect(uint16.length).toEqual(2);
M
mahaifeng 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    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 已提交
60 61 62
    let uint16 = new Uint16Array(buffer);
    uint16[1] = 42;
    expect(uint16.toString()).toEqual("0,42,0,0,0,0,0,0");
M
mahaifeng 已提交
63 64 65
  }

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

  testCopyWith() {
M
mahaifeng 已提交
76 77
    // #TEST Uint16Array.copyWithin
    console.log("testCopyWith 1");
M
mahaifeng 已提交
78 79
    let uint16 = new Uint16Array(8);
    uint16.set([1, 2, 3], 1);
M
mahaifeng 已提交
80
    console.log("testCopyWith 2");
M
mahaifeng 已提交
81
    uint16.copyWithin(3, 0, 3);
M
mahaifeng 已提交
82 83
    console.log("testCopyWith 3");
    console.log(uint16.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
84
    // #END
M
mahaifeng 已提交
85
    expect(uint16.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
86
  }
M
mahaifeng 已提交
87

M
mahaifeng 已提交
88
  testEvery() {
M
mahaifeng 已提交
89 90 91
    // #TEST Uint16Array.every
    let result = new Uint16Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint16Array) :
      boolean => value < 40);
M
mahaifeng 已提交
92
    console.log(result); // false
M
mahaifeng 已提交
93
    // #END
M
mahaifeng 已提交
94
    expect(result).toEqual(false);
M
mahaifeng 已提交
95 96 97
  }

  testFill() {
M
mahaifeng 已提交
98
    // #TEST Uint16Array.fill
M
mahaifeng 已提交
99
    let uint16 = new Uint16Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
100
    console.log(uint16.toString()); // "4,4,4"
M
mahaifeng 已提交
101

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

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

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

111 112
    let uint16_t4 = new Uint16Array([1, 2, 3]).fill(4, -3, -2);
    console.log(uint16_t4.toString()); // "4,2,3"
M
mahaifeng 已提交
113
    // #END
114 115 116 117 118
    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 已提交
119 120 121
  }

  testFilter() {
M
mahaifeng 已提交
122 123
    // #TEST Uint16Array.filter
    let uint16 = new Uint16Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint16Array) :
M
mahaifeng 已提交
124
      boolean => value >= 10);
M
mahaifeng 已提交
125
    console.log(uint16.toString()); // "12,44"
M
mahaifeng 已提交
126
    // #END
M
mahaifeng 已提交
127
    expect(uint16.toString()).toEqual("12,44");
M
mahaifeng 已提交
128 129 130
  }

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

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

M
mahaifeng 已提交
145

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

    expect(res).toEqual(-1);
M
mahaifeng 已提交
153 154 155
  }

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

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

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

179 180
    let res1 = uint16.includes(4);
    console.log(res1); // false
M
mahaifeng 已提交
181

182 183
    let res2 = uint16.includes(3, 3);
    console.log(res2); // false
M
mahaifeng 已提交
184
    // #END
185 186 187
    expect(res2).toEqual(false);
    expect(res1).toEqual(false);
    expect(res).toEqual(true);
M
mahaifeng 已提交
188
  }
M
mahaifeng 已提交
189

M
mahaifeng 已提交
190
  indexOf() {
M
mahaifeng 已提交
191
    // #TEST Uint16Array.indexOf
M
mahaifeng 已提交
192
    let uint16 = new Uint16Array([2, 5, 9]);
M
mahaifeng 已提交
193

194 195
    let res1 = uint16.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
196

197 198
    let res2 = uint16.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
199

200 201 202 203 204
    let res3 = uint16.indexOf(9, 2);
    console.log(res3); // 2

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

206 207
    let res5 = uint16.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
208
    // #END
M
mahaifeng 已提交
209

210 211 212 213 214 215
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
  }
M
mahaifeng 已提交
216
  join() {
M
mahaifeng 已提交
217
    // #TEST Uint16Array.join
M
mahaifeng 已提交
218
    let uint16 = new Uint16Array([1, 2, 3]);
219 220
    let res1 = uint16.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
221

222 223
    let res2 = uint16.join(" / ");
    console.log(res2); // "1 / 2 / 3"
M
mahaifeng 已提交
224

225 226
    let res3 = uint16.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
227
    // #END
228 229 230 231

    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
232 233 234
  }

  keys() {
M
mahaifeng 已提交
235
    // #TEST Uint16Array.keys
M
mahaifeng 已提交
236 237
    let arr = new Uint16Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
238 239
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
240
    // #END
M
mahaifeng 已提交
241
    expect(value).toEqual(0);
M
mahaifeng 已提交
242 243 244
  }

  map() {
M
mahaifeng 已提交
245
    // #TEST Uint16Array.map
M
mahaifeng 已提交
246
    let numbers = new Uint16Array([1, 4, 9]);
M
mahaifeng 已提交
247
    let doubles = numbers.map((value : number, _ : number, _a : Uint16Array) : number => value * 2);
M
mahaifeng 已提交
248 249 250
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
251 252 253 254
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }
  reduce() {
M
mahaifeng 已提交
255
    // #TEST Uint16Array.reduce
M
mahaifeng 已提交
256
    let total = new Uint16Array([0, 1, 2, 3]);
257 258 259 260
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue
    );
    console.log(res1); // 6
M
mahaifeng 已提交
261 262

    total = new Uint16Array([0, 1, 2, 3]);
263 264 265 266
    let res2 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
267
    // #END
268 269 270

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
271 272 273
  }

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

    total = new Uint16Array([0, 1, 2, 3]);
282 283 284 285
    let res2 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint16Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
286
    // #END
287 288 289

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
290 291 292
  }

  reverse() {
M
mahaifeng 已提交
293
    // #TEST Uint16Array.reverse
M
mahaifeng 已提交
294 295
    let uint16 = new Uint16Array([1, 2, 3]);
    uint16.reverse();
M
mahaifeng 已提交
296
    console.log(uint16.toString()); // "3,2,1"
M
mahaifeng 已提交
297
    // #END
M
mahaifeng 已提交
298
    expect(uint16.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
299 300 301
  }

  slice() {
M
mahaifeng 已提交
302
    // #TEST Uint16Array.slice
M
mahaifeng 已提交
303
    let uint16 = new Uint16Array([1, 2, 3]);
304 305
    let res1 = uint16.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
306

307 308
    let res2 = uint16.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
309

310 311
    let res3 = uint16.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
312

313 314
    let res4 = uint16.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
315
    // #END
316 317 318 319 320

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

323

M
mahaifeng 已提交
324
  sort() {
M
mahaifeng 已提交
325
    // #TEST Uint16Array.sort
M
mahaifeng 已提交
326 327
    let numbers = new Uint16Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
328
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
329

M
mahaifeng 已提交
330
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
331
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
332
    // #END
333 334

    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
335
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
336 337
  }

338

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

M
mahaifeng 已提交
346
    let sub = uint16.subarray(0, 4);
M
mahaifeng 已提交
347
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
348
    // #END
349 350

    expect(uint16.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
351
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
352 353
  }

354

M
mahaifeng 已提交
355
  values() {
M
mahaifeng 已提交
356
    // #TEST Uint16Array.values
M
mahaifeng 已提交
357 358
    let arr = new Uint16Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
359 360
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
361
    // #END
M
mahaifeng 已提交
362
    expect(value).toEqual(1);
M
mahaifeng 已提交
363 364 365
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
366
    // #TEST Uint16Array.arrayBufferSlice
M
mahaifeng 已提交
367
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
368 369
    let uint16 = new Uint16Array(buffer);
    uint16[4] = 42;
M
mahaifeng 已提交
370
    console.log(uint16.toString()); // "0,0,0,0,42,0,0,0"
M
mahaifeng 已提交
371 372 373

    let res = buffer.slice(8);
    let sliced = new Uint16Array(res);
M
mahaifeng 已提交
374
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
375
    // #END
M
mahaifeng 已提交
376
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
377
  }
M
mahaifeng 已提交
378

M
mahaifeng 已提交
379 380 381 382
  testSome() {
    // #TEST Uint16Array.some
    const uint16 = new Uint16Array([8, 20, 30, 40, 50]);
    const positives = new Uint16Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
383

M
mahaifeng 已提交
384 385 386 387 388 389 390 391 392
    expect(uint16.some((element : number, index : number, array : Uint16Array) : boolean =>
      element < 10
    )).toEqual(true);

    expect(positives.some((element : number, index : number, array : Uint16Array) : boolean =>
      element < 0
    )).toEqual(false);
    // #END
  }
M
mahaifeng 已提交
393

394
  // #endif
M
mahaifeng 已提交
395
}