TUInt8ClampedArray.uts 9.1 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
    // #ifndef APP-IOS
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 34
    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 已提交
35
    // #endif
M
mahaifeng 已提交
36 37
  }

M
mahaifeng 已提交
38
  // #ifndef APP-IOS
M
mahaifeng 已提交
39
  testMAX() {
M
mahaifeng 已提交
40 41 42
    let uint8Clamped = new Uint8ClampedArray(16);
    uint8Clamped[0] = 255;
    expect(uint8Clamped[0]).toEqual(255);
M
mahaifeng 已提交
43 44 45 46
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
47 48 49
    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 已提交
50 51 52
  }

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

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

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

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

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

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

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

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

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

M
mahaifeng 已提交
100 101


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  subarray() {
M
mahaifeng 已提交
274
    // #TEST Uint8ClampedArray.subarray
M
mahaifeng 已提交
275
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
276 277 278
    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 已提交
279

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

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

  arrayBufferSlice() {
M
mahaifeng 已提交
294 295
    
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
296
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
297 298 299
    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 已提交
300 301 302 303

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

M
mahaifeng 已提交
311 312 313 314 315 316 317 318 319 320
    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
  }
M
mahaifeng 已提交
321
  // #endif
M
mahaifeng 已提交
322
}