TDataView.uts 10.0 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
import {
  describe,
  test,
  expect,
  Result
} from './tests.uts'

export class TDataView {
  test() {
    // this.setDv();
    // this.setInt8();
    // this.setFloat32();
    // this.setFloat64();
    // this.setInt16();
    // this.setInt32();
    // this.setUint16();
    // this.setUint32();
    // this.setUint8();
    // this.testMix();
  }
M
mahaifeng 已提交
21
  // #ifdef  APP-ANDROID || WEB
M
mahaifeng 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  setConstructor() {
    let buffer = new ArrayBuffer(16);
    let dataview = new DataView(buffer);
    dataview.setInt8(1, 12);
    dataview.setFloat32(1, 34);
    expect(dataview.getInt8(1)).toEqual(66);
    expect(this.numberEquals(34, dataview.getFloat32(1))).toEqual(true);

    buffer = new ArrayBuffer(16);
    let view = new DataView(buffer, 1, 4);
    view.setInt16(1, 42); // 42
    expect(view.getInt16(1)).toEqual(42);
  }

  setInt8() {
M
mahaifeng 已提交
37
    // #TEST DataView.setInt8,DataView.getInt8
M
mahaifeng 已提交
38 39 40 41
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setInt8(1, 127); // Max signed 8-bit integer
42 43
    let ret = view.getInt8(1)
    console.log(ret)//127
M
mahaifeng 已提交
44
    // #END
45
    expect(ret).toEqual(127);
M
mahaifeng 已提交
46
  }
M
mahaifeng 已提交
47 48

  setFloat32() {
M
mahaifeng 已提交
49
    // #TEST DataView.setFloat32,DataView.getFloat32
M
mahaifeng 已提交
50 51 52
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
M
mahaifeng 已提交
53
    view.setFloat32(1, 3);
54 55
    let ret = view.getFloat32(1);
    console.log(ret); // 3
M
mahaifeng 已提交
56
    // #END
57
    expect(this.numberEquals(3, ret)).toEqual(true);
M
mahaifeng 已提交
58
  }
M
mahaifeng 已提交
59 60

  setFloat64() {
M
mahaifeng 已提交
61
    // #TEST DataView.setFloat64,DataView.getFloat64
M
mahaifeng 已提交
62 63 64 65
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setFloat64(1, Math.PI);
66 67
    let ret = view.getFloat64(1);
    console.log(ret); // 3.141592653589793
M
mahaifeng 已提交
68
    // #END
69
    expect(this.numberEquals(3.141592653589793, ret)).toEqual(true);
M
mahaifeng 已提交
70
  }
M
mahaifeng 已提交
71 72

  setInt16() {
M
mahaifeng 已提交
73
    // #TEST DataView.setInt16,DataView.getInt16
M
mahaifeng 已提交
74 75 76 77
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setInt16(1, 32767); // Max signed 16-bit integer
78 79
    let ret = view.getInt16(1);
    console.log(ret); // 32767
M
mahaifeng 已提交
80
    // #END
81
    expect(ret).toEqual(32767);
M
mahaifeng 已提交
82
  }
M
mahaifeng 已提交
83

84

M
mahaifeng 已提交
85
  setInt32() {
M
mahaifeng 已提交
86
    // #TEST DataView.setInt32,DataView.getInt32
M
mahaifeng 已提交
87 88 89 90
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setInt32(1, 2147483647); // Max signed 32-bit integer
91 92
    let ret = view.getInt32(1);
    console.log(ret); // 2147483647
M
mahaifeng 已提交
93
    // #END
94
    expect(this.numberEquals(2147483647, ret)).toEqual(true);
M
mahaifeng 已提交
95
  }
M
mahaifeng 已提交
96 97

  setUint16() {
M
mahaifeng 已提交
98
    // #TEST DataView.setUint16,DataView.getUint16
M
mahaifeng 已提交
99 100 101 102
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setUint16(1, 65535); // Max unsigned 16-bit integer
103 104
    let ret = view.getUint16(1);
    console.log(ret); // 65535
M
mahaifeng 已提交
105
    // #END
106
    expect(this.numberEquals(65535, ret)).toEqual(true);
M
mahaifeng 已提交
107 108 109
  }

