TUInt8ClampedArray.uts 12.3 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 41 42 43 44 45 46 47 48 49 50 51 52 53
  from() {
    // #TEST Uint8ClampedArray.from
    var array = Uint8ClampedArray.from([1, 2, 3], (v : number, _ : number) : number => v + v);
    console.log(array.toString()); // '2,4,6'
    // #END
    expect(array.toString()).toEqual('2,4,6');
  }
  of() {
    // #TEST Uint8ClampedArray.of
    var array = Uint8ClampedArray.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
54
  testMAX() {
M
mahaifeng 已提交
55 56 57
    let uint8Clamped = new Uint8ClampedArray(16);
    uint8Clamped[0] = 255;
    expect(uint8Clamped[0]).toEqual(255);
M
mahaifeng 已提交
58 59 60 61
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
62 63 64
    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 已提交
65 66 67
  }

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

  testCopyWith() {
M
mahaifeng 已提交
78
    // #TEST Uint8ClampedArray.copyWithin
M
mahaifeng 已提交
79
    console.log("testCopyWith 1");
M
mahaifeng 已提交
80 81 82
    let uint8Clamped = new Uint8ClampedArray(8);
    uint8Clamped.set([1, 2, 3], 1);
    uint8Clamped.copyWithin(3, 0, 3);
M
mahaifeng 已提交
83
    console.log(uint8Clamped.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
84
    // #END
M
mahaifeng 已提交
85
    expect(uint8Clamped.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
86 87 88
  }

  testEvery() {
M
mahaifeng 已提交
89
    // #TEST Uint8ClampedArray.every
M
mahaifeng 已提交
90
    // const isBelowThreshold = (currentValue: number, index: number, array:Uint8ClampedArray): boolean => currentValue < 40;    
M
mahaifeng 已提交
91
    let result = new Uint8ClampedArray([12, 5, 8, 130, 44]).every((value : number, _ : number, _a :
M
mahaifeng 已提交
92 93
      Uint8ClampedArray) : boolean => value < 40);
    console.log(result); // false
M
mahaifeng 已提交
94
    // #END
M
mahaifeng 已提交
95
    expect(result).toEqual(false);
M
mahaifeng 已提交
96 97 98
  }

  testFill() {
M
mahaifeng 已提交
99
    // #TEST Uint8ClampedArray.fill
100 101
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]).fill(4);
    console.log(uint8Clamped_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
102

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

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

109 110 111 112 113
    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 已提交
114

M
mahaifeng 已提交
115
    // #END
116 117 118 119 120
    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 已提交
121 122
  }

M
mahaifeng 已提交
123

124

M
mahaifeng 已提交
125
  testFilter() {
M
mahaifeng 已提交
126
    // #TEST Uint8ClampedArray.filter
M
mahaifeng 已提交
127 128
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;

M
mahaifeng 已提交
129 130
    let uint8Clamped = new Uint8ClampedArray([12, 5, 8, 44]).filter((value : number, _ : number, _a :
      Uint8ClampedArray) : boolean => value >= 10);
M
mahaifeng 已提交
131
    console.log(uint8Clamped.toString()); // "12,44"
M
mahaifeng 已提交
132
    // #END
M
mahaifeng 已提交
133
    expect(uint8Clamped.toString()).toEqual("12,44");
M
mahaifeng 已提交
134 135 136
  }

  find() {
M
mahaifeng 已提交
137
    // #TEST Uint8ClampedArray.find
M
mahaifeng 已提交
138
    let uint8Clamped = new Uint8ClampedArray([4, 5, 8, 12]);
M
mahaifeng 已提交
139
    let res = uint8Clamped.find((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 5);
M
mahaifeng 已提交
140
    console.log(res); // 8
M
mahaifeng 已提交
141
    // #END
M
mahaifeng 已提交
142
    expect(res).toEqual(8);
M
mahaifeng 已提交
143 144 145
  }

  findIndex() {
M
mahaifeng 已提交
146
    // #TEST Uint8ClampedArray.findIndex
147 148 149 150 151 152 153
    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 已提交
154

M
mahaifeng 已提交
155
    // #END
156 157
    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
158 159
  }

160

M
mahaifeng 已提交
161
  foreach() {
M
mahaifeng 已提交
162 163
    // #TEST Uint8ClampedArray.forEach
    new Uint8ClampedArray([0, 1, 2, 3]).forEach((value : number, index : number, _ : Uint8ClampedArray) => {
M
mahaifeng 已提交
164 165
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
166
    // #END
M
mahaifeng 已提交
167
  }
M
mahaifeng 已提交
168

M
mahaifeng 已提交
169
  iterator() {
M
mahaifeng 已提交
170
    // #TEST Uint8ClampedArray.entries
M
mahaifeng 已提交
171 172
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
173 174
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
175
    // #END
M
mahaifeng 已提交
176
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
177 178 179
  }

  includes() {
M
mahaifeng 已提交
180
    // #TEST Uint8ClampedArray.includes
181 182 183
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.includes(2);
    console.log(res1); // true
M
mahaifeng 已提交
184

185 186 187 188 189 190 191
    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 已提交
192

M
mahaifeng 已提交
193
    // #END
194 195 196
    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
197 198 199
  }

  indexOf() {
M
mahaifeng 已提交
200
    // #TEST Uint8ClampedArray.indexOf
201 202 203 204 205 206 207
    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 已提交
208

209 210 211
    let uint8Clamped_t3 = new Uint8ClampedArray([2, 5, 9]);
    let res3 = uint8Clamped_t3.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
212

213 214 215
    let uint8Clamped_t4 = new Uint8ClampedArray([2, 5, 9]);
    let res4 = uint8Clamped_t4.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
216

217 218 219
    let uint8Clamped_t5 = new Uint8ClampedArray([2, 5, 9]);
    let res5 = uint8Clamped_t5.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
220

M
mahaifeng 已提交
221
    // #END
222 223 224 225 226
    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
227
  }
M
mahaifeng 已提交
228

229

M
mahaifeng 已提交
230
  join() {
M
mahaifeng 已提交
231
    // #TEST Uint8ClampedArray.join
232 233 234
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
235

236 237 238 239 240 241 242
    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 已提交
243

M
mahaifeng 已提交
244
    // #END
245 246 247
    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
248 249 250
  }

  keys() {
M
mahaifeng 已提交
251
    // #TEST Uint8ClampedArray.keys
M
mahaifeng 已提交
252 253
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
254 255
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
256
    // #END
M
mahaifeng 已提交
257
    expect(value).toEqual(0);
M
mahaifeng 已提交
258 259 260
  }

  map() {
M
mahaifeng 已提交
261
    // #TEST Uint8ClampedArray.map
M
mahaifeng 已提交
262
    let numbers = new Uint8ClampedArray([1, 4, 9]);
M
mahaifeng 已提交
263
    let doubles = numbers.map((value : number, _ : number, _a : Uint8ClampedArray) : number => value * 2);
M
mahaifeng 已提交
264 265
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
M
mahaifeng 已提交
266
    // #END
M
mahaifeng 已提交
267
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
268
  }
M
mahaifeng 已提交
269

M
mahaifeng 已提交
270
  reduce() {
M
mahaifeng 已提交
271
    // #TEST Uint8ClampedArray.reduce
272 273
    let total_t1 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res1 = total_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
274
      accumulator + currentValue);
275
    console.log(res1); // 6
M
mahaifeng 已提交
276

277 278
    let total_t2 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res2 = total_t2.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
279
      accumulator + currentValue, 8);
280 281
    console.log(res2); // 14

M
mahaifeng 已提交
282
    // #END
283 284
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
285 286 287
  }

  reduceRight() {
M
mahaifeng 已提交
288
    // #TEST Uint8ClampedArray.reduceRight
289 290
    let total_t1 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res1 = total_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
291
      accumulator + currentValue);
292
    console.log(res1); // 6
M
mahaifeng 已提交
293

294 295
    let total_t2 = new Uint8ClampedArray([0, 1, 2, 3]);
    let res2 = total_t2.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
M
mahaifeng 已提交
296
      accumulator + currentValue, 8);
297 298
    console.log(res2); // 14

M
mahaifeng 已提交
299
    // #END
300 301
    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
302 303
  }

M
mahaifeng 已提交
304

M
mahaifeng 已提交
305
  reverse() {
M
mahaifeng 已提交
306
    // #TEST Uint8ClampedArray.reverse
M
mahaifeng 已提交
307 308
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    uint8Clamped.reverse();
M
mahaifeng 已提交
309
    console.log(uint8Clamped.toString()); // "3,2,1"
M
mahaifeng 已提交
310
    // #END
M
mahaifeng 已提交
311
    expect(uint8Clamped.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
312 313 314
  }

  slice() {
M
mahaifeng 已提交
315
    // #TEST Uint8ClampedArray.slice
316 317 318
    let uint8Clamped_t1 = new Uint8ClampedArray([1, 2, 3]);
    let res1 = uint8Clamped_t1.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
319

320 321
    let res2 = uint8Clamped_t1.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
322

323 324
    let res3 = uint8Clamped_t1.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
325

326 327
    let res4 = uint8Clamped_t1.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
328
    // #END
329 330 331 332
    expect(res1.toString()).toEqual("2,3");
    expect(res2.toString()).toEqual("3");
    expect(res3.toString()).toEqual("2,3");
    expect(res4.toString()).toEqual("1");
M
mahaifeng 已提交
333 334
  }
  sort() {
M
mahaifeng 已提交
335
    // #TEST Uint8ClampedArray.sort
336 337 338 339 340 341 342 343
    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 已提交
344
    // #END
345 346
    expect(res1).toEqual("1,5,40");
    expect(res2).toEqual("1,5,40");
M
mahaifeng 已提交
347 348
  }

M
mahaifeng 已提交
349

M
mahaifeng 已提交
350
  subarray() {
M
mahaifeng 已提交
351
    // #TEST Uint8ClampedArray.subarray
M
mahaifeng 已提交
352
    let buffer = new ArrayBuffer(16);
353 354 355 356 357 358 359 360
    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 已提交
361
    // #END
362 363
    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 已提交
364 365
  }

M
mahaifeng 已提交
366

M
mahaifeng 已提交
367
  values() {
M
mahaifeng 已提交
368
    // #TEST Uint8ClampedArray.values
M
mahaifeng 已提交
369 370
    let arr = new Uint8ClampedArray([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
371 372
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
373
    // #END
M
mahaifeng 已提交
374
    expect(value).toEqual(1);
M
mahaifeng 已提交
375 376 377
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
378
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
379
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
380 381
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped[4] = 42;
M
mahaifeng 已提交
382
    console.log(uint8Clamped.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
383 384 385

    let res = buffer.slice(4, 12);
    let sliced = new Uint8ClampedArray(res);
M
mahaifeng 已提交
386
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
387
    // #END
M
mahaifeng 已提交
388
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
389
  }
M
mahaifeng 已提交
390

M
mahaifeng 已提交
391 392 393 394
  testSome() {
    // #TEST Uint8ClampedArray.some
    const uint8Clamped = new Uint8ClampedArray([8, 20, 30, 40, 50]);
    const positives = new Uint8ClampedArray([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
395

M
mahaifeng 已提交
396
    console.log(uint8Clamped.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
M
mahaifeng 已提交
397
      element < 10
M
mahaifeng 已提交
398
    )); // true
M
mahaifeng 已提交
399 400


M
mahaifeng 已提交
401 402 403 404
    console.log(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )); // false
    // #END
M
mahaifeng 已提交
405 406 407
    expect(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )).toEqual(false);
M
mahaifeng 已提交
408 409 410 411

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

M
mahaifeng 已提交
414
  // #endif
M
mahaifeng 已提交
415
}