TFloat32Array.uts 11.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 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 39 40
  from() {
    var float32Array = Float32Array.from([1, 2, 3], (v : number, _ : number) : number => v + v);
    expect(float32Array.toString()).toEqual('2,4,6');
M
mahaifeng 已提交
41
  }
M
mahaifeng 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

  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 已提交
66

M
mahaifeng 已提交
67 68 69 70 71 72 73
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32[1] = 42;
    expect(float32.toString()).toEqual("0,42,0,0");
  }

  testSet() {
M
mahaifeng 已提交
74
    // #TEST Float32Array.set
M
mahaifeng 已提交
75
    let float32 = new Float32Array(8);
76
    var array = [1, 2, 3];
M
mahaifeng 已提交
77
    float32.set(array, 1);
78
    console.log(float32.toString()); // 0,1,2,3,0,0,0,0
M
mahaifeng 已提交
79

M
mahaifeng 已提交
80 81 82
    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);
83 84
    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 已提交
85

M
mahaifeng 已提交
86 87 88
    let typed_src = new Float32Array(src);
    typed_dest = new Float32Array(16);
    typed_dest.set(typed_src);
89
    console.log(typed_dest.toString()); // 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4
M
mahaifeng 已提交
90
    // #END
91 92
    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 已提交
93 94 95
  }

  testCopyWith() {
M
mahaifeng 已提交
96
    // #TEST Float32Array.copyWithin
M
mahaifeng 已提交
97 98 99
    let float32 = new Float32Array(8);
    float32.set([1, 2, 3], 1);
    float32.copyWithin(3, 0, 3);
100
    console.log(float32.toString()); // 0,1,2,0,1,2,0,0
M
mahaifeng 已提交
101
    // #END
102
    expect(float32.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
103 104 105
  }

  testEvery() {
M
mahaifeng 已提交
106 107
    // #TEST Float32Array.every
    let result = new Float32Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Float32Array) : boolean => value < 40);
108
    console.log(result); // false
M
mahaifeng 已提交
109
    // #END
110
    expect(result).toEqual(false);
M
mahaifeng 已提交
111 112
  }

113

M
mahaifeng 已提交
114
  testFill() {
M
mahaifeng 已提交
115
    // #TEST Float32Array.fill
116 117
    let float32_t1 = new Float32Array([1, 2, 3]).fill(4);
    console.log(float32_t1.toString()); // 4,4,4
M
mahaifeng 已提交
118

119 120
    let float32_t2 = new Float32Array([1, 2, 3]).fill(4, 1);
    console.log(float32_t2.toString()); // 1,4,4
M
mahaifeng 已提交
121 122


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

126 127 128 129 130
    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 已提交
131
    // #END
132 133 134 135 136
    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 已提交
137 138
  }

139

M
mahaifeng 已提交
140
  testFilter() {
M
mahaifeng 已提交
141 142
    // #TEST Float32Array.filter
    let float32 = new Float32Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float32Array) : boolean => value >= 10);
143
    console.log(float32.toString()); // 12,44
M
mahaifeng 已提交
144
    // #END
145
    expect(float32.toString()).toEqual("12,44");
M
mahaifeng 已提交
146 147 148
  }

  find() {
M
mahaifeng 已提交
149
    // #TEST Float32Array.find
M
mahaifeng 已提交
150
    let float32 = new Float32Array([4, 5, 8, 12]);
M
mahaifeng 已提交
151
    let res = float32.find((value : number, _ : number, _a : Float32Array) : boolean => value > 5);
152
    console.log(res); // 8
M
mahaifeng 已提交
153
    // #END
154
    expect(res).toEqual(8);
M
mahaifeng 已提交
155 156
  }

157

