TUInt8Array.uts 10.5 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 40 41 42 43 44 45
  of() {
    // #TEST Uint8Array.of
    var array = Uint8Array.of(1, 2, 3)
    console.log(array.toString()); // '1,2,3'
    // #END
    expect(array.toString()).toEqual("1,2,3");
  }
M
mahaifeng 已提交
46
  from() {
M
mahaifeng 已提交
47
    // #TEST Uint8Array.from
M
mahaifeng 已提交
48 49
    var s = new Set([1, 2, 3]);
    var unit8 = Uint8Array.from(s);
M
mahaifeng 已提交
50 51
    console.log(unit8.toString()); // '1,2,3'
    // #END
M
mahaifeng 已提交
52 53
    expect(unit8.toString()).toEqual('1,2,3');
  }
M
mahaifeng 已提交
54
  testMAX() {
M
mahaifeng 已提交
55 56 57
    let uint8 = new Uint8Array(16);
    uint8[0] = 255;
    expect(uint8[0]).toEqual(255);
M
mahaifeng 已提交
58 59 60 61
  }

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

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

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

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

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

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

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

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

111 112
    let uint8_t5 = new Uint8Array([1, 2, 3]).fill(4, -3, -2);
    console.log(uint8_t5.toString()); // "4,2,3"
M
mahaifeng 已提交
113
    // #END
114 115 116 117 118 119

    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 已提交
120 121
  }

122

M
mahaifeng 已提交
123
  testFilter() {
M
mahaifeng 已提交
124 125
    // #TEST Uint8Array.filter
    let uint8 = new Uint8Array([12, 5, 8, 44]).filter((value : number, _ : number, _a : Uint8Array) : boolean => value >= 10);
M
mahaifeng 已提交
126
    console.log(uint8.toString()); // "12,44"
M
mahaifeng 已提交
127
    // #END
M
mahaifeng 已提交
128
    expect(uint8.toString()).toEqual("12,44");
M
mahaifeng 已提交
129
  }
M
mahaifeng 已提交
130

M
mahaifeng 已提交
131
  find() {
M
mahaifeng 已提交
132
    // #TEST Uint8Array.find
M
mahaifeng 已提交
133
    let uint8 = new Uint8Array([4, 5, 8, 12]);
M
mahaifeng 已提交
134
    let res = uint8.find((value : number, _ : number, _a : Uint8Array) : boolean => value > 5);
M
mahaifeng 已提交
135
    console.log(res); // 8
M
mahaifeng 已提交
136
    // #END
M
mahaifeng 已提交
137
    expect(res).toEqual(8);
M
mahaifeng 已提交
138 139
  }
  findIndex() {
M
mahaifeng 已提交
140
    // #TEST Uint8Array.findIndex
141 142 143
    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 已提交
144

145 146 147
    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 已提交
148
    // #END
149 150 151

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

154

M
mahaifeng 已提交
155
  foreach() {
M
mahaifeng 已提交
156 157
    // #TEST Uint8Array.forEach
    new Uint8Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Uint8Array) => {
M
mahaifeng 已提交
158 159
      console.log(`a[${index}] = ${value}`);
    });
M
mahaifeng 已提交
160
    // #END
M
mahaifeng 已提交
161
  }
M
mahaifeng 已提交
162

