diff --git a/test/basic_types/array.cc b/test/basic_types/array.cc new file mode 100644 index 0000000..401d936 --- /dev/null +++ b/test/basic_types/array.cc @@ -0,0 +1,41 @@ +#define NAPI_EXPERIMENTAL +#include "napi.h" + +using namespace Napi; + +Value CreateArray(const CallbackInfo& info) { + if (info.Length() > 0) { + size_t length = info[0].As().Uint32Value(); + return Array::New(info.Env(), length); + } else { + return Array::New(info.Env()); + } +} + +Value GetLength(const CallbackInfo& info) { + Array array = info[0].As(); + return Number::New(info.Env(), static_cast(array.Length())); +} + +Value GetElement(const CallbackInfo& info) { + Array array = info[0].As(); + size_t index = info[1].As().Uint32Value(); + return array[index]; +} + +void SetElement(const CallbackInfo& info) { + Array array = info[0].As(); + size_t index = info[1].As().Uint32Value(); + array[index] = info[2].As(); +} + +Object InitBasicTypesArray(Env env) { + Object exports = Object::New(env); + + exports["createArray"] = Function::New(env, CreateArray); + exports["getLength"] = Function::New(env, GetLength); + exports["get"] = Function::New(env, GetElement); + exports["set"] = Function::New(env, SetElement); + + return exports; +} diff --git a/test/basic_types/array.js b/test/basic_types/array.js new file mode 100644 index 0000000..925022a --- /dev/null +++ b/test/basic_types/array.js @@ -0,0 +1,37 @@ +'use strict'; +const buildType = process.config.target_defaults.default_configuration; +const assert = require('assert'); + +test(require(`../build/${buildType}/binding.node`)); +test(require(`../build/${buildType}/binding_noexcept.node`)); + +function test(binding) { + + // create empty array + const array = binding.basic_types_array.createArray(); + assert.strictEqual(binding.basic_types_array.getLength(array), 0); + + // create array with length + const arrayWithLength = binding.basic_types_array.createArray(10); + assert.strictEqual(binding.basic_types_array.getLength(arrayWithLength), 10); + + // set function test + binding.basic_types_array.set(array, 0, 10); + binding.basic_types_array.set(array, 1, "test"); + binding.basic_types_array.set(array, 2, 3.0); + + // check length after set data + assert.strictEqual(binding.basic_types_array.getLength(array), 3); + + // get function test + assert.strictEqual(binding.basic_types_array.get(array, 0), 10); + assert.strictEqual(binding.basic_types_array.get(array, 1), "test"); + assert.strictEqual(binding.basic_types_array.get(array, 2), 3.0); + + // overwrite test + binding.basic_types_array.set(array, 0, 5); + assert.strictEqual(binding.basic_types_array.get(array, 0), 5); + + // out of index test + assert.strictEqual(binding.basic_types_array.get(array, 5), undefined); +} diff --git a/test/binding.cc b/test/binding.cc index 09b4ff6..ffe1ed7 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -6,6 +6,7 @@ using namespace Napi; Object InitArrayBuffer(Env env); Object InitAsyncContext(Env env); Object InitAsyncWorker(Env env); +Object InitBasicTypesArray(Env env); Object InitBasicTypesBoolean(Env env); Object InitBasicTypesNumber(Env env); Object InitBasicTypesValue(Env env); @@ -39,6 +40,7 @@ Object Init(Env env, Object exports) { exports.Set("arraybuffer", InitArrayBuffer(env)); exports.Set("asynccontext", InitAsyncContext(env)); exports.Set("asyncworker", InitAsyncWorker(env)); + exports.Set("basic_types_array", InitBasicTypesArray(env)); exports.Set("basic_types_boolean", InitBasicTypesBoolean(env)); exports.Set("basic_types_number", InitBasicTypesNumber(env)); exports.Set("basic_types_value", InitBasicTypesValue(env)); diff --git a/test/binding.gyp b/test/binding.gyp index dc648bd..77c3880 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -8,6 +8,7 @@ 'arraybuffer.cc', 'asynccontext.cc', 'asyncworker.cc', + 'basic_types/array.cc', 'basic_types/boolean.cc', 'basic_types/number.cc', 'basic_types/value.cc', diff --git a/test/index.js b/test/index.js index 87237ff..8bfd59f 100644 --- a/test/index.js +++ b/test/index.js @@ -11,6 +11,7 @@ let testModules = [ 'arraybuffer', 'asynccontext', 'asyncworker', + 'basic_types/array', 'basic_types/boolean', 'basic_types/number', 'basic_types/value',