TUInt8ClampedArray.uts 10.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 11
    // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

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();
36
    // #endif
M
mahaifeng 已提交
37 38
  }

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

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
M
mahaifeng 已提交
87
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4);
M
mahaifeng 已提交
88
    console.log(uint8Clamped.toString()); // "4,4,4"
M
mahaifeng 已提交
89

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

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

M
mahaifeng 已提交
96
    uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 1);
M
mahaifeng 已提交
97
    console.log(uint8Clamped.toString()); // "1,2,3"
M
mahaifeng 已提交
98

M
mahaifeng 已提交
99
    uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4, -3, -2);
M
mahaifeng 已提交
100
    console.log(uint8Clamped.toString()); // "4,2,3"
M
mahaifeng 已提交
101
    // #END
M
mahaifeng 已提交
102
    expect(uint8Clamped.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
103 104
  }

M
mahaifeng 已提交
105

M
mahaifeng 已提交
106
  testFilter() {
M
mahaifeng 已提交
107
    // #TEST Uint8ClampedArray.filter
M
mahaifeng 已提交
108 109
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;

M
mahaifeng 已提交
110 111
    let uint8Clamped = new Uint8ClampedArray([12, 5, 8, 44]).filter((value : number, _ : number, _a :
      Uint8ClampedArray) : boolean => value >= 10);
M
mahaifeng 已提交
112
    console.log(uint8Clamped.toString()); // "12,44"
M
mahaifeng 已提交
113
    // #END
M
mahaifeng 已提交
114
    expect(uint8Clamped.toString()).toEqual("12,44");
M
mahaifeng 已提交
115 116 117
  }

  find() {
M
mahaifeng 已提交
118
    // #TEST Uint8ClampedArray.find
M
mahaifeng 已提交
119
    let uint8Clamped = new Uint8ClampedArray([4, 5, 8, 12]);
M
mahaifeng 已提交
120
    let res = uint8Clamped.find((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 5);
M
mahaifeng 已提交
121
    console.log(res); // 8
M
mahaifeng 已提交
122
    // #END
M
mahaifeng 已提交
123
    expect(res).toEqual(8);
M
mahaifeng 已提交
124 125 126
  }

  findIndex() {
M
mahaifeng 已提交
127
    // #TEST Uint8ClampedArray.findIndex
M
mahaifeng 已提交
128
    let uint8Clamped = new Uint8ClampedArray([4, 6, 8, 12]);
M
mahaifeng 已提交
129
    let res = uint8Clamped.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
M
mahaifeng 已提交
130
    console.log(res); // -1
M
mahaifeng 已提交
131

M
mahaifeng 已提交
132
    let uuint8Clamped = new Uint8ClampedArray([4, 6, 7, 120]);
M
mahaifeng 已提交
133
    res = uuint8Clamped.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
M
mahaifeng 已提交
134
    console.log(res); // 3
M
mahaifeng 已提交
135
    // #END
M
mahaifeng 已提交
136
    expect(res).toEqual(3);
M
mahaifeng 已提交
137 138 139
  }

  foreach() {
M
mahaifeng 已提交
140 141
    // #TEST Uint8ClampedArray.forEach
    new Uint8ClampedArray([0, 1, 2, 3]).forEach((value : number, index : number, _ : Uint8ClampedArray) => {
M
mahaifeng 已提交
142 143
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
144
    // #END
M
mahaifeng 已提交
145
  }
M
mahaifeng 已提交
146

M
mahaifeng 已提交
147
  iterator() {
M
mahaifeng 已提交
148
    // #TEST Uint8ClampedArray.entries
M
mahaifeng 已提交
149 150
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
151 152
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
153
    // #END
M
mahaifeng 已提交
154
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
155 156 157
  }

  includes() {
M
mahaifeng 已提交
158
    // #TEST Uint8ClampedArray.includes
M
mahaifeng 已提交
159 160
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.includes(2);
M
mahaifeng 已提交
161
    console.log(res); // true
M
mahaifeng 已提交
162

M
mahaifeng 已提交
163
    res = uint8Clamped.includes(4);
M
mahaifeng 已提交
164
    console.log(res); // false
M
mahaifeng 已提交
165

M
mahaifeng 已提交
166
    res = uint8Clamped.includes(3, 3);
M
mahaifeng 已提交
167
    console.log(res); // false
M
mahaifeng 已提交
168
    // #END
M
mahaifeng 已提交
169
    expect(res).toEqual(false);
M
mahaifeng 已提交
170 171 172
  }

  indexOf() {
M
mahaifeng 已提交
173
    // #TEST Uint8ClampedArray.indexOf
M
mahaifeng 已提交
174 175
    let uint8Clamped = new Uint8ClampedArray([2, 5, 9]);
    let res = uint8Clamped.indexOf(2);
M
mahaifeng 已提交
176
    console.log(res); // 0
M
mahaifeng 已提交
177

M
mahaifeng 已提交
178
    res = uint8Clamped.indexOf(7);
M
mahaifeng 已提交
179
    console.log(res); // -1
M
mahaifeng 已提交
180

M
mahaifeng 已提交
181
    res = uint8Clamped.indexOf(9, 2);
M
mahaifeng 已提交
182
    console.log(res); // 2
M
mahaifeng 已提交
183

M
mahaifeng 已提交
184
    res = uint8Clamped.indexOf(2, -1);
M
mahaifeng 已提交
185
    console.log(res); // -1
M
mahaifeng 已提交
186

M
mahaifeng 已提交
187
    res = uint8Clamped.indexOf(2, -3);
M
mahaifeng 已提交
188
    console.log(res); // 0
M
mahaifeng 已提交
189
    // #END
M
mahaifeng 已提交
190
    expect(res).toEqual(0);
M
mahaifeng 已提交
191
  }
M
mahaifeng 已提交
192

M
mahaifeng 已提交
193
  join() {
M
mahaifeng 已提交
194
    // #TEST Uint8ClampedArray.join
M
mahaifeng 已提交
195 196
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.join();
M
mahaifeng 已提交
197
    console.log(res); // "1,2,3"
M
mahaifeng 已提交
198

M
mahaifeng 已提交
199
    res = uint8Clamped.join(" / ");
M
mahaifeng 已提交
200
    console.log(res); // "1 / 2 / 3"
M
mahaifeng 已提交
201

M
mahaifeng 已提交
202
    res = uint8Clamped.join("");
M
mahaifeng 已提交
203
    console.log(res); // "123"
M
mahaifeng 已提交
204
    // #END
M
mahaifeng 已提交
205
    expect(res).toEqual("123");
M
mahaifeng 已提交
206 207 208
  }

  keys() {
M
mahaifeng 已提交
209
    // #TEST Uint8ClampedArray.keys
M
mahaifeng 已提交
210 211
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
212 213
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
214
    // #END
M
mahaifeng 已提交
215
    expect(value).toEqual(0);
M
mahaifeng 已提交
216 217 218
  }

  map() {
M
mahaifeng 已提交
219
    // #TEST Uint8ClampedArray.map
M
mahaifeng 已提交
220
    let numbers = new Uint8ClampedArray([1, 4, 9]);
M
mahaifeng 已提交
221
    let doubles = numbers.map((value : number, _ : number, _a : Uint8ClampedArray) : number => value * 2);
M
mahaifeng 已提交
222 223
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
M
mahaifeng 已提交
224
    // #END
M
mahaifeng 已提交
225
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
226
  }
M
mahaifeng 已提交
227

M
mahaifeng 已提交
228
  reduce() {
M
mahaifeng 已提交
229
    // #TEST Uint8ClampedArray.reduce
M
mahaifeng 已提交
230
    let total = new Uint8ClampedArray([0, 1, 2, 3]);
M
mahaifeng 已提交
231 232
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
      accumulator + currentValue);
M
mahaifeng 已提交
233
    console.log(res); // 6
M
mahaifeng 已提交
234 235

    total = new Uint8ClampedArray([0, 1, 2, 3]);
M
mahaifeng 已提交
236 237
    res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
      accumulator + currentValue, 8);
M
mahaifeng 已提交
238
    console.log(res); // 14
M
mahaifeng 已提交
239
    // #END
M
mahaifeng 已提交
240
    expect(res).toEqual(14);
M
mahaifeng 已提交
241 242 243
  }

  reduceRight() {
M
mahaifeng 已提交
244
    // #TEST Uint8ClampedArray.reduceRight
M
mahaifeng 已提交
245
    let total = new Uint8ClampedArray([0, 1, 2, 3]);
M
mahaifeng 已提交
246 247
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
      accumulator + currentValue);
M
mahaifeng 已提交
248
    console.log(res); // 6
M
mahaifeng 已提交
249 250

    total = new Uint8ClampedArray([0, 1, 2, 3]);
M
mahaifeng 已提交
251 252
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
      accumulator + currentValue, 8);