M
mahaifeng 已提交
163
  iterator() {
M
mahaifeng 已提交
164
    // #TEST Uint8Array.entries
M
mahaifeng 已提交
165 166
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
M
mahaifeng 已提交
167 168
    let entry = entries.next().value;
    console.log(entry[1]); // 10
M
mahaifeng 已提交
169
    // #END
M
mahaifeng 已提交
170
    expect(entry[1]).toEqual(10);
M
mahaifeng 已提交
171 172
  }
  includes() {
M
mahaifeng 已提交
173
    // #TEST Uint8Array.includes
174 175 176
    let uint8_t1 = new Uint8Array([1, 2, 3]);
    let res1 = uint8_t1.includes(2);
    console.log(res1); // true
M
mahaifeng 已提交
177

178 179
    let res2 = uint8_t1.includes(4);
    console.log(res2); // false
M
mahaifeng 已提交
180

181 182
    let res3 = uint8_t1.includes(3, 3);
    console.log(res3); // false
M
mahaifeng 已提交
183
    // #END
184 185 186 187

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

M
mahaifeng 已提交
190

M
mahaifeng 已提交
191
  indexOf() {
M
mahaifeng 已提交
192
    // #TEST Uint8Array.indexOf
193 194 195
    let uint8_t1 = new Uint8Array([2, 5, 9]);
    let res1 = uint8_t1.indexOf(2);
    console.log(res1); // 0
M
mahaifeng 已提交
196

197 198
    let res2 = uint8_t1.indexOf(7);
    console.log(res2); // -1
M
mahaifeng 已提交
199

200 201
    let res3 = uint8_t1.indexOf(9, 2);
    console.log(res3); // 2
M
mahaifeng 已提交
202

203 204
    let res4 = uint8_t1.indexOf(2, -1);
    console.log(res4); // -1
M
mahaifeng 已提交
205

206 207
    let res5 = uint8_t1.indexOf(2, -3);
    console.log(res5); // 0
M
mahaifeng 已提交
208
    // #END
209 210 211 212 213 214

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

217

M
mahaifeng 已提交
218
  join() {
M
mahaifeng 已提交
219
    // #TEST Uint8Array.join
220 221 222
    let uint8_t1 = new Uint8Array([1, 2, 3]);
    let res1 = uint8_t1.join();
    console.log(res1); // "1,2,3"
M
mahaifeng 已提交
223

224 225
    let res2 = uint8_t1.join(" / ");
    console.log(res2); // "1 / 2 / 3"
M
mahaifeng 已提交
226

227 228
    let res3 = uint8_t1.join("");
    console.log(res3); // "123"
M
mahaifeng 已提交
229
    // #END
230 231 232 233

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

236

M
mahaifeng 已提交
237
  keys() {
M
mahaifeng 已提交
238
    // #TEST Uint8Array.keys
M
mahaifeng 已提交
239 240
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
241 242
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
243
    // #END
M
mahaifeng 已提交
244
    expect(value).toEqual(0);
M
mahaifeng 已提交
245 246 247
  }

  map() {
M
mahaifeng 已提交
248
    // #TEST Uint8Array.map
M
mahaifeng 已提交
249
    let numbers = new Uint8Array([1, 4, 9]);
M
mahaifeng 已提交
250
    let doubles = numbers.map((value : number, _ : number, _a : Uint8Array) : number => value * 2);
M
mahaifeng 已提交
251 252 253
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
254 255 256 257
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }
  reduce() {
M
mahaifeng 已提交
258
    // #TEST Uint8Array.reduce
259 260
    let uint8_t1 = new Uint8Array([0, 1, 2, 3]);
    let res1 = uint8_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
261
      accumulator + currentValue);
262
    console.log(res1); // 6
M
mahaifeng 已提交
263

264
    let res2 = uint8_t1.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
265
      accumulator + currentValue, 8);
266
    console.log(res2); // 14
M
mahaifeng 已提交
267
    // #END
268 269 270

    expect(res1).toEqual(6);
    expect(res2).toEqual(14);
M
mahaifeng 已提交
271 272 273
  }

  reduceRight() {
M
mahaifeng 已提交
274
    // #TEST Uint8Array.reduceRight
275 276
    let uint8_t1 = new Uint8Array([0, 1, 2, 3]);
    let res1 = uint8_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
277
      accumulator + currentValue);
278
    console.log(res1); // 6
M
mahaifeng 已提交
279

280
    let res2 = uint8_t1.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
281
      accumulator + currentValue, 8);
282
    console.log(res2); // 14
