TFloat32Array.uts 11.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 TFloat32Array {
  test() {
M
mahaifeng 已提交
10
    // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    this.testfloat32();
    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();
35
    // #endif
M
mahaifeng 已提交
36
  }
M
mahaifeng 已提交
37
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
38
  from() {
M
mahaifeng 已提交
39
    // #TEST Float32Array.from
M
mahaifeng 已提交
40
    var float32Array = Float32Array.from([1, 2, 3], (v : number, _ : number) : number => v + v);
M
mahaifeng 已提交
41 42
    console.log(float32Array.toString()); // '2,4,6'
    // #END
M
mahaifeng 已提交
43
    expect(float32Array.toString()).toEqual('2,4,6');
M
mahaifeng 已提交
44
  }
M
mahaifeng 已提交
45 46 47 48 49 50 51
  of() {
    // #TEST Float32Array.of
    var float32Array = Float32Array.of(1, 2, 3)
    console.log(float32Array.toString()); // '1,2,3'
    // #END
    expect(float32Array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  testfloat32() {
    let float32 = new Float32Array(2);
    float32[0] = 42;
    expect(float32[0]).toEqual(42);
    expect(float32.length).toEqual(2);
    expect(Float32Array.BYTES_PER_ELEMENT).toEqual(4);

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

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

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

    var k = Float32Array.of(1, 2, 3)
    expect(k.toString()).toEqual("1,2,3");
  }

  testConstructor() {
M
mahaifeng 已提交
76

M
mahaifeng 已提交
77 78 79 80 81 82 83
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32[1] = 42;
    expect(float32.toString()).toEqual("0,42,0,0");
  }

  testSet() {
M
mahaifeng 已提交
84
    // #TEST Float32Array.set
M
mahaifeng 已提交
85
    let float32 = new Float32Array(8);
86
    var array = [1, 2, 3];
M
mahaifeng 已提交
87
    float32.set(array, 1);
88
    console.log(float32.toString()); // 0,1,2,3,0,0,0,0
M
mahaifeng 已提交
89

M
mahaifeng 已提交
90 91 92
    let src = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
    let typed_dest = new Float32Array(16);
    typed_dest.set(src);
93 94
    console.log(typed_dest.toString()); // 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
    // expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4");
M
mahaifeng 已提交
95

M
mahaifeng 已提交
96 97 98
    let typed_src = new Float32Array(src);
    typed_dest = new Float32Array(16);
    typed_dest.set(typed_src);
99
    console.log(typed_dest.toString()); // 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
M
mahaifeng 已提交
100
    // #END
101 102
    expect(float32.toString()).toEqual("0,1,2,3,0,0,0,0");
    expect(typed_dest.toString()).toEqual("1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4");
M
mahaifeng 已提交
103 104 105
  }

  testCopyWith() {
M
mahaifeng 已提交
106
    // #TEST Float32Array.copyWithin
M
mahaifeng 已提交
107 108 109
    let float32 = new Float32Array(8);
    float32.set([1, 2, 3], 1);
    float32.copyWithin(3, 0, 3);
110
    console.log(float32.toString()); // 0,1,2,0,1,2,0,0
M
mahaifeng 已提交
111
    // #END
112
    expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
113 114 115
  }

  testEvery() {
M
mahaifeng 已提交
116 117
    // #TEST Float32Array.every
    let result = new Float32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Float32Array) : boolean => value < 40);
118
    console.log(result); // false
M
mahaifeng 已提交
119
    // #END
120
    expect(result).toEqual(false);
M
mahaifeng 已提交
121 122
  }

123

M
mahaifeng 已提交
124
  testFill() {
M
mahaifeng 已提交
125
    // #TEST Float32Array.fill
126 127
    let float32_t1 = new Float32Array([1, 2, 3]).fill(4);
    console.log(float32_t1.toString()); // 4,4,4
M
mahaifeng 已提交
128

129 130
    let float32_t2 = new Float32Array([1, 2, 3]).fill(4, 1);
    console.log(float32_t2.toString()); // 1,4,4
M
mahaifeng 已提交
131 132


133 134
    let float32_t3 = new Float32Array([1, 2, 3]).fill(4, 1, 2);
    console.log(float32_t3.toString()); // 1,4,3
M
mahaifeng 已提交
135

136 137 138 139 140
    let float32_t4 = new Float32Array([1, 2, 3]).fill(4, 1, 1);
    console.log(float32_t4.toString()); // 1,2,3

    let float32 = new Float32Array([1, 2, 3]).fill(4, -3, -2);
    console.log(float32.toString()); // 4,2,3
M
mahaifeng 已提交
141
    // #END
142 143 144 145 146
    expect(float32_t4.toString()).toEqual("1,2,3");
    expect(float32_t3.toString()).toEqual("1,4,3");
    expect(float32_t2.toString()).toEqual("1,4,4");
    expect(float32_t1.toString()).toEqual("4,4,4");
    expect(float32.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
147 148
  }

149

M
mahaifeng 已提交
150
  testFilter() {
M
mahaifeng 已提交
151 152
    // #TEST Float32Array.filter
    let float32 = new Float32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float32Array) : boolean => value >= 10);
153
    console.log(float32.toString()); // 12,44
M
mahaifeng 已提交
154
    // #END
155
    expect(float32.toString()).toEqual("12,44");
M
mahaifeng 已提交
156 157 158
  }

  find() {
M
mahaifeng 已提交
159
    // #TEST Float32Array.find
M
mahaifeng 已提交
160
    let float32 = new Float32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
161
    let res = float32.find((value : number, _ : number, _a : Float32Array) : boolean => value > 5);
162
    console.log(res); // 8
M
mahaifeng 已提交
163
    // #END
164
    expect(res).toEqual(8);
M
mahaifeng 已提交
165 166
  }

