TFloat64Array.uts 10.1 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
    // #ifndef APP-IOS
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
  // #ifndef APP-IOS
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 69
    float64.set(array, 1);
    expect(float64.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
70
    // #END
M
mahaifeng 已提交
71 72 73
  }

  testCopyWith() {
M
mahaifeng 已提交
74
    // #TEST Float64Array.copyWithin
M
mahaifeng 已提交
75 76 77 78
    let float64 = new Float64Array(8);
    float64.set([1, 2, 3], 1);
    float64.copyWithin(3, 0, 3);
    expect(float64.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91

    let initialLargeArray : Array<number> = new Array(10000, 0);
    for (let i = 0; i < 5000; ++i) {
      initialLargeArray[i] = i;
    }
    let initialLargeFloat64Array = new Float64Array(initialLargeArray);
    let largeFloat64Array : Float64Array = new Float64Array(initialLargeFloat64Array);
    largeFloat64Array.copyWithin(5000, 0);
    for (let i = 0; i < 5000; ++i) {
      if (largeFloat64Array[i + 5000] !== i) {
        expect(true).toEqual(false);
      }
    }
M
mahaifeng 已提交
92
    // #END
M
mahaifeng 已提交
93 94 95
  }

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

  testFill() {
M
mahaifeng 已提交
103
    // #TEST Float64Array.fill
M
mahaifeng 已提交
104 105
    let float64 = new Float64Array([1, 2, 3]).fill(4);
    expect(float64.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
106

M
mahaifeng 已提交
107 108
    float64 = new Float64Array([1, 2, 3]).fill(4, 1);
    expect(float64.toString()).toEqual("1,4,4");
M
mahaifeng 已提交
109

M
mahaifeng 已提交
110 111
    float64 = new Float64Array([1, 2, 3]).fill(4, 1, 2);
    expect(float64.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
112

M
mahaifeng 已提交
113 114
    float64 = new Float64Array([1, 2, 3]).fill(4, 1, 1);
    expect(float64.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
115

M
mahaifeng 已提交
116 117
    float64 = new Float64Array([1, 2, 3]).fill(4, -3, -2);
    expect(float64.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
118
    // #END
M
mahaifeng 已提交
119 120 121
  }

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

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

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

M
mahaifeng 已提交
142
    let ufloat64 = new Float64Array([4, 6, 7, 120]);
M
mahaifeng 已提交
143
    res = ufloat64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
144
    expect(res).toEqual(3);
M
mahaifeng 已提交
145
    // #END
M
mahaifeng 已提交
146 147
  }

M
mahaifeng 已提交
148

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

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

  includes() {
M
mahaifeng 已提交
167
    // #TEST Float64Array.includes
M
mahaifeng 已提交
168 169
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.includes(2);
M
mahaifeng 已提交
170 171
    expect(res).toEqual(true);

M
mahaifeng 已提交
172
    res = float64.includes(4);
M
mahaifeng 已提交
173 174
    expect(res).toEqual(false);

M
mahaifeng 已提交
175
    res = float64.includes(3, 3);
M
mahaifeng 已提交
176
    expect(res).toEqual(false);
M
mahaifeng 已提交
177
    // #END
M
mahaifeng 已提交
178 179 180
  }

  indexOf() {
M
mahaifeng 已提交
181
    // #TEST Float64Array.indexOf
M
mahaifeng 已提交
182 183
    let float64 = new Float64Array([2, 5, 9]);
    let res = float64.indexOf(2);
M
mahaifeng 已提交
184 185
    expect(res).toEqual(0);

M
mahaifeng 已提交
186
    res = float64.indexOf(7);
M
mahaifeng 已提交
187 188
    expect(res).toEqual(-1);

M
mahaifeng 已提交
189
    res = float64.indexOf(9, 2);
M
mahaifeng 已提交
190 191
    expect(res).toEqual(2);

M
mahaifeng 已提交
192
    res = float64.indexOf(2, -1);
M
mahaifeng 已提交
193 194
    expect(res).toEqual(-1);

M
mahaifeng 已提交
195
    res = float64.indexOf(2, -3);
M
mahaifeng 已提交
196
    expect(res).toEqual(0);
M
mahaifeng 已提交
197
    // #END
M
mahaifeng 已提交
198 199 200
  }

  join() {
M
mahaifeng 已提交
201
    // #TEST Float64Array.join
M
mahaifeng 已提交
202 203
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.join();
M
mahaifeng 已提交
204 205
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
206
    res = float64.join(" / ");
M
mahaifeng 已提交
207 208
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
209
    res = float64.join("");
M
mahaifeng 已提交
210
    expect(res).toEqual("123");
M
mahaifeng 已提交
211
    // #END
M
mahaifeng 已提交
212 213
  }

M
mahaifeng 已提交
214

M
mahaifeng 已提交
215
  keys() {
M
mahaifeng 已提交
216
    // #TEST Float64Array.keys
M
mahaifeng 已提交
217 218 219 220 221 222 223
    let arr = new Float64Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
    expect(keys.next().value).toEqual(0);
    expect(keys.next().value).toEqual(1);
    expect(keys.next().value).toEqual(2);
    expect(keys.next().value).toEqual(3);
    expect(keys.next().value).toEqual(4);
M
mahaifeng 已提交
224
    // #END
M
mahaifeng 已提交
225 226 227
  }

  map() {
M
mahaifeng 已提交
228
    // #TEST Float64Array.map
M
mahaifeng 已提交
229
    let numbers = new Float64Array([1, 4, 9]);
M
mahaifeng 已提交
230
    let doubles = numbers.map((value : number, _ : number, _a : Float64Array) : number => value * 2);
M
mahaifeng 已提交
231 232
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
233
    // #END
M
mahaifeng 已提交
234 235 236
  }

  reduce() {
M
mahaifeng 已提交
237
    // #TEST Float64Array.reduce
M
mahaifeng 已提交
238
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
239
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
240
    expect(res).toEqual(6);
M
mahaifeng 已提交
241 242

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
243
    res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
244
    expect(res).toEqual(14);
M
mahaifeng 已提交
245
    // #END
M
mahaifeng 已提交
246 247 248
  }

  reduceRight() {
M
mahaifeng 已提交
249
    // #TEST Float64Array.reduceRight
M
mahaifeng 已提交
250
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
251
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
252
    expect(res).toEqual(6);
M
mahaifeng 已提交
253 254

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
255
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
256
    expect(res).toEqual(14);
M
mahaifeng 已提交
257
    // #END
M
mahaifeng 已提交
258 259 260
  }

  reverse() {
M
mahaifeng 已提交
261
    // #TEST Float64Array.reverse
M
mahaifeng 已提交
262 263 264
    let float64 = new Float64Array([1, 2, 3]);
    float64.reverse();
    expect(float64.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
265
    // #END
M
mahaifeng 已提交
266 267 268
  }

  slice() {
M
mahaifeng 已提交
269
    // #TEST Float64Array.slice
M
mahaifeng 已提交
270 271
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.slice(1);
M
mahaifeng 已提交
272 273
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
274
    res = float64.slice(2);
M
mahaifeng 已提交
275 276
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
277
    res = float64.slice(-2);
M
mahaifeng 已提交
278 279
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
280
    res = float64.slice(0, 1);
M
mahaifeng 已提交
281
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300



    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);
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
        expect(true).toEqual(false);
        break
      }
    }
M
mahaifeng 已提交
301
    // #END
M
mahaifeng 已提交
302 303 304
  }

  sort() {
M
mahaifeng 已提交
305
    // #TEST Float64Array.sort
M
mahaifeng 已提交
306 307 308 309
    let numbers = new Float64Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

M
mahaifeng 已提交
310
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
311
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
312
    // #END
M
mahaifeng 已提交
313 314
  }

M
mahaifeng 已提交
315

M
mahaifeng 已提交
316
  subarray() {
M
mahaifeng 已提交
317
    // #TEST Float64Array.subarray
M
mahaifeng 已提交
318
    let buffer = new ArrayBuffer(32);
M
mahaifeng 已提交
319 320 321
    let float64 = new Float64Array(buffer);
    float64.set([1, 2, 3]);
    expect(float64.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
322

M
mahaifeng 已提交
323
    let sub = float64.subarray(0, 4);
M
mahaifeng 已提交
324
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337

    const size = 1000;
    const initialFloat64Array = new Float64Array(size);
    for (let i = 0; i < size; ++i) {
      initialFloat64Array[i] = Math.random();
    }
    let arr  = new Float64Array(initialFloat64Array);
    let new_arr  = arr.subarray(1, size - 1);
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
        expect(true).toEqual(false);
      }
    }
M
mahaifeng 已提交
338
    // #END
M
mahaifeng 已提交
339 340 341
  }

  values() {
M
mahaifeng 已提交
342
    // #TEST Float64Array.values
M
mahaifeng 已提交
343 344 345 346 347
    let arr = new Float64Array([1, 2, 3]);
    let values = arr.values();
    expect(values.next().value).toEqual(1);
    expect(values.next().value).toEqual(2);
    expect(values.next().value).toEqual(3);
M
mahaifeng 已提交
348
    // #END
M
mahaifeng 已提交
349 350 351
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
352

M
mahaifeng 已提交
353
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
354
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
355 356 357
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
    expect(float64.toString()).toEqual("0,42");
M
mahaifeng 已提交
358 359 360 361

    let res = buffer.slice(8);
    let sliced = new Float64Array(res);
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
362
    // #END
M
mahaifeng 已提交
363

M
mahaifeng 已提交
364
  }
M
mahaifeng 已提交
365 366 367 368
  testSome() {
    // #TEST Float64Array.some
    const float64 = new Float64Array([-10, 20, -30, 40, -50]);
    const positives = new Float64Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
369

M
mahaifeng 已提交
370 371 372 373 374 375 376 377 378 379
    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);
    // #END
  }
380
  // #endif
M
mahaifeng 已提交
381
}