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 11
    // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

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 40
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

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

177 178
    let res2 = int32_t1.includes(4);
    console.log(res2); // false
M
mahaifeng 已提交
179

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

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

189

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

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

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

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

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

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

216

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

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

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

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

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

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

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

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

294

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

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

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

326

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

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

342

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

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

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

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

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

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

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

M
mahaifeng 已提交
387

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

M
mahaifeng 已提交
393
  }
M
mahaifeng 已提交
394

M
mahaifeng 已提交
395
  // #endif
M
mahaifeng 已提交
396
}