TUInt8ClampedArray.uts 9.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 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 59
    uint8Clamped.set(array, 1);
    expect(uint8Clamped.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
60
    // #END
M
mahaifeng 已提交
61 62 63
  }

  testCopyWith() {
M
mahaifeng 已提交
64
    // #TEST Uint8ClampedArray.copyWithin
M
mahaifeng 已提交
65
    console.log("testCopyWith 1")
M
mahaifeng 已提交
66 67 68
    let uint8Clamped = new Uint8ClampedArray(8);
    uint8Clamped.set([1, 2, 3], 1);
    uint8Clamped.copyWithin(3, 0, 3);
M
mahaifeng 已提交
69

M
mahaifeng 已提交
70
    expect(uint8Clamped.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
71
    // #END
M
mahaifeng 已提交
72 73 74
  }

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

  testFill() {
M
mahaifeng 已提交
84
    // #TEST Uint8ClampedArray.fill
M
mahaifeng 已提交
85 86
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4);
    expect(uint8Clamped.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
87

M
mahaifeng 已提交
88 89
    uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4, 1);
    expect(uint8Clamped.toString()).toEqual("1,4,4");
M
mahaifeng 已提交
90

M
mahaifeng 已提交
91 92
    uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 2);
    expect(uint8Clamped.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
93

M
mahaifeng 已提交
94 95
    uint8Clamped = new Uint8ClampedArray([1, 2, 3]).fill(4, 1, 1);
    expect(uint8Clamped.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
96

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

M
mahaifeng 已提交
102 103


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

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

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

  findIndex() {
M
mahaifeng 已提交
123
    // #TEST Uint8ClampedArray.findIndex
M
mahaifeng 已提交
124
    let uint8Clamped = new Uint8ClampedArray([4, 6, 8, 12]);
M
mahaifeng 已提交
125
    let res = uint8Clamped.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
M
mahaifeng 已提交
126 127
    expect(res).toEqual(-1);

M
mahaifeng 已提交
128
    let uuint8Clamped = new Uint8ClampedArray([4, 6, 7, 120]);
M
mahaifeng 已提交
129
    res = uuint8Clamped.findIndex((value : number, _ : number, _a : Uint8ClampedArray) : boolean => value > 100);
M
mahaifeng 已提交
130
    expect(res).toEqual(3);
M
mahaifeng 已提交
131
    // #END
M
mahaifeng 已提交
132 133 134
  }

  foreach() {
M
mahaifeng 已提交
135 136
    // #TEST Uint8ClampedArray.forEach
    new Uint8ClampedArray([0, 1, 2, 3]).forEach((value : number, index : number, _ : Uint8ClampedArray) => {
M
mahaifeng 已提交
137 138
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
139
    // #END
M
mahaifeng 已提交
140 141
  }
  iterator() {
M
mahaifeng 已提交
142
    // #TEST Uint8ClampedArray.entries
M
mahaifeng 已提交
143 144 145
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let entries = arr.entries();
    expect(entries.next().value[1]).toEqual(10);
M
mahaifeng 已提交
146
    // #END
M
mahaifeng 已提交
147 148 149
  }

  includes() {
M
mahaifeng 已提交
150
    // #TEST Uint8ClampedArray.includes
M
mahaifeng 已提交
151 152
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.includes(2);
M
mahaifeng 已提交
153 154
    expect(res).toEqual(true);

M
mahaifeng 已提交
155
    res = uint8Clamped.includes(4);
M
mahaifeng 已提交
156 157
    expect(res).toEqual(false);

M
mahaifeng 已提交
158
    res = uint8Clamped.includes(3, 3);
M
mahaifeng 已提交
159
    expect(res).toEqual(false);
M
mahaifeng 已提交
160
    // #END
M
mahaifeng 已提交
161 162 163
  }

  indexOf() {
M
mahaifeng 已提交
164
    // #TEST Uint8ClampedArray.indexOf
M
mahaifeng 已提交
165 166
    let uint8Clamped = new Uint8ClampedArray([2, 5, 9]);
    let res = uint8Clamped.indexOf(2);
M
mahaifeng 已提交
167 168
    expect(res).toEqual(0);

M
mahaifeng 已提交
169
    res = uint8Clamped.indexOf(7);
M
mahaifeng 已提交
170 171
    expect(res).toEqual(-1);

M
mahaifeng 已提交
172
    res = uint8Clamped.indexOf(9, 2);
M
mahaifeng 已提交
173 174
    expect(res).toEqual(2);

M
mahaifeng 已提交
175
    res = uint8Clamped.indexOf(2, -1);
M
mahaifeng 已提交
176 177
    expect(res).toEqual(-1);

M
mahaifeng 已提交
178
    res = uint8Clamped.indexOf(2, -3);
M
mahaifeng 已提交
179
    expect(res).toEqual(0);
M
mahaifeng 已提交
180
    // #END
M
mahaifeng 已提交
181 182
  }
  join() {
M
mahaifeng 已提交
183
    // #TEST Uint8ClampedArray.join
M
mahaifeng 已提交
184 185
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.join();
M
mahaifeng 已提交
186 187
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
188
    res = uint8Clamped.join(" / ");
M
mahaifeng 已提交
189 190
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
191
    res = uint8Clamped.join("");
M
mahaifeng 已提交
192
    expect(res).toEqual("123");
M
mahaifeng 已提交
193
    // #END
M
mahaifeng 已提交
194 195 196
  }

  keys() {
M
mahaifeng 已提交
197
    // #TEST Uint8ClampedArray.keys
M
mahaifeng 已提交
198 199 200
    let arr = new Uint8ClampedArray([10, 20, 30, 40, 50]);
    let keys = arr.keys();
    expect(keys.next().value).toEqual(0);
M
mahaifeng 已提交
201
    // #END
M
mahaifeng 已提交
202 203 204
  }

  map() {
M
mahaifeng 已提交
205
    // #TEST Uint8ClampedArray.map
M
mahaifeng 已提交
206
    let numbers = new Uint8ClampedArray([1, 4, 9]);
M
mahaifeng 已提交
207
    let doubles = numbers.map((value : number, _ : number, _a : Uint8ClampedArray) : number => value * 2);
M
mahaifeng 已提交
208 209
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
210
    // #END
M
mahaifeng 已提交
211 212
  }
  reduce() {
M
mahaifeng 已提交
213
    // #TEST Uint8ClampedArray.reduce
M
mahaifeng 已提交
214
    let total = new Uint8ClampedArray([0, 1, 2, 3]);
M
mahaifeng 已提交
215 216
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8ClampedArray) : number =>
      accumulator + currentValue);
M
mahaifeng 已提交
217
    expect(res).toEqual(6);
M
mahaifeng 已提交
218 219

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

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

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

  reverse() {
M
mahaifeng 已提交
241
    // #TEST Uint8ClampedArray.reverse
M
mahaifeng 已提交
242 243 244
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    uint8Clamped.reverse();
    expect(uint8Clamped.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
245
    // #END
M
mahaifeng 已提交
246 247 248
  }

  slice() {
M
mahaifeng 已提交
249
    // #TEST Uint8ClampedArray.slice
M
mahaifeng 已提交
250 251
    let uint8Clamped = new Uint8ClampedArray([1, 2, 3]);
    let res = uint8Clamped.slice(1);
M
mahaifeng 已提交
252 253
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
254
    res = uint8Clamped.slice(2);
M
mahaifeng 已提交
255 256
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
257
    res = uint8Clamped.slice(-2);
M
mahaifeng 已提交
258 259
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
260
    res = uint8Clamped.slice(0, 1);
M
mahaifeng 已提交
261
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
262
    // #END
M
mahaifeng 已提交
263 264
  }
  sort() {
M
mahaifeng 已提交
265
    // #TEST Uint8ClampedArray.sort
M
mahaifeng 已提交
266 267 268 269
    let numbers = new Uint8ClampedArray([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

M
mahaifeng 已提交
270
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
271
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
272
    // #END
M
mahaifeng 已提交
273 274 275
  }

  subarray() {
M
mahaifeng 已提交
276
    // #TEST Uint8ClampedArray.subarray
M
mahaifeng 已提交
277
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
278 279 280
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped.set([1, 2, 3]);
    expect(uint8Clamped.toString()).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
281

M
mahaifeng 已提交
282
    let sub = uint8Clamped.subarray(0, 4);
M
mahaifeng 已提交
283
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
284
    // #END
M
mahaifeng 已提交
285 286 287
  }

  values() {
M
mahaifeng 已提交
288
    // #TEST Uint8ClampedArray.values
M
mahaifeng 已提交
289 290 291
    let arr = new Uint8ClampedArray([1, 2, 3]);
    let values = arr.values();
    expect(values.next().value).toEqual(1);
M
mahaifeng 已提交
292
    // #END
M
mahaifeng 已提交
293 294 295
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
296

M
mahaifeng 已提交
297
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
298
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
299 300 301
    let uint8Clamped = new Uint8ClampedArray(buffer);
    uint8Clamped[4] = 42;
    expect(uint8Clamped.toString()).toEqual("0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
302 303 304 305

    let res = buffer.slice(4, 12);
    let sliced = new Uint8ClampedArray(res);
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
306
    // #END
M
mahaifeng 已提交
307
  }
M
mahaifeng 已提交
308 309 310 311
  testSome() {
    // #TEST Uint8ClampedArray.some
    const uint8Clamped = new Uint8ClampedArray([8, 20, 30, 40, 50]);
    const positives = new Uint8ClampedArray([10, 20, 30, 40, 50]);
M
mahaifeng 已提交
312

M
mahaifeng 已提交
313 314 315 316 317 318 319 320 321 322
    expect(uint8Clamped.some((element : number, index : number, array : Uint8ClampedArray) : boolean =>
      element < 10
    )).toEqual(true);


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