Skip to content

Commit

Permalink
test: add a test for passing Arrays to a native addon function
Browse files Browse the repository at this point in the history
See #8.
  • Loading branch information
TooTallNate committed Aug 27, 2015
1 parent e1c537c commit c636c37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,21 @@ describe('Array', function () {
assert.equal(y[0], x[1])
assert.equal(y[1], x[2])
assert.equal(y[2], x[3])
})
})
})

describe('native tests', function () {
var IntArray = ArrayType('int')

it('should be pass an Array to a native function', function () {
var input = [-1, -25, -3111, -41234, -5]
var array = new IntArray(input)

bindings.arrayAbs(array.buffer, array.length)

assert.deepEqual(array.toJSON(), input.map(Math.abs));
})

})

})
11 changes: 10 additions & 1 deletion test/native_tests.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#include <stdlib.h>
#include <stdlib.h> /* abs */
#include "nan.h"

using namespace node;

namespace {

NAN_METHOD(ArrayAbs) {
int *arr = reinterpret_cast<int *>(Buffer::Data(info[0].As<v8::Object>()));
uint32_t length = info[1]->Uint32Value();
for (uint32_t i = 0; i < length; i++) {
*(arr + i) = abs(arr[i]);
}
}

void Initialize(v8::Handle<v8::Object> target) {
Nan::HandleScope scope;

Nan::SetMethod(target, "arrayAbs", ArrayAbs);
}

} // anonymous namespace
Expand Down

0 comments on commit c636c37

Please sign in to comment.