M
mahaifeng 已提交
253
    console.log(res); // 14
M
mahaifeng 已提交
254
    // #END
M
mahaifeng 已提交
255
    expect(res).toEqual(14);
M
mahaifeng 已提交
256 257
  }

M
mahaifeng 已提交
258

M
mahaifeng 已提交
259
  reverse() {
M
mahaifeng 已提交
260
    // #TEST Uint8ClampedArray.reverse
M
mahaifeng 已提交
261 262
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    uint8Clamped.reverse();
M
mahaifeng 已提交
263
    console.log(uint8Clamped.toString()); // "3,2,1"
M
mahaifeng 已提交
264
    // #END
M
mahaifeng 已提交
265
    expect(uint8Clamped.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
266 267 268
  }

  slice() {
M
mahaifeng 已提交
269
    // #TEST Uint8ClampedArray.slice
M
mahaifeng 已提交
270 271
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.slice(1);
M
mahaifeng 已提交
272
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
273

M
mahaifeng 已提交
274
    res = uint8Clamped.slice(2);
M
mahaifeng 已提交
275
    console.log(res.toString()); // "3"
M
mahaifeng 已提交
276

M
mahaifeng 已提交
277
    res = uint8Clamped.slice(-2);
M
mahaifeng 已提交
278
    console.log(res.toString()); // "2,3"
M
mahaifeng 已提交
279

M
mahaifeng 已提交
280
    res = uint8Clamped.slice(0, 1);
M
mahaifeng 已提交
281
    console.log(res.toString()); // "1"
M
mahaifeng 已提交
282
    // #END
M
mahaifeng 已提交
283
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
284
  }
M
mahaifeng 已提交
285 286


M
mahaifeng 已提交
287
  sort() {
M
mahaifeng 已提交
288
    // #TEST Uint8ClampedArray.sort
M
mahaifeng 已提交
289 290
    let numbers = new Uint8ClampedArray([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
291
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
292

M
mahaifeng 已提交
293
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
294
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
295
    // #END
M
mahaifeng 已提交
296
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
297 298
  }

M
mahaifeng 已提交
299

M
mahaifeng 已提交
300
  subarray() {
M
mahaifeng 已提交
301
    // #TEST Uint8ClampedArray.subarray
M
mahaifeng 已提交
302
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
303 304
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped.set([1, 2, 3]);
M
mahaifeng 已提交
305
    console.log(uint8Clamped.toString()); // "1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
306

M
mahaifeng 已提交
307
    let sub = uint8Clamped.subarray(0, 4);
M
mahaifeng 已提交
308
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
309
    // #END
M
mahaifeng 已提交
310
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
311 312
  }

M
mahaifeng 已提交
313

M
mahaifeng 已提交
314
  values() {
M
mahaifeng 已提交
315
    // #TEST Uint8ClampedArray.values
M
mahaifeng 已提交
316 317
    let arr = new Uint8ClampedArray([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
318 319
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
320
    // #END
M
mahaifeng 已提交
321
    expect(value).toEqual(1);
M
mahaifeng 已提交
322 323 324
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
325
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
326
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
327 328
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped[4] = 42;
M
mahaifeng 已提交
329
    console.log(uint8Clamped.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
330 331 332

    let res = buffer.slice(4, 12);
    let sliced = new Uint8ClampedArray(res);
M
mahaifeng 已提交
333
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
334
    // #END
M
mahaifeng 已提交
335
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
336
  }
M
mahaifeng 已提交
337

M
mahaifeng 已提交
338 339 340 341
  testSome() {
    // #TEST Uint8ClampedArray.some
    const uint8Clamped = new Uint8ClampedArray([8, 20, 30, 40, 50]);
    const positives = new Uint8ClampedArray([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
342

M
mahaifeng 已提交
343
    console.log(uint8Clamped.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
M
mahaifeng 已提交
344
      element < 10
M
mahaifeng 已提交
345
    )); // true
M
mahaifeng 已提交
346 347


M
mahaifeng 已提交
348 349 350 351
    console.log(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )); // false
    // #END
M
mahaifeng 已提交
352 353 354
    expect(positives.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 0
    )).toEqual(false);
M
mahaifeng 已提交
355 356 357 358

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

361
  // #endif
M
mahaifeng 已提交
362
}