TUInt8Array.uts 10.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 TUint8Array {
  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
  }
M
mahaifeng 已提交
38
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
39

M
mahaifeng 已提交
40 41 42 43 44 45
  from() {
    var s = new Set([1, 2, 3]);
    var unit8 = Uint8Array.from(s);
    expect(unit8.toString()).toEqual('1,2,3');
    console.log(unit8.toString())
  }
M
mahaifeng 已提交
46
  testMAX() {
M
mahaifeng 已提交
47 48 49
    let uint8 = new Uint8Array(16);
    uint8[0] = 255;
    expect(uint8[0]).toEqual(255);
M
mahaifeng 已提交
50 51 52 53
  }

  testConstructor() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
54 55 56
    let uint8 = new Uint8Array(buffer);
    uint8[5] = 42;
    expect(uint8.toString()).toEqual("0,0,0,0,0,42,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
57 58 59
  }

  testSet() {
M
mahaifeng 已提交
60
    // #TEST Uint8Array.set
M
mahaifeng 已提交
61
    let uint8 = new Uint8Array(8);
M
mahaifeng 已提交
62
    var array = [1, 2, 3];
M
mahaifeng 已提交
63
    uint8.set(array, 1);
M
mahaifeng 已提交
64
    console.log(uint8.toString()); // "0,1,2,3,0,0,0,0"
M
mahaifeng 已提交
65
    // #END
M
mahaifeng 已提交
66
    expect(uint8.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
67 68 69
  }

  testCopyWith() {
M
mahaifeng 已提交
70 71
    // #TEST Uint8Array.copyWithin
    console.log("testCopyWith 1");
M
mahaifeng 已提交
72 73 74
    let uint8 = new Uint8Array(8);
    uint8.set([1, 2, 3], 1);
    uint8.copyWithin(3, 0, 3);
M
mahaifeng 已提交
75
    console.log(uint8.toString()); // "0,1,2,0,1,2,0,0"
M
mahaifeng 已提交
76
    // #END
M
mahaifeng 已提交
77
    expect(uint8.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
78 79 80
  }

  testEvery() {
M
mahaifeng 已提交
81 82 83
    // #TEST Uint8Array.every
    let result = new Uint8Array([12, 5, 8, 130, 44]).every((value : number, _ : number, _a : Uint8Array) : boolean =>
      value < 40);
M
mahaifeng 已提交
84
    console.log(result); // false
M
mahaifeng 已提交
85
    // #END
M
mahaifeng 已提交
86
    expect(result).toEqual(false);
M
mahaifeng 已提交
87 88 89
  }

  testFill() {
M
mahaifeng 已提交
90
    // #TEST Uint8Array.fill
91 92
    let uint8_t1 = new Uint8Array([1, 2, 3]).fill(4);
    console.log(uint8_t1.toString()); // "4,4,4"
M
mahaifeng 已提交
93

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

97 98
    let uint8_t3 = new Uint8Array([1, 2, 3]).fill(4, 1, 2);
    console.log(uint8_t3.toString()); // "1,4,3"
M
mahaifeng 已提交
99

100 101
    let uint8_t4 = new Uint8Array([1, 2, 3]).fill(4, 1, 1);
    console.log(uint8_t4.toString()); // "1,2,3"
M
mahaifeng 已提交
102

103 104
    let uint8_t5 = new Uint8Array([1, 2, 3]).fill(4, -3, -2);
    console.log(uint8_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
105
    // #END
106 107 108 109 110 111

    expect(uint8_t1.toString()).toEqual("4,4,4");
    expect(uint8_t2.toString()).toEqual("1,4,4");
    expect(uint8_t3.toString()).toEqual("1,4,3");
    expect(uint8_t4.toString()).toEqual("1,2,3");
    expect(uint8_t5.toString()).toEqual("4,2,3");
M
mahaifeng 已提交
112 113
  }

114

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

M
mahaifeng 已提交
123
  find() {
M
mahaifeng 已提交
124
    // #TEST Uint8Array.find
M
mahaifeng 已提交
125
    let uint8 = new Uint8Array([4, 5, 8, 12]);
M
mahaifeng 已提交
126
    let res = uint8.find((value : number, _ : number, _a : Uint8Array) : 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
  }
  findIndex() {
M
mahaifeng 已提交
132
    // #TEST Uint8Array.findIndex
133 134 135
    let uint8_t1 = new Uint8Array([4, 6, 8, 12]);
    let res1 = uint8_t1.findIndex((value : number, _ : number, _a : Uint8Array) : boolean => value > 100);
    console.log(res1); // -1
M
mahaifeng 已提交
136

137 138 139
    let uint8_t2 = new Uint8Array([4, 6, 7, 120]);
    let res2 = uint8_t2.findIndex((value : number, _ : number, _a : Uint8Array) : boolean => value > 100);
    console.log(res2); // 3
M
mahaifeng 已提交
140
    // #END
141 142 143

    expect(res1).toEqual(-1);
    expect(res2).toEqual(3);
M
mahaifeng 已提交
144 145
  }

146

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

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

170 171
    let res2 = uint8_t1.includes(4);
    console.log(res2); // false
M
mahaifeng 已提交
172

173 174
    let res3 = uint8_t1.includes(3, 3);
    console.log(res3); // false
M
mahaifeng 已提交
175
    // #END
176 177 178 179

    expect(res1).toEqual(true);
    expect(res2).toEqual(false);
    expect(res3).toEqual(false);
M
mahaifeng 已提交
180 181
  }

M
mahaifeng 已提交
182

M
mahaifeng 已提交
183
  indexOf() {
M
mahaifeng 已提交
184
    // #TEST Uint8Array.indexOf
185 186 187
    let uint8_t1 = new Uint8Array([2, 5, 9]);
    let res1 = uint8_t1.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
188

189 190
    let res2 = uint8_t1.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
191

192 193
    let res3 = uint8_t1.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
194

195 196
    let res4 = uint8_t1.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
197

198 199
    let res5 = uint8_t1.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
200
    // #END
201 202 203 204 205 206

    expect(res1).toEqual(0);
    expect(res2).toEqual(-1);
    expect(res3).toEqual(2);
    expect(res4).toEqual(-1);
    expect(res5).toEqual(0);
M
mahaifeng 已提交
207 208
  }

209

M
mahaifeng 已提交
210
  join() {
M
mahaifeng 已提交
211
    // #TEST Uint8Array.join
212 213 214
    let uint8_t1 = new Uint8Array([1, 2, 3]);
    let res1 = uint8_t1.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
215

216 217
    let res2 = uint8_t1.join(" / ");
    console.log(res2); // "1 / 2 / 3"
M
mahaifeng 已提交
218

219 220
    let res3 = uint8_t1.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
221
    // #END
222 223 224 225

    expect(res1).toEqual("1,2,3");
    expect(res2).toEqual("1 / 2 / 3");
    expect(res3).toEqual("123");
M
mahaifeng 已提交
226
  }
M
mahaifeng 已提交
227

228

M
mahaifeng 已提交
229
  keys() {
M
mahaifeng 已提交
230
    // #TEST Uint8Array.keys
M
mahaifeng 已提交
231 232
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
233 234
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
235
    // #END
M
mahaifeng 已提交
236
    expect(value).toEqual(0);
M
mahaifeng 已提交
237 238 239
  }

  map() {
M
mahaifeng 已提交
240
    // #TEST Uint8Array.map
M
mahaifeng 已提交
241
    let numbers = new Uint8Array([1, 4, 9]);
M
mahaifeng 已提交
242
    let doubles = numbers.map((value : number, _ : number, _a : Uint8Array) : number => value * 2);
M
mahaifeng 已提交
243 244 245
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
246 247 248 249
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }
  reduce() {
M
mahaifeng 已提交
250
    // #TEST Uint8Array.reduce
251 252
    let uint8_t1 = new Uint8Array([0, 1, 2, 3]);
    let res1 = uint8_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
253
      accumulator + currentValue);
254
    console.log(res1); // 6
M
mahaifeng 已提交
255

256
    let res2 = uint8_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
257
      accumulator + currentValue, 8);
258
    console.log(res2); // 14
M
mahaifeng 已提交
259
    // #END
260 261 262

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
263 264 265
  }

  reduceRight() {
M
mahaifeng 已提交
266
    // #TEST Uint8Array.reduceRight
267 268
    let uint8_t1 = new Uint8Array([0, 1, 2, 3]);
    let res1 = uint8_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
269
      accumulator + currentValue);
270
    console.log(res1); // 6
M
mahaifeng 已提交
271

272
    let res2 = uint8_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
273
      accumulator + currentValue, 8);