  setUint32() {
M
mahaifeng 已提交
110
    // #TEST DataView.setUint32,DataView.getUint32
M
mahaifeng 已提交
111 112 113 114
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setUint32(1, 4294967295); // Max unsigned 32-bit integer
115 116
    let ret = view.getUint32(1);
    console.log(ret); // 4294967295
M
mahaifeng 已提交
117
    // #END
118
    expect(this.numberEquals(4294967295, ret)).toEqual(true);
M
mahaifeng 已提交
119
  }
M
mahaifeng 已提交
120 121

  setUint8() {
M
mahaifeng 已提交
122
    // #TEST DataView.setUint8,DataView.getUint8
M
mahaifeng 已提交
123 124 125 126
    // Create an ArrayBuffer with a size in bytes
    let buffer = new ArrayBuffer(16);
    let view = new DataView(buffer);
    view.setUint8(1, 255); // Max unsigned 8-bit integer
127 128
    let ret = view.getUint8(1);
    console.log(ret); // 255
M
mahaifeng 已提交
129
    // #END
130
    expect(this.numberEquals(255, ret)).toEqual(true);
M
mahaifeng 已提交
131 132
  }

M
mahaifeng 已提交
133 134

  numberEquals(a : number, b : number) : Boolean {
M
mahaifeng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    return a == b
  }


  doTestDataViewBigEndian() {
    this.doIterations(false, true);
  }

  doTestDataViewLittleEndian() {
    this.doIterations(true, true);
  }

  doTestTypedArrayBigEndian() {
    this.doIterations(false, false);
  }

  doTestTypedArrayLittleEndian() {
    this.doIterations(true, false);
  }

  doTestDataViewFloats() {
    this.doFloatIterations(true);
  }

