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

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

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

  testCopyWith() {
M
mahaifeng 已提交
55
    // #TEST Int16Array.copyWithin
M
mahaifeng 已提交
56 57 58 59
    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 已提交
60
    // #END
M
mahaifeng 已提交
61 62 63
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

296
  // #endif
M
mahaifeng 已提交
297
}