TUInt8Array.uts 9.7 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 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();
M
mahaifeng 已提交
36
    // #endif
M
mahaifeng 已提交
37
  }
M
mahaifeng 已提交
38 39
  // #ifdef (UNI-APP-X && APP-ANDROID) || WEB

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
M
mahaifeng 已提交
91
    let uint8 = new Uint8Array([1, 2, 3]).fill(4);
M
mahaifeng 已提交
92
    console.log(uint8.toString()); // "4,4,4"
M
mahaifeng 已提交
93

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

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

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

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

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

M
mahaifeng 已提交
117
  find() {
M
mahaifeng 已提交
118
    // #TEST Uint8Array.find
M
mahaifeng 已提交
119
    let uint8 = new Uint8Array([4, 5, 8, 12]);
M
mahaifeng 已提交
120
    let res = uint8.find((value : number, _ : number, _a : Uint8Array) : 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 Uint8Array.findIndex
M
mahaifeng 已提交
128
    let uint8 = new Uint8Array([4, 6, 8, 12]);
M
mahaifeng 已提交
129
    let res = uint8.findIndex((value : number, _ : number, _a : Uint8Array) : boolean => value > 100);
M
mahaifeng 已提交
130
    console.log(res); // -1
M
mahaifeng 已提交
131

M
mahaifeng 已提交
132
    let uuint8 = new Uint8Array([4, 6, 7, 120]);
M
mahaifeng 已提交
133
    res = uuint8.findIndex((value : number, _ : number, _a : Uint8Array) : 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 Uint8Array.forEach
    new Uint8Array([0, 1, 2, 3]).forEach((value : number, index : number, _ : Uint8Array) => {
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 Uint8Array.entries
M
mahaifeng 已提交
149 150
    let arr = new Uint8Array([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
  }
  includes() {
M
mahaifeng 已提交
157
    // #TEST Uint8Array.includes
M
mahaifeng 已提交
158 159
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.includes(2);
M
mahaifeng 已提交
160
    console.log(res); // true
M
mahaifeng 已提交
161

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

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

M
mahaifeng 已提交
171

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

    // expect(res).toEqual(0);
M
mahaifeng 已提交
179

M
mahaifeng 已提交
180
    res = uint8.indexOf(7);
M
mahaifeng 已提交
181 182 183
    console.log(res); // -1

    // expect(res).toEqual(-1);
M
mahaifeng 已提交
184

M
mahaifeng 已提交
185
    res = uint8.indexOf(9, 2);
M
mahaifeng 已提交
186 187 188
    console.log(res); // 2

    // expect(res).toEqual(2);
M
mahaifeng 已提交
189

M
mahaifeng 已提交
190
    res = uint8.indexOf(2, -1);
M
mahaifeng 已提交
191 192 193
    console.log(res); // -1

    // expect(res).toEqual(-1);
M
mahaifeng 已提交
194

M
mahaifeng 已提交
195
    res = uint8.indexOf(2, -3);
M
mahaifeng 已提交
196
    console.log(res); // 0
M
mahaifeng 已提交
197
    // #END
M
mahaifeng 已提交
198
    expect(res).toEqual(0);
M
mahaifeng 已提交
199 200 201
  }

  join() {
M
mahaifeng 已提交
202
    // #TEST Uint8Array.join
M
mahaifeng 已提交
203 204
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.join();
M
mahaifeng 已提交
205 206 207
    console.log(res); // "1,2,3"

    // expect(res).toEqual("1,2,3");
M
mahaifeng 已提交
208

M
mahaifeng 已提交
209
    res = uint8.join(" / ");
M
mahaifeng 已提交
210 211 212
    console.log(res); // "1 / 2 / 3"

    // expect(res).toEqual("1 / 2 / 3");
M
mahaifeng 已提交
213

M
mahaifeng 已提交
214
    res = uint8.join("");
M
mahaifeng 已提交
215
    console.log(res); // "123"
M
mahaifeng 已提交
216
    // #END
M
mahaifeng 已提交
217
    expect(res).toEqual("123");
M
mahaifeng 已提交
218
  }
M
mahaifeng 已提交
219

M
mahaifeng 已提交
220
  keys() {
M
mahaifeng 已提交
221
    // #TEST Uint8Array.keys
M
mahaifeng 已提交
222 223
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
M
mahaifeng 已提交
224 225
    let value = keys.next().value
    console.log(value); // 0
M
mahaifeng 已提交
226
    // #END
M
mahaifeng 已提交
227
    expect(value).toEqual(0);
M
mahaifeng 已提交
228 229 230
  }

  map() {
M
mahaifeng 已提交
231
    // #TEST Uint8Array.map
M
mahaifeng 已提交
232
    let numbers = new Uint8Array([1, 4, 9]);
M
mahaifeng 已提交
233
    let doubles = numbers.map((value : number, _ : number, _a : Uint8Array) : number => value * 2);
M
mahaifeng 已提交
234 235 236
    console.log(numbers.toString()); // "1,4,9"
    console.log(doubles.toString()); // "2,8,18"
    // #END
M
mahaifeng 已提交
237 238 239 240
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }
  reduce() {
M
mahaifeng 已提交
241
    // #TEST Uint8Array.reduce
M
mahaifeng 已提交
242
    let total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
243
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
244
      accumulator + currentValue);
M
mahaifeng 已提交
245 246 247
    console.log(res); // 6

    // expect(res).toEqual(6);
M
mahaifeng 已提交
248 249

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

  reduceRight() {
M
mahaifeng 已提交
258
    // #TEST Uint8Array.reduceRight
M
mahaifeng 已提交
259
    let total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
260
    let res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
261
      accumulator + currentValue);
M
mahaifeng 已提交
262 263 264
    console.log(res); // 6

    // expect(res).toEqual(6);
M
mahaifeng 已提交
265 266

    total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
267
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
268
      accumulator + currentValue, 8);
M
mahaifeng 已提交
269
    console.log(res); // 14
M
mahaifeng 已提交
270
    // #END
M
mahaifeng 已提交
271
    expect(res).toEqual(14);
M
mahaifeng 已提交
272
  }
M
mahaifeng 已提交
273

M
mahaifeng 已提交
274
  reverse() {
M
mahaifeng 已提交
275
    // #TEST Uint8Array.reverse
M
mahaifeng 已提交
276 277
    let uint8 = new Uint8Array([1, 2, 3]);
    uint8.reverse();
M
mahaifeng 已提交
278
    console.log(uint8.toString()); // "3,2,1"
M
mahaifeng 已提交
279
    // #END
M
mahaifeng 已提交
280
    expect(uint8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
281 282 283
  }

  slice() {
M
mahaifeng 已提交
284
    // #TEST Uint8Array.slice
M
mahaifeng 已提交
285 286
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.slice(1);
M
mahaifeng 已提交
287 288 289
    console.log(res.toString()); // "2,3"

    // expect(res.toString()).toEqual("2,3");
M
mahaifeng 已提交
290

M
mahaifeng 已提交
291
    res = uint8.slice(2);
M
mahaifeng 已提交
292 293 294
    console.log(res.toString()); // "3"

    // expect(res.toString()).toEqual("3");
M
mahaifeng 已提交
295

M
mahaifeng 已提交
296
    res = uint8.slice(-2);
M
mahaifeng 已提交
297 298 299
    console.log(res.toString()); // "2,3"

    // expect(res.toString()).toEqual("2,3");
M
mahaifeng 已提交
300

M
mahaifeng 已提交
301
    res = uint8.slice(0, 1);
M
mahaifeng 已提交
302
    console.log(res.toString()); // "1"
M
mahaifeng 已提交
303
    // #END
M
mahaifeng 已提交
304
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
305 306 307
  }

  sort() {
M
mahaifeng 已提交
308
    // #TEST Uint8Array.sort
M
mahaifeng 已提交
309 310
    let numbers = new Uint8Array([40, 1, 5]);
    numbers.sort();
M
mahaifeng 已提交
311 312 313
    console.log(numbers.toString()); // "1,5,40"

    // expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
314

M
mahaifeng 已提交
315
    numbers.sort((a, b) : number => a - b);
M
mahaifeng 已提交
316
    console.log(numbers.toString()); // "1,5,40"
M
mahaifeng 已提交
317
    // #END
M
mahaifeng 已提交
318
    expect(numbers.toString()).toEqual("1,5,40");
M
mahaifeng 已提交
319 320 321
  }

  subarray() {
M
mahaifeng 已提交
322
    // #TEST Uint8Array.subarray
M
mahaifeng 已提交
323
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
324 325
    let uint8 = new Uint8Array(buffer);
    uint8.set([1, 2, 3]);
M
mahaifeng 已提交
326 327 328
    console.log(uint8.toString()); // "1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0"

    // expect(uint8.toString()).toEqual("1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0");
M
mahaifeng 已提交
329

M
mahaifeng 已提交
330
    let sub = uint8.subarray(0, 4);
M
mahaifeng 已提交
331
    console.log(sub.toString()); // "1,2,3,0"
M
mahaifeng 已提交
332
    // #END
M
mahaifeng 已提交
333
    expect(sub.toString()).toEqual("1,2,3,0");
M
mahaifeng 已提交
334 335 336
  }

  values() {
M
mahaifeng 已提交
337
    // #TEST Uint8Array.values
M
mahaifeng 已提交
338 339
    let arr = new Uint8Array([1, 2, 3]);
    let values = arr.values();
M
mahaifeng 已提交
340 341
    let value = values.next().value
    console.log(value); // 1
M
mahaifeng 已提交
342
    // #END
M
mahaifeng 已提交
343
    expect(value).toEqual(1);
M
mahaifeng 已提交
344 345 346
  }

  arrayBufferSlice() {
M
mahaifeng 已提交
347
    // #TEST ArrayBuffer.slice
M
mahaifeng 已提交
348
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
349 350
    let uint8 = new Uint8Array(buffer);
    uint8[4] = 42;
M
mahaifeng 已提交
351
    console.log(uint8.toString()); // "0,0,0,0,42,0,0,0,0,0,0,0,0,0,0,0"
M
mahaifeng 已提交
352 353 354

    let res = buffer.slice(4, 12);
    let sliced = new Uint8Array(res);
M
mahaifeng 已提交
355
    console.log(sliced[0]); // 42
M
mahaifeng 已提交
356
    // #END
M
mahaifeng 已提交
357
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
358
  }
M
mahaifeng 已提交
359

M
mahaifeng 已提交
360 361 362 363 364 365
  testSome() {
    // #TEST Uint8Array.some
    const uint8 = new Uint8Array([8, 20, 30, 40, 50]);
    const positives = new Uint8Array([10, 20, 30, 40, 50]);
    var res = uint8.some((element : number, index : number, array : Uint8Array) : boolean =>
      element < 10
M
mahaifeng 已提交
366 367 368 369 370
    );
    console.log('uint8', res); // true

    // expect(res).toEqual(true);

M
mahaifeng 已提交
371 372
    res = positives.some((element : number, index : number, array : Uint8Array) : boolean =>
      element < 0
M
mahaifeng 已提交
373 374
    );
    console.log('positives', res); // false
M
mahaifeng 已提交
375
    // #END
M
mahaifeng 已提交
376
    expect(res).toEqual(false);
M
mahaifeng 已提交
377
  }
M
mahaifeng 已提交
378

M
mahaifeng 已提交
379
  // #endif
M
mahaifeng 已提交
380
}