TFloat64Array.uts 11.8 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
  testfloat64() {
M
mahaifeng 已提交
39 40 41 42
    let float64 = new Float64Array(2);
    float64[0] = 42;
    expect(float64[0]).toEqual(42);
    expect(float64.length).toEqual(2);
M
mahaifeng 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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 已提交
59 60 61
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
    expect(float64.toString()).toEqual("0,42");
M
mahaifeng 已提交
62 63 64
  }

  testSet() {
M
mahaifeng 已提交
65
    // #TEST Float64Array.set
M
mahaifeng 已提交
66
    let float64 = new Float64Array(8);
M
mahaifeng 已提交
67
    var array = [1, 2, 3];
M
mahaifeng 已提交
68
    float64.set(array, 1);
M
mahaifeng 已提交
69
    console.log(float64.toString()); // 0,1,2,3,0,0,0,0
M
mahaifeng 已提交
70
    // #END
M
mahaifeng 已提交
71
    expect(float64.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
72 73 74
  }

  testCopyWith() {
M
mahaifeng 已提交
75
    // #TEST Float64Array.copyWithin
M
mahaifeng 已提交
76 77 78
    let float64 = new Float64Array(8);
    float64.set([1, 2, 3], 1);
    float64.copyWithin(3, 0, 3);
M
mahaifeng 已提交
79
    console.log(float64.toString()); // 0,1,2,0,1,2,0,0
M
mahaifeng 已提交
80
    // #END
M
mahaifeng 已提交
81
    expect(float64.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
82 83 84
  }

  testEvery() {
M
mahaifeng 已提交
85 86
    // #TEST Float64Array.every
    let result = new Float64Array([-10, -20, -30, -40, -50]).every((value : number, _ : number, _a : Float64Array) : boolean => value < 0);
M
mahaifeng 已提交
87
    console.log(result); // true
M
mahaifeng 已提交
88
    // #END
M
mahaifeng 已提交
89
    expect(result).toEqual(true);
M
mahaifeng 已提交
90 91 92
  }

  testFill() {
M
mahaifeng 已提交
93
    // #TEST Float64Array.fill
M
mahaifeng 已提交
94 95
    let float64_t1 = new Float64Array([1, 2, 3]).fill(4);
    console.log(float64_t1.toString()); // 4,4,4   
M
mahaifeng 已提交
96

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

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

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

M
mahaifeng 已提交
106 107
    let float64_t5 = new Float64Array([1, 2, 3]).fill(4, -3, -2);
    console.log(float64_t5.toString()); // 4,2,3
M
mahaifeng 已提交
108
    // #END
M
mahaifeng 已提交
109 110 111 112 113
    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 已提交
114 115
  }

M
mahaifeng 已提交
116

M
mahaifeng 已提交
117
  testFilter() {
M
mahaifeng 已提交
118 119
    // #TEST Float64Array.filter
    let float64 = new Float64Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float64Array) : boolean => value >= 10);
M
mahaifeng 已提交
120
    console.log(float64.toString()); // 12,44
M
mahaifeng 已提交
121
    // #END
M
mahaifeng 已提交
122
    expect(float64.toString()).toEqual("12,44");
M
mahaifeng 已提交
123 124 125
  }

  find() {
M
mahaifeng 已提交
126
    // #TEST Float64Array.find
M
mahaifeng 已提交
127
    let float64 = new Float64Array([4, 5, 8, 12]);
M
mahaifeng 已提交
128
    let res = float64.find((value : number, _ : number, _a : Float64Array) : boolean => value > 5);
M
mahaifeng 已提交
129
    console.log(res); // 8
M
mahaifeng 已提交
130
    // #END
M
mahaifeng 已提交
131
    expect(res).toEqual(8);
M
mahaifeng 已提交
132 133 134
  }

  findIndex() {
M
mahaifeng 已提交
135
    // #TEST Float64Array.findIndex
M
mahaifeng 已提交
136
    let float64 = new Float64Array([4, 6, 8, 12]);
M
mahaifeng 已提交
137
    let res = float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
138
    console.log(res); // -1
M
mahaifeng 已提交
139

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

M
mahaifeng 已提交
144
    // #END
M
mahaifeng 已提交
145 146
    expect(res).toEqual(3);
    expect(float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100)).toEqual(-1);
M
mahaifeng 已提交
147 148
  }

M
mahaifeng 已提交
149

