TInt32Array.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 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();
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 97
  }

  testFill() {
M
mahaifeng 已提交
98
    // #TEST Int32Array.fill
M
mahaifeng 已提交
99
    let int32 = new Int32Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
100 101 102
    console.log(int32.toString()); // "4,4,4"

    // expect(int32.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
103

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

M
mahaifeng 已提交
109
    int32 = new Int32Array([1, 2, 3]).fill(4, 1, 2);
M
mahaifeng 已提交
110 111 112
    console.log(int32.toString()); // "1,4,3"

    // expect(int32.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
113

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

M
mahaifeng 已提交
119
    int32 = new Int32Array([1, 2, 3]).fill(4, -3, -2);
M
mahaifeng 已提交
120
    console.log(int32.toString()); // "4,2,3"
M
mahaifeng 已提交
121
    // #END
M
mahaifeng 已提交
122
    expect(int32.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
123 124 125
  }

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

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

  findIndex() {
M
mahaifeng 已提交
143
    // #TEST Int32Array.findIndex
M
mahaifeng 已提交
144
    let int32 = new Int32Array([4, 6, 8, 12]);
M
mahaifeng 已提交
145
    let res = int32.findIndex((value : number, _ : number, _a : Int32Array) : boolean => value > 100);
M
mahaifeng 已提交
146 147 148
    console.log(res); // -1

    // expect(res).toEqual(-1);
M
mahaifeng 已提交
149

M
mahaifeng 已提交
150
    int32 = new Int32Array([4, 6, 7, 120]);
M
mahaifeng 已提交
151
    res = int32.findIndex((value : number, _ : number, _a : Int32Array) : boolean => value > 100);
M
mahaifeng 已提交
152
    console.log(res); // 3
M
mahaifeng 已提交
153
    // #END
M
mahaifeng 已提交
154
    expect(res).toEqual(3);
M
mahaifeng 已提交
155 156 157
  }

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

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

  includes() {
M
mahaifeng 已提交
176
    // #TEST Int32Array.includes
M
mahaifeng 已提交
177 178
    let int32 = new Int32Array([1, 2, 3]);
    let res = int32.includes(2);
M
mahaifeng 已提交
179 180 181
    console.log(res); // true

    // expect(res).toEqual(true);
M
mahaifeng 已提交
182

M
mahaifeng 已提交
183
    res = int32.includes(4);
M
mahaifeng 已提交
184 185 186
    console.log(res); // false
 
    // expect(res).toEqual(false);
M
mahaifeng 已提交
187

M
mahaifeng 已提交
188
    res = int32.includes(3, 3);
M
mahaifeng 已提交
189
    console.log(res); // false
M
mahaifeng 已提交
190
    // #END
M
mahaifeng 已提交
191
    expect(res).toEqual(false);
M
mahaifeng 已提交
192 193 194
  }

  indexOf() {
M
mahaifeng 已提交
195
    // #TEST Int32Array.indexOf
M
mahaifeng 已提交
196 197
    let int32 = new Int32Array([2, 5, 9]);
    let res = int32.indexOf(2);
M
mahaifeng 已提交
198 199 200
    console.log(res); // 0
   
    // expect(res).toEqual(0);
M
mahaifeng 已提交
201

M
mahaifeng 已提交
202
    res = int32.indexOf(7);
M
mahaifeng 已提交
203 204 205
    console.log(res); // -1

    // expect(res).toEqual(-1);
M
mahaifeng 已提交
206

M
mahaifeng 已提交
207
    res = int32.indexOf(9, 2);
M
mahaifeng 已提交
208 209 210
    console.log(res); // 2

    // expect(res).toEqual(2);
M
mahaifeng 已提交
211

M
mahaifeng 已提交
212
    res = int32.indexOf(2, -1);
M
mahaifeng 已提交
213 214 215
    console.log(res); // -1

    // expect(res).toEqual(-1);
M
mahaifeng 已提交
216

M
mahaifeng 已提交
217
    res = int32.indexOf(2, -3);
M
mahaifeng 已提交
218
    console.log(res); // 0
M
mahaifeng 已提交
219
    // #END
M
mahaifeng 已提交
220
    expect(res).toEqual(0);
M
mahaifeng 已提交
221 222 223
  }

  join() {
M
mahaifeng 已提交
224
    // #TEST Int32Array.join
M
mahaifeng 已提交
225 226
    let int32 = new Int32Array([1, 2, 3]);
    let res = int32.join();
M
mahaifeng 已提交
227 228 229
    console.log(res); // "1,2,3"
  
    // expect(res).toEqual("1,2,3");
M
mahaifeng 已提交
230

M
mahaifeng 已提交
231
    res = int32.join(" / ");
M
mahaifeng 已提交
232 233 234
    console.log(res); // "1 / 2 / 3"
   
    // expect(res).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
235

M
mahaifeng 已提交
236
    res = int32.join("");
M
mahaifeng 已提交
237
    console.log(res); // "123"
M
mahaifeng 已提交
238
    // #END
M
mahaifeng 已提交
239
    expect(res).toEqual("123");
M
mahaifeng 已提交
240 241 242
  }

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

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

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

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

    total = new Int32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
286
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Int32Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
287
    console.log(res); // 14
M
mahaifeng 已提交
288
    // #END
M
mahaifeng 已提交
289
    expect(res).toEqual(14);
M
mahaifeng 已提交
290 291 292
  }

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

  slice() {
M
mahaifeng 已提交
302
    // #TEST Int32Array.slice
M
mahaifeng 已提交
303 304
    let int32 = new Int32Array([1, 2, 3]);
    let res = int32.slice(1);
M
mahaifeng 已提交
305 306 307
    console.log(res.toString()); // "2,3"
  
    // expect(res.toString()).toEqual("2,3");
M
mahaifeng 已提交
308

M
mahaifeng 已提交
309
    res = int32.slice(2);
M
mahaifeng 已提交
310 311 312
    console.log(res.toString()); // "3"
    
    // expect(res.toString()).toEqual("3");
M
mahaifeng 已提交
313

M
mahaifeng 已提交
314
    res = int32.slice(-2);
M
mahaifeng 已提交
315 316 317
    console.log(res.toString()); // "2,3"
  
    // expect(res.toString()).toEqual("2,3");
M
mahaifeng 已提交
318

M
mahaifeng 已提交
319
    res = int32.slice(0, 1);
M
mahaifeng 已提交
320
    console.log(res.toString()); // "1"
M
mahaifeng 已提交
321
    // #END
M
mahaifeng 已提交
322
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
323 324 325
  }

  sort() {
M
mahaifeng 已提交
326
    // #TEST Int32Array.sort
M
mahaifeng 已提交
327 328
    let numbers = new Int32Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
329 330 331
    console.log(numbers.toString()); // "1,5,40"
   
    // expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
332

M
mahaifeng 已提交
333
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
334
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
335
    // #END
M
mahaifeng 已提交
336
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
337 338 339
  }

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

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

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

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

    let res = buffer.slice(8);
    let sliced = new Int32Array(res);
M
mahaifeng 已提交
371
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
372
    // #END
M
mahaifeng 已提交
373
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
374
  }
M
mahaifeng 已提交
375

M
mahaifeng 已提交
376 377 378 379
  testSome() {
    // #TEST Int32Array.some
    const int32 = new Int32Array([-10, 20, -30, 40, -50]);
    const positives = new Int32Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
380

M
mahaifeng 已提交
381 382 383
    console.log(int32.some((element : number, index : number, array : Int32Array) : boolean => element < 0)); // true
   
    // expect(int32.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(true);
M
mahaifeng 已提交
384

M
mahaifeng 已提交
385
    console.log(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)); // false
M
mahaifeng 已提交
386
    // #END
M
mahaifeng 已提交
387
    expect(positives.some((element : number, index : number, array : Int32Array) : boolean => element < 0)).toEqual(false);
M
mahaifeng 已提交
388
  }
M
mahaifeng 已提交
389

390
  // #endif
M
mahaifeng 已提交
391
}