summaryrefslogtreecommitdiff
path: root/test/simple/test-typed-arrays.js
blob: d3be0ddef27e568e234e1b036920cf9ce5aee9a2 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Joyent, Inc. and other Node contributors.

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:

// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

/*
 * Test to verify we are using Typed Arrays
 * (http://www.khronos.org/registry/typedarray/specs/latest/) correctly Test to
 * verify Buffer can used in Typed Arrays
 */

var assert = require('assert');
var SlowBuffer = process.binding('buffer').SlowBuffer;
var ArrayBuffer = process.binding('typed_array').ArrayBuffer;
var Int32Array = process.binding('typed_array').Int32Array;
var Int16Array = process.binding('typed_array').Int16Array;
var Uint8Array = process.binding('typed_array').Uint8Array;

function test(clazz) {
  var size = clazz.length;
  var b = clazz;

  // create a view v1 referring to b, of type Int32, starting at
  // the default byte index (0) and extending until the end of the buffer
  var v1 = new Int32Array(b);
  assert(4, v1.BYTES_PER_ELEMENT);

  // create a view v2 referring to b, of type Uint8, starting at
  // byte index 2 and extending until the end of the buffer
  var v2 = new Uint8Array(b, 2);
  assert(1, v1.BYTES_PER_ELEMENT);

  // create a view v3 referring to b, of type Int16, starting at
  // byte index 2 and having a length of 2
  var v3 = new Int16Array(b, 2, 2);
  assert(2, v1.BYTES_PER_ELEMENT);

  // The layout is now
  // var index
  // b = |0|1|2|3|4|5|6|7| bytes (not indexable)
  // v1 = |0 |1 | indices (indexable)
  // v2 = |0|1|2|3|4|5|
  // v3 = |0 |1 |

  // testing values
  v1[0] = 0x1234;
  v1[1] = 0x5678;

  assert(0x1234, v1[0]);
  assert(0x5678, v1[1]);

  assert(0x3, v2[0]);
  assert(0x4, v2[1]);
  assert(0x5, v2[2]);
  assert(0x6, v2[3]);
  assert(0x7, v2[4]);
  assert(0x8, v2[5]);

  assert(0x34, v3[0]);
  assert(0x56, v3[1]);

  // test get/set
  v2.set(1, 0x8);
  v2.set(2, 0xF);
  assert(0x8, v2.get(1));
  assert(0xF, v2.get(2));
  assert(0x38, v3.get(0));
  assert(0xF6, v3.get(1));

  // test subarray
  var v4 = v1.subarray(1);
  assert(Int32Array, typeof v4);
  assert(0xF678, v4[0]);

  // test set with typed array and []
  v2.set([ 1, 2, 3, 4 ], 2);
  assert(0x1234, v1[0]);

  var sub = new Int32Array(4);
  sub[0] = 0xabcd;
  v2.set(sub, 1);
  assert(0x3a, v3[0]);
  assert(0xbc, v3[1]);
}

// basic Typed Arrays tests
var size = 8;
var ab = new ArrayBuffer(size);
assert.equal(size, ab.byteLength);
test(ab);

// testing sharing Buffer object
var buffer = new Buffer(size);
test(buffer);