TFloat64Array.uts 9.7 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 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
    // #END
M
mahaifeng 已提交
80 81 82
  }

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

  testFill() {
M
mahaifeng 已提交
90
    // #TEST Float64Array.fill
M
mahaifeng 已提交
91 92
    let float64 = new Float64Array([1, 2, 3]).fill(4);
    expect(float64.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
93

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

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

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

M
mahaifeng 已提交
103 104
    float64 = new Float64Array([1, 2, 3]).fill(4, -3, -2);
    expect(float64.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
105
    // #END
M
mahaifeng 已提交
106 107 108
  }

  testFilter() {
M
mahaifeng 已提交
109 110
    // #TEST Float64Array.filter
    let float64 = new Float64Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Float64Array) : boolean => value >= 10);
M
mahaifeng 已提交
111
    expect(float64.toString()).toEqual("12,44");
M
mahaifeng 已提交
112
    // #END
M
mahaifeng 已提交
113 114 115
  }

  find() {
M
mahaifeng 已提交
116
    // #TEST Float64Array.find
M
mahaifeng 已提交
117
    let float64 = new Float64Array([4, 5, 8, 12]);
M
mahaifeng 已提交
118
    let res = float64.find((value : number, _ : number, _a : Float64Array) : boolean => value > 5);
M
mahaifeng 已提交
119
    expect(res).toEqual(8);
M
mahaifeng 已提交
120
    // #END
M
mahaifeng 已提交
121 122 123
  }

  findIndex() {
M
mahaifeng 已提交
124
    // #TEST Float64Array.findIndex
M
mahaifeng 已提交
125
    let float64 = new Float64Array([4, 6, 8, 12]);
M
mahaifeng 已提交
126
    let res = float64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
127 128
    expect(res).toEqual(-1);

M
mahaifeng 已提交
129
    let ufloat64 = new Float64Array([4, 6, 7, 120]);
M
mahaifeng 已提交
130
    res = ufloat64.findIndex((value : number, _ : number, _a : Float64Array) : boolean => value > 100);
M
mahaifeng 已提交
131
    expect(res).toEqual(3);
M
mahaifeng 已提交
132
    // #END
M
mahaifeng 已提交
133 134
  }

M
mahaifeng 已提交
135

M
mahaifeng 已提交
136
  foreach() {
M
mahaifeng 已提交
137 138
    // #TEST Float64Array.forEach
    new Float64Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Float64Array) => {
M
mahaifeng 已提交
139 140
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
141
    // #END
M
mahaifeng 已提交
142 143 144
  }

  iterator() {
M
mahaifeng 已提交
145
    // #TEST Float64Array.entries
M
mahaifeng 已提交
146 147 148 149
    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 已提交
150
    // #END
M
mahaifeng 已提交
151 152 153
  }

  includes() {
M
mahaifeng 已提交
154
    // #TEST Float64Array.includes
M
mahaifeng 已提交
155 156
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.includes(2);
M
mahaifeng 已提交
157 158
    expect(res).toEqual(true);

M
mahaifeng 已提交
159
    res = float64.includes(4);
M
mahaifeng 已提交
160 161
    expect(res).toEqual(false);

M
mahaifeng 已提交
162
    res = float64.includes(3, 3);
M
mahaifeng 已提交
163
    expect(res).toEqual(false);
M
mahaifeng 已提交
164
    // #END
M
mahaifeng 已提交
165 166 167
  }

  indexOf() {
M
mahaifeng 已提交
168
    // #TEST Float64Array.indexOf
M
mahaifeng 已提交
169 170
    let float64 = new Float64Array([2, 5, 9]);
    let res = float64.indexOf(2);
M
mahaifeng 已提交
171 172
    expect(res).toEqual(0);

M
mahaifeng 已提交
173
    res = float64.indexOf(7);
M
mahaifeng 已提交
174 175
    expect(res).toEqual(-1);

M
mahaifeng 已提交
176
    res = float64.indexOf(9, 2);
M
mahaifeng 已提交
177 178
    expect(res).toEqual(2);

M
mahaifeng 已提交
179
    res = float64.indexOf(2, -1);
M
mahaifeng 已提交
180 181
    expect(res).toEqual(-1);

M
mahaifeng 已提交
182
    res = float64.indexOf(2, -3);
M
mahaifeng 已提交
183
    expect(res).toEqual(0);
M
mahaifeng 已提交
184
    // #END
M
mahaifeng 已提交
185 186 187
  }

  join() {
M
mahaifeng 已提交
188
    // #TEST Float64Array.join
M
mahaifeng 已提交
189 190
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.join();
M
mahaifeng 已提交
191 192
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
193
    res = float64.join(" / ");
M
mahaifeng 已提交
194 195
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
196
    res = float64.join("");
M
mahaifeng 已提交
197
    expect(res).toEqual("123");
M
mahaifeng 已提交
198
    // #END
M
mahaifeng 已提交
199 200
  }

