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

export class TFloat64Array {
  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.testfloat64();
    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 41 42 43 44 45 46 47 48 49 50 51
  from() {
    // #TEST Float64Array.from
    var float64Array = Float64Array.from([1, 2, 3], (v : number, _ : number) : number => v + v);
    console.log(float64Array.toString()); // '2,4,6'
    // #END
    expect(float64Array.toString()).toEqual('2,4,6');
  }
  of() {
    // #TEST Float64Array.of
    var float64Array = Float64Array.of(1, 2, 3)
    console.log(float64Array.toString()); // '1,2,3'
    // #END
    expect(float64Array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
52
  testfloat64() {
M
mahaifeng 已提交
53 54 55 56
    let float64 = new Float64Array(2);
    float64[0] = 42;
    expect(float64[0]).toEqual(42);
    expect(float64.length).toEqual(2);
M
mahaifeng 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    expect(Float64Array.BYTES_PER_ELEMENT).toEqual(8);

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

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

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

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
73 74 75
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
    expect(float64.toString()).toEqual("0,42");
M
mahaifeng 已提交
76 77 78
  }

  testSet() {
M
mahaifeng 已提交
79
    // #TEST Float64Array.set
M
mahaifeng 已提交
80
    let float64 = new Float64Array(8);
M
mahaifeng 已提交
81
    var array = [1, 2, 3];
M
mahaifeng 已提交
82
    float64.set(array, 1);
M
mahaifeng 已提交
83
    console.log(float64.toString()); // 0,1,2,3,0,0,0,0
M
mahaifeng 已提交
84
    // #END
M
mahaifeng 已提交
85
    expect(float64.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
86 87 88
  }

  testCopyWith() {
M
mahaifeng 已提交
89
    // #TEST Float64Array.copyWithin
M
mahaifeng 已提交
90 91 92
    let float64 = new Float64Array(8);
    float64.set([1, 2, 3], 1);
    float64.copyWithin(3, 0, 3);
M
mahaifeng 已提交
93
    console.log(float64.toString()); // 0,1,2,0,1,2,0,0
M
mahaifeng 已提交
94
    // #END
M
mahaifeng 已提交
95
    expect(float64.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
96 97 98
  }

  testEvery() {
M
mahaifeng 已提交
99 100
    // #TEST Float64Array.every
    let result = new Float64Array([-10, -20, -30, -40, -50]).every((value : number, _ : number, _a : Float64Array) : boolean => value < 0);
M
mahaifeng 已提交
101
    console.log(result); // true
M
mahaifeng 已提交
102
    // #END
M
mahaifeng 已提交
103
    expect(result).toEqual(true);
M
mahaifeng 已提交
104 105 106
  }

  testFill() {
M
mahaifeng 已提交
107
    // #TEST Float64Array.fill
M
mahaifeng 已提交
108 109
    let float64_t1 = new Float64Array([1, 2, 3]).fill(4);
    console.log(float64_t1.toString()); // 4,4,4   
M
mahaifeng 已提交
110

M
mahaifeng 已提交
111 112
    let float64_t2 = new Float64Array([1, 2, 3]).fill(4, 1);
    console.log(float64_t2.toString()); // 1,4,4   
M
mahaifeng 已提交
113

M
mahaifeng 已提交
114 115
    let float64_t3 = new Float64Array([1, 2, 3]).fill(4, 1, 2);
    console.log(float64_t3.toString()); // 1,4,3    
M
mahaifeng 已提交
116

M
mahaifeng 已提交
117 118
    let float64_t4 = new Float64Array([1, 2, 3]).fill(4, 1, 1);
    console.log(float64_t4.toString()); // 1,2,3    
M
mahaifeng 已提交
119

M
mahaifeng 已提交
120 121
    let float64_t5 = new Float64Array([1, 2, 3]).fill(4, -3, -2);
    console.log(float64_t5.toString()); // 4,2,3
M
mahaifeng 已提交
122
    // #END
M
mahaifeng 已提交
123 124 125 126 127
    expect(float64_t4.toString()).toEqual("1,2,3");
    expect(float64_t3.toString()).toEqual("1,4,3");
    expect(float64_t2.toString()).toEqual("1,4,4");
    expect(float64_t1.toString()).toEqual("4,4,4");
    expect(float64_t5.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
128 129
  }

M
mahaifeng 已提交
130

M
mahaifeng 已提交
131
  testFilter() {
M
mahaifeng 已提交
132 133
    // #TEST Float64Array.filter
    let float64 = new Float64Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float64Array) : boolean => value >= 10);
M
mahaifeng 已提交
134
    console.log(float64.toString()); // 12,44
M
mahaifeng 已提交
135
    // #END
M
mahaifeng 已提交
136
    expect(float64.toString()).toEqual("12,44");
M
mahaifeng 已提交
137 138 139
  }

  find() {
M
mahaifeng 已提交
140
    // #TEST Float64Array.find
M
mahaifeng 已提交
141
    let float64 = new Float64Array([4, 5, 8, 12]);
M
mahaifeng 已提交
142
    let res = float64.find((value : number, _ : number, _a : Float64Array) : boolean => value > 5);
M
mahaifeng 已提交
143
    console.log(res); // 8
M
mahaifeng 已提交
144
    // #END
M
mahaifeng 已提交
145
    expect(res).toEqual(8);
M
mahaifeng 已提交
146 147 148
  }

  findIndex() {
M
mahaifeng 已提交
149
    // #TEST Float64Array.findIndex
M
mahaifeng 已提交
150
    let float64 = new Float64Array([4, 6, 8, 12]);
M
mahaifeng 已提交
151
    let res = float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
152
    console.log(res); // -1
M
mahaifeng 已提交
153

M
mahaifeng 已提交
154
    let ufloat64 = new Float64Array([4, 6, 7, 120]);
M
mahaifeng 已提交
155
    res = ufloat64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
156 157
    console.log(res); // 3

M
mahaifeng 已提交
158
    // #END
M
mahaifeng 已提交
159 160
    expect(res).toEqual(3);
    expect(float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100)).toEqual(-1);
M
mahaifeng 已提交
161 162
  }

M
mahaifeng 已提交
163

M
mahaifeng 已提交
164
  foreach() {
M
mahaifeng 已提交
165 166
    // #TEST Float64Array.forEach
    new Float64Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float64Array) => {
M
mahaifeng 已提交
167 168
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
169
    // #END
M
mahaifeng 已提交
170 171 172
  }

  iterator() {
M
mahaifeng 已提交
173
    // #TEST Float64Array.entries
M
mahaifeng 已提交
174 175
    let arr = new Float64Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
176 177
    console.log(entries.next().value[1]); // 10
    console.log(entries.next().value[1]); // 20
M
mahaifeng 已提交
178
    // #END
M
mahaifeng 已提交
179 180
    expect(entries.next().value[1]).toEqual(30);
    expect(entries.next().value[1]).toEqual(40);
M
mahaifeng 已提交
181 182 183
  }

  includes() {
M
mahaifeng 已提交
184
    // #TEST Float64Array.includes
M
mahaifeng 已提交
185
    let float64 = new Float64Array([1, 2, 3]);
M
mahaifeng 已提交
186

M
mahaifeng 已提交
187
    let res = float64.includes(2);
M
mahaifeng 已提交
188
    console.log(res); // true
M
mahaifeng 已提交
189

M
mahaifeng 已提交
190
    res = float64.includes(4);
M
mahaifeng 已提交
191
    console.log(res); // false
M
mahaifeng 已提交
192

M
mahaifeng 已提交
193
    res = float64.includes(3, 3);
M
mahaifeng 已提交
194
    console.log(res); // false
M
mahaifeng 已提交
195
    // #END
M
mahaifeng 已提交
196 197 198 199

    expect(res).toEqual(false);
    expect(float64.includes(4)).toEqual(false);
    expect(float64.includes(2)).toEqual(true);
M
mahaifeng 已提交
200 201
  }

M
mahaifeng 已提交
202

M
mahaifeng 已提交
203
  indexOf() {
M
mahaifeng 已提交
204
    // #TEST Float64Array.indexOf
M
mahaifeng 已提交
205 206
    let float64 = new Float64Array([2, 5, 9]);
    let res = float64.indexOf(2);
M
mahaifeng 已提交
207
    console.log(res); // 0
M
mahaifeng 已提交
208 209


M
mahaifeng 已提交
210 211
    let res1 = float64.indexOf(7);
    console.log(res1); // -1
M
mahaifeng 已提交
212

M
mahaifeng 已提交
213 214
    let res2 = float64.indexOf(9, 2);
    console.log(res2); // 2
M
mahaifeng 已提交
215

M
mahaifeng 已提交
216 217 218 219 220 221

    let res3 = float64.indexOf(2, -1);
    console.log(res3); // -1


    let res4 = float64.indexOf(2, -3);
M
mahaifeng 已提交
222
    console.log(res4); // 0
M
mahaifeng 已提交
223
    // #END
M
mahaifeng 已提交
224
    expect(res).toEqual(0);
M
mahaifeng 已提交
225
    expect(res4).toEqual(0);
M
mahaifeng 已提交
226 227 228
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
229 230 231
  }

  join() {
M
mahaifeng 已提交
232
    // #TEST Float64Array.join
M
mahaifeng 已提交
233 234
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.join();
M
mahaifeng 已提交
235
    console.log(res); // 1,2,3
M
mahaifeng 已提交
236 237


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


    let res2 = float64.join("");
    console.log(res2); // 123
M
mahaifeng 已提交
244
    // #END
M
mahaifeng 已提交
245 246 247
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
248 249 250
  }

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

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

  reduce() {
M
mahaifeng 已提交
276
    // #TEST Float64Array.reduce
M
mahaifeng 已提交
277
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
278
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
279 280
    console.log(res); // 6

M
mahaifeng 已提交
281 282

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
283 284
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
285
    // #END
M
mahaifeng 已提交
286 287
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
288 289 290
  }

  reduceRight() {
M
mahaifeng 已提交
291
    // #TEST Float64Array.reduceRight
M
mahaifeng 已提交
292
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
293
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
294 295
    console.log(res); // 6

M
mahaifeng 已提交
296 297

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
298 299
    let res1 = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
300
    // #END
M
mahaifeng 已提交
301 302
    expect(res).toEqual(6);
    expect(res1).toEqual(14);
M
mahaifeng 已提交
303 304 305
  }

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

  slice() {
M
mahaifeng 已提交
315
    // #TEST Float64Array.slice
M
mahaifeng 已提交
316 317
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.slice(1);
M
mahaifeng 已提交
318 319 320
    let ret1 = res.toString()
    console.log(ret1); // 2,3

M
mahaifeng 已提交
321

M
mahaifeng 已提交
322
    res = float64.slice(2);
M
mahaifeng 已提交
323 324
    let ret2 = res.toString()
    console.log(ret2); // 3
M
mahaifeng 已提交
325

M
mahaifeng 已提交
326
    res = float64.slice(-2);
M
mahaifeng 已提交
327 328
    let ret3 = res.toString()
    console.log(ret3); // 2,3   
M
mahaifeng 已提交
329

M
mahaifeng 已提交
330
    res = float64.slice(0, 1);
M
mahaifeng 已提交
331 332
    let ret4 = res.toString()
    console.log(ret4); // 1
M
mahaifeng 已提交
333 334 335 336 337 338 339 340 341 342 343

    const size = 1000;
    const initialFloat64Array = new Float64Array(size);
    for (let i = 0; i < size; ++i) {
      initialFloat64Array[i] = Math.random();
    }

    let arr : Float64Array;
    let new_arr : Float64Array;
    arr = new Float64Array(initialFloat64Array);
    new_arr = arr.slice(1, -1);
M
mahaifeng 已提交
344
    let ret5 = true
M
mahaifeng 已提交
345 346
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
M
mahaifeng 已提交
347 348
        ret5 = false
        break;
M
mahaifeng 已提交
349 350
      }
    }
M
mahaifeng 已提交
351
    console.log(ret5); //  true;
352

M
mahaifeng 已提交
353
    // #END
M
mahaifeng 已提交
354 355 356 357 358
    expect(ret4).toEqual("1");
    expect(ret3.toString()).toEqual("2,3");
    expect(ret2).toEqual("3");
    expect(ret1.toString()).toEqual("2,3");
    expect(true).toEqual(ret5);
M
mahaifeng 已提交
359 360 361
  }

