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

M
mahaifeng 已提交
41 42 43 44 45
  testInt32Array() {
    let int32 = new Int32Array(2);
    int32[0] = 42;
    expect(int32[0]).toEqual(42);
    expect(int32.length).toEqual(2);
M
mahaifeng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    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 已提交
62 63 64
    let int32 = new Int32Array(buffer);
    int32[1] = 42;
    expect(int32.toString()).toEqual("0,42,0,0");
M
mahaifeng 已提交
65 66 67
  }

  testSet() {
M
mahaifeng 已提交
68
    // #TEST Int32Array.set
M
mahaifeng 已提交
69
    let int32 = new Int32Array(8);
M
mahaifeng 已提交
70
    var array = [1, 2, 3];
M
mahaifeng 已提交
71
    int32.set(array, 1);
M
mahaifeng 已提交
72
    console.log(int32.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
73
    // #END
M
mahaifeng 已提交
74
    expect(int32.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
75 76 77
  }

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

  testEvery() {
M
mahaifeng 已提交
90 91
    // #TEST Int32Array.every
    let result = new Int32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Int32Array) : 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
  }
  testFill() {
M
mahaifeng 已提交
97
    // #TEST Int32Array.fill
98 99
    let int32_t1 = new Int32Array([1, 2, 3]).fill(4);
    console.log(int32_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
100

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

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

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

110 111
    let int32_t5 = new Int32Array([1, 2, 3]).fill(4, -3, -2);
    console.log(int32_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
112
    // #END
113 114 115 116 117 118

    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 已提交
119 120 121
  }

  testFilter() {
M
mahaifeng 已提交
122 123
    // #TEST Int32Array.filter
    let int32 = new Int32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Int32Array) : boolean => value >= 10);
M
mahaifeng 已提交
124
    console.log(int32.toString()); // "12,44"
M
mahaifeng 已提交
125
    // #END
M
mahaifeng 已提交
126
    expect(int32.toString()).toEqual("12,44");
M
mahaifeng 已提交
127 128 129
  }

  find() {
M
mahaifeng 已提交
130
    // #TEST Int32Array.find
M
mahaifeng 已提交
131
    let int32 = new Int32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
132
    let res = int32.find((value : number, _ : number, _a : Int32Array) : boolean => value > 5);
M
mahaifeng 已提交
133
    console.log(res); // 8
M
mahaifeng 已提交
134
    // #END
M
mahaifeng 已提交
135
    expect(res).toEqual(8);
M
mahaifeng 已提交
136 137
  }
  findIndex() {
M
mahaifeng 已提交
138
    // #TEST Int32Array.findIndex
139 140 141
    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 已提交
142

143 144 145
    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 已提交
146
    // #END
147 148 149

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

152

M
mahaifeng 已提交
153
  foreach() {
M
mahaifeng 已提交
154
    // #TEST Int32Array.forEach
M
mahaifeng 已提交
155
    new Int32Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Int32Array) => {
M
mahaifeng 已提交
156 157
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
158
    // #END
M
mahaifeng 已提交
159 160 161
  }

  iterator() {
M
mahaifeng 已提交
162
    // #TEST Int32Array.entries
M
mahaifeng 已提交
163 164
    let arr = new Int32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
165 166
    let entry = entries.next();
    console.log(entry.value[1]); // 10
M
mahaifeng 已提交
167
    // #END
M
mahaifeng 已提交
168
    expect(entry.value[1]).toEqual(10);
M
mahaifeng 已提交
169 170 171
  }

  includes() {
M
mahaifeng 已提交
172
    // #TEST Int32Array.includes
173 174 175
    let int32_t1 = new Int32Array([1, 2, 3]);
    let res1 = int32_t1.includes(2);
    console.log(res1); // true
M
mahaifeng 已提交
176

M
mahaifeng 已提交
177 178 179 180
    res = int32.includes(4);
    console.log(res); // false
 
    // expect(res).toEqual(false);
M
mahaifeng 已提交
181

182 183
    let res3 = int32_t1.includes(3, 3);
    console.log(res3); // false
M
mahaifeng 已提交
184
    // #END
185 186 187 188

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

191

M
mahaifeng 已提交
192
  indexOf() {
M
mahaifeng 已提交
193
    // #TEST Int32Array.indexOf
194 195 196
    let int32_t1 = new Int32Array([2, 5, 9]);
    let res1 = int32_t1.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
197

198 199
    let res2 = int32_t1.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
200

201 202
    let res3 = int32_t1.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
203

204 205
    let res4 = int32_t1.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
206

207 208
    let res5 = int32_t1.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
209
    // #END
210 211 212 213 214 215

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

218

M
mahaifeng 已提交
219
  join() {
M
mahaifeng 已提交
220
    // #TEST Int32Array.join
221 222 223 224 225 226 227 228 229
    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 已提交
230
    // #END
231 232 233 234

    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
235 236 237
  }

  keys() {
M
mahaifeng 已提交
238
    // #TEST Int32Array.keys
M
mahaifeng 已提交
239 240
    let arr = new Int32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
241 242
    let key = keys.next();
    console.log(key.value); // 0
M
mahaifeng 已提交
243
    // #END
M
mahaifeng 已提交
244
    expect(key.value).toEqual(0);
M
mahaifeng 已提交
245 246 247
  }

  map() {
M
mahaifeng 已提交
248
    // #TEST Int32Array.map
M
mahaifeng 已提交
249
    let numbers = new Int32Array([1, 4, 9]);
M
mahaifeng 已提交
250
    let doubles = numbers.map((value : number, _ : number, _a : Int32Array) : number => value * 2);
M
mahaifeng 已提交
251 252 253
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
254 255 256 257 258
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
M
mahaifeng 已提交
259
    // #TEST Int32Array.reduce
260 261 262 263 264 265 266 267 268 269 270
    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 已提交
271
    // #END
272 273 274

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

M
mahaifeng 已提交
277
  reduceRight() {
M
mahaifeng 已提交
278
    // #TEST Int32Array.reduceRight
279 280 281 282 283 284 285 286 287 288 289
    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 已提交
290
    // #END
291 292 293

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

296

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

  slice() {
M
mahaifeng 已提交
307
    // #TEST Int32Array.slice
308 309 310 311 312 313 314 315 316 317 318 319
    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 已提交
320
    // #END
321 322 323 324 325

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

328

M
mahaifeng 已提交
329
  sort() {
M
mahaifeng 已提交
330
    // #TEST Int32Array.sort
331 332 333 334 335 336 337
    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 已提交
338
    // #END
339 340 341

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

344

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

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

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

  arrayBufferSlice() {
M
mahaifeng 已提交
369
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
370
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
371 372
    let int32 = new Int32Array(buffer);
    int32[3] = 42;
M
mahaifeng 已提交
373
    console.log(int32.toString()); // "0,0,0,42"
M
mahaifeng 已提交
374 375 376

    let res = buffer.slice(8);
    let sliced = new Int32Array(res);
M
mahaifeng 已提交
377
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
378
    // #END
M
mahaifeng 已提交
379
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
380
  }
M
mahaifeng 已提交
381

M
mahaifeng 已提交
382 383 384 385
  testSome() {
    // #TEST Int32Array.some
    const int32 = new Int32Array([-10, 20, -30, 40, -50]);
    const positives = new Int32Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
386

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

M
mahaifeng 已提交
389

M
mahaifeng 已提交
390
    console.log(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
391
    // #END
M
mahaifeng 已提交
392
    expect(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(false);
393 394
    expect(int32.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(true);

M
mahaifeng 已提交
395
  }
M
mahaifeng 已提交
396

M
mahaifeng 已提交
397
  // #endif
M
mahaifeng 已提交
398
}