167

M
mahaifeng 已提交
168
  findIndex() {
M
mahaifeng 已提交
169
    // #TEST Float32Array.findIndex
M
mahaifeng 已提交
170
    let float32 = new Float32Array([4, 6, 8, 12]);
171 172
    let res1 = float32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
    console.log(res1); // -1
M
mahaifeng 已提交
173 174

    let ufloat32 = new Float32Array([4, 6, 7, 120]);
175 176
    let res = ufloat32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
    console.log(res); // 3
M
mahaifeng 已提交
177
    // #END
178 179
    expect(res).toEqual(3);
    expect(res1).toEqual(-1);
M
mahaifeng 已提交
180 181 182
  }

  foreach() {
M
mahaifeng 已提交
183
    // #TEST Float32Array.forEach
M
mahaifeng 已提交
184
    new Float32Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float32Array) => {
M
mahaifeng 已提交
185 186
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
187
    // #END
M
mahaifeng 已提交
188 189 190
  }

  iterator() {
M
mahaifeng 已提交
191
    // #TEST Float32Array.entries
M
mahaifeng 已提交
192 193
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
194 195 196 197
    let value1 = entries.next().value[1]
    let value2 = entries.next().value[1]
    console.log(value1); // 10
    console.log(value2); // 20
M
mahaifeng 已提交
198
    // #END
199 200
    expect(value1).toEqual(10);
    expect(value2).toEqual(20);
M
mahaifeng 已提交
201 202 203
  }

  includes() {
M
mahaifeng 已提交
204
    // #TEST Float32Array.includes
M
mahaifeng 已提交
205
    let float32 = new Float32Array([1, 2, 3]);
206 207 208 209 210 211
    let res1 = float32.includes(2.0);
    console.log(res1); // true
    let res2 = float32.includes(4.0);
    console.log(res2); // false
    let res = float32.includes(3.0, 3);
    console.log(res); // false
M
mahaifeng 已提交
212
    // #END
213 214 215
    expect(res2).toEqual(false);
    expect(res1).toEqual(true);
    expect(res).toEqual(false);
M
mahaifeng 已提交
216 217 218
  }

  indexOf() {
M
mahaifeng 已提交
219
    // #TEST Float32Array.indexOf
M
mahaifeng 已提交
220 221
    let float32 = new Float32Array([2, 5, 9]);
    let res = float32.indexOf(2);
222 223
    console.log(res); // 0

M
mahaifeng 已提交
224 225 226
    let res1 = float32.indexOf(7);
    console.log(res1); // -1

M
mahaifeng 已提交
227

M
mahaifeng 已提交
228 229
    let res2 = float32.indexOf(9, 2);
    console.log(res2); // 2
M
mahaifeng 已提交
230 231


M
mahaifeng 已提交
232 233
    let res3 = float32.indexOf(2, -1);
    console.log(res3); // -1
M
mahaifeng 已提交
234

M
mahaifeng 已提交
235 236 237 238 239 240 241 242

    let res4 = float32.indexOf(2, -3);
    console.log(res4); // 0
    // #END
    expect(res4).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
243 244 245
  }

  join() {
M
mahaifeng 已提交
246
    // #TEST Float32Array.join
M
mahaifeng 已提交
247 248
    let float32 = new Float32Array([1, 2, 3]);
    let res = float32.join();
249
    console.log(res); // 1,2,3
M
mahaifeng 已提交
250

M
mahaifeng 已提交
251 252
    let res1 = float32.join(" / ");
    console.log(res1); // 1 / 2 / 3
M
mahaifeng 已提交
253

M
mahaifeng 已提交
254 255 256 257 258 259
    let res2 = float32.join("");
    console.log(res2); // 123
    // #END
    expect(res).toEqual("1,2,3");
    expect(res2).toEqual("123");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
260 261 262
  }

  keys() {
M
mahaifeng 已提交
263
    // #TEST Float32Array.keys
M
mahaifeng 已提交
264 265
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
266 267
    let ret = keys.next().value
    console.log(ret); // 0
M
mahaifeng 已提交
268
    // #END
269
    expect(ret).toEqual(0);
M
mahaifeng 已提交
270 271 272
  }

  map() {
M
mahaifeng 已提交
273
    // #TEST Float32Array.map
M
mahaifeng 已提交
274
    let numbers = new Float32Array([1, 4, 9]);
M
mahaifeng 已提交
275
    let doubles = numbers.map((value : number, _ : number, _a : Float32Array) : number => value * 2);
276
    console.log(doubles.toString()); // 2,8,18
M
mahaifeng 已提交
277
    // #END
278
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
279 280 281
  }

  reduce() {
M
mahaifeng 已提交
282
    // #TEST Float32Array.reduce
M
mahaifeng 已提交
283
    let total = new Float32Array([0, 1, 2, 3]);
284 285 286 287 288 289 290 291 292 293
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number =>
      accumulator + currentValue
    );
    console.log(res1); // 6

    total = new Float32Array([0, 1, 2, 3]);
    let res2 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number =>
      accumulator + currentValue, 8
    );
    console.log(res2); // 14
