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

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

M
mahaifeng 已提交
12
    this.testuint32();
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
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB
M
mahaifeng 已提交
40 41 42 43 44
  testuint32() {
    let uint32 = new Uint32Array(2);
    uint32[0] = 42;
    expect(uint32[0]).toEqual(42);
    expect(uint32.length).toEqual(2);
M
mahaifeng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    expect(Uint32Array.BYTES_PER_ELEMENT).toEqual(4);

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

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

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

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
61 62 63
    let uint32 = new Uint32Array(buffer);
    uint32[1] = 42;
    expect(uint32.toString()).toEqual("0,42,0,0");
M
mahaifeng 已提交
64 65 66
  }

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

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

  testEvery() {
M
mahaifeng 已提交
90 91
    // #TEST Uint32Array.every
    let result = new Uint32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint32Array) : 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 Uint32Array.fill
M
mahaifeng 已提交
99
    let uint32 = new Uint32Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
100
    console.log(uint32.toString()); // "4,4,4"
M
mahaifeng 已提交
101
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1);
M
mahaifeng 已提交
102
    console.log(uint32.toString()); // "1,4,4"
M
mahaifeng 已提交
103
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 2);
M
mahaifeng 已提交
104
    console.log(uint32.toString()); // "1,4,3"
M
mahaifeng 已提交
105
    uint32 = new Uint32Array([1, 2, 3]).fill(4, 1, 1);
M
mahaifeng 已提交
106
    console.log(uint32.toString()); // "1,2,3"
M
mahaifeng 已提交
107
    uint32 = new Uint32Array([1, 2, 3]).fill(4, -3, -2);
M
mahaifeng 已提交
108
    console.log(uint32.toString()); // "4,2,3"
M
mahaifeng 已提交
109
    // #END
M
mahaifeng 已提交
110
    expect(uint32.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
111
  }
M
mahaifeng 已提交
112 113


M
mahaifeng 已提交
114
  testFilter() {
M
mahaifeng 已提交
115 116
    // #TEST Uint32Array.filter
    let uint32 = new Uint32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint32Array) : boolean => value >= 10);
M
mahaifeng 已提交
117
    console.log(uint32.toString()); // "12,44"
M
mahaifeng 已提交
118
    // #END
M
mahaifeng 已提交
119
    expect(uint32.toString()).toEqual("12,44");
