TUInt8Array.uts 6.9 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import {
  describe,
  test,
  expect,
  Result
} from './tests.uts'

export class TUint8Array {
  test() {
    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 36 37 38 39 40
  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 已提交
41
  testMAX() {
M
mahaifeng 已提交
42 43 44
    let uint8 = new Uint8Array(16);
    uint8[0] = 255;
    expect(uint8[0]).toEqual(255);
M
mahaifeng 已提交
45 46 47 48
  }

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

  testSet() {
M
mahaifeng 已提交
55
    let uint8 = new Uint8Array(8);
M
mahaifeng 已提交
56
    var array = [1, 2, 3]
M
mahaifeng 已提交
57 58
    uint8.set(array, 1);
    expect(uint8.toString()).toEqual("0,1,2,3,0,0,0,0");
M
mahaifeng 已提交
59 60 61 62
  }

  testCopyWith() {
    console.log("testCopyWith 1")
M
mahaifeng 已提交
63 64 65
    let uint8 = new Uint8Array(8);
    uint8.set([1, 2, 3], 1);
    uint8.copyWithin(3, 0, 3);
M
mahaifeng 已提交
66

M
mahaifeng 已提交
67
    expect(uint8.toString()).toEqual("0,1,2,0,1,2,0,0");
M
mahaifeng 已提交
68 69 70 71
  }

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

  testFill() {
M
mahaifeng 已提交
78 79
    let uint8 = new Uint8Array([1, 2, 3]).fill(4);
    expect(uint8.toString()).toEqual("4,4,4");
M
mahaifeng 已提交
80

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

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

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

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

  testFilter() {
    // const isBelowThreshold = (currentValue: number, index: number, _): boolean => currentValue >= 10;
M
mahaifeng 已提交
96

M
mahaifeng 已提交
97
    let uint8 = new Uint8Array([12, 5, 8, 44]).filter((value: number, _: number, _a: Uint8Array): boolean => value >=
M
mahaifeng 已提交
98
      10);
M
mahaifeng 已提交
99
    expect(uint8.toString()).toEqual("12,44");
M
mahaifeng 已提交
100 101 102
  }

  find() {
M
mahaifeng 已提交
103 104
    let uint8 = new Uint8Array([4, 5, 8, 12]);
    let res = uint8.find((value: number, _: number, _a: Uint8Array): boolean => value > 5);
M
mahaifeng 已提交
105 106 107 108
    expect(res).toEqual(8);
  }

  findIndex() {
M
mahaifeng 已提交
109 110
    let uint8 = new Uint8Array([4, 6, 8, 12]);
    let res = uint8.findIndex((value: number, _: number, _a: Uint8Array): boolean => value > 100);
M
mahaifeng 已提交
111 112
    expect(res).toEqual(-1);

M
mahaifeng 已提交
113 114
    let uuint8 = new Uint8Array([4, 6, 7, 120]);
    res = uuint8.findIndex((value: number, _: number, _a: Uint8Array): boolean => value > 100);
M
mahaifeng 已提交
115 116 117 118
    expect(res).toEqual(3);
  }

  foreach() {
M
mahaifeng 已提交
119
    new Uint8Array([0, 1, 2, 3]).forEach((value: number, index: number, _: Uint8Array) => {
M
mahaifeng 已提交
120 121 122 123 124 125 126 127
      console.log(`a[${index}] = ${value}`);
    });
  }

  iterator() {
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let entries = arr.entries();
    expect(entries.next().value[1]).toEqual(10);
M
mahaifeng 已提交
128

M
mahaifeng 已提交
129 130 131
  }

  includes() {
M
mahaifeng 已提交
132 133
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.includes(2);
M
mahaifeng 已提交
134 135
    expect(res).toEqual(true);

M
mahaifeng 已提交
136
    res = uint8.includes(4);
M
mahaifeng 已提交
137 138
    expect(res).toEqual(false);

M
mahaifeng 已提交
139
    res = uint8.includes(3, 3);
M
mahaifeng 已提交
140 141 142 143
    expect(res).toEqual(false);
  }

  indexOf() {
M
mahaifeng 已提交
144 145
    let uint8 = new Uint8Array([2, 5, 9]);
    let res = uint8.indexOf(2);
M
mahaifeng 已提交
146 147
    expect(res).toEqual(0);

M
mahaifeng 已提交
148
    res = uint8.indexOf(7);
M
mahaifeng 已提交
149 150
    expect(res).toEqual(-1);

M
mahaifeng 已提交
151
    res = uint8.indexOf(9, 2);
M
mahaifeng 已提交
152 153
    expect(res).toEqual(2);

M
mahaifeng 已提交
154
    res = uint8.indexOf(2, -1);
M
mahaifeng 已提交
155 156
    expect(res).toEqual(-1);

M
mahaifeng 已提交
157
    res = uint8.indexOf(2, -3);
M
mahaifeng 已提交
158 159 160 161
    expect(res).toEqual(0);
  }

  join() {
M
mahaifeng 已提交
162 163
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.join();
M
mahaifeng 已提交
164 165
    expect(res).toEqual("1,2,3");

M
mahaifeng 已提交
166
    res = uint8.join(" / ");
M
mahaifeng 已提交
167 168
    expect(res).toEqual("1 / 2 / 3");

M
mahaifeng 已提交
169
    res = uint8.join("");
M
mahaifeng 已提交
170 171 172 173 174 175 176 177 178 179 180
    expect(res).toEqual("123");
  }

  keys() {
    let arr = new Uint8Array([10, 20, 30, 40, 50]);
    let keys = arr.keys();
    expect(keys.next().value).toEqual(0);
  }

  map() {
    let numbers = new Uint8Array([1, 4, 9]);
M
mahaifeng 已提交
181
    let doubles = numbers.map((value: number, _: number, _a: Uint8Array): number => value * 2);
M
mahaifeng 已提交
182 183 184 185 186 187
    expect(numbers.toString()).toEqual("1,4,9");
    expect(doubles.toString()).toEqual("2,8,18");
  }

  reduce() {
    let total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
188
    let res = total.reduce((accumulator: number, currentValue: number, _: number, _a: Uint8Array): number =>
M
mahaifeng 已提交
189
      accumulator + currentValue);
M
mahaifeng 已提交
190
    expect(res).toEqual(6);
M
mahaifeng 已提交
191 192

    total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
193
    res = total.reduce((accumulator: number, currentValue: number, _: number, _a: Uint8Array): number => accumulator +
M
mahaifeng 已提交
194
      currentValue, 8);
M
mahaifeng 已提交
195
    expect(res).toEqual(14);
M
mahaifeng 已提交
196 197 198 199
  }

  reduceRight() {
    let total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
200
    let res = total.reduceRight((accumulator: number, currentValue: number, _: number, _a: Uint8Array): number =>
M
mahaifeng 已提交
201
      accumulator + currentValue);
M
mahaifeng 已提交
202
    expect(res).toEqual(6);
M
mahaifeng 已提交
203 204

    total = new Uint8Array([0, 1, 2, 3]);
M
mahaifeng 已提交
205
    res = total.reduceRight((accumulator: number, currentValue: number, _: number, _a: Uint8Array): number =>
M
mahaifeng 已提交
206
      accumulator + currentValue, 8);
M
mahaifeng 已提交
207
    expect(res).toEqual(14);
M
mahaifeng 已提交
208 209 210
  }

  reverse() {
M
mahaifeng 已提交
211 212 213
    let uint8 = new Uint8Array([1, 2, 3]);
    uint8.reverse();
    expect(uint8.toString()).toEqual("3,2,1");
M
mahaifeng 已提交
214 215 216
  }

  slice() {
M
mahaifeng 已提交
217 218
    let uint8 = new Uint8Array([1, 2, 3]);
    let res = uint8.slice(1);
M
mahaifeng 已提交
219 220
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
221
    res = uint8.slice(2);
M
mahaifeng 已提交
222 223
    expect(res.toString()).toEqual("3");

M
mahaifeng 已提交
224
    res = uint8.slice(-2);
M
mahaifeng 已提交
225 226
    expect(res.toString()).toEqual("2,3");

M
mahaifeng 已提交
227
    res = uint8.slice(0, 1);
M
mahaifeng 已提交
228 229 230 231 232 233 234 235
    expect(res.toString()).toEqual("1");
  }

  sort() {
    let numbers = new Uint8Array([40, 1, 5]);
    numbers.sort();
    expect(numbers.toString()).toEqual("1,5,40");

236
    numbers.sort((a, b): number => a - b);
M
mahaifeng 已提交
237 238 239 240 241
    expect(numbers.toString()).toEqual("1,5,40");
  }

  subarray() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
242 243 244
    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 已提交
245

M
mahaifeng 已提交
246
    let sub = uint8.subarray(0, 4);
M
mahaifeng 已提交
247 248 249 250 251 252 253
    expect(sub.toString()).toEqual("1,2,3,0");
  }

  values() {
    let arr = new Uint8Array([1, 2, 3]);
    let values = arr.values();
    expect(values.next().value).toEqual(1);
M
mahaifeng 已提交
254

M
mahaifeng 已提交
255 256 257 258
  }

  arrayBufferSlice() {
    let buffer = new ArrayBuffer(16);
M
mahaifeng 已提交
259 260 261
    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 已提交
262 263 264 265 266

    let res = buffer.slice(4, 12);
    let sliced = new Uint8Array(res);
    expect(sliced[0]).toEqual(42);
  }
M
mahaifeng 已提交
267
}