274
    console.log(res2); // 14
M
mahaifeng 已提交
275
    // #END
276 277 278

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
279
  }
M
mahaifeng 已提交
280

281

M
mahaifeng 已提交
282
  reverse() {
M
mahaifeng 已提交
283
    // #TEST Uint8Array.reverse
M
mahaifeng 已提交
284 285
    let uint8 = new Uint8Array([1, 2, 3]);
    uint8.reverse();
M
mahaifeng 已提交
286
    console.log(uint8.toString()); // "3,2,1"
M
mahaifeng 已提交
287
    // #END
M
mahaifeng 已提交
288
    expect(uint8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
289 290 291
  }

  slice() {
M
mahaifeng 已提交
292
    // #TEST Uint8Array.slice
293 294 295
    let uint8_t1 = new Uint8Array([1, 2, 3]);
    let res1 = uint8_t1.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
296

297 298
    let res2 = uint8_t1.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
299

300 301
    let res3 = uint8_t1.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
302

303 304
    let res4 = uint8_t1.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
305
    // #END
306 307 308 309 310

    expect(res1.toString()).toEqual("2,3");
    expect(res2.toString()).toEqual("3");
    expect(res3.toString()).toEqual("2,3");
    expect(res4.toString()).toEqual("1");
M
mahaifeng 已提交
311 312
  }

313

M
mahaifeng 已提交
314
  sort() {
M
mahaifeng 已提交
315
    // #TEST Uint8Array.sort
316 317 318
    let numbers_t1 = new Uint8Array([40, 1, 5]);
    numbers_t1.sort();
    console.log(numbers_t1.toString()); // "1,5,40"
M
mahaifeng 已提交
319

320 321 322
    let numbers_t2 = new Uint8Array([40, 1, 5]);
    numbers_t2.sort((a, b) : number => a - b);
    console.log(numbers_t2.toString()); // "1,5,40"
M
mahaifeng 已提交
323
    // #END
324 325 326

    expect(numbers_t1.toString()).toEqual("1,5,40");
    expect(numbers_t2.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
327 328
  }

329

M
mahaifeng 已提交
330
  subarray() {
M
mahaifeng 已提交
331
    // #TEST Uint8Array.subarray
M
mahaifeng 已提交
332
    let buffer = new ArrayBuffer(16);
333 334 335
    let uint8_t1 = new Uint8Array(buffer);
    uint8_t1.set([1, 2, 3]);
    console.log(uint8_t1.toString()); // "1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
336

337 338
    let sub_t1 = uint8_t1.subarray(0, 4);
    console.log(sub_t1.toString()); // "1,2,3,0"
M
mahaifeng 已提交
339
    // #END
340 341 342

    expect(uint8_t1.toString()).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0");
    expect(sub_t1.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
343 344 345
  }

  values() {
M
mahaifeng 已提交
346
    // #TEST Uint8Array.values
M
mahaifeng 已提交
347 348
    let arr = new Uint8Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
349 350
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
351
    // #END
M
mahaifeng 已提交
352
    expect(value).toEqual(1);
M
mahaifeng 已提交
353 354 355
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
356
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
357
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
358 359
    let uint8 = new Uint8Array(buffer);
    uint8[4] = 42;
M
mahaifeng 已提交
360
    console.log(uint8.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
361 362 363

    let res = buffer.slice(4, 12);
    let sliced = new Uint8Array(res);
M
mahaifeng 已提交
364
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
365
    // #END
M
mahaifeng 已提交
366
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
367
  }
M
mahaifeng 已提交
368

M
mahaifeng 已提交
369 370
  testSome() {
    // #TEST Uint8Array.some
371 372 373 374
    const uint8_t1 = new Uint8Array([8, 20, 30, 40, 50]);
    const positives_t1 = new Uint8Array([10, 20, 30, 40, 50]);

    let res_t1 = uint8_t1.some((element : number, index : number, array : Uint8Array) : boolean =>
M
mahaifeng 已提交
375
      element < 10
M
mahaifeng 已提交
376
    );
377
    console.log('uint8_t1', res_t1); // true
M
mahaifeng 已提交
378

379
    let res_t2 = positives_t1.some((element : number, index : number, array : Uint8Array) : boolean =>
M
mahaifeng 已提交
380
      element < 0
M
mahaifeng 已提交
381
    );
382 383
    console.log('positives_t1', res_t2); // false

M
mahaifeng 已提交
384
    // #END
385 386
    expect(res_t1).toEqual(true);
    expect(res_t2).toEqual(false);
M
mahaifeng 已提交
387
  }
M
mahaifeng 已提交
388

389

M
mahaifeng 已提交
390
  // #endif
M
mahaifeng 已提交
391
}