TFloat32Array.uts 11.9 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
    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 214 215 216 217 218
    console.log(res); // 0
    // #END
    // expect(res).toEqual(0);

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

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

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

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

    expect(res).toEqual(0);
  }

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

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

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

  keys() {
M
mahaifeng 已提交
252
    // #TEST Float32Array.keys
M
mahaifeng 已提交
253 254
    let arr = new Float32Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
255 256
    let ret = keys.next().value
    console.log(ret); // 0
M
mahaifeng 已提交
257
    // #END
258 259 260 261 262 263 264 265 266
    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 已提交
267 268 269
  }

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

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

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

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

300 301 302 303
    // 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 已提交
304 305 306
  }

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

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

323 324 325
    // res = float32.slice(2);
    // console.log(res.toString()); // 3
    // expect(res.toString()).toEqual("3");
M
mahaifeng 已提交
326

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

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

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

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

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

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

  values() {
M
mahaifeng 已提交
364
    // #TEST Float32Array.values
M
mahaifeng 已提交
365
    let arr = new Float32Array([1, 2, 3]);
366 367
    let values = arr.values().next().value;
    console.log(values); // 1
M
mahaifeng 已提交
368
    // #END
369 370 371 372 373
    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 已提交
374 375 376
  }

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

385 386 387 388
    // let res = buffer.slice(8);
    // let sliced = new Float32Array(res);
    // console.log(sliced[1]); // 42
    // expect(sliced[1]).toEqual(42);
M
mahaifeng 已提交
389
  }
M
mahaifeng 已提交
390

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

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

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

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

413
  // #endif
414
}