summaryrefslogtreecommitdiff
path: root/test/addons-napi/test_array/test.js
blob: 75c181d9da826962d2703443c50c90916a0cf80c (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
'use strict';
const common = require('../../common');
const assert = require('assert');

// Testing api calls for arrays
const test_array = require(`./build/${common.buildType}/test_array`);

const array = [
  1,
  9,
  48,
  13493,
  9459324,
  { name: 'hello' },
  [
    'world',
    'node',
    'abi'
  ]
];

assert.throws(
  () => {
    test_array.TestGetElement(array, array.length + 1);
  },
  /^Error: assertion \(\(\(uint32_t\)index < length\)\) failed: Index out of bounds!$/
);

assert.throws(
  () => {
    test_array.TestGetElement(array, -2);
  },
  /^Error: assertion \(index >= 0\) failed: Invalid index\. Expects a positive integer\.$/
);

array.forEach(function(element, index) {
  assert.strictEqual(test_array.TestGetElement(array, index), element);
});


assert.deepStrictEqual(test_array.New(array), array);

assert(test_array.TestHasElement(array, 0));
assert.strictEqual(test_array.TestHasElement(array, array.length + 1), false);

assert(test_array.NewWithLength(0) instanceof Array);
assert(test_array.NewWithLength(1) instanceof Array);
// check max allowed length for an array 2^32 -1
assert(test_array.NewWithLength(4294967295) instanceof Array);

{
  // Verify that array elements can be deleted.
  const arr = ['a', 'b', 'c', 'd'];

  assert.strictEqual(arr.length, 4);
  assert.strictEqual(2 in arr, true);
  assert.strictEqual(test_array.TestDeleteElement(arr, 2), true);
  assert.strictEqual(arr.length, 4);
  assert.strictEqual(2 in arr, false);
}