M
mahaifeng 已提交
150
  foreach() {
M
mahaifeng 已提交
151 152
    // #TEST Float64Array.forEach
    new Float64Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float64Array) => {
M
mahaifeng 已提交
153 154
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
155
    // #END
M
mahaifeng 已提交
156 157 158
  }

  iterator() {
M
mahaifeng 已提交
159
    // #TEST Float64Array.entries
M
mahaifeng 已提交
160 161
    let arr = new Float64Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
162 163
    console.log(entries.next().value[1]); // 10
    console.log(entries.next().value[1]); // 20
M
mahaifeng 已提交
164
    // #END
M
mahaifeng 已提交
165 166
    expect(entries.next().value[1]).toEqual(30);
    expect(entries.next().value[1]).toEqual(40);
M
mahaifeng 已提交
167 168 169
  }

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

M
mahaifeng 已提交
173
    let res = float64.includes(2);
M
mahaifeng 已提交
174
    console.log(res); // true
M
mahaifeng 已提交
175

M
mahaifeng 已提交
176
    res = float64.includes(4);
M
mahaifeng 已提交
177
    console.log(res); // false
M
mahaifeng 已提交
178

M
mahaifeng 已提交
179
    res = float64.includes(3, 3);
M
mahaifeng 已提交
180
    console.log(res); // false
M
mahaifeng 已提交
181
    // #END
M
mahaifeng 已提交
182 183 184 185

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

M
mahaifeng 已提交
188

M
mahaifeng 已提交
189
  indexOf() {
M
mahaifeng 已提交
190
    // #TEST Float64Array.indexOf
M
mahaifeng 已提交
191 192
    let float64 = new Float64Array([2, 5, 9]);
    let res = float64.indexOf(2);
M
mahaifeng 已提交
193
    console.log(res); // 0
M
mahaifeng 已提交
194 195


M
mahaifeng 已提交
196 197
    let res1 = float64.indexOf(7);
    console.log(res1); // -1
M
mahaifeng 已提交
198

M
mahaifeng 已提交
199 200
    let res2 = float64.indexOf(9, 2);
    console.log(res2); // 2
M
mahaifeng 已提交
201

M
mahaifeng 已提交
202 203 204 205 206 207 208

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


    let res4 = float64.indexOf(2, -3);
    console.log(res); // 0
M
mahaifeng 已提交
209
    // #END
M
mahaifeng 已提交
210 211 212 213 214
    expect(res).toEqual(0);
    expect(res).toEqual(0);
    expect(res1).toEqual(-1);
    expect(res2).toEqual(2);
    expect(res3).toEqual(-1);
M
mahaifeng 已提交
215 216 217
  }

  join() {
M
mahaifeng 已提交
218
    // #TEST Float64Array.join
M
mahaifeng 已提交
219 220
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.join();
M
mahaifeng 已提交
221
    console.log(res); // 1,2,3
M
mahaifeng 已提交
222 223


M
mahaifeng 已提交
224 225 226 227 228 229
    let res1 = float64.join(" / ");
    console.log(res1); // 1 / 2 / 3


    let res2 = float64.join("");
    console.log(res2); // 123
M
mahaifeng 已提交
230
    // #END
M
mahaifeng 已提交
231 232 233
    expect(res2).toEqual("123");
    expect(res).toEqual("1,2,3");
    expect(res1).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
234 235 236
  }

  keys() {
M
mahaifeng 已提交
237
    // #TEST Float64Array.keys
M
mahaifeng 已提交
238 239
    let arr = new Float64Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
240 241
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
242 243 244 245
    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 已提交
246
    // #END
247
    expect(value).toEqual(0);
M
mahaifeng 已提交
248 249 250
  }

  map() {
M
mahaifeng 已提交
251
    // #TEST Float64Array.map
M
mahaifeng 已提交
252
    let numbers = new Float64Array([1, 4, 9]);
M
mahaifeng 已提交
253
    let doubles = numbers.map((value : number, _ : number, _a : Float64Array) : number => value * 2);
M
mahaifeng 已提交
254 255 256
    console.log(numbers.toString()); // 1,4,9
    console.log(doubles.toString()); // 2,8,18
    // #END
M
mahaifeng 已提交
257 258 259 260 261
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
M
mahaifeng 已提交
262
    // #TEST Float64Array.reduce
M
mahaifeng 已提交
263
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
264
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
265 266
    console.log(res); // 6

M
mahaifeng 已提交
267 268

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
269 270
    let res1 = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
271
    // #END
M
mahaifeng 已提交
272 273
    expect(res1).toEqual(14);
    expect(res).toEqual(6);
M
mahaifeng 已提交
274 275 276
  }

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

