TInt16Array.uts 7.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 TInt16Array {
  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
    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();
M
mahaifeng 已提交
34
    // #endif
M
mahaifeng 已提交
35 36
  }

M
mahaifeng 已提交
37 38
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

M
mahaifeng 已提交
39 40
  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
41 42 43
    let int16 = new Int16Array(buffer);
    int16[5] = 42;
    expect(int16.toString()).toEqual("0,0,0,0,0,42,0,0");
M
mahaifeng 已提交
44 45 46
  }

  testSet() {
M
mahaifeng 已提交
47
    // #TEST Int16Array.set
M
mahaifeng 已提交
48
    let int16 = new Int16Array(8);
M
mahaifeng 已提交
49
    var array = [1, 2, 3];
M
mahaifeng 已提交
50 51
    int16.set(array, 1);
    expect(int16.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
52
    // #END
M
mahaifeng 已提交
53 54 55
  }

  testCopyWith() {
M
mahaifeng 已提交
56
    // #TEST Int16Array.copyWithin
M
mahaifeng 已提交
57 58 59 60
    let int16 = new Int16Array(8);
    int16.set([1, 2, 3], 1);
    int16.copyWithin(3, 0, 3);
    expect(int16.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
61
    // #END
M
mahaifeng 已提交
62 63 64
  }

  testEvery() {
M
mahaifeng 已提交
65
    // #TEST Int16Array.every
M
mahaifeng 已提交
66
    let result = new Int16Array([12, 5, 8, 130, 44]).every((value, _, _a : Int16Array) : boolean => value < 40);
M
mahaifeng 已提交
67
    expect(result).toEqual(false);
M
mahaifeng 已提交
68
    // #END
M
mahaifeng 已提交
69 70 71
  }

  testFill() {
M
mahaifeng 已提交
72
    // #TEST Int16Array.fill
M
mahaifeng 已提交
73 74
    let int16 = new Int16Array([1, 2, 3]).fill(4);
    expect(int16.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
75

M
mahaifeng 已提交
76 77
    int16 = new Int16Array([1, 2, 3]).fill(4, 1);
    expect(int16.toString()).toEqual("1,4,4");
M
mahaifeng 已提交
78

M
mahaifeng 已提交
79 80
    int16 = new Int16Array([1, 2, 3]).fill(4, 1, 2);
    expect(int16.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
81

M
mahaifeng 已提交
82 83
    int16 = new Int16Array([1, 2, 3]).fill(4, 1, 1);
    expect(int16.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
84

M
mahaifeng 已提交
85 86
    int16 = new Int16Array([1, 2, 3]).fill(4, -3, -2);
    expect(int16.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
87
    // #END
M
mahaifeng 已提交
88 89 90
  }

  testFilter() {
M
mahaifeng 已提交
91
    // #TEST Int16Array.filter
M
mahaifeng 已提交
92
    let int16 = new Int16Array([12, 5, 8, 44]).filter((value, _, _a : Int16Array) : boolean => value >= 10);
M
mahaifeng 已提交
93
    expect(int16.toString()).toEqual("12,44");
M
mahaifeng 已提交
94
    // #END
M
mahaifeng 已提交
95 96 97
  }

  find() {
M
mahaifeng 已提交
98
    // #TEST Int16Array.find
M
mahaifeng 已提交
99
    let int16 = new Int16Array([4, 5, 8, 12]);
M
mahaifeng 已提交
100
    let res = int16.find((value, _, _a : Int16Array) : boolean => value > 5);
M
mahaifeng 已提交
101
    expect(res).toEqual(8);
M
mahaifeng 已提交
102
    // #END
M
mahaifeng 已提交
103 104 105
  }

  findIndex() {
M
mahaifeng 已提交
106
    // #TEST Int16Array.findIndex
M
mahaifeng 已提交
107
    let int16 = new Int16Array([4, 6, 8, 12]);
M
mahaifeng 已提交
108
    let res = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
M
mahaifeng 已提交
109 110
    expect(res).toEqual(-1);

M
mahaifeng 已提交
111
    int16 = new Int16Array([4, 6, 7, 120]);
M
mahaifeng 已提交
112
    res = int16.findIndex((value, _, _a : Int16Array) : boolean => value > 100);
M
mahaifeng 已提交
113
    expect(res).toEqual(3);
M
mahaifeng 已提交
114
    // #END
M
mahaifeng 已提交
115 116 117
  }

  foreach() {
M
mahaifeng 已提交
118
    // #TEST Int16Array.forEach
M
mahaifeng 已提交
119
    new Int16Array([0, 1, 2, 3]).forEach((value, index, _ : Int16Array) => {
M
mahaifeng 已提交
120 121
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
122
    // #END
M
mahaifeng 已提交
123 124 125
  }

  iterator() {
M
mahaifeng 已提交
126
    // #TEST Int16Array.entries
M
mahaifeng 已提交
127 128 129
    let arr = new Int16Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
    expect(entries.next().value[1]).toEqual(10);
M
mahaifeng 已提交
130
    // #END
M
mahaifeng 已提交
131 132 133
  }

  includes() {
M
mahaifeng 已提交
134
    // #TEST Int16Array.includes
M
mahaifeng 已提交
135 136
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.includes(2);
M
mahaifeng 已提交
137 138
    expect(res).toEqual(true);

M
mahaifeng 已提交
139
    res = int16.includes(4);
M
mahaifeng 已提交
140 141
    expect(res).toEqual(false);

M
mahaifeng 已提交
142
    res = int16.includes(3, 3);
M
mahaifeng 已提交
143
    expect(res).toEqual(false);
M
mahaifeng 已提交
144
    // #END
M
mahaifeng 已提交
145 146 147
  }

  indexOf() {
M
mahaifeng 已提交
148
    // #TEST Int16Array.indexOf
M
mahaifeng 已提交
149 150
    let int16 = new Int16Array([2, 5, 9]);
    let res = int16.indexOf(2);
M
mahaifeng 已提交
151 152
    expect(res).toEqual(0);

M
mahaifeng 已提交
153
    res = int16.indexOf(7);
M
mahaifeng 已提交
154 155
    expect(res).toEqual(-1);

M
mahaifeng 已提交
156
    res = int16.indexOf(9, 2);
M
mahaifeng 已提交
157 158
    expect(res).toEqual(2);

M
mahaifeng 已提交
159
    res = int16.indexOf(2, -1);
M
mahaifeng 已提交
160 161
    expect(res).toEqual(-1);

M
mahaifeng 已提交
162
    res = int16.indexOf(2, -3);
M
mahaifeng 已提交
163
    expect(res).toEqual(0);
M
mahaifeng 已提交
164
    // #END
M
mahaifeng 已提交
165 166 167
  }

  join() {
M
mahaifeng 已提交
168
    // #TEST Int16Array.join
M
mahaifeng 已提交
169 170
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.join();
M
mahaifeng 已提交
171 172
    expect(res).toEqual("1,2,3");

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

M
mahaifeng 已提交
176
    res = int16.join("");
M
mahaifeng 已提交
177
    expect(res).toEqual("123");
M
mahaifeng 已提交
178
    // #END
M
mahaifeng 已提交
179 180 181
  }

  keys() {
M
mahaifeng 已提交
182
    // #TEST Int16Array.keys
M
mahaifeng 已提交
183 184 185 186 187 188 189
    let arr = new Int16Array([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 已提交
190
    // #END
M
mahaifeng 已提交
191 192 193
  }

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

  reduce() {
M
mahaifeng 已提交
203
    // #TEST Int16Array.reduce
M
mahaifeng 已提交
204
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
205
    let res = total.reduce((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
206
    expect(res).toEqual(6);
M
mahaifeng 已提交
207 208

    total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
209
    res = total.reduce((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
210
    expect(res).toEqual(14);
M
mahaifeng 已提交
211
    // #END
M
mahaifeng 已提交
212 213 214
  }

  reduceRight() {
M
mahaifeng 已提交
215
    // #TEST Int16Array.reduceRight
M
mahaifeng 已提交
216
    let total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
217
    let res = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue);
M
mahaifeng 已提交
218
    expect(res).toEqual(6);
M
mahaifeng 已提交
219 220

    total = new Int16Array([0, 1, 2, 3]);
M
mahaifeng 已提交
221
    res = total.reduceRight((accumulator, currentValue, _, _a : Int16Array) : number => accumulator + currentValue, 8);
M
mahaifeng 已提交
222
    expect(res).toEqual(14);
M
mahaifeng 已提交
223
    // #END
M
mahaifeng 已提交
224 225 226
  }

  reverse() {
M
mahaifeng 已提交
227
    // #TEST Int16Array.reverse
M
mahaifeng 已提交
228 229 230
    let int16 = new Int16Array([1, 2, 3]);
    int16.reverse();
    expect(int16.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
231
    // #END
M
mahaifeng 已提交
232 233 234
  }

  slice() {
M
mahaifeng 已提交
235
    // #TEST Int16Array.slice
M
mahaifeng 已提交
236 237
    let int16 = new Int16Array([1, 2, 3]);
    let res = int16.slice(1);
M
mahaifeng 已提交
238 239
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
240
    res = int16.slice(2);
M
mahaifeng 已提交
241 242
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
243
    res = int16.slice(-2);
M
mahaifeng 已提交
244 245
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
246
    res = int16.slice(0, 1);
M
mahaifeng 已提交
247
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
248
    // #END
M
mahaifeng 已提交
249 250 251
  }

  sort() {
M
mahaifeng 已提交
252
    // #TEST Int16Array.sort
M
mahaifeng 已提交
253 254 255 256
    let numbers = new Int16Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

M
mahaifeng 已提交
257
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
258
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
259
    // #END
M
mahaifeng 已提交
260 261 262
  }

  subarray() {
M
mahaifeng 已提交
263
    // #TEST Int16Array.subarray
M
mahaifeng 已提交
264
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
265 266 267
    let int16 = new Int16Array(buffer);
    int16.set([1, 2, 3]);
    expect(int16.toString()).toEqual("1,2,3,0,0,0,0,0");
M
mahaifeng 已提交
268

M
mahaifeng 已提交
269
    let sub = int16.subarray(0, 4);
M
mahaifeng 已提交
270
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
271
    // #END
M
mahaifeng 已提交
272 273 274
  }

  values() {
M
mahaifeng 已提交
275
    // #TEST Int16Array.values
M
mahaifeng 已提交
276 277 278
    let arr = new Int16Array([1, 2, 3]);
    let values = arr.values();
    expect(values.next().value).toEqual(1);
M
mahaifeng 已提交
279
    // #END
M
mahaifeng 已提交
280 281 282
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
283

M
mahaifeng 已提交
284
    // #TEST ArrayBuffer.slice with Int16Array
M
mahaifeng 已提交
285
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
286 287 288
    let int16 = new Int16Array(buffer);
    int16[4] = 42;
    expect(int16.toString()).toEqual("0,0,0,0,42,0,0,0");
M
mahaifeng 已提交
289

M
mahaifeng 已提交
290
    let res = buffer.slice(8, 12);
M
mahaifeng 已提交
291 292
    let sliced = new Int16Array(res);
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
293
    // #END
M
mahaifeng 已提交
294

M
mahaifeng 已提交
295
  }
M
mahaifeng 已提交
296 297 298 299
  testSome() {
    // #TEST Int16Array.some
    const int16 = new Int16Array([-10, 20, -30, 40, -50]);
    const positives = new Int16Array([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
300

M
mahaifeng 已提交
301 302 303 304 305 306 307 308 309 310
    expect(int16.some((element : number, index : number, array : Int16Array) : boolean =>
      element < 0
    )).toEqual(true);


    expect(positives.some((element : number, index : number, array : Int16Array) : boolean =>
      element < 0
    )).toEqual(false);
    // #END
  }
M
mahaifeng 已提交
311
  // #endif
M
mahaifeng 已提交
312
}