TFloat64Array.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 TFloat64Array {
  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.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 (UNI-APP-X && 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();
M
mahaifeng 已提交
240 241 242 243 244
    console.log(keys.next().value); // 0
    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 已提交
245
    // #END
M
mahaifeng 已提交
246
    expect(keys.next().value).toEqual(-1);
M
mahaifeng 已提交
247 248 249
  }

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

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

M
mahaifeng 已提交
266 267

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

  reduceRight() {
M
mahaifeng 已提交
276
    // #TEST Float64Array.reduceRight
M
mahaifeng 已提交
277
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
278
    let res = total.reduceRight((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.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
    console.log(res1); // 14
M
mahaifeng 已提交
285
    // #END
M
mahaifeng 已提交
286 287
    expect(res).toEqual(6);
    expect(res1).toEqual(14);
M
mahaifeng 已提交
288 289 290
  }

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

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

M
mahaifeng 已提交
306

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

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

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

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

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

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

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

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

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

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

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

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

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

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

435
  // #endif
M
mahaifeng 已提交
436
}