M
mahaifeng 已提交
120 121 122
  }

  find() {
M
mahaifeng 已提交
123
    // #TEST Uint32Array.find
M
mahaifeng 已提交
124
    let uint32 = new Uint32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
125
    let res = uint32.find((value : number, _ : number, _a : Uint32Array) : boolean => value > 5);
M
mahaifeng 已提交
126
    console.log(res); // 8
M
mahaifeng 已提交
127
    // #END
M
mahaifeng 已提交
128
    expect(res).toEqual(8);
M
mahaifeng 已提交
129 130 131
  }

  findIndex() {
M
mahaifeng 已提交
132
    // #TEST Uint32Array.findIndex
M
mahaifeng 已提交
133
    let uint32 = new Uint32Array([4, 6, 8, 12]);
M
mahaifeng 已提交
134
    let res = uint32.findIndex((value : number, _ : number, _a : Uint32Array) : boolean => value > 100);
M
mahaifeng 已提交
135 136 137
    console.log(res); // -1
    res = new Uint32Array([4, 6, 7, 120]).findIndex((value : number, _ : number, _a : Uint32Array) : boolean => value > 100);
    console.log(res); // 3
M
mahaifeng 已提交
138
    // #END
M
mahaifeng 已提交
139
    expect(res).toEqual(3);
M
mahaifeng 已提交
140 141 142
  }

  foreach() {
M
mahaifeng 已提交
143 144
    // #TEST Uint32Array.forEach
    new Uint32Array([0, 1, 2, 3]).forEach((value : number, index : number, _a : Uint32Array) => {
M
mahaifeng 已提交
145 146
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
147
    // #END
M
mahaifeng 已提交
148
  }
M
mahaifeng 已提交
149

M
mahaifeng 已提交
150
  iterator() {
M
mahaifeng 已提交
151
    // #TEST Uint32Array.entries
M
mahaifeng 已提交
152 153
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
154 155
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
156
    // #END
M
mahaifeng 已提交
157
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
158 159 160
  }

  includes() {
M
mahaifeng 已提交
161
    // #TEST Uint32Array.includes
M
mahaifeng 已提交
162 163
    let uint32 = new Uint32Array([1, 2, 3]);
    let res = uint32.includes(2);
M
mahaifeng 已提交
164
    console.log(res); // true
M
mahaifeng 已提交
165
    res = uint32.includes(4);
M
mahaifeng 已提交
166
    console.log(res); // false
M
mahaifeng 已提交
167
    res = uint32.includes(3, 3);
M
mahaifeng 已提交
168
    console.log(res); // false
M
mahaifeng 已提交
169
    // #END
M
mahaifeng 已提交
170
    expect(res).toEqual(false);
M
mahaifeng 已提交
171 172
  }

M
mahaifeng 已提交
173

M
mahaifeng 已提交
174
  indexOf() {
M
mahaifeng 已提交
175
    // #TEST Uint32Array.indexOf
M
mahaifeng 已提交
176 177
    let uint32 = new Uint32Array([2, 5, 9]);
    let res = uint32.indexOf(2);
M
mahaifeng 已提交
178
    console.log(res); // 0
M
mahaifeng 已提交
179
    res = uint32.indexOf(7);
M
mahaifeng 已提交
180
    console.log(res); // -1
M
mahaifeng 已提交
181
    res = uint32.indexOf(9, 2);
M
mahaifeng 已提交
182
    console.log(res); // 2
M
mahaifeng 已提交
183
    res = uint32.indexOf(2, -1);
M
mahaifeng 已提交
184
    console.log(res); // -1
M
mahaifeng 已提交
185
    res = uint32.indexOf(2, -3);
M
mahaifeng 已提交
186
    console.log(res); // 0
M
mahaifeng 已提交
187
    // #END
M
mahaifeng 已提交
188
    expect(res).toEqual(0);
M
mahaifeng 已提交
189
  }
M
mahaifeng 已提交
190

M
mahaifeng 已提交
191
  join() {
M
mahaifeng 已提交
192
    // #TEST Uint32Array.join
M
mahaifeng 已提交
193 194
    let uint32 = new Uint32Array([1, 2, 3]);
    let res = uint32.join();
M
mahaifeng 已提交
195
    console.log(res); // "1,2,3"
M
mahaifeng 已提交
196
    res = uint32.join(" / ");
M
mahaifeng 已提交
197
    console.log(res); // "1 / 2 / 3"
M
mahaifeng 已提交
198
    res = uint32.join("");
M
mahaifeng 已提交
199
    console.log(res); // "123"
M
mahaifeng 已提交
200
    // #END
M
mahaifeng 已提交
201
    expect(res).toEqual("123");
M
mahaifeng 已提交
202 203 204
  }

  keys() {
M
mahaifeng 已提交
205
    // #TEST Uint32Array.keys
M
mahaifeng 已提交
206 207
    let arr = new Uint32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
208 209
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
210
    // #END
M
mahaifeng 已提交
211
    expect(value).toEqual(0);
M
mahaifeng 已提交
212 213
  }

M
mahaifeng 已提交
214

M
mahaifeng 已提交
215
  map() {
M
mahaifeng 已提交
216
    // #TEST Uint32Array.map
M
mahaifeng 已提交
217
    let numbers = new Uint32Array([1, 4, 9]);
M
mahaifeng 已提交
218
    let doubles = numbers.map((value : number, _ : number, _a : Uint32Array) : number => value * 2);
M
mahaifeng 已提交
219 220
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
M
mahaifeng 已提交
221
    // #END
M
mahaifeng 已提交
222
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
223 224 225
  }

  reduce() {
M
mahaifeng 已提交
226
    // #TEST Uint32Array.reduce
M
mahaifeng 已提交
227
    let total = new Uint32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
228 229
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue);
    console.log(res); // 6
M
mahaifeng 已提交
230
    total = new Uint32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
231 232
    res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue, 8);
    console.log(res); // 14
M
mahaifeng 已提交
233
    // #END
M
mahaifeng 已提交
234
    expect(res).toEqual(14);
M
mahaifeng 已提交
235
  }
M
mahaifeng 已提交
236

M
mahaifeng 已提交
237
  reduceRight() {
M
mahaifeng 已提交
238
    // #TEST Uint32Array.reduceRight
M
mahaifeng 已提交
239
    let total = new Uint32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
240 241
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue);
    console.log(res); // 6
M
mahaifeng 已提交
242
    total = new Uint32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
243 244
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint32Array) : number => accumulator + currentValue, 8);
    console.log(res); // 14
M
mahaifeng 已提交
245
    // #END
M
mahaifeng 已提交
246
    expect(res).toEqual(14);