M
mahaifeng 已提交
283
    // #END
284 285 286

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

289

M
mahaifeng 已提交
290
  reverse() {
M
mahaifeng 已提交
291
    // #TEST Uint8Array.reverse
M
mahaifeng 已提交
292 293
    let uint8 = new Uint8Array([1, 2, 3]);
    uint8.reverse();
M
mahaifeng 已提交
294
    console.log(uint8.toString()); // "3,2,1"
M
mahaifeng 已提交
295
    // #END
M
mahaifeng 已提交
296
    expect(uint8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
297 298 299
  }

  slice() {
M
mahaifeng 已提交
300
    // #TEST Uint8Array.slice
301 302 303
    let uint8_t1 = new Uint8Array([1, 2, 3]);
    let res1 = uint8_t1.slice(1);
    console.log(res1.toString()); // "2,3"
M
mahaifeng 已提交
304

305 306
    let res2 = uint8_t1.slice(2);
    console.log(res2.toString()); // "3"
M
mahaifeng 已提交
307

308 309
    let res3 = uint8_t1.slice(-2);
    console.log(res3.toString()); // "2,3"
M
mahaifeng 已提交
310

311 312
    let res4 = uint8_t1.slice(0, 1);
    console.log(res4.toString()); // "1"
M
mahaifeng 已提交
313
    // #END
314 315 316 317 318

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

321

M
mahaifeng 已提交
322
  sort() {
M
mahaifeng 已提交
323
    // #TEST Uint8Array.sort
324 325 326
    let numbers_t1 = new Uint8Array([40, 1, 5]);
    numbers_t1.sort();
    console.log(numbers_t1.toString()); // "1,5,40"
M
mahaifeng 已提交
327

328 329 330
    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 已提交
331
    // #END
332 333 334

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

337

M
mahaifeng 已提交
338
  subarray() {
M
mahaifeng 已提交
339
    // #TEST Uint8Array.subarray
M
mahaifeng 已提交
340
    let buffer = new ArrayBuffer(16);
341 342 343
    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 已提交
344

345 346
    let sub_t1 = uint8_t1.subarray(0, 4);
    console.log(sub_t1.toString()); // "1,2,3,0"
M
mahaifeng 已提交
347
    // #END
348 349 350

    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 已提交
351 352 353
  }

  values() {
M
mahaifeng 已提交
354
    // #TEST Uint8Array.values
M
mahaifeng 已提交
355 356
    let arr = new Uint8Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
357 358
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
359
    // #END
M
mahaifeng 已提交
360
    expect(value).toEqual(1);
M
mahaifeng 已提交
361 362 363
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
364
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
365
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
366 367
    let uint8 = new Uint8Array(buffer);
    uint8[4] = 42;
M
mahaifeng 已提交
368
    console.log(uint8.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
369 370 371

    let res = buffer.slice(4, 12);
    let sliced = new Uint8Array(res);
M
mahaifeng 已提交
372
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
373
    // #END
M
mahaifeng 已提交
374
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
375
  }
M
mahaifeng 已提交
376

M
mahaifeng 已提交
377 378
  testSome() {
    // #TEST Uint8Array.some
379 380 381 382
    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 已提交
383
      element < 10
M
mahaifeng 已提交
384
    );
385
    console.log('uint8_t1', res_t1); // true
M
mahaifeng 已提交
386

387
    let res_t2 = positives_t1.some((element : number, index : number, array : Uint8Array) : boolean =>
M
mahaifeng 已提交
388
      element < 0
M
mahaifeng 已提交
389
    );
390 391
    console.log('positives_t1', res_t2); // false

M
mahaifeng 已提交
392
    // #END
393 394
    expect(res_t1).toEqual(true);
    expect(res_t2).toEqual(false);
M
mahaifeng 已提交
395
  }
M
mahaifeng 已提交
396

397

M
mahaifeng 已提交
398
  // #endif
M
mahaifeng 已提交
399
}