Skip to content

Commit c636c37

Browse files
committed
test: add a test for passing Arrays to a native addon function
See #8.
1 parent e1c537c commit c636c37

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

test/array.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,21 @@ describe('Array', function () {
185185
assert.equal(y[0], x[1])
186186
assert.equal(y[1], x[2])
187187
assert.equal(y[2], x[3])
188-
})
188+
})
189+
})
190+
191+
describe('native tests', function () {
192+
var IntArray = ArrayType('int')
193+
194+
it('should be pass an Array to a native function', function () {
195+
var input = [-1, -25, -3111, -41234, -5]
196+
var array = new IntArray(input)
197+
198+
bindings.arrayAbs(array.buffer, array.length)
199+
200+
assert.deepEqual(array.toJSON(), input.map(Math.abs));
201+
})
202+
189203
})
190204

191205
})

test/native_tests.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
#include <stdlib.h>
2+
#include <stdlib.h> /* abs */
23
#include "nan.h"
34

45
using namespace node;
56

67
namespace {
78

9+
NAN_METHOD(ArrayAbs) {
10+
int *arr = reinterpret_cast<int *>(Buffer::Data(info[0].As<v8::Object>()));
11+
uint32_t length = info[1]->Uint32Value();
12+
for (uint32_t i = 0; i < length; i++) {
13+
*(arr + i) = abs(arr[i]);
14+
}
15+
}
16+
817
void Initialize(v8::Handle<v8::Object> target) {
918
Nan::HandleScope scope;
10-
19+
Nan::SetMethod(target, "arrayAbs", ArrayAbs);
1120
}
1221

1322
} // anonymous namespace

0 commit comments

Comments
 (0)