  doTestTypedArrayFloats() {
    this.doFloatIterations(false);
  }

M
mahaifeng 已提交
163
  doIterations(littleEndian : boolean, dataView : boolean) {
M
mahaifeng 已提交
164 165 166 167 168 169 170 171 172 173 174
    var buffer = this.makeBuffer(1000, littleEndian);
    var iterations = 10;
    if (dataView) {
      for (var i = 0; i < iterations; i++)
        this.doOneIterationDV(buffer, littleEndian);
    } else {
      for (var i = 0; i < iterations; i++)
        this.doOneIterationJS(buffer, littleEndian);
    }
  }

M
mahaifeng 已提交
175
  makeBuffer(size : number, littleEndian : boolean) : ArrayBuffer {
176
    var buffer = new ArrayBuffer(1000 * 14);
M
mahaifeng 已提交
177
    var view = new DataView(buffer);
178
    for (var i = 0; i < 1000; ++i) {
M
mahaifeng 已提交
179 180 181 182 183 184 185 186 187 188
      view.setInt8(i * 14, i);
      view.setUint8(i * 14 + 1, i);
      view.setInt16(i * 14 + 2, i * i, littleEndian);
      view.setUint16(i * 14 + 4, i * i, littleEndian);
      view.setInt32(i * 14 + 6, i * i * i, littleEndian);
      view.setUint32(i * 14 + 10, i * i * i, littleEndian);
    }
    return buffer;
  }

M
mahaifeng 已提交
189
  doOneIterationDV(buffer : ArrayBuffer, littleEndian : boolean) {
M
mahaifeng 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202
    var xor = 0;
    var view = new DataView(buffer);
    for (var i = 0; i < view.byteLength; i += 14) {
      xor ^= view.getInt8(i);
      xor ^= view.getUint8(i + 1);
      xor ^= view.getInt16(i + 2, littleEndian);
      xor ^= view.getUint16(i + 4, littleEndian);
      xor ^= view.getInt32(i + 6, littleEndian);
      xor ^= view.getUint32(i + 10, littleEndian);
    }
    expect(xor).toEqual(0);
  }

M
mahaifeng 已提交
203
  doOneIterationJS(buffer : ArrayBuffer, littleEndian : boolean) {
M
mahaifeng 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
    var xor = 0;
    if (littleEndian) {
      var reader = new LittleEndian(buffer);
      for (var i = 0; i < buffer.byteLength; i += 14) {
        xor ^= reader.getInt8(i);
        xor ^= reader.getUint8(i + 1);
        xor ^= reader.getInt16(i + 2);
        xor ^= reader.getUint16(i + 4);
        xor ^= reader.getInt32(i + 6);
        xor ^= reader.getUint32(i + 10);
      }
      expect(xor).toEqual(0);
    } else {
217
      var reader1 = new BigEndian(buffer);
M
mahaifeng 已提交
218
      for (var i = 0; i < buffer.byteLength; i += 14) {
219 220 221 222 223 224
        xor ^= reader1.getInt8(i);
        xor ^= reader1.getUint8(i + 1);
        xor ^= reader1.getInt16(i + 2);
        xor ^= reader1.getUint16(i + 4);
        xor ^= reader1.getInt32(i + 6);
        xor ^= reader1.getUint32(i + 10);
M
mahaifeng 已提交
225 226 227 228 229
      }
    }

  }

M
mahaifeng 已提交
230
  doFloatIterations(dataView : boolean) {
231
    var buffer = this.makeFloatBuffer();
M
mahaifeng 已提交
232 233 234 235 236 237 238 239 240 241
    var iterations = 10;
    if (dataView) {
      for (var i = 0; i < iterations; i++)
        this.doOneFloatIterationDV(buffer);
    } else {
      for (var i = 0; i < iterations; i++)
        this.doOneFloatIterationJS(buffer);
    }
  }

M
mahaifeng 已提交
242
  makeFloatBuffer() : ArrayBuffer {
243
    var buffer = new ArrayBuffer(1000 * 16);
M
mahaifeng 已提交
244
    var view = new DataView(buffer);
245
    for (var i = 0; i < 1000; i++) {
M
mahaifeng 已提交
246 247 248 249 250 251 252
      view.setFloat64(i * 16, Math.log10(i + 1));
      view.setFloat32(i * 16 + 8, Math.sqrt(i));
      view.setFloat32(i * 16 + 12, Math.cos(i));
    }
    return buffer;
  }

M
mahaifeng 已提交
253
  doOneFloatIterationDV(buffer : ArrayBuffer) {
M
mahaifeng 已提交
254 255 256 257 258 259 260
    var sum = 0;
    var view = new DataView(buffer);
    for (var i = 0; i < view.byteLength; i += 16) {
      sum += view.getFloat64(i);
      sum += view.getFloat32(i + 8);
      sum += view.getFloat32(i + 12);
    }
M
mahaifeng 已提交
261
    expect(sum).toEqual(23634.413356150446);
M
mahaifeng 已提交
262 263
  }

M
mahaifeng 已提交
264
  doOneFloatIterationJS(buffer : ArrayBuffer) {
M
mahaifeng 已提交
265 266 267 268 269 270 271 272 273 274 275
    var sum = 0;
    var float32array = new Float32Array(buffer);
    var float64array = new Float64Array(buffer);
    for (var i = 0; i < buffer.byteLength; i += 16) {
      sum += float64array[i / 8];
      sum += float32array[i / 4 + 2];
      sum += float32array[i / 4 + 3];
    }
  }
}
class BigEndian {
M
mahaifeng 已提交
276 277
  private uint8View_ : Uint8Array;
  private int8View_ : Int8Array;
M
mahaifeng 已提交
278

M
mahaifeng 已提交
279
  constructor(buffer : ArrayBuffer,) {
M
mahaifeng 已提交
280 281 282 283 284
    const offset = 0;
    this.uint8View_ = new Uint8Array(buffer, offset);
    this.int8View_ = new Int8Array(buffer, offset);
  }

M
mahaifeng 已提交
285
  getInt8(byteOffset : number) : number {
M
mahaifeng 已提交
286 287 288
    return this.int8View_[byteOffset];
  }

M
mahaifeng 已提交
289
  getUint8(byteOffset : number) : number {
M
mahaifeng 已提交
290 291 292
    return this.uint8View_[byteOffset];
  }

M
mahaifeng 已提交
293
  getInt16(byteOffset : number) : number {
M
mahaifeng 已提交
294 295 296
    return this.uint8View_[byteOffset + 1] | (this.int8View_[byteOffset] << 8);
  }

M
mahaifeng 已提交
297
  getUint16(byteOffset : number) : number {
M
mahaifeng 已提交
298 299 300
    return this.uint8View_[byteOffset + 1] | (this.uint8View_[byteOffset] << 8);
  }

M
mahaifeng 已提交
301
  getInt32(byteOffset : number) : number {
M
mahaifeng 已提交
302 303 304 305 306 307 308 309
    return (
      this.uint8View_[byteOffset + 3] |
      (this.uint8View_[byteOffset + 2] << 8) |
      (this.uint8View_[byteOffset + 1] << 16) |
      (this.int8View_[byteOffset] << 24)
    );
  }

M
mahaifeng 已提交
310
  getUint32(byteOffset : number) : number {
M
mahaifeng 已提交
311 312 313 314 315 316 317 318 319 320
    return (
      this.uint8View_[byteOffset + 3] +
      (this.uint8View_[byteOffset + 2] << 8) +
      (this.uint8View_[byteOffset + 1] << 16) +
      this.uint8View_[byteOffset] * (1 << 24)
    );
  }
}

