TFloat32Array.uts 12.0 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 (UNI-APP-X && 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 (UNI-APP-X && 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 163 164
    let res1 = float32.findIndex((value : number, _ : number, _a : Float32Array) : boolean => value > 100);
    console.log(res1); // -1
    // #END

M
mahaifeng 已提交
165 166

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

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

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

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

  indexOf() {
M
mahaifeng 已提交
211
    // #TEST Float32Array.indexOf
M
mahaifeng 已提交
212 213
    let float32 = new Float32Array([2, 5, 9]);
    let res = float32.indexOf(2);
214 215 216 217 218 219 220
    console.log(res); // 0
    // #END
    // expect(res).toEqual(0);

    // res = float32.indexOf(7);
    // console.log(res); // -1
    // expect(res).toEqual(-1);
M
mahaifeng 已提交
221

222 223 224
    // res = float32.indexOf(9, 2);
    // console.log(res); // 2
    // expect(res).toEqual(2);
M
mahaifeng 已提交
225

226 227 228
    // res = float32.indexOf(2, -1);
    // console.log(res); // -1
    // expect(res).toEqual(-1);
M
mahaifeng 已提交
229

230 231
    // res = float32.indexOf(2, -3);
    // console.log(res); // 0
M
mahaifeng 已提交
232 233 234 235 236

    expect(res).toEqual(0);
  }

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

244 245 246
    // res = float32.join(" / ");
    // console.log(res); // 1 / 2 / 3
    // expect(res).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
247

248 249 250
    // res = float32.join("");
    // console.log(res); // 123
    // expect(res).toEqual("123");
M
mahaifeng 已提交
251 252 253
  }

  keys() {
M
mahaifeng 已提交
254
    // #TEST Float32Array.keys
M
mahaifeng 已提交
255 256
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
257 258
    let ret = keys.next().value
    console.log(ret); // 0
M
mahaifeng 已提交
259
    // #END
260 261 262 263 264 265 266 267 268
    expect(ret).toEqual(0);
    // console.log(keys.next().value); // 1
    // expect(keys.next().value).toEqual(1);
    // console.log(keys.next().value); // 2
    // expect(keys.next().value).toEqual(2);
    // console.log(keys.next().value); // 3
    // expect(keys.next().value).toEqual(3);
    // console.log(keys.next().value); // 4
    // expect(keys.next().value).toEqual(4);
M
mahaifeng 已提交
269 270 271
  }

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

  reduce() {
M
mahaifeng 已提交
281
    // #TEST Float32Array.reduce
M
mahaifeng 已提交
282
    let total = new Float32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
283
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
284 285
    console.log(res); // 6
    // #END
M
mahaifeng 已提交
286
    expect(res).toEqual(6);
M
mahaifeng 已提交
287

288 289 290 291
    // 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 已提交
292 293 294
  }

  reduceRight() {
M
mahaifeng 已提交
295
    // #TEST Float32Array.reduceRight
M
mahaifeng 已提交
296
    let total = new Float32Array([0, 1, 2, 3]);
M
mahaifeng 已提交
297
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float32Array) : number => accumulator + currentValue);
298 299
    console.log(res); // 6
    // #END
M
mahaifeng 已提交
300
    expect(res).toEqual(6);
M
mahaifeng 已提交
301

302 303 304 305 306
    // 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
    // // #END
    // expect(res).toEqual(14);
M
mahaifeng 已提交
307 308 309
  }

  reverse() {
M
mahaifeng 已提交
310
    // #TEST Float32Array.reverse
M
mahaifeng 已提交
311 312
    let float32 = new Float32Array([1, 2, 3]);
    float32.reverse();
313
    console.log(float32.toString()); // 3,2,1
M
mahaifeng 已提交
314
    // #END
315
    expect(float32.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
316 317 318
  }

  slice() {
M
mahaifeng 已提交
319
    // #TEST Float32Array.slice
M
mahaifeng 已提交
320 321
    let float32 = new Float32Array([1, 2, 3]);
    let res = float32.slice(1);
322 323
    console.log(res.toString()); // 2,3
    // #END
M
mahaifeng 已提交
324 325
    expect(res.toString()).toEqual("2,3");

326 327 328
    // res = float32.slice(2);
    // console.log(res.toString()); // 3
    // expect(res.toString()).toEqual("3");
M
mahaifeng 已提交
329

330 331 332
    // res = float32.slice(-2);
    // console.log(res.toString()); // 2,3
    // expect(res.toString()).toEqual("2,3");
M
mahaifeng 已提交
333

334 335 336
    // res = float32.slice(0, 1);
    // console.log(res.toString()); // 1
    // expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
337 338 339
  }

  sort() {
M
mahaifeng 已提交
340
    // #TEST Float32Array.sort
M
mahaifeng 已提交
341 342
    let numbers = new Float32Array([40, 1, 5]);
    numbers.sort();
343 344
    console.log(numbers.toString()); // 1,5,40
    // #END
M
mahaifeng 已提交
345 346
    expect(numbers.toString()).toEqual("1,5,40");

347 348 349
    // numbers.sort((a, b) : number => a - b);
    // console.log(numbers.toString()); // 1,5,40
    // expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
350 351 352
  }

  subarray() {
M
mahaifeng 已提交
353
    // #TEST Float32Array.subarray
M
mahaifeng 已提交
354 355 356
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32.set([1, 2, 3]);
357 358
    console.log(float32.toString()); // 1,2,3,0
    // #END
M
mahaifeng 已提交
359 360
    expect(float32.toString()).toEqual("1,2,3,0");

361 362 363
    // let sub = float32.subarray(0, 4);
    // console.log(sub.toString()); // 1,2,3,0
    // expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
364 365 366
  }

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

  arrayBufferSlice() {
M
mahaifeng 已提交
380
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
381 382 383
    let buffer = new ArrayBuffer(16);
    let float32 = new Float32Array(buffer);
    float32[3] = 42;
384 385
    console.log(float32.toString()); // 0,0,0,42
    // #END
M
mahaifeng 已提交
386 387
    expect(float32.toString()).toEqual("0,0,0,42");

388 389 390 391
    // let res = buffer.slice(8);
    // let sliced = new Float32Array(res);
    // console.log(sliced[1]); // 42
    // 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
M
mahaifeng 已提交
417
}