294
    // #END
M
mahaifeng 已提交
295

296 297
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
298 299 300
  }

  reduceRight() {
M
mahaifeng 已提交
301
    // #TEST Float32Array.reduceRight
M
mahaifeng 已提交
302
    let total = new Float32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
303
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
304 305
    console.log(res); // 6
    // #END
M
mahaifeng 已提交
306
    expect(res).toEqual(6);
M
mahaifeng 已提交
307

308 309 310 311
    // total = new Float32Array([0, 1, 2, 3]);
    // res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8);
    // console.log(res); // 14
    // expect(res).toEqual(14);
M
mahaifeng 已提交
312 313 314
  }

  reverse() {
M
mahaifeng 已提交
315
    // #TEST Float32Array.reverse
M
mahaifeng 已提交
316 317
    let float32 = new Float32Array([1, 2, 3]);
    float32.reverse();
318
    console.log(float32.toString()); // 3,2,1
M
mahaifeng 已提交
319
    // #END
320
    expect(float32.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
321 322 323
  }

  slice() {
M
mahaifeng 已提交
324
    // #TEST Float32Array.slice
M
mahaifeng 已提交
325 326
    let float32 = new Float32Array([1, 2, 3]);
    let res = float32.slice(1);
327
    console.log(res.toString()); // 2,3
M
mahaifeng 已提交
328

M
mahaifeng 已提交
329 330
    let res1 = float32.slice(2);
    console.log(res1.toString()); // 3
M
mahaifeng 已提交
331

M
mahaifeng 已提交
332 333
    let res2 = float32.slice(-2);
    console.log(res2.toString()); // 2,3
M
mahaifeng 已提交
334

M
mahaifeng 已提交
335 336 337 338 339 340 341
    let res3 = float32.slice(0, 1);
    console.log(res3.toString()); // 1
    // #END
    expect(res.toString()).toEqual("2,3");
    expect(res1.toString()).toEqual("3");
    expect(res3.toString()).toEqual("1");
    expect(res2.toString()).toEqual("2,3");
M
mahaifeng 已提交
342 343 344
  }

  sort() {
M
mahaifeng 已提交
345
    // #TEST Float32Array.sort
M
mahaifeng 已提交
346 347
    let numbers = new Float32Array([40, 1, 5]);
    numbers.sort();
348
    console.log(numbers.toString()); // 1,5,40
M
mahaifeng 已提交
349 350 351 352 353

    let ret = numbers.toString()
    numbers.sort((a, b) : number => a - b);
    console.log(numbers.toString()); // 1,5,40

354
    // #END
M
mahaifeng 已提交
355
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
356
    expect(ret).toEqual("1,5,40");
M
mahaifeng 已提交
357 358 359
  }

  subarray() {
M
mahaifeng 已提交
360
    // #TEST Float32Array.subarray
M
mahaifeng 已提交
361 362 363
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32.set([1, 2, 3]);
364
    console.log(float32.toString()); // 1,2,3,0
M
mahaifeng 已提交
365

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

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

  arrayBufferSlice() {
M
mahaifeng 已提交
382
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
383 384 385
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32[3] = 42;
386
    console.log(float32.toString()); // 0,0,0,42
M
mahaifeng 已提交
387 388 389
    let res = buffer.slice(8);
    let sliced = new Float32Array(res);
    console.log(sliced[1]); // 42
390
    // #END
M
mahaifeng 已提交
391
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
392
  }
M
mahaifeng 已提交
393

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

399
    console.log(float32.some((element : number, index : number, array : Float32Array) : boolean =>
M
mahaifeng 已提交
400
      element < 0
401
    )); // true
M
mahaifeng 已提交
402

403 404 405 406
    console.log(positives.some((element : number, index : number, array : Float32Array) : boolean =>
      element < 0
    )); // false
    // #END
M
mahaifeng 已提交
407 408 409
    expect(positives.some((element : number, index : number, array : Float32Array) : boolean =>
      element < 0
    )).toEqual(false);
410

411 412 413
    expect(float32.some((element : number, index : number, array : Float32Array) : boolean =>
      element < 0
    )).toEqual(true);
M
mahaifeng 已提交
414
  }
415

416
  // #endif
417
}