TInt16Array.uts 10.2 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  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
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
 from() {
    // #TEST Int16Array.from
    var array = Int16Array.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 Int16Array.of
    var array = Int16Array.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
52 53
  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
54 55 56
    let int16 = new Int16Array(buffer);
    int16[5] = 42;
    expect(int16.toString()).toEqual("0,0,0,0,0,42,0,0");
M
mahaifeng 已提交
57 58 59
  }

  testSet() {
M
mahaifeng 已提交
60
    // #TEST Int16Array.set
M
mahaifeng 已提交
61
    let int16 = new Int16Array(8);
M
mahaifeng 已提交
62
    var array = [1, 2, 3];
M
mahaifeng 已提交
63
    int16.set(array, 1);
M
mahaifeng 已提交
64
    console.log(int16.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
65
    // #END
M
mahaifeng 已提交
66
    expect(int16.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
67 68 69
  }

  testCopyWith() {
M
mahaifeng 已提交
70
    // #TEST Int16Array.copyWithin
M
mahaifeng 已提交
71 72 73
    let int16 = new Int16Array(8);
    int16.set([1, 2, 3], 1);
    int16.copyWithin(3, 0, 3);
M
mahaifeng 已提交
74
    console.log(int16.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
75
    // #END
M
mahaifeng 已提交
76
    expect(int16.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
77 78 79
  }

  testEvery() {
M
mahaifeng 已提交
80
    // #TEST Int16Array.every
M
mahaifeng 已提交
81
    let result = new Int16Array([12, 5, 8, 130, 44]).every((value, _, _a : Int16Array) : boolean => value < 40);
M
mahaifeng 已提交
82
    console.log(result); // false
M
mahaifeng 已提交
83
    // #END
M
mahaifeng 已提交
84
    expect(result).toEqual(false);
M
mahaifeng 已提交
85 86 87
  }

  testFill() {
M
mahaifeng 已提交
88
    // #TEST Int16Array.fill
89 90
    let int16_t1 = new Int16Array([1, 2, 3]).fill(4);
    console.log(int16_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
91

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

95 96
    let int16_t3 = new Int16Array([1, 2, 3]).fill(4, 1, 2);
    console.log(int16_t3.toString()); // "1,4,3"
M
mahaifeng 已提交
97

98 99
    let int16_t4 = new Int16Array([1, 2, 3]).fill(4, 1, 1);
    console.log(int16_t4.toString()); // "1,2,3"
M
mahaifeng 已提交
100

101 102
    let int16_t5 = new Int16Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int16_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
103
    // #END
104 105 106 107 108 109

    expect(int16_t1.toString()).toEqual("4,4,4");
    expect(int16_t2.toString()).toEqual("1,4,4");
    expect(int16_t3.toString()).toEqual("1,4,3");
    expect(int16_t4.toString()).toEqual("1,2,3");
    expect(int16_t5.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
110 111
  }

112

M
mahaifeng 已提交
113
  testFilter() {
M
mahaifeng 已提交
114
    // #TEST Int16Array.filter
M
mahaifeng 已提交
115
    let int16 = new Int16Array([12, 5, 8, 44]).filter((value, _, _a : Int16Array) : boolean => value >= 10);
M
mahaifeng 已提交
116
    console.log(int16.toString()); // "12,44"
M
mahaifeng 已提交
117
    // #END
M
mahaifeng 已提交
118
    expect(int16.toString()).toEqual("12,44");
M
mahaifeng 已提交
119 120 121
  }

  find() {
M
mahaifeng 已提交
122
    // #TEST Int16Array.find
M
mahaifeng 已提交
123
    let int16 = new Int16Array([4, 5, 8, 12]);
M
mahaifeng 已提交
124
    let res = int16.find((value, _, _a : Int16Array) : boolean => value > 5);
M
mahaifeng 已提交
125
    console.log(res); // 8
M
mahaifeng 已提交
126
    // #END
M
mahaifeng 已提交
127
    expect(res).toEqual(8);
M
mahaifeng 已提交
128 129 130
  }

  findIndex() {
M
mahaifeng 已提交
131
    // #TEST Int16Array.findIndex
M
mahaifeng 已提交
132
    let int16 = new Int16Array([4, 6, 8, 12]);
M
mahaifeng 已提交
133
    let res = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
M
mahaifeng 已提交
134 135
    console.log(res); // -1

M
mahaifeng 已提交
136

M
mahaifeng 已提交
137
    int16 = new Int16Array([4, 6, 7, 120]);
M
mahaifeng 已提交
138 139
    let res1 = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
    console.log(res1); // 3
M
mahaifeng 已提交
140
    // #END
M
mahaifeng 已提交
141 142
    expect(res1).toEqual(3);
    expect(res).toEqual(-1);
M
mahaifeng 已提交
143 144 145
  }

  foreach() {
M
mahaifeng 已提交
146
    // #TEST Int16Array.forEach
M
mahaifeng 已提交
147
    new Int16Array([0, 1, 2, 3]).forEach((value, index, _ : Int16Array) => {
M
mahaifeng 已提交
148 149
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
150
    // #END
M
mahaifeng 已提交
151 152 153
  }

  iterator() {
M
mahaifeng 已提交
154
    // #TEST Int16Array.entries
M
mahaifeng 已提交
155 156
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
157 158
    let firstEntry = entries.next().value[1];
    console.log(firstEntry); // 10
M
mahaifeng 已提交
159
    // #END
M
mahaifeng 已提交
160
    expect(firstEntry).toEqual(10);
M
mahaifeng 已提交
161 162 163
  }

  includes() {
M
mahaifeng 已提交
164
    // #TEST Int16Array.includes
M
mahaifeng 已提交
165 166
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.includes(2);
M
mahaifeng 已提交
167 168
    console.log(res); // true

M
mahaifeng 已提交
169

M
mahaifeng 已提交
170 171
    let res1 = int16.includes(4);
    console.log(res1); // false
M
mahaifeng 已提交
172

M
mahaifeng 已提交
173 174 175

    let res2 = int16.includes(3, 3);
    console.log(res2); // false
M
mahaifeng 已提交
176
    // #END
M
mahaifeng 已提交
177 178 179
    expect(res2).toEqual(false);
    expect(res).toEqual(true);
    expect(res1).toEqual(false);
M
mahaifeng 已提交
180 181 182
  }

  indexOf() {
M
mahaifeng 已提交
183
    // #TEST Int16Array.indexOf
M
mahaifeng 已提交
184 185
    let int16 = new Int16Array([2, 5, 9]);
    let res = int16.indexOf(2);
M
mahaifeng 已提交
186
    console.log(res); // 0
M
mahaifeng 已提交
187 188


M
mahaifeng 已提交
189 190
    let res1 = int16.indexOf(7);
    console.log(res1); // -1
M
mahaifeng 已提交
191 192


M
mahaifeng 已提交
193 194 195 196 197 198 199 200 201 202
    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 已提交
203
    // #END
M
mahaifeng 已提交
204 205 206 207 208
    expect(res4).toEqual(0);
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
209 210 211
  }

  join() {
M
mahaifeng 已提交
212
    // #TEST Int16Array.join
M
mahaifeng 已提交
213 214
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.join();
M
mahaifeng 已提交
215
    console.log(res); // "1,2,3"
M
mahaifeng 已提交
216

M
mahaifeng 已提交
217 218
    let res1 = int16.join(" / ");
    console.log(res1); // "1 / 2 / 3"
M
mahaifeng 已提交
219

M
mahaifeng 已提交
220 221
    let res2 = int16.join("");
    console.log(res2); // "123"
M
mahaifeng 已提交
222
    // #END
M
mahaifeng 已提交
223 224 225
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
226 227 228
  }

  keys() {
M
mahaifeng 已提交
229
    // #TEST Int16Array.keys
M
mahaifeng 已提交
230 231
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
232 233 234 235 236 237
    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 已提交
238
    // #END
M
mahaifeng 已提交
239
    expect(value1).toEqual(0);
M
mahaifeng 已提交
240 241 242
  }

  map() {
M
mahaifeng 已提交
243
    // #TEST Int16Array.map
M
mahaifeng 已提交
244
    let numbers = new Int16Array([1, 4, 9]);
M
mahaifeng 已提交
245
    let doubles = numbers.map((value, _, _a : Int16Array) : number => value * 2);
M
mahaifeng 已提交
246 247 248
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
249 250 251 252
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

M
mahaifeng 已提交
253 254


M
mahaifeng 已提交
255
  reduce() {
M
mahaifeng 已提交
256
    // #TEST Int16Array.reduce
M
mahaifeng 已提交
257
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
258
    let res = total.reduce((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
259 260
    console.log(res); // 6

M
mahaifeng 已提交
261 262

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

  reduceRight() {
M
mahaifeng 已提交
271
    // #TEST Int16Array.reduceRight
M
mahaifeng 已提交
272
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
273
    let res = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
274 275
    console.log(res); // 6

M
mahaifeng 已提交
276 277

    total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
278 279
    let res1 = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
280
    // #END
M
mahaifeng 已提交
281 282
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
283 284 285
  }

  reverse() {
M
mahaifeng 已提交
286
    // #TEST Int16Array.reverse
M
mahaifeng 已提交
287 288
    let int16 = new Int16Array([1, 2, 3]);
    int16.reverse();
M
mahaifeng 已提交
289
    console.log(int16.toString()); // "3,2,1"
M
mahaifeng 已提交
290
    // #END
M
mahaifeng 已提交
291
    expect(int16.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
292 293 294
  }

  slice() {
M
mahaifeng 已提交
295
    // #TEST Int16Array.slice
M
mahaifeng 已提交
296 297
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.slice(1);
M
mahaifeng 已提交
298
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
299 300


M
mahaifeng 已提交
301 302 303 304 305 306
    let res1 = int16.slice(2);
    console.log(res1.toString()); // "3"


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

M
mahaifeng 已提交
308 309 310

    let res3 = int16.slice(0, 1);
    console.log(res3.toString()); // "1"
M
mahaifeng 已提交
311
    // #END
M
mahaifeng 已提交
312 313 314 315 316 317 318
    expect(res3.toString()).toEqual("1");

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

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

    expect(res2.toString()).toEqual("2,3");
M
mahaifeng 已提交
319 320 321
  }

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

M
mahaifeng 已提交
328

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

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

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

M
mahaifeng 已提交
345

M
mahaifeng 已提交
346
    let sub = int16.subarray(0, 4);
M
mahaifeng 已提交
347
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
348
    // #END
M
mahaifeng 已提交
349 350
    expect(sub.toString()).toEqual("1,2,3,0");
    expect(res).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
351 352 353
  }

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

  arrayBufferSlice() {
M
mahaifeng 已提交
364
    // #TEST ArrayBuffer.slice with Int16Array
M
mahaifeng 已提交
365
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
366 367
    let int16 = new Int16Array(buffer);
    int16[4] = 42;
M
mahaifeng 已提交
368
    console.log(int16.toString()); // "0,0,0,0,42,0,0,0"
M
mahaifeng 已提交
369

M
mahaifeng 已提交
370
    let res = buffer.slice(8, 12);
M
mahaifeng 已提交
371
    let sliced = new Int16Array(res);
M
mahaifeng 已提交
372
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
373
    // #END
M
mahaifeng 已提交
374 375
    expect(sliced[0]).toEqual(42);
    expect(int16.toString()).toEqual("0,0,0,0,42,0,0,0");
M
mahaifeng 已提交
376

M
mahaifeng 已提交
377
  }
M
mahaifeng 已提交
378

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

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

M
mahaifeng 已提交
386
    console.log(positives.some((element : number, index : number, array : Int16Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
387
    // #END
M
mahaifeng 已提交
388 389 390
    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 已提交
391
  }
M
mahaifeng 已提交
392

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