M
mahaifeng 已提交
158
  findIndex() {
M
mahaifeng 已提交
159
    // #TEST Float32Array.findIndex
M
mahaifeng 已提交
160
    let float32 = new Float32Array([4, 6, 8, 12]);
161 162
    let res1 = float32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
    console.log(res1); // -1
M
mahaifeng 已提交
163 164

    let ufloat32 = new Float32Array([4, 6, 7, 120]);
165 166
    let res = ufloat32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
    console.log(res); // 3
M
mahaifeng 已提交
167
    // #END
168 169
    expect(res).toEqual(3);
    expect(res1).toEqual(-1);
M
mahaifeng 已提交
170 171 172
  }

  foreach() {
M
mahaifeng 已提交
173
    // #TEST Float32Array.forEach
M
mahaifeng 已提交
174
    new Float32Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float32Array) => {
M
mahaifeng 已提交
175 176
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
177
    // #END
M
mahaifeng 已提交
178 179 180
  }

  iterator() {
M
mahaifeng 已提交
181
    // #TEST Float32Array.entries
M
mahaifeng 已提交
182 183
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
184 185 186 187
    let value1 = entries.next().value[1]
    let value2 = entries.next().value[1]
    console.log(value1); // 10
    console.log(value2); // 20
M
mahaifeng 已提交
188
    // #END
189 190
    expect(value1).toEqual(10);
    expect(value2).toEqual(20);
M
mahaifeng 已提交
191 192 193
  }

  includes() {
M
mahaifeng 已提交
194
    // #TEST Float32Array.includes
M
mahaifeng 已提交
195
    let float32 = new Float32Array([1, 2, 3]);
196 197 198 199 200 201
    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 已提交
202
    // #END
203 204 205
    expect(res2).toEqual(false);
    expect(res1).toEqual(true);
    expect(res).toEqual(false);
M
mahaifeng 已提交
206 207 208
  }

  indexOf() {
M
mahaifeng 已提交
209
    // #TEST Float32Array.indexOf
M
mahaifeng 已提交
210 211
    let float32 = new Float32Array([2, 5, 9]);
    let res = float32.indexOf(2);
212 213
    console.log(res); // 0

M
mahaifeng 已提交
214 215 216
    let res1 = float32.indexOf(7);
    console.log(res1); // -1

M
mahaifeng 已提交
217

M
mahaifeng 已提交
218 219
    let res2 = float32.indexOf(9, 2);
    console.log(res2); // 2
M
mahaifeng 已提交
220 221


M
mahaifeng 已提交
222 223
    let res3 = float32.indexOf(2, -1);
    console.log(res3); // -1
M
mahaifeng 已提交
224

M
mahaifeng 已提交
225 226 227 228 229 230 231 232

    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 已提交
233 234 235
  }

  join() {
M
mahaifeng 已提交
236
    // #TEST Float32Array.join
M
mahaifeng 已提交
237 238
    let float32 = new Float32Array([1, 2, 3]);
    let res = float32.join();
239
    console.log(res); // 1,2,3
M
mahaifeng 已提交
240

M
mahaifeng 已提交
241 242
    let res1 = float32.join(" / ");
    console.log(res1); // 1 / 2 / 3
M
mahaifeng 已提交
243

M
mahaifeng 已提交
244 245 246 247 248 249
    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 已提交
250 251 252
  }

  keys() {
M
mahaifeng 已提交
253
    // #TEST Float32Array.keys
M
mahaifeng 已提交
254 255
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
256 257
    let ret = keys.next().value
    console.log(ret); // 0
M
mahaifeng 已提交
258
    // #END
259
    expect(ret).toEqual(0);
M
mahaifeng 已提交
260 261 262
  }

  map() {
M
mahaifeng 已提交
263
    // #TEST Float32Array.map
M
mahaifeng 已提交
264
    let numbers = new Float32Array([1, 4, 9]);
M
mahaifeng 已提交
265
    let doubles = numbers.map((value : number, _ : number, _a : Float32Array) : number => value * 2);
266
    console.log(doubles.toString()); // 2,8,18
M
mahaifeng 已提交
267
    // #END
268
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
269 270 271
  }

  reduce() {
M
mahaifeng 已提交
272
    // #TEST Float32Array.reduce
M
mahaifeng 已提交
273
    let total = new Float32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
274
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
275 276
    console.log(res); // 6
    // #END
M
mahaifeng 已提交
277
    expect(res).toEqual(6);
M
mahaifeng 已提交
278

279 280 281 282
    // total = new Float32Array([0, 1, 2, 3]);
    // res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue, 8);
    // console.log(res); // 14
    // expect(res).toEqual(14);
M
mahaifeng 已提交
283 284 285
  }

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

293 294 295 296
    // 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 已提交
297 298 299
  }

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

  slice() {
M
mahaifeng 已提交
309
    // #TEST Float32Array.slice
M
mahaifeng 已提交
310 311
    let float32 = new Float32Array([1, 2, 3]);
    let res = float32.slice(1);
312
    console.log(res.toString()); // 2,3
M
mahaifeng 已提交
313

M
mahaifeng 已提交
314 315
    let res1 = float32.slice(2);
    console.log(res1.toString()); // 3
M
mahaifeng 已提交
316

M
mahaifeng 已提交
317 318
    let res2 = float32.slice(-2);
    console.log(res2.toString()); // 2,3
M
mahaifeng 已提交
319

M
mahaifeng 已提交
320 321 322 323 324 325 326
    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 已提交
327 328 329
  }

  sort() {
M
mahaifeng 已提交
330
    // #TEST Float32Array.sort
M
mahaifeng 已提交
331 332
    let numbers = new Float32Array([40, 1, 5]);
    numbers.sort();
333
    console.log(numbers.toString()); // 1,5,40
M
mahaifeng 已提交
334 335 336 337 338

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

339
    // #END
M
mahaifeng 已提交
340
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
341
    expect(ret).toEqual("1,5,40");
M
mahaifeng 已提交
342 343 344
  }

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

M
mahaifeng 已提交
351 352 353 354
    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 已提交
355 356 357
  }

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

  arrayBufferSlice() {
M
mahaifeng 已提交
367
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
368 369 370
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32[3] = 42;
371
    console.log(float32.toString()); // 0,0,0,42
M
mahaifeng 已提交
372 373 374
    let res = buffer.slice(8);
    let sliced = new Float32Array(res);
    console.log(sliced[1]); // 42
375
    // #END
M
mahaifeng 已提交
376
    expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
377
  }
M
mahaifeng 已提交
378

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

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

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

396 397 398
    expect(float32.some((element : number, index : number, array : Float32Array) : boolean =>
      element < 0
    )).toEqual(true);
M
mahaifeng 已提交
399
  }
400

401
  // #endif
M
mahaifeng 已提交
402
}