M
mahaifeng 已提交
247 248 249
  }

  reverse() {
M
mahaifeng 已提交
250
    // #TEST Uint32Array.reverse
M
mahaifeng 已提交
251 252
    let uint32 = new Uint32Array([1, 2, 3]);
    uint32.reverse();
M
mahaifeng 已提交
253
    console.log(uint32.toString()); // "3,2,1"
M
mahaifeng 已提交
254
    // #END
M
mahaifeng 已提交
255
    expect(uint32.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
256 257
  }

M
mahaifeng 已提交
258

M
mahaifeng 已提交
259
  slice() {
M
mahaifeng 已提交
260
    // #TEST Uint32Array.slice
M
mahaifeng 已提交
261 262
    let uint32 = new Uint32Array([1, 2, 3]);
    let res = uint32.slice(1);
M
mahaifeng 已提交
263
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
264

M
mahaifeng 已提交
265
    res = uint32.slice(2);
M
mahaifeng 已提交
266
    console.log(res.toString()); // "3"
M
mahaifeng 已提交
267

M
mahaifeng 已提交
268
    res = uint32.slice(-2);
M
mahaifeng 已提交
269
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
270

M
mahaifeng 已提交
271
    res = uint32.slice(0, 1);
M
mahaifeng 已提交
272
    console.log(res.toString()); // "1"
M
mahaifeng 已提交
273
    // #END
M
mahaifeng 已提交
274
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
275 276 277
  }

  sort() {
M
mahaifeng 已提交
278
    // #TEST Uint32Array.sort
M
mahaifeng 已提交
279 280
    let numbers = new Uint32Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
281
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
282

M
mahaifeng 已提交
283
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
284
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
285
    // #END
M
mahaifeng 已提交
286
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
287
  }
M
mahaifeng 已提交
288 289


M
mahaifeng 已提交
290
  subarray() {
M
mahaifeng 已提交
291
    // #TEST Uint32Array.subarray
M
mahaifeng 已提交
292
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
293 294
    let uint32 = new Uint32Array(buffer);
    uint32.set([1, 2, 3]);
M
mahaifeng 已提交
295
    console.log(uint32.toString()); // "1,2,3,0"
M
mahaifeng 已提交
296

M
mahaifeng 已提交
297
    let sub = uint32.subarray(0, 4);
M
mahaifeng 已提交
298
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
299
    // #END
M
mahaifeng 已提交
300
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
301 302
  }

M
mahaifeng 已提交
303

M
mahaifeng 已提交
304
  values() {
M
mahaifeng 已提交
305
    // #TEST Uint32Array.values
M
mahaifeng 已提交
306 307
    let arr = new Uint32Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
308 309
    let value = values.next().value;
    console.log(value); // 1
M
mahaifeng 已提交
310
    // #END
M
mahaifeng 已提交
311
    expect(value).toEqual(1);
M
mahaifeng 已提交
312 313 314
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
315
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
316
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
317 318
    let uint32 = new Uint32Array(buffer);
    uint32[3] = 42;
M
mahaifeng 已提交
319
    console.log(uint32.toString()); // "0,0,0,42"
M
mahaifeng 已提交
320 321 322

    let res = buffer.slice(8);
    let sliced = new Uint32Array(res);
M
mahaifeng 已提交
323
    console.log(sliced[1]); // 42
M
mahaifeng 已提交
324
    // #END
M
mahaifeng 已提交
325
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
326
  }
M
mahaifeng 已提交
327 328


M
mahaifeng 已提交
329 330 331 332 333
  testSome() {
    // #TEST Uint32Array.some
    const uint32 = new Uint32Array([8, 20, 30, 40, 50]);
    const positives = new Uint32Array([10, 20, 30, 40, 50]);

M
mahaifeng 已提交
334
    let result = uint32.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
335
      element < 10
M
mahaifeng 已提交
336 337
    );
    console.log(result); // true
M
mahaifeng 已提交
338

M
mahaifeng 已提交
339
    result = positives.some((element : number, index : number, array : Uint32Array) : boolean =>
M
mahaifeng 已提交
340
      element < 0
M
mahaifeng 已提交
341 342
    );
    console.log(result); // false
M
mahaifeng 已提交
343
    // #END
M
mahaifeng 已提交
344
    expect(result).toEqual(false);
M
mahaifeng 已提交
345
  }
M
mahaifeng 已提交
346

M
mahaifeng 已提交
347 348 349



350
  // #endif
M
mahaifeng 已提交
351
}