TUInt8Array.uts 8.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 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 64
    uint8.set(array, 1);
    expect(uint8.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
65
    // #END
M
mahaifeng 已提交
66 67 68
  }

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

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

M
mahaifeng 已提交
87

M
mahaifeng 已提交
88
  testFill() {
M
mahaifeng 已提交
89
    // #TEST Uint8Array.fill
M
mahaifeng 已提交
90 91
    let uint8 = new Uint8Array([1, 2, 3]).fill(4);
    expect(uint8.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
92

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

M
mahaifeng 已提交
96 97
    uint8 = new Uint8Array([1, 2, 3]).fill(4, 1, 2);
    expect(uint8.toString()).toEqual("1,4,3");
M
mahaifeng 已提交
98

M
mahaifeng 已提交
99 100
    uint8 = new Uint8Array([1, 2, 3]).fill(4, 1, 1);
    expect(uint8.toString()).toEqual("1,2,3");
M
mahaifeng 已提交
101

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

  testFilter() {
M
mahaifeng 已提交
108
    // #TEST Uint8Array.filter
M
mahaifeng 已提交
109
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;
M
mahaifeng 已提交
110

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

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

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

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

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

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

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

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

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

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

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

M
mahaifeng 已提交
179
    res = uint8.indexOf(2, -3);
M
mahaifeng 已提交
180
    expect(res).toEqual(0);
M
mahaifeng 已提交
181
    // #END
M
mahaifeng 已提交
182 183 184
  }

  join() {
M
mahaifeng 已提交
185
    // #TEST Uint8Array.join
M
mahaifeng 已提交
186 187
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.join();
M
mahaifeng 已提交
188 189
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
190
    res = uint8.join(" / ");
M
mahaifeng 已提交
191 192
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
193
    res = uint8.join("");
M
mahaifeng 已提交
194
    expect(res).toEqual("123");
M
mahaifeng 已提交
195
    // #END
M
mahaifeng 已提交
196 197
  }
  keys() {
M
mahaifeng 已提交
198
    // #TEST Uint8Array.keys
M
mahaifeng 已提交
199 200 201
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
    expect(keys.next().value).toEqual(0);
M
mahaifeng 已提交
202
    // #END
M
mahaifeng 已提交
203 204 205
  }

  map() {
M
mahaifeng 已提交
206
    // #TEST Uint8Array.map
M
mahaifeng 已提交
207
    let numbers = new Uint8Array([1, 4, 9]);
M
mahaifeng 已提交
208
    let doubles = numbers.map((value : number, _ : number, _a : Uint8Array) : number => value * 2);
M
mahaifeng 已提交
209 210
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
M
mahaifeng 已提交
211
    // #END
M
mahaifeng 已提交
212 213 214
  }

  reduce() {
M
mahaifeng 已提交
215
    // #TEST Uint8Array.reduce
M
mahaifeng 已提交
216
    let total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
217
    let res = total.reduce((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
218
      accumulator + currentValue);
M
mahaifeng 已提交
219
    expect(res).toEqual(6);
M
mahaifeng 已提交
220 221

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

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

    total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
236
    res = total.reduceRight((accumulator : number, currentValue : number, _ : number, _a : Uint8Array) : number =>
M
mahaifeng 已提交
237
      accumulator + currentValue, 8);
M
mahaifeng 已提交
238
    expect(res).toEqual(14);
M
mahaifeng 已提交
239
    // #END
M
mahaifeng 已提交
240 241
  }
  reverse() {
M
mahaifeng 已提交
242
    // #TEST Uint8Array.reverse
M
mahaifeng 已提交
243 244 245
    let uint8 = new Uint8Array([1, 2, 3]);
    uint8.reverse();
    expect(uint8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
246
    // #END
M
mahaifeng 已提交
247 248 249
  }

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

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

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

M
mahaifeng 已提交
261
    res = uint8.slice(0, 1);
M
mahaifeng 已提交
262
    expect(res.toString()).toEqual("1");
M
mahaifeng 已提交
263
    // #END
M
mahaifeng 已提交
264 265 266
  }

  sort() {
M
mahaifeng 已提交
267
    // #TEST Uint8Array.sort
M
mahaifeng 已提交
268 269 270 271
    let numbers = new Uint8Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

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

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

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

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

  arrayBufferSlice() {
M
mahaifeng 已提交
298

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

    let res = buffer.slice(4, 12);
    let sliced = new Uint8Array(res);
    expect(sliced[0]).toEqual(42);
M
mahaifeng 已提交
308 309
    // #END

M
mahaifeng 已提交
310 311 312 313 314 315 316 317 318
  }
  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
    )
    expect(res).toEqual(true);
M
mahaifeng 已提交
319
    console.log('uint8', res)
M
mahaifeng 已提交
320 321 322
    res = positives.some((element : number, index : number, array : Uint8Array) : boolean =>
      element < 0
    )
M
mahaifeng 已提交
323
    console.log('uint8', res)
M
mahaifeng 已提交
324 325 326
    expect(res).toEqual(false);
    // #END
  }
M
mahaifeng 已提交
327
  // #endif
M
mahaifeng 已提交
328
}