class LittleEndian {
M
mahaifeng 已提交
321 322
  private uint8View_ : Uint8Array;
  private int8View_ : Int8Array;
M
mahaifeng 已提交
323

M
mahaifeng 已提交
324
  constructor(buffer : ArrayBuffer) {
M
mahaifeng 已提交
325 326 327 328 329
    const offset = 0;
    this.uint8View_ = new Uint8Array(buffer, offset);
    this.int8View_ = new Int8Array(buffer, offset);
  }

M
mahaifeng 已提交
330
  getInt8(byteOffset : number) : number {
M
mahaifeng 已提交
331 332 333
    return this.int8View_[byteOffset];
  }

M
mahaifeng 已提交
334
  getUint8(byteOffset : number) : number {
M
mahaifeng 已提交
335 336 337
    return this.uint8View_[byteOffset];
  }

M
mahaifeng 已提交
338
  getInt16(byteOffset : number) : number {
M
mahaifeng 已提交
339 340 341
    return this.uint8View_[byteOffset] | (this.int8View_[byteOffset + 1] << 8);
  }

M
mahaifeng 已提交
342
  getUint16(byteOffset : number) : number {
M
mahaifeng 已提交
343 344 345
    return this.uint8View_[byteOffset] | (this.uint8View_[byteOffset + 1] << 8);
  }

M
mahaifeng 已提交
346
  getInt32(byteOffset : number) : number {
M
mahaifeng 已提交
347 348 349 350 351 352 353 354
    return (
      this.uint8View_[byteOffset] |
      (this.uint8View_[byteOffset + 1] << 8) |
      (this.uint8View_[byteOffset + 2] << 16) |
      (this.int8View_[byteOffset + 3] << 24)
    );
  }

M
mahaifeng 已提交
355
  getUint32(byteOffset : number) : number {
M
mahaifeng 已提交
356 357 358 359 360 361 362
    return (
      this.uint8View_[byteOffset] +
      (this.uint8View_[byteOffset + 1] << 8) +
      (this.uint8View_[byteOffset + 2] << 16) +
      this.uint8View_[byteOffset + 3] * (1 << 24)
    );
  }
M
mahaifeng 已提交
363
  // #endif
M
mahaifeng 已提交
364
}