TUInt8ClampedArray.uts 11.9 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4 5 6 7 8 9
import {
  describe,
  test,
  expect,
  Result
} from './tests.uts'

export class TUint8ClampedArray {
  test() {
M
mahaifeng 已提交
10
    // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
11

M
mahaifeng 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    this.testMAX();
    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 已提交
36
    // #endif
M
mahaifeng 已提交
37 38
  }

M
mahaifeng 已提交
39
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
40

M
mahaifeng 已提交
41
  testMAX() {
M
mahaifeng 已提交
42 43 44
    let uint8Clamped = new Uint8ClampedArray(16);
    uint8Clamped[0] = 255;
    expect(uint8Clamped[0]).toEqual(255);
M
mahaifeng 已提交
45 46 47 48
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
49 50 51
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped[5] = 42;
    expect(uint8Clamped.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
52 53 54
  }

  testSet() {
M
mahaifeng 已提交
55
    // #TEST Uint8ClampedArray.set
M
mahaifeng 已提交
56
    let uint8Clamped = new Uint8ClampedArray(8);
M
mahaifeng 已提交
57
    var array = [1, 2, 3];
M
mahaifeng 已提交
58
    uint8Clamped.set(array, 1);
M
mahaifeng 已提交
59
    console.log(uint8Clamped.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
60
    // #END
M
mahaifeng 已提交
61
    expect(uint8Clamped.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
62 63 64
  }

  testCopyWith() {
M
mahaifeng 已提交
65
    // #TEST Uint8ClampedArray.copyWithin
M
mahaifeng 已提交
66
    console.log("testCopyWith 1");
M
mahaifeng 已提交
67 68 69
    let uint8Clamped = new Uint8ClampedArray(8);
    uint8Clamped.set([1, 2, 3], 1);
    uint8Clamped.copyWithin(3, 0, 3);
M
mahaifeng 已提交
70
    console.log(uint8Clamped.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
71
    // #END
M
mahaifeng 已提交
72
    expect(uint8Clamped.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
73 74 75
  }

  testEvery() {
M
mahaifeng 已提交
76
    // #TEST Uint8ClampedArray.every
M
mahaifeng 已提交
77
    // const isBelowThreshold = (currentValue: number, index: number, array:Uint8ClampedArray): boolean => currentValue < 40;    
M
mahaifeng 已提交
78
    let result = new Uint8ClampedArray([12, 5, 8, 130, 44]).every((value : number, _ : number, _a :
M
mahaifeng 已提交
79 80
      Uint8ClampedArray) : boolean => value < 40);
    console.log(result); // false
M
mahaifeng 已提交
81
    // #END
M
mahaifeng 已提交
82
    expect(result).toEqual(false);
M
mahaifeng 已提交
83 84 85
  }

  testFill() {
M
mahaifeng 已提交
86
    // #TEST Uint8ClampedArray.fill
87 88
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]).fill(4);
    console.log(uint8Clamped_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
89

90 91
    let uint8Clamped_t2 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1);
    console.log(uint8Clamped_t2.toString()); // "1,4,4"
M
mahaifeng 已提交
92

93 94
    let uint8Clamped_t3 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 2);
    console.log(uint8Clamped_t3.toString()); // "1,4,3"
M
mahaifeng 已提交
95

96 97 98 99 100
    let uint8Clamped_t4 = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 1);
    console.log(uint8Clamped_t4.toString()); // "1,2,3"

    let uint8Clamped_t5 = new Uint8ClampedArray([1, 2, 3]).fill(4, -3, -2);
    console.log(uint8Clamped_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
101

M
mahaifeng 已提交
102
    // #END
103 104 105 106 107
    expect(uint8Clamped_t1.toString()).toEqual("4,4,4");
    expect(uint8Clamped_t2.toString()).toEqual("1,4,4");
    expect(uint8Clamped_t3.toString()).toEqual("1,4,3");
    expect(uint8Clamped_t4.toString()).toEqual("1,2,3");
    expect(uint8Clamped_t5.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
108 109
  }

M
mahaifeng 已提交
110

111

M
mahaifeng 已提交
112
  testFilter() {
M
mahaifeng 已提交
113
    // #TEST Uint8ClampedArray.filter
M
mahaifeng 已提交
114 115
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;

M
mahaifeng 已提交
116 117
    let uint8Clamped = new Uint8ClampedArray([12, 5, 8, 44]).filter((value : number, _ : number, _a :
      Uint8ClampedArray) : boolean => value >= 10);
M
mahaifeng 已提交
118
    console.log(uint8Clamped.toString()); // "12,44"
M
mahaifeng 已提交
119
    // #END
M
mahaifeng 已提交
120
    expect(uint8Clamped.toString()).toEqual("12,44");
M
mahaifeng 已提交
121 122 123
  }

  find() {
M
mahaifeng 已提交
124
    // #TEST Uint8ClampedArray.find
M
mahaifeng 已提交
125
    let uint8Clamped = new Uint8ClampedArray([4, 5, 8, 12]);
M
mahaifeng 已提交
126
    let res = uint8Clamped.find((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 5);
M
mahaifeng 已提交
127
    console.log(res); // 8
M
mahaifeng 已提交
128
    // #END
M
mahaifeng 已提交
129
    expect(res).toEqual(8);
M
mahaifeng 已提交
130 131 132
  }

  findIndex() {
M
mahaifeng 已提交
133
    // #TEST Uint8ClampedArray.findIndex
134 135 136 137 138 139 140
    let uint8Clamped_t1 = new Uint8ClampedArray([4, 6, 8, 12]);
    let res1 = uint8Clamped_t1.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
    console.log(res1); // -1

    let uint8Clamped_t2 = new Uint8ClampedArray([4, 6, 7, 120]);
    let res2 = uint8Clamped_t2.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
    console.log(res2); // 3
M
mahaifeng 已提交
141

M
mahaifeng 已提交
142
    // #END
143 144
    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
145 146
  }

147

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

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

  includes() {
M
mahaifeng 已提交
167
    // #TEST Uint8ClampedArray.includes
168 169 170
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.includes(2);
    console.log(res1); // true
M
mahaifeng 已提交
171

172 173 174 175 176 177 178
    let uint8Clamped_t2 = new Uint8ClampedArray([1, 2, 3]);
    let res2 = uint8Clamped_t2.includes(4);
    console.log(res2); // false

    let uint8Clamped_t3 = new Uint8ClampedArray([1, 2, 3]);
    let res3 = uint8Clamped_t3.includes(3, 3);
    console.log(res3); // false
M
mahaifeng 已提交
179

M
mahaifeng 已提交
180
    // #END
181 182 183
    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
184 185 186
  }

  indexOf() {
M
mahaifeng 已提交
187
    // #TEST Uint8ClampedArray.indexOf
188 189 190 191 192 193 194
    let uint8Clamped_t1 = new Uint8ClampedArray([2, 5, 9]);
    let res1 = uint8Clamped_t1.indexOf(2);
    console.log(res1); // 0

    let uint8Clamped_t2 = new Uint8ClampedArray([2, 5, 9]);
    let res2 = uint8Clamped_t2.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
195

196 197 198
    let uint8Clamped_t3 = new Uint8ClampedArray([2, 5, 9]);
    let res3 = uint8Clamped_t3.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
199

200 201 202
    let uint8Clamped_t4 = new Uint8ClampedArray([2, 5, 9]);
    let res4 = uint8Clamped_t4.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
203

204 205 206
    let uint8Clamped_t5 = new Uint8ClampedArray([2, 5, 9]);
    let res5 = uint8Clamped_t5.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
207

M
mahaifeng 已提交
208
    // #END
209 210 211 212 213
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
214
  }
M
mahaifeng 已提交
215

216

M
mahaifeng 已提交
217
  join() {
M
mahaifeng 已提交
218
    // #TEST Uint8ClampedArray.join
219 220 221
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
222

223 224 225 226 227 228 229
    let uint8Clamped_t2 = new Uint8ClampedArray([1, 2, 3]);
    let res2 = uint8Clamped_t2.join(" / ");
    console.log(res2); // "1 / 2 / 3"

    let uint8Clamped_t3 = new Uint8ClampedArray([1, 2, 3]);
    let res3 = uint8Clamped_t3.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
230

M
mahaifeng 已提交
231
    // #END
232 233 234
    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
235 236 237
  }

  keys() {
M
mahaifeng 已提交
238
    // #TEST Uint8ClampedArray.keys
M
mahaifeng 已提交
239 240
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
241 242
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
243
    // #END
M
mahaifeng 已提交
244
    expect(value).toEqual(0);
M
mahaifeng 已提交
245 246 247
  }

  map() {
M
mahaifeng 已提交
248
    // #TEST Uint8ClampedArray.map
M
mahaifeng 已提交
249
    let numbers = new Uint8ClampedArray([1, 4, 9]);
M
mahaifeng 已提交
250
    let doubles = numbers.map((value : number, _ : number, _a : Uint8ClampedArray) : number => value * 2);
M
mahaifeng 已提交
251 252
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
M
mahaifeng 已提交
253
    // #END
M
mahaifeng 已提交
254
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
255
  }
M
mahaifeng 已提交
256

M
mahaifeng 已提交
257
  reduce() {
M
mahaifeng 已提交
258
    // #TEST Uint8ClampedArray.reduce
259 260
    let total_t1 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res1 = total_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
261
      accumulator + currentValue);
262
    console.log(res1); // 6
M
mahaifeng 已提交
263

264 265
    let total_t2 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res2 = total_t2.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
266
      accumulator + currentValue, 8);
267 268
    console.log(res2); // 14

M
mahaifeng 已提交
269
    // #END
270 271
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
272 273 274
  }

  reduceRight() {
M
mahaifeng 已提交
275
    // #TEST Uint8ClampedArray.reduceRight
276 277
    let total_t1 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res1 = total_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
278
      accumulator + currentValue);
279
    console.log(res1); // 6
M
mahaifeng 已提交
280

281 282
    let total_t2 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res2 = total_t2.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
283
      accumulator + currentValue, 8);
284 285
    console.log(res2); // 14

M
mahaifeng 已提交
286
    // #END
287 288
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
289 290
  }

M
mahaifeng 已提交
291

M
mahaifeng 已提交
292
  reverse() {
M
mahaifeng 已提交
293
    // #TEST Uint8ClampedArray.reverse
M
mahaifeng 已提交
294 295
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    uint8Clamped.reverse();
M
mahaifeng 已提交
296
    console.log(uint8Clamped.toString()); // "3,2,1"
M
mahaifeng 已提交
297
    // #END
M
mahaifeng 已提交
298
    expect(uint8Clamped.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
299 300 301
  }

  slice() {
M
mahaifeng 已提交
302
    // #TEST Uint8ClampedArray.slice
303 304 305
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
306

307 308
    let res2 = uint8Clamped_t1.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
309

310 311
    let res3 = uint8Clamped_t1.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
312

313 314
    let res4 = uint8Clamped_t1.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
315
    // #END
316 317 318 319
    expect(res1.toString()).toEqual("2,3");
    expect(res2.toString()).toEqual("3");
    expect(res3.toString()).toEqual("2,3");
    expect(res4.toString()).toEqual("1");
M
mahaifeng 已提交
320 321
  }
  sort() {
M
mahaifeng 已提交
322
    // #TEST Uint8ClampedArray.sort
323 324 325 326 327 328 329 330
    let uint8Clamped_t1 = new Uint8ClampedArray([40, 1, 5]);
    uint8Clamped_t1.sort();
    let res1 = uint8Clamped_t1.toString();
    console.log(res1); // "1,5,40"

    uint8Clamped_t1.sort((a, b) : number => a - b);
    let res2 = uint8Clamped_t1.toString();
    console.log(res2); // "1,5,40"
M
mahaifeng 已提交
331
    // #END
332 333
    expect(res1).toEqual("1,5,40");
    expect(res2).toEqual("1,5,40");
M
mahaifeng 已提交
334 335
  }

M
mahaifeng 已提交
336

M
mahaifeng 已提交
337
  subarray() {
M
mahaifeng 已提交
338
    // #TEST Uint8ClampedArray.subarray
M
mahaifeng 已提交
339
    let buffer = new ArrayBuffer(16);
340 341 342 343 344 345 346 347
    let uint8Clamped_t1 = new Uint8ClampedArray(buffer);
    uint8Clamped_t1.set([1, 2, 3]);
    let res1 = uint8Clamped_t1.toString();
    console.log(res1); // "1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"

    let sub = uint8Clamped_t1.subarray(0, 4);
    let res2 = sub.toString();
    console.log(res2); // "1,2,3,0"
M
mahaifeng 已提交
348
    // #END
349 350
    expect(res1).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0");
    expect(res2).toEqual("1,2,3,0");
M
mahaifeng 已提交
351 352
  }

M
mahaifeng 已提交
353

M
mahaifeng 已提交
354
  values() {
M
mahaifeng 已提交
355
    // #TEST Uint8ClampedArray.values
M
mahaifeng 已提交
356 357
    let arr = new Uint8ClampedArray([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
358 359
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
360
    // #END
M
mahaifeng 已提交
361
    expect(value).toEqual(1);
M
mahaifeng 已提交
362 363 364
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
365
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
366
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
367 368
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped[4] = 42;
M
mahaifeng 已提交
369
    console.log(uint8Clamped.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
370 371 372

    let res = buffer.slice(4, 12);
    let sliced = new Uint8ClampedArray(res);
M
mahaifeng 已提交
373
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
374
    // #END
M
mahaifeng 已提交
375
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
376
  }
M
mahaifeng 已提交
377

M
mahaifeng 已提交
378 379 380 381
  testSome() {
    // #TEST Uint8ClampedArray.some
    const uint8Clamped = new Uint8ClampedArray([8, 20, 30, 40, 50]);
    const positives = new Uint8ClampedArray([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
382

M
mahaifeng 已提交
383
    console.log(uint8Clamped.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
M
mahaifeng 已提交
384
      element < 10
M
mahaifeng 已提交
385
    )); // true
M
mahaifeng 已提交
386 387


M
mahaifeng 已提交
388 389 390 391
    console.log(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )); // false
    // #END
M
mahaifeng 已提交
392 393 394
    expect(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )).toEqual(false);
M
mahaifeng 已提交
395 396 397 398

    expect(uint8Clamped.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 10
    )).toEqual(true);
M
mahaifeng 已提交
399
  }
M
mahaifeng 已提交
400

M
mahaifeng 已提交
401
  // #endif
M
mahaifeng 已提交
402
}