  sort() {
M
mahaifeng 已提交
362
    // #TEST Float64Array.sort
M
mahaifeng 已提交
363 364
    let numbers = new Float64Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
365 366
    let ret = numbers.toString()
    console.log(ret); // 1,5,40
M
mahaifeng 已提交
367

M
mahaifeng 已提交
368
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
369
    console.log(numbers.toString()); // 1,5,40
M
mahaifeng 已提交
370
    // #END
M
mahaifeng 已提交
371 372
    expect(ret).toEqual("1,5,40");
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
373 374 375
  }

  subarray() {
M
mahaifeng 已提交
376
    // #TEST Float64Array.subarray
M
mahaifeng 已提交
377
    let buffer = new ArrayBuffer(32);
M
mahaifeng 已提交
378 379
    let float64 = new Float64Array(buffer);
    float64.set([1, 2, 3]);
M
mahaifeng 已提交
380
    console.log(float64.toString()); // 1,2,3,0
M
mahaifeng 已提交
381

M
mahaifeng 已提交
382
    let sub = float64.subarray(0, 4);
M
mahaifeng 已提交
383
    console.log(sub.toString()); // 1,2,3,0
M
mahaifeng 已提交
384 385 386 387 388 389

    const size = 1000;
    const initialFloat64Array = new Float64Array(size);
    for (let i = 0; i < size; ++i) {
      initialFloat64Array[i] = Math.random();
    }
M
mahaifeng 已提交
390 391
    let arr = new Float64Array(initialFloat64Array);
    let new_arr = arr.subarray(1, size - 1);
M
mahaifeng 已提交
392
    let ret = true
M
mahaifeng 已提交
393 394
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
M
mahaifeng 已提交
395 396
        ret = false
        break
M
mahaifeng 已提交
397 398
      }
    }