M
mahaifeng 已提交
201

M
mahaifeng 已提交
202
  keys() {
M
mahaifeng 已提交
203
    // #TEST Float64Array.keys
M
mahaifeng 已提交
204 205 206 207 208 209 210
    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 已提交
211
    // #END
M
mahaifeng 已提交
212 213 214
  }

  map() {
M
mahaifeng 已提交
215
    // #TEST Float64Array.map
M
mahaifeng 已提交
216
    let numbers = new Float64Array([1, 4, 9]);
M
mahaifeng 已提交
217
    let doubles = numbers.map((value : number, _ : number, _a : Float64Array) : number => value * 2);
M
mahaifeng 已提交
218 219
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
220
    // #END
M
mahaifeng 已提交
221 222 223
  }

  reduce() {
M
mahaifeng 已提交
224
    // #TEST Float64Array.reduce
M
mahaifeng 已提交
225
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
226
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
227
    expect(res).toEqual(6);
M
mahaifeng 已提交
228 229

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
230
    res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Float64Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
231
    expect(res).toEqual(14);
M
mahaifeng 已提交
232
    // #END
M
mahaifeng 已提交
233 234 235
  }

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

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

  reverse() {
M
mahaifeng 已提交
248
    // #TEST Float64Array.reverse
M
mahaifeng 已提交
249 250 251
    let float64 = new Float64Array([1, 2, 3]);
    float64.reverse();
    expect(float64.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
252
    // #END
M
mahaifeng 已提交
253 254 255
  }

  slice() {
M
mahaifeng 已提交
256
    // #TEST Float64Array.slice
M
mahaifeng 已提交
257 258
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.slice(1);
M
mahaifeng 已提交
259 260
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
261
    res = float64.slice(2);
M
mahaifeng 已提交
262 263
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
264
    res = float64.slice(-2);
M
mahaifeng 已提交
265 266
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
267
    res = float64.slice(0, 1);
M
mahaifeng 已提交
268
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287



    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 已提交
288
    // #END
M
mahaifeng 已提交
289 290 291
  }

  sort() {
M
mahaifeng 已提交
292
    // #TEST Float64Array.sort
M
mahaifeng 已提交
293 294 295 296
    let numbers = new Float64Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

M
mahaifeng 已提交
297
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
298
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
299
    // #END
M
mahaifeng 已提交
300 301
  }

M
mahaifeng 已提交
302

M
mahaifeng 已提交
303
  subarray() {
M
mahaifeng 已提交
304
    // #TEST Float64Array.subarray
M
mahaifeng 已提交
305
    let buffer = new ArrayBuffer(32);
M
mahaifeng 已提交
306 307 308
    let float64 = new Float64Array(buffer);
    float64.set([1, 2, 3]);
    expect(float64.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
309

M
mahaifeng 已提交
310
    let sub = float64.subarray(0, 4);
M
mahaifeng 已提交
311
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
312 313 314 315 316 317

    const size = 1000;
    const initialFloat64Array = new Float64Array(size);
    for (let i = 0; i < size; ++i) {
      initialFloat64Array[i] = Math.random();
    }
M
mahaifeng 已提交
318 319
    let arr = new Float64Array(initialFloat64Array);
    let new_arr = arr.subarray(1, size - 1);
M
mahaifeng 已提交
320 321 322 323 324
    for (let i = 1; i < size - 1; ++i) {
      if (arr[i] !== new_arr[i - 1]) {
        expect(true).toEqual(false);
      }
    }
M
mahaifeng 已提交
325
    // #END
M
mahaifeng 已提交
326 327 328
  }

  values() {
M
mahaifeng 已提交
329
    // #TEST Float64Array.values
M
mahaifeng 已提交
330 331 332 333 334
    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 已提交
335
    // #END
M
mahaifeng 已提交
336 337 338
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
339

M
mahaifeng 已提交
340
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
341
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
342 343 344
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
    expect(float64.toString()).toEqual("0,42");
M
mahaifeng 已提交
345 346 347 348

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

M
mahaifeng 已提交
351
  }
M
mahaifeng 已提交
352 353 354 355
  testSome() {
    // #TEST Float64Array.some
    const float64 = new Float64Array([-10, 20, -30, 40, -50]);
    const positives = new Float64Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
356

M
mahaifeng 已提交
357 358 359 360 361 362 363 364 365 366
    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
  }
367
  // #endif
M
mahaifeng 已提交
368
}