forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I would like to write tests fro array class. ISSUE=nodejs#332
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Number>().Uint32Value(); | ||
return Array::New(info.Env(), length); | ||
} else { | ||
return Array::New(info.Env()); | ||
} | ||
} | ||
|
||
Value GetLength(const CallbackInfo& info) { | ||
Array array = info[0].As<Array>(); | ||
return Number::New(info.Env(), static_cast<uint32_t>(array.Length())); | ||
} | ||
|
||
Value GetElement(const CallbackInfo& info) { | ||
Array array = info[0].As<Array>(); | ||
size_t index = info[1].As<Number>().Uint32Value(); | ||
return array[index]; | ||
} | ||
|
||
void SetElement(const CallbackInfo& info) { | ||
Array array = info[0].As<Array>(); | ||
size_t index = info[1].As<Number>().Uint32Value(); | ||
array[index] = info[2].As<Value>(); | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 arrry 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); | ||
|
||
// overrite test | ||
binding.basic_types_array.set(array, 0, 5); | ||
assert.strictEqual(binding.basic_types_array.get(array, 0), 5); | ||
|
||
// out of index | ||
assert.strictEqual(binding.basic_types_array.get(array, 5), undefined); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters