1'use strict'; 2const common = require('../../common'); 3const assert = require('assert'); 4 5// Testing api calls for arrays 6const test_array = require(`./build/${common.buildType}/test_array`); 7 8const array = [ 9 1, 10 9, 11 48, 12 13493, 13 9459324, 14 { name: 'hello' }, 15 [ 16 'world', 17 'node', 18 'abi', 19 ], 20]; 21 22assert.throws( 23 () => { 24 test_array.TestGetElement(array, array.length + 1); 25 }, 26 /^Error: assertion \(\(\(uint32_t\)index < length\)\) failed: Index out of bounds!$/ 27); 28 29assert.throws( 30 () => { 31 test_array.TestGetElement(array, -2); 32 }, 33 /^Error: assertion \(index >= 0\) failed: Invalid index\. Expects a positive integer\.$/ 34); 35 36array.forEach(function(element, index) { 37 assert.strictEqual(test_array.TestGetElement(array, index), element); 38}); 39 40 41assert.deepStrictEqual(test_array.New(array), array); 42 43assert(test_array.TestHasElement(array, 0)); 44assert.strictEqual(test_array.TestHasElement(array, array.length + 1), false); 45 46assert(test_array.NewWithLength(0) instanceof Array); 47assert(test_array.NewWithLength(1) instanceof Array); 48// Check max allowed length for an array 2^32 -1 49assert(test_array.NewWithLength(4294967295) instanceof Array); 50 51{ 52 // Verify that array elements can be deleted. 53 const arr = ['a', 'b', 'c', 'd']; 54 55 assert.strictEqual(arr.length, 4); 56 assert.strictEqual(2 in arr, true); 57 assert.strictEqual(test_array.TestDeleteElement(arr, 2), true); 58 assert.strictEqual(arr.length, 4); 59 assert.strictEqual(2 in arr, false); 60} 61