TFloat64Array.uts 7.6 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() {
10
    // #ifdef APP-ANDROID
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
  }
37
// #ifdef APP-ANDROID
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
    let float64 = new Float64Array(8);
M
mahaifeng 已提交
66
    var array = [1, 2, 3]
M
mahaifeng 已提交
67 68
    float64.set(array, 1);
    expect(float64.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
69 70 71
  }

  testCopyWith() {
M
mahaifeng 已提交
72 73 74 75
    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 已提交
76 77 78 79
  }

  testEvery() {
    // const isBelowThreshold = (currentValue: number, index: number, array:Float64Array): boolean => currentValue < 40;    
M
mahaifeng 已提交
80
    let result = new Float64Array([-10, -20, -30, -40, -50]).every((value: number, _: number, _a:
M
mahaifeng 已提交
81 82 83 84 85
      Float64Array): boolean => value < 0); // 
    expect(result).toEqual(true);
  }

  testFill() {
M
mahaifeng 已提交
86 87
    let float64 = new Float64Array([1, 2, 3]).fill(4);
    expect(float64.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
88

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

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

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

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

  testFilter() {
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;

M
mahaifeng 已提交
105
    let float64 = new Float64Array([12, 5, 8, 44]).filter((value: number, _: number, _a: Float64Array):
M
mahaifeng 已提交
106
      boolean => value >= 10);
M
mahaifeng 已提交
107
    expect(float64.toString()).toEqual("12,44");
M
mahaifeng 已提交
108 109 110
  }

  find() {
M
mahaifeng 已提交
111 112
    let float64 = new Float64Array([4, 5, 8, 12]);
    let res = float64.find((value: number, _: number, _a: Float64Array): boolean => value > 5);
M
mahaifeng 已提交
113 114 115 116
    expect(res).toEqual(8);
  }

  findIndex() {
M
mahaifeng 已提交
117 118
    let float64 = new Float64Array([4, 6, 8, 12]);
    let res = float64.findIndex((value: number, _: number, _a: Float64Array): boolean => value > 100);
M
mahaifeng 已提交
119 120
    expect(res).toEqual(-1);

M
mahaifeng 已提交
121 122
    let ufloat64 = new Float64Array([4, 6, 7, 120]);
    res = ufloat64.findIndex((value: number, _: number, _a: Float64Array): boolean => value > 100);
M
mahaifeng 已提交
123 124 125 126
    expect(res).toEqual(3);
  }

  foreach() {
M
mahaifeng 已提交
127
    new Float64Array([0, 1, 2, 3]).forEach((value: number, index: number, _: Float64Array) => {
M
mahaifeng 已提交
128 129 130 131 132 133 134 135 136 137 138 139
      console.log(`a[${index}] = ${value}`);
    });
  }

  iterator() {
    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);
  }

  includes() {
M
mahaifeng 已提交
140 141
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.includes(2);
M
mahaifeng 已提交
142 143
    expect(res).toEqual(true);

M
mahaifeng 已提交
144
    res = float64.includes(4);
M
mahaifeng 已提交
145 146
    expect(res).toEqual(false);

M
mahaifeng 已提交
147
    res = float64.includes(3, 3);
M
mahaifeng 已提交
148 149 150 151
    expect(res).toEqual(false);
  }

  indexOf() {
M
mahaifeng 已提交
152 153
    let float64 = new Float64Array([2, 5, 9]);
    let res = float64.indexOf(2);
M
mahaifeng 已提交
154 155
    expect(res).toEqual(0);

M
mahaifeng 已提交
156
    res = float64.indexOf(7);
M
mahaifeng 已提交
157 158
    expect(res).toEqual(-1);

M
mahaifeng 已提交
159
    res = float64.indexOf(9, 2);
M
mahaifeng 已提交
160 161
    expect(res).toEqual(2);

M
mahaifeng 已提交
162
    res = float64.indexOf(2, -1);
M
mahaifeng 已提交
163 164
    expect(res).toEqual(-1);

M
mahaifeng 已提交
165
    res = float64.indexOf(2, -3);
M
mahaifeng 已提交
166 167 168 169
    expect(res).toEqual(0);
  }

  join() {
M
mahaifeng 已提交
170 171
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.join();
M
mahaifeng 已提交
172 173
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
174
    res = float64.join(" / ");
M
mahaifeng 已提交
175 176
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
177
    res = float64.join("");
M
mahaifeng 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    expect(res).toEqual("123");
  }

  keys() {
    //todo 有差异 js 是keys.next().value
    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);
  }

  map() {
    let numbers = new Float64Array([1, 4, 9]);
M
mahaifeng 已提交
194
    let doubles = numbers.map((value: number, _: number, _a: Float64Array): number => value * 2);
M
mahaifeng 已提交
195 196 197 198 199 200
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
201
    let res = total.reduce((accumulator: number, currentValue: number, _: number, _a: Float64Array):
M
mahaifeng 已提交
202
      number => accumulator + currentValue);
M
mahaifeng 已提交
203
    expect(res).toEqual(6);
M
mahaifeng 已提交
204 205

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
206
    res = total.reduce((accumulator: number, currentValue: number, _: number, _a: Float64Array):
M
mahaifeng 已提交
207
      number => accumulator + currentValue, 8);
M
mahaifeng 已提交
208
    expect(res).toEqual(14);
M
mahaifeng 已提交
209 210 211 212
  }

  reduceRight() {
    let total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
213
    let res = total.reduceRight((accumulator: number, currentValue: number, _: number, _a:
M
mahaifeng 已提交
214
      Float64Array): number => accumulator + currentValue);
M
mahaifeng 已提交
215
    expect(res).toEqual(6);
M
mahaifeng 已提交
216 217

    total = new Float64Array([0, 1, 2, 3]);
M
mahaifeng 已提交
218
    res = total.reduceRight((accumulator: number, currentValue: number, _: number, _a: Float64Array):
M
mahaifeng 已提交
219
      number => accumulator + currentValue, 8);
M
mahaifeng 已提交
220
    expect(res).toEqual(14);
M
mahaifeng 已提交
221 222 223
  }

  reverse() {
M
mahaifeng 已提交
224 225 226
    let float64 = new Float64Array([1, 2, 3]);
    float64.reverse();
    expect(float64.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
227 228 229
  }

  slice() {
M
mahaifeng 已提交
230 231
    let float64 = new Float64Array([1, 2, 3]);
    let res = float64.slice(1);
M
mahaifeng 已提交
232 233
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
234
    res = float64.slice(2);
M
mahaifeng 已提交
235 236
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
237
    res = float64.slice(-2);
M
mahaifeng 已提交
238 239
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
240
    res = float64.slice(0, 1);
M
mahaifeng 已提交
241 242 243 244 245 246 247 248
    expect(res.toString()).toEqual("1");
  }

  sort() {
    let numbers = new Float64Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

249
    numbers.sort((a, b): number => a - b);
M
mahaifeng 已提交
250 251 252 253 254
    expect(numbers.toString()).toEqual("1,5,40");
  }

  subarray() {
    let buffer = new ArrayBuffer(32);
M
mahaifeng 已提交
255 256 257
    let float64 = new Float64Array(buffer);
    float64.set([1, 2, 3]);
    expect(float64.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
258

M
mahaifeng 已提交
259
    let sub = float64.subarray(0, 4);
M
mahaifeng 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272
    expect(sub.toString()).toEqual("1,2,3,0");
  }

  values() {
    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);
  }

  arrayBufferSlice() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
273 274 275
    let float64 = new Float64Array(buffer);
    float64[1] = 42;
    expect(float64.toString()).toEqual("0,42");
M
mahaifeng 已提交
276 277 278 279 280

    let res = buffer.slice(8);
    let sliced = new Float64Array(res);
    expect(sliced[0]).toEqual(42);
  }
281
  // #endif
M
mahaifeng 已提交
282
}