M
mahaifeng 已提交
282 283

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

  reverse() {
M
mahaifeng 已提交
292
    // #TEST Float64Array.reverse
M
mahaifeng 已提交
293 294
    let float64 = new Float64Array([1, 2, 3]);
    float64.reverse();
M
mahaifeng 已提交
295
    console.log(float64.toString()); // 3,2,1
M
mahaifeng 已提交
296
    // #END
M
mahaifeng 已提交
297
    expect(float64.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
298 299 300
  }

  slice() {
M
mahaifeng 已提交
301
    // #TEST Float64Array.slice
M
mahaifeng 已提交
302 303
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.slice(1);
M
mahaifeng 已提交
304 305 306
    let ret1 = res.toString()
    console.log(ret1); // 2,3

M
mahaifeng 已提交
307

M
mahaifeng 已提交
308
    res = float64.slice(2);
M
mahaifeng 已提交
309 310
    let ret2 = res.toString()
    console.log(ret2); // 3
M
mahaifeng 已提交
311

M
mahaifeng 已提交
312
    res = float64.slice(-2);
M
mahaifeng 已提交
313 314
    let ret3 = res.toString()
    console.log(ret3); // 2,3   
M
mahaifeng 已提交
315

M
mahaifeng 已提交
316
    res = float64.slice(0, 1);
M
mahaifeng 已提交
317 318
    let ret4 = res.toString()
    console.log(ret4); // 1
M
mahaifeng 已提交
319 320 321 322 323 324 325 326 327 328 329

    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 已提交
330
    let ret5 = true
M
mahaifeng 已提交
331 332
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
M
mahaifeng 已提交
333 334
        ret5 = false
        break;
M
mahaifeng 已提交
335 336
      }
    }
M
mahaifeng 已提交
337
    console.log(ret5); //  true;
338

M
mahaifeng 已提交
339
    // #END
M
mahaifeng 已提交
340 341 342 343 344
    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 已提交
345 346 347
  }

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

M
mahaifeng 已提交
354
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
355
    console.log(numbers.toString()); // 1,5,40
M
mahaifeng 已提交
356
    // #END
M
mahaifeng 已提交
357 358
    expect(ret).toEqual("1,5,40");
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
359 360 361
  }

  subarray() {
M
mahaifeng 已提交
362
    // #TEST Float64Array.subarray
M
mahaifeng 已提交
363
    let buffer = new ArrayBuffer(32);
M
mahaifeng 已提交
364 365
    let float64 = new Float64Array(buffer);
    float64.set([1, 2, 3]);
M
mahaifeng 已提交
366
    console.log(float64.toString()); // 1,2,3,0
M
mahaifeng 已提交
367

M
mahaifeng 已提交
368
    let sub = float64.subarray(0, 4);
M
mahaifeng 已提交
369
    console.log(sub.toString()); // 1,2,3,0
M
mahaifeng 已提交
370 371 372 373 374 375

    const size = 1000;
    const initialFloat64Array = new Float64Array(size);
    for (let i = 0; i < size; ++i) {
      initialFloat64Array[i] = Math.random();
    }
M
mahaifeng 已提交
376 377
    let arr = new Float64Array(initialFloat64Array);
    let new_arr = arr.subarray(1, size - 1);
M
mahaifeng 已提交
378
    let ret = true
M
mahaifeng 已提交
379 380
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
M
mahaifeng 已提交
381 382
        ret = false
        break
M
mahaifeng 已提交
383 384
      }
    }
M
mahaifeng 已提交
385
    // #END
M
mahaifeng 已提交
386
    expect(true).toEqual(ret);
M
mahaifeng 已提交
387 388 389
  }

  values() {
M
mahaifeng 已提交
390
    // #TEST Float64Array.values
M
mahaifeng 已提交
391 392
    let arr = new Float64Array([1, 2, 3]);
    let values = arr.values();
393 394
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
395 396
    console.log(values.next().value); // 2
    console.log(values.next().value); // 3
M
mahaifeng 已提交
397
    // #END
398
    expect(value).toEqual(1);
M
mahaifeng 已提交
399 400 401
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
402
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
403
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
404 405
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
M
mahaifeng 已提交
406
    console.log(float64.toString()); // 0,42
M
mahaifeng 已提交
407 408 409

    let res = buffer.slice(8);
    let sliced = new Float64Array(res);
M
mahaifeng 已提交
410
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
411
    // #END
M
mahaifeng 已提交
412
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
413
  }
M
mahaifeng 已提交
414

M
mahaifeng 已提交
415 416 417 418
  testSome() {
    // #TEST Float64Array.some
    const float64 = new Float64Array([-10, 20, -30, 40, -50]);
    const positives = new Float64Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
419

M
mahaifeng 已提交
420 421 422 423 424 425 426 427
    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 已提交
428 429 430 431 432 433 434 435
    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 已提交
436

437
  // #endif
M
mahaifeng 已提交
438
}