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  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

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
76 77
    let int16_t1 = new Int16Array([1, 2, 3]).fill(4);
    console.log(int16_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
78

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

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

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

88 89
    let int16_t5 = new Int16Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int16_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
90
    // #END
91 92 93 94 95 96

    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 已提交
97 98
  }

99

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

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

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

M
mahaifeng 已提交
123

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

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

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

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

M
mahaifeng 已提交
156

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

M
mahaifeng 已提交
160 161 162

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

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


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


M
mahaifeng 已提交
180 181 182 183 184 185 186 187 188 189
    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 已提交
190
    // #END
M
mahaifeng 已提交
191 192 193 194 195
    expect(res4).toEqual(0);
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
196 197 198
  }

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

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

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

  keys() {
M
mahaifeng 已提交
216
    // #TEST Int16Array.keys
M
mahaifeng 已提交
217 218
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
219 220 221 222 223 224
    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 已提交
225
    // #END
M
mahaifeng 已提交
226
    expect(value1).toEqual(0);
M
mahaifeng 已提交
227 228 229
  }

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

M
mahaifeng 已提交
240 241


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

M
mahaifeng 已提交
248 249

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

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

M
mahaifeng 已提交
263 264

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

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

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


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


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

M
mahaifeng 已提交
295 296 297

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

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

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

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

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

M
mahaifeng 已提交
315

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

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

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

M
mahaifeng 已提交
332

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

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

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

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

M
mahaifeng 已提交
364
  }
M
mahaifeng 已提交
365

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

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

M
mahaifeng 已提交
373
    console.log(positives.some((element : number, index : number, array : Int16Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
374
    // #END
M
mahaifeng 已提交
375 376 377
    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 已提交
378
  }
M
mahaifeng 已提交
379

M
mahaifeng 已提交
380
  // #endif
M
mahaifeng 已提交
381
}