TUint16Array.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 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();
M
mahaifeng 已提交
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 101
    console.log(uint16.toString()); // "4,4,4"
    // #END
M
mahaifeng 已提交
102
    expect(uint16.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
103

M
mahaifeng 已提交
104
    uint16 = new Uint16Array([1, 2, 3]).fill(4, 1);
M
mahaifeng 已提交
105 106
    console.log(uint16.toString()); // "1,4,4"
    // #END
M
mahaifeng 已提交
107
    expect(uint16.toString()).toEqual("1,4,4");
M
mahaifeng 已提交
108

M
mahaifeng 已提交
109
    uint16 = new Uint16Array([1, 2, 3]).fill(4, 1, 2);
M
mahaifeng 已提交
110 111
    console.log(uint16.toString()); // "1,4,3"
    // #END
M
mahaifeng 已提交
112
    expect(uint16.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
113

M
mahaifeng 已提交
114
    uint16 = new Uint16Array([1, 2, 3]).fill(4, 1, 1);
M
mahaifeng 已提交
115 116
    console.log(uint16.toString()); // "1,2,3"
    // #END
M
mahaifeng 已提交
117
    expect(uint16.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
118

M
mahaifeng 已提交
119
    uint16 = new Uint16Array([1, 2, 3]).fill(4, -3, -2);
M
mahaifeng 已提交
120
    console.log(uint16.toString()); // "4,2,3"
M
mahaifeng 已提交
121
    // #END
M
mahaifeng 已提交
122
    expect(uint16.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
123 124 125
  }

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

  find() {
M
mahaifeng 已提交
135
    // #TEST Uint16Array.find
M
mahaifeng 已提交
136
    let uint16 = new Uint16Array([4, 5, 8, 12]);
M
mahaifeng 已提交
137
    let res = uint16.find((value : number, _ : number, _a : Uint16Array) : 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 Uint16Array.findIndex
M
mahaifeng 已提交
145
    let uint16 = new Uint16Array([4, 6, 8, 12]);
M
mahaifeng 已提交
146
    let res = uint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100);
M
mahaifeng 已提交
147 148
    console.log(res); // -1
    // #END
M
mahaifeng 已提交
149 150
    expect(res).toEqual(-1);

M
mahaifeng 已提交
151
    let uuint16 = new Uint16Array([4, 6, 7, 120]);
M
mahaifeng 已提交
152
    res = uuint16.findIndex((value : number, _ : number, _a : Uint16Array) : boolean => value > 100);
M
mahaifeng 已提交
153
    console.log(res); // 3
M
mahaifeng 已提交
154
    // #END
M
mahaifeng 已提交
155
    expect(res).toEqual(3);
M
mahaifeng 已提交
156 157 158
  }

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

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

  includes() {
M
mahaifeng 已提交
177
    // #TEST Uint16Array.includes
M
mahaifeng 已提交
178 179
    let uint16 = new Uint16Array([1, 2, 3]);
    let res = uint16.includes(2);
M
mahaifeng 已提交
180 181
    console.log(res); // true
    // #END
M
mahaifeng 已提交
182 183
    expect(res).toEqual(true);

M
mahaifeng 已提交
184
    res = uint16.includes(4);
M
mahaifeng 已提交
185 186
    console.log(res); // false
    // #END
M
mahaifeng 已提交
187 188
    expect(res).toEqual(false);

M
mahaifeng 已提交
189
    res = uint16.includes(3, 3);
M
mahaifeng 已提交
190
    console.log(res); // false
M
mahaifeng 已提交
191
    // #END
M
mahaifeng 已提交
192
    expect(res).toEqual(false);
M
mahaifeng 已提交
193
  }
M
mahaifeng 已提交
194

M
mahaifeng 已提交
195
  indexOf() {
M
mahaifeng 已提交
196
    // #TEST Uint16Array.indexOf
M
mahaifeng 已提交
197 198
    let uint16 = new Uint16Array([2, 5, 9]);
    let res = uint16.indexOf(2);
M
mahaifeng 已提交
199 200
    console.log(res); // 0
    // #END
M
mahaifeng 已提交
201 202
    expect(res).toEqual(0);

M
mahaifeng 已提交
203
    res = uint16.indexOf(7);
M
mahaifeng 已提交
204 205
    console.log(res); // -1
    // #END
M
mahaifeng 已提交
206 207
    expect(res).toEqual(-1);

M
mahaifeng 已提交
208
    res = uint16.indexOf(9, 2);
M
mahaifeng 已提交
209 210
    console.log(res); // 2
    // #END
M
mahaifeng 已提交
211 212
    expect(res).toEqual(2);

M
mahaifeng 已提交
213
    res = uint16.indexOf(2, -1);
M
mahaifeng 已提交
214 215
    console.log(res); // -1
    // #END
M
mahaifeng 已提交
216 217
    expect(res).toEqual(-1);

M
mahaifeng 已提交
218
    res = uint16.indexOf(2, -3);
M
mahaifeng 已提交
219
    console.log(res); // 0
M
mahaifeng 已提交
220
    // #END
M
mahaifeng 已提交
221
    expect(res).toEqual(0);
M
mahaifeng 已提交
222 223 224
  }

  join() {
M
mahaifeng 已提交
225
    // #TEST Uint16Array.join
M
mahaifeng 已提交
226 227
    let uint16 = new Uint16Array([1, 2, 3]);
    let res = uint16.join();
M
mahaifeng 已提交
228 229
    console.log(res); // "1,2,3"
    // #END
M
mahaifeng 已提交
230 231
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
232
    res = uint16.join(" / ");
M
mahaifeng 已提交
233 234
    console.log(res); // "1 / 2 / 3"
    // #END
M
mahaifeng 已提交
235 236
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
237
    res = uint16.join("");
M
mahaifeng 已提交
238
    console.log(res); // "123"
M
mahaifeng 已提交
239
    // #END
M
mahaifeng 已提交
240
    expect(res).toEqual("123");
M
mahaifeng 已提交
241 242 243
  }

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

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

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

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

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

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

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

M
mahaifeng 已提交
314
    res = uint16.slice(2);
M
mahaifeng 已提交
315 316
    console.log(res.toString()); // "3"
    // #END
M
mahaifeng 已提交
317 318
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
319
    res = uint16.slice(-2);
M
mahaifeng 已提交
320 321
    console.log(res.toString()); // "2,3"
    // #END
M
mahaifeng 已提交
322 323
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
324
    res = uint16.slice(0, 1);
M
mahaifeng 已提交
325
    console.log(res.toString()); // "1"
M
mahaifeng 已提交
326
    // #END
M
mahaifeng 已提交
327
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
328
  }
M
mahaifeng 已提交
329

M
mahaifeng 已提交
330
  sort() {
M
mahaifeng 已提交
331
    // #TEST Uint16Array.sort
M
mahaifeng 已提交
332 333
    let numbers = new Uint16Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
334 335
    console.log(numbers.toString()); // "1,5,40"
    // #END
M
mahaifeng 已提交
336 337
    expect(numbers.toString()).toEqual("1,5,40");

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

  subarray() {
M
mahaifeng 已提交
345
    // #TEST Uint16Array.subarray
M
mahaifeng 已提交
346
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
347 348
    let uint16 = new Uint16Array(buffer);
    uint16.set([1, 2, 3]);
M
mahaifeng 已提交
349 350
    console.log(uint16.toString()); // "1,2,3,0,0,0,0,0"
    // #END
M
mahaifeng 已提交
351
    expect(uint16.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
352

M
mahaifeng 已提交
353
    let sub = uint16.subarray(0, 4);
M
mahaifeng 已提交
354
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
355
    // #END
M
mahaifeng 已提交
356
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
357 358 359
  }

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

  arrayBufferSlice() {
M
mahaifeng 已提交
370
    // #TEST Uint16Array.arrayBufferSlice
M
mahaifeng 已提交
371
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
372 373
    let uint16 = new Uint16Array(buffer);
    uint16[4] = 42;
M
mahaifeng 已提交
374 375
    console.log(uint16.toString()); // "0,0,0,0,42,0,0,0"
    // #END
M
mahaifeng 已提交
376
    expect(uint16.toString()).toEqual("0,0,0,0,42,0,0,0");
M
mahaifeng 已提交
377 378 379

    let res = buffer.slice(8);
    let sliced = new Uint16Array(res);
M
mahaifeng 已提交
380
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
381
    // #END
M
mahaifeng 已提交
382
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
383
  }
M
mahaifeng 已提交
384

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

M
mahaifeng 已提交
390 391 392 393 394 395 396 397 398
    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 已提交
399

M
mahaifeng 已提交
400
  // #endif
M
mahaifeng 已提交
401
}