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

export class TInt32Array {
  test() {
M
mahaifeng 已提交
10
    // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
11

M
mahaifeng 已提交
12
    this.testInt32Array();
M
mahaifeng 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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 已提交
36
    // #endif
M
mahaifeng 已提交
37 38
  }

M
mahaifeng 已提交
39
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
 from() {
    // #TEST Int32Array.from
    var array = Int32Array.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 Int32Array.of
    var array = Int32Array.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
54 55 56 57 58
  testInt32Array() {
    let int32 = new Int32Array(2);
    int32[0] = 42;
    expect(int32[0]).toEqual(42);
    expect(int32.length).toEqual(2);
M
mahaifeng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    expect(Int32Array.BYTES_PER_ELEMENT).toEqual(4);

    let x = new Int32Array([21, 31, 3]);
    expect(x[1]).toEqual(31);

    let y = new Int32Array(x);
    expect(y[0]).toEqual(21);

    let buffer = new ArrayBuffer(16);
    let z = new Int32Array(buffer, 2, 4);
    expect(z.byteOffset).toEqual(2);
    expect(z.length).toEqual(4);
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
75 76 77
    let int32 = new Int32Array(buffer);
    int32[1] = 42;
    expect(int32.toString()).toEqual("0,42,0,0");
M
mahaifeng 已提交
78 79 80
  }

  testSet() {
M
mahaifeng 已提交
81
    // #TEST Int32Array.set
M
mahaifeng 已提交
82
    let int32 = new Int32Array(8);
M
mahaifeng 已提交
83
    var array = [1, 2, 3];
M
mahaifeng 已提交
84
    int32.set(array, 1);
M
mahaifeng 已提交
85
    console.log(int32.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
86
    // #END
M
mahaifeng 已提交
87
    expect(int32.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
88 89 90
  }

  testCopyWith() {
M
mahaifeng 已提交
91
    // #TEST Int32Array.copyWithin
M
mahaifeng 已提交
92
    console.log("testCopyWith start");
M
mahaifeng 已提交
93 94
    let int32 = new Int32Array(8);
    int32.set([1, 2, 3], 1);
M
mahaifeng 已提交
95
    console.log(int32.toString()); // "0,1,2,0,0,0,0,0"
M
mahaifeng 已提交
96
    int32.copyWithin(3, 0, 3);
M
mahaifeng 已提交
97
    console.log(int32.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
98
    // #END
M
mahaifeng 已提交
99
    expect(int32.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
100 101 102
  }

  testEvery() {
M
mahaifeng 已提交
103 104
    // #TEST Int32Array.every
    let result = new Int32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Int32Array) : boolean => value < 40);
M
mahaifeng 已提交
105
    console.log(result); // false
M
mahaifeng 已提交
106
    // #END
M
mahaifeng 已提交
107
    expect(result).toEqual(false);
M
mahaifeng 已提交
108 109
  }
  testFill() {
M
mahaifeng 已提交
110
    // #TEST Int32Array.fill
111 112
    let int32_t1 = new Int32Array([1, 2, 3]).fill(4);
    console.log(int32_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
113

114 115
    let int32_t2 = new Int32Array([1, 2, 3]).fill(4, 1);
    console.log(int32_t2.toString()); // "1,4,4"
M
mahaifeng 已提交
116

117 118
    let int32_t3 = new Int32Array([1, 2, 3]).fill(4, 1, 2);
    console.log(int32_t3.toString()); // "1,4,3"
M
mahaifeng 已提交
119

120 121
    let int32_t4 = new Int32Array([1, 2, 3]).fill(4, 1, 1);
    console.log(int32_t4.toString()); // "1,2,3"
M
mahaifeng 已提交
122

123 124
    let int32_t5 = new Int32Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int32_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
125
    // #END
126 127 128 129 130 131

    expect(int32_t1.toString()).toEqual("4,4,4");
    expect(int32_t2.toString()).toEqual("1,4,4");
    expect(int32_t3.toString()).toEqual("1,4,3");
    expect(int32_t4.toString()).toEqual("1,2,3");
    expect(int32_t5.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
132 133 134
  }

  testFilter() {
M
mahaifeng 已提交
135 136
    // #TEST Int32Array.filter
    let int32 = new Int32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Int32Array) : boolean => value >= 10);
M
mahaifeng 已提交
137
    console.log(int32.toString()); // "12,44"
M
mahaifeng 已提交
138
    // #END
M
mahaifeng 已提交
139
    expect(int32.toString()).toEqual("12,44");
M
mahaifeng 已提交
140 141 142
  }

  find() {
M
mahaifeng 已提交
143
    // #TEST Int32Array.find
M
mahaifeng 已提交
144
    let int32 = new Int32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
145
    let res = int32.find((value : number, _ : number, _a : Int32Array) : boolean => value > 5);
M
mahaifeng 已提交
146
    console.log(res); // 8
M
mahaifeng 已提交
147
    // #END
M
mahaifeng 已提交
148
    expect(res).toEqual(8);
M
mahaifeng 已提交
149 150
  }
  findIndex() {
M
mahaifeng 已提交
151
    // #TEST Int32Array.findIndex
152 153 154
    let int32_t1 = new Int32Array([4, 6, 8, 12]);
    let res1 = int32_t1.findIndex((value : number, _ : number, _a : Int32Array) : boolean => value > 100);
    console.log(res1); // -1
M
mahaifeng 已提交
155

156 157 158
    let int32_t2 = new Int32Array([4, 6, 7, 120]);
    let res2 = int32_t2.findIndex((value : number, _ : number, _a : Int32Array) : boolean => value > 100);
    console.log(res2); // 3
M
mahaifeng 已提交
159
    // #END
160 161 162

    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
163 164
  }

165

M
mahaifeng 已提交
166
  foreach() {
M
mahaifeng 已提交
167
    // #TEST Int32Array.forEach
M
mahaifeng 已提交
168
    new Int32Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Int32Array) => {
M
mahaifeng 已提交
169 170
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
171
    // #END
M
mahaifeng 已提交
172 173 174
  }

  iterator() {
M
mahaifeng 已提交
175
    // #TEST Int32Array.entries
M
mahaifeng 已提交
176 177
    let arr = new Int32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
178 179
    let entry = entries.next();
    console.log(entry.value[1]); // 10
M
mahaifeng 已提交
180
    // #END
M
mahaifeng 已提交
181
    expect(entry.value[1]).toEqual(10);
M
mahaifeng 已提交
182 183 184
  }

  includes() {
M
mahaifeng 已提交
185
    // #TEST Int32Array.includes
186 187 188
    let int32_t1 = new Int32Array([1, 2, 3]);
    let res1 = int32_t1.includes(2);
    console.log(res1); // true
M
mahaifeng 已提交
189

190 191
    let res2 = int32_t1.includes(4);
    console.log(res2); // false
M
mahaifeng 已提交
192

193 194
    let res3 = int32_t1.includes(3, 3);
    console.log(res3); // false
M
mahaifeng 已提交
195
    // #END
196 197 198 199

    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
200 201
  }

202

M
mahaifeng 已提交
203
  indexOf() {
M
mahaifeng 已提交
204
    // #TEST Int32Array.indexOf
205 206 207
    let int32_t1 = new Int32Array([2, 5, 9]);
    let res1 = int32_t1.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
208

209 210
    let res2 = int32_t1.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
211

212 213
    let res3 = int32_t1.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
214

215 216
    let res4 = int32_t1.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
217

218 219
    let res5 = int32_t1.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
220
    // #END
221 222 223 224 225 226

    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
227 228
  }

229

M
mahaifeng 已提交
230
  join() {
M
mahaifeng 已提交
231
    // #TEST Int32Array.join
232 233 234 235 236 237 238 239 240
    let int32_t1 = new Int32Array([1, 2, 3]);
    let res1 = int32_t1.join();
    console.log(res1); // "1,2,3"

    let res2 = int32_t1.join(" / ");
    console.log(res2); // "1 / 2 / 3"

    let res3 = int32_t1.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
241
    // #END
242 243 244 245

    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
246 247 248
  }

  keys() {
M
mahaifeng 已提交
249
    // #TEST Int32Array.keys
M
mahaifeng 已提交
250 251
    let arr = new Int32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
252 253
    let key = keys.next();
    console.log(key.value); // 0
M
mahaifeng 已提交
254
    // #END
M
mahaifeng 已提交
255
    expect(key.value).toEqual(0);
M
mahaifeng 已提交
256 257 258
  }

  map() {
M
mahaifeng 已提交
259
    // #TEST Int32Array.map
M
mahaifeng 已提交
260
    let numbers = new Int32Array([1, 4, 9]);
M
mahaifeng 已提交
261
    let doubles = numbers.map((value : number, _ : number, _a : Int32Array) : number => value * 2);
M
mahaifeng 已提交
262 263 264
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
265 266 267 268 269
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

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

    let total_t2 = new Int32Array([0, 1, 2, 3]);
    let res2 = total_t2.reduce((accumulator : number, currentValue : number, _ : number, _a : Int32Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
282
    // #END
283 284 285

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
286
  }
287

M
mahaifeng 已提交
288
  reduceRight() {
M
mahaifeng 已提交
289
    // #TEST Int32Array.reduceRight
290 291 292 293 294 295 296 297 298 299 300
    let total_t1 = new Int32Array([0, 1, 2, 3]);
    let res1 = total_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int32Array) : number =>
      accumulator + currentValue
    );
    console.log(res1); // 6

    let total_t2 = new Int32Array([0, 1, 2, 3]);
    let res2 = total_t2.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int32Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
M
mahaifeng 已提交
301
    // #END
302 303 304

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
305 306
  }

307

M
mahaifeng 已提交
308
  reverse() {
M
mahaifeng 已提交
309
    // #TEST Int32Array.reverse
M
mahaifeng 已提交
310 311
    let int32 = new Int32Array([1, 2, 3]);
    int32.reverse();
M
mahaifeng 已提交
312
    console.log(int32.toString()); // "3,2,1"
M
mahaifeng 已提交
313
    // #END
M
mahaifeng 已提交
314
    expect(int32.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
315 316 317
  }

  slice() {
M
mahaifeng 已提交
318
    // #TEST Int32Array.slice
319 320 321 322 323 324 325 326 327 328 329 330
    let int32_t1 = new Int32Array([1, 2, 3]);
    let res1 = int32_t1.slice(1);
    console.log(res1.toString()); // "2,3"

    let res2 = int32_t1.slice(2);
    console.log(res2.toString()); // "3"

    let res3 = int32_t1.slice(-2);
    console.log(res3.toString()); // "2,3"

    let res4 = int32_t1.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
331
    // #END
332 333 334 335 336

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

339

M
mahaifeng 已提交
340
  sort() {
M
mahaifeng 已提交
341
    // #TEST Int32Array.sort
342 343 344 345 346 347 348
    let numbers_t1 = new Int32Array([40, 1, 5]);
    numbers_t1.sort();
    console.log(numbers_t1.toString()); // "1,5,40"

    let numbers_t2 = new Int32Array([40, 1, 5]);
    numbers_t2.sort((a, b) : number => a - b);
    console.log(numbers_t2.toString()); // "1,5,40"
M
mahaifeng 已提交
349
    // #END
350 351 352

    expect(numbers_t1.toString()).toEqual("1,5,40");
    expect(numbers_t2.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
353 354
  }

355

M
mahaifeng 已提交
356
  subarray() {
M
mahaifeng 已提交
357
    // #TEST Int32Array.subarray
M
mahaifeng 已提交
358
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
359 360
    let int32 = new Int32Array(buffer);
    int32.set([1, 2, 3]);
M
mahaifeng 已提交
361
    console.log(int32.toString()); // "1,2,3,0"
M
mahaifeng 已提交
362

M
mahaifeng 已提交
363
    let sub = int32.subarray(0, 4);
M
mahaifeng 已提交
364
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
365
    // #END
M
mahaifeng 已提交
366
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
367 368 369
  }

  values() {
M
mahaifeng 已提交
370
    // #TEST Int32Array.values
M
mahaifeng 已提交
371 372
    let arr = new Int32Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
373 374
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
375
    // #END
M
mahaifeng 已提交
376
    expect(value).toEqual(1);
M
mahaifeng 已提交
377 378 379
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
380
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
381
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
382 383
    let int32 = new Int32Array(buffer);
    int32[3] = 42;
M
mahaifeng 已提交
384
    console.log(int32.toString()); // "0,0,0,42"
M
mahaifeng 已提交
385 386 387

    let res = buffer.slice(8);
    let sliced = new Int32Array(res);
M
mahaifeng 已提交
388
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
389
    // #END
M
mahaifeng 已提交
390
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
391
  }
M
mahaifeng 已提交
392

M
mahaifeng 已提交
393 394 395 396
  testSome() {
    // #TEST Int32Array.some
    const int32 = new Int32Array([-10, 20, -30, 40, -50]);
    const positives = new Int32Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
397

M
mahaifeng 已提交
398
    console.log(int32.some((element : number, index : number, array : Int32Array) : boolean => element < 0)); // true
399

M
mahaifeng 已提交
400

M
mahaifeng 已提交
401
    console.log(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
402
    // #END
M
mahaifeng 已提交
403
    expect(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(false);
404 405
    expect(int32.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(true);

M
mahaifeng 已提交
406
  }
M
mahaifeng 已提交
407

M
mahaifeng 已提交
408
  // #endif
M
mahaifeng 已提交
409
}