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

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

M
mahaifeng 已提交
37 38
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

M
mahaifeng 已提交
39 40
  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
41 42 43
    let int16 = new Int16Array(buffer);
    int16[5] = 42;
    expect(int16.toString()).toEqual("0,0,0,0,0,42,0,0");
M
mahaifeng 已提交
44 45 46
  }

  testSet() {
M
mahaifeng 已提交
47
    // #TEST Int16Array.set
M
mahaifeng 已提交
48
    let int16 = new Int16Array(8);
M
mahaifeng 已提交
49
    var array = [1, 2, 3];
M
mahaifeng 已提交
50
    int16.set(array, 1);
M
mahaifeng 已提交
51
    console.log(int16.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
52
    // #END
M
mahaifeng 已提交
53
    expect(int16.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
54 55 56
  }

  testCopyWith() {
M
mahaifeng 已提交
57
    // #TEST Int16Array.copyWithin
M
mahaifeng 已提交
58 59 60
    let int16 = new Int16Array(8);
    int16.set([1, 2, 3], 1);
    int16.copyWithin(3, 0, 3);
M
mahaifeng 已提交
61
    console.log(int16.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
62
    // #END
M
mahaifeng 已提交
63
    expect(int16.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
64 65 66
  }

  testEvery() {
M
mahaifeng 已提交
67
    // #TEST Int16Array.every
M
mahaifeng 已提交
68
    let result = new Int16Array([12, 5, 8, 130, 44]).every((value, _, _a : Int16Array) : boolean => value < 40);
M
mahaifeng 已提交
69
    console.log(result); // false
M
mahaifeng 已提交
70
    // #END
M
mahaifeng 已提交
71
    expect(result).toEqual(false);
M
mahaifeng 已提交
72 73 74
  }

  testFill() {
M
mahaifeng 已提交
75
    // #TEST Int16Array.fill
M
mahaifeng 已提交
76
    let int16 = new Int16Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
77 78 79
    console.log(int16.toString()); // "4,4,4"

    // expect(int16.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
80

M
mahaifeng 已提交
81
    int16 = new Int16Array([1, 2, 3]).fill(4, 1);
M
mahaifeng 已提交
82 83 84
    console.log(int16.toString()); // "1,4,4"

    // expect(int16.toString()).toEqual("1,4,4");
M
mahaifeng 已提交
85

M
mahaifeng 已提交
86
    int16 = new Int16Array([1, 2, 3]).fill(4, 1, 2);
M
mahaifeng 已提交
87 88 89
    console.log(int16.toString()); // "1,4,3"

    // expect(int16.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
90

M
mahaifeng 已提交
91
    int16 = new Int16Array([1, 2, 3]).fill(4, 1, 1);
M
mahaifeng 已提交
92 93 94
    console.log(int16.toString()); // "1,2,3"

    // expect(int16.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
95

M
mahaifeng 已提交
96
    int16 = new Int16Array([1, 2, 3]).fill(4, -3, -2);
M
mahaifeng 已提交
97
    console.log(int16.toString()); // "4,2,3"
M
mahaifeng 已提交
98
    // #END
M
mahaifeng 已提交
99
    expect(int16.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
100 101 102
  }

  testFilter() {
M
mahaifeng 已提交
103
    // #TEST Int16Array.filter
M
mahaifeng 已提交
104
    let int16 = new Int16Array([12, 5, 8, 44]).filter((value, _, _a : Int16Array) : boolean => value >= 10);
M
mahaifeng 已提交
105
    console.log(int16.toString()); // "12,44"
M
mahaifeng 已提交
106
    // #END
M
mahaifeng 已提交
107
    expect(int16.toString()).toEqual("12,44");
M
mahaifeng 已提交
108 109 110
  }

  find() {
M
mahaifeng 已提交
111
    // #TEST Int16Array.find
M
mahaifeng 已提交
112
    let int16 = new Int16Array([4, 5, 8, 12]);
M
mahaifeng 已提交
113
    let res = int16.find((value, _, _a : Int16Array) : boolean => value > 5);
M
mahaifeng 已提交
114
    console.log(res); // 8
M
mahaifeng 已提交
115
    // #END
M
mahaifeng 已提交
116
    expect(res).toEqual(8);
M
mahaifeng 已提交
117 118 119
  }

  findIndex() {
M
mahaifeng 已提交
120
    // #TEST Int16Array.findIndex
M
mahaifeng 已提交
121
    let int16 = new Int16Array([4, 6, 8, 12]);
M
mahaifeng 已提交
122
    let res = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
M
mahaifeng 已提交
123 124
    console.log(res); // -1

M
mahaifeng 已提交
125

M
mahaifeng 已提交
126
    int16 = new Int16Array([4, 6, 7, 120]);
M
mahaifeng 已提交
127 128
    let res1 = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
    console.log(res1); // 3
M
mahaifeng 已提交
129
    // #END
M
mahaifeng 已提交
130 131
    expect(res1).toEqual(3);
    expect(res).toEqual(-1);
M
mahaifeng 已提交
132 133 134
  }

  foreach() {
M
mahaifeng 已提交
135
    // #TEST Int16Array.forEach
M
mahaifeng 已提交
136
    new Int16Array([0, 1, 2, 3]).forEach((value, index, _ : Int16Array) => {
M
mahaifeng 已提交
137 138
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
139
    // #END
M
mahaifeng 已提交
140 141 142
  }

  iterator() {
M
mahaifeng 已提交
143
    // #TEST Int16Array.entries
M
mahaifeng 已提交
144 145
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
146 147
    let firstEntry = entries.next().value[1];
    console.log(firstEntry); // 10
M
mahaifeng 已提交
148
    // #END
M
mahaifeng 已提交
149
    expect(firstEntry).toEqual(10);
M
mahaifeng 已提交
150 151 152
  }

  includes() {
M
mahaifeng 已提交
153
    // #TEST Int16Array.includes
M
mahaifeng 已提交
154 155
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.includes(2);
M
mahaifeng 已提交
156 157
    console.log(res); // true

M
mahaifeng 已提交
158

M
mahaifeng 已提交
159 160
    let res1 = int16.includes(4);
    console.log(res1); // false
M
mahaifeng 已提交
161

M
mahaifeng 已提交
162 163 164

    let res2 = int16.includes(3, 3);
    console.log(res2); // false
M
mahaifeng 已提交
165
    // #END
M
mahaifeng 已提交
166 167 168
    expect(res2).toEqual(false);
    expect(res).toEqual(true);
    expect(res1).toEqual(false);
M
mahaifeng 已提交
169 170 171
  }

  indexOf() {
M
mahaifeng 已提交
172
    // #TEST Int16Array.indexOf
M
mahaifeng 已提交
173 174
    let int16 = new Int16Array([2, 5, 9]);
    let res = int16.indexOf(2);
M
mahaifeng 已提交
175
    console.log(res); // 0
M
mahaifeng 已提交
176 177


M
mahaifeng 已提交
178 179
    let res1 = int16.indexOf(7);
    console.log(res1); // -1
M
mahaifeng 已提交
180 181


M
mahaifeng 已提交
182 183 184 185 186 187 188 189 190 191
    let res2 = int16.indexOf(9, 2);
    console.log(res2); // 2


    let res3 = int16.indexOf(2, -1);
    console.log(res3); // -1


    let res4 = int16.indexOf(2, -3);
    console.log(res4); // 0
M
mahaifeng 已提交
192
    // #END
M
mahaifeng 已提交
193 194 195 196 197
    expect(res4).toEqual(0);
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
198 199 200
  }

  join() {
M
mahaifeng 已提交
201
    // #TEST Int16Array.join
M
mahaifeng 已提交
202 203
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.join();
M
mahaifeng 已提交
204
    console.log(res); // "1,2,3"
M
mahaifeng 已提交
205

M
mahaifeng 已提交
206 207
    let res1 = int16.join(" / ");
    console.log(res1); // "1 / 2 / 3"
M
mahaifeng 已提交
208

M
mahaifeng 已提交
209 210
    let res2 = int16.join("");
    console.log(res2); // "123"
M
mahaifeng 已提交
211
    // #END
M
mahaifeng 已提交
212 213 214
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
215 216 217
  }

  keys() {
M
mahaifeng 已提交
218
    // #TEST Int16Array.keys
M
mahaifeng 已提交
219 220
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
221 222 223 224 225 226
    let value1 = keys.next().value
    console.log(value1); // 0
    console.log(keys.next().value); // 1
    console.log(keys.next().value); // 2
    console.log(keys.next().value); // 3
    console.log(keys.next().value); // 4
M
mahaifeng 已提交
227
    // #END
M
mahaifeng 已提交
228
    expect(value1).toEqual(0);
M
mahaifeng 已提交
229 230 231
  }

  map() {
M
mahaifeng 已提交
232
    // #TEST Int16Array.map
M
mahaifeng 已提交
233
    let numbers = new Int16Array([1, 4, 9]);
M
mahaifeng 已提交
234
    let doubles = numbers.map((value, _, _a : Int16Array) : number => value * 2);
M
mahaifeng 已提交
235 236 237
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
238 239 240 241
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

M
mahaifeng 已提交
242 243


M
mahaifeng 已提交
244
  reduce() {
M
mahaifeng 已提交
245
    // #TEST Int16Array.reduce
M
mahaifeng 已提交
246
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
247
    let res = total.reduce((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
248 249
    console.log(res); // 6

M
mahaifeng 已提交
250 251

    total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
252 253
    let res1 = total.reduce((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
254
    // #END
M
mahaifeng 已提交
255 256
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
257 258 259
  }

  reduceRight() {
M
mahaifeng 已提交
260
    // #TEST Int16Array.reduceRight
M
mahaifeng 已提交
261
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
262
    let res = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
263 264
    console.log(res); // 6

M
mahaifeng 已提交
265 266

    total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
267 268
    let res1 = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
269
    // #END
M
mahaifeng 已提交
270 271
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
272 273 274
  }

  reverse() {
M
mahaifeng 已提交
275
    // #TEST Int16Array.reverse
M
mahaifeng 已提交
276 277
    let int16 = new Int16Array([1, 2, 3]);
    int16.reverse();
M
mahaifeng 已提交
278
    console.log(int16.toString()); // "3,2,1"
M
mahaifeng 已提交
279
    // #END
M
mahaifeng 已提交
280
    expect(int16.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
281 282 283
  }

  slice() {
M
mahaifeng 已提交
284
    // #TEST Int16Array.slice
M
mahaifeng 已提交
285 286
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.slice(1);
M
mahaifeng 已提交
287
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
288 289


M
mahaifeng 已提交
290 291 292 293 294 295
    let res1 = int16.slice(2);
    console.log(res1.toString()); // "3"


    let res2 = int16.slice(-2);
    console.log(res2.toString()); // "2,3"
M
mahaifeng 已提交
296

M
mahaifeng 已提交
297 298 299

    let res3 = int16.slice(0, 1);
    console.log(res3.toString()); // "1"
M
mahaifeng 已提交
300
    // #END
M
mahaifeng 已提交
301 302 303 304 305 306 307
    expect(res3.toString()).toEqual("1");

    expect(res.toString()).toEqual("2,3");

    expect(res1.toString()).toEqual("3");

    expect(res2.toString()).toEqual("2,3");
M
mahaifeng 已提交
308 309 310
  }

  sort() {
M
mahaifeng 已提交
311
    // #TEST Int16Array.sort
M
mahaifeng 已提交
312 313
    let numbers = new Int16Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
314 315 316
    console.log(numbers.toString()); // "1,5,40"
    let res = numbers.toString()

M
mahaifeng 已提交
317

M
mahaifeng 已提交
318
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
319
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
320
    // #END
M
mahaifeng 已提交
321 322 323
    expect(numbers.toString()).toEqual("1,5,40");

    expect(res).toEqual("1,5,40");
M
mahaifeng 已提交
324 325 326
  }

  subarray() {
M
mahaifeng 已提交
327
    // #TEST Int16Array.subarray
M
mahaifeng 已提交
328
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
329 330
    let int16 = new Int16Array(buffer);
    int16.set([1, 2, 3]);
M
mahaifeng 已提交
331 332 333
    let res = int16.toString()
    console.log(res); // "1,2,3,0,0,0,0,0"

M
mahaifeng 已提交
334

M
mahaifeng 已提交
335
    let sub = int16.subarray(0, 4);
M
mahaifeng 已提交
336
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
337
    // #END
M
mahaifeng 已提交
338 339
    expect(sub.toString()).toEqual("1,2,3,0");
    expect(res).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
340 341 342
  }

  values() {
M
mahaifeng 已提交
343
    // #TEST Int16Array.values
M
mahaifeng 已提交
344 345
    let arr = new Int16Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
346 347
    let res = values.next().value
    console.log(res); // 1
M
mahaifeng 已提交
348
    // #END
M
mahaifeng 已提交
349
    expect(res).toEqual(1);
M
mahaifeng 已提交
350 351 352
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
353
    // #TEST ArrayBuffer.slice with Int16Array
M
mahaifeng 已提交
354
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
355 356
    let int16 = new Int16Array(buffer);
    int16[4] = 42;
M
mahaifeng 已提交
357
    console.log(int16.toString()); // "0,0,0,0,42,0,0,0"
M
mahaifeng 已提交
358

M
mahaifeng 已提交
359
    let res = buffer.slice(8, 12);
M
mahaifeng 已提交
360
    let sliced = new Int16Array(res);
M
mahaifeng 已提交
361
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
362
    // #END
M
mahaifeng 已提交
363 364
    expect(sliced[0]).toEqual(42);
    expect(int16.toString()).toEqual("0,0,0,0,42,0,0,0");
M
mahaifeng 已提交
365

M
mahaifeng 已提交
366
  }
M
mahaifeng 已提交
367

M
mahaifeng 已提交
368 369 370 371
  testSome() {
    // #TEST Int16Array.some
    const int16 = new Int16Array([-10, 20, -30, 40, -50]);
    const positives = new Int16Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
372

M
mahaifeng 已提交
373
    console.log(int16.some((element : number, index : number, array : Int16Array) : boolean => element < 0)); // true
M
mahaifeng 已提交
374

M
mahaifeng 已提交
375
    console.log(positives.some((element : number, index : number, array : Int16Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
376
    // #END
M
mahaifeng 已提交
377 378 379
    expect(positives.some((element : number, index : number, array : Int16Array) : boolean => element < 0)).toEqual(false);
    expect(int16.some((element : number, index : number, array : Int16Array) : boolean => element < 0)).toEqual(true);

M
mahaifeng 已提交
380
  }
M
mahaifeng 已提交
381

M
mahaifeng 已提交
382
  // #endif
M
mahaifeng 已提交
383
}