M
mahaifeng 已提交
399
    // #END
M
mahaifeng 已提交
400
    expect(true).toEqual(ret);
M
mahaifeng 已提交
401 402 403
  }

  values() {
M
mahaifeng 已提交
404
    // #TEST Float64Array.values
M
mahaifeng 已提交
405 406
    let arr = new Float64Array([1, 2, 3]);
    let values = arr.values();
407 408
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
409 410
    console.log(values.next().value); // 2
    console.log(values.next().value); // 3
M
mahaifeng 已提交
411
    // #END
412
    expect(value).toEqual(1);
M
mahaifeng 已提交
413 414 415
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
416
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
417
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
418 419
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
M
mahaifeng 已提交
420
    console.log(float64.toString()); // 0,42
M
mahaifeng 已提交
421 422 423

    let res = buffer.slice(8);
    let sliced = new Float64Array(res);
M
mahaifeng 已提交
424
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
425
    // #END
M
mahaifeng 已提交
426
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
427
  }
M
mahaifeng 已提交
428

M
mahaifeng 已提交
429 430 431 432
  testSome() {
    // #TEST Float64Array.some
    const float64 = new Float64Array([-10, 20, -30, 40, -50]);
    const positives = new Float64Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
433

M
mahaifeng 已提交
434 435 436 437 438 439 440 441
    console.log(float64.some((element : number, index : number, array : Float64Array) : boolean =>
      element < 0
    )); // true

    console.log(positives.some((element : number, index : number, array : Float64Array) : boolean =>
      element < 0
    )); // false
    // #END
M
mahaifeng 已提交
442 443 444 445 446 447 448 449
    expect(float64.some((element : number, index : number, array : Float64Array) : boolean =>
      element < 0
    )).toEqual(true);

    expect(positives.some((element : number, index : number, array : Float64Array) : boolean =>
      element < 0
    )).toEqual(false);
  }
M
mahaifeng 已提交
450

451
  // #endif
M
mahaifeng 已提交
452
}