Permalink
Browse files

test: add some more tests for the "Function" type

  • Loading branch information...
1 parent 16ab861 commit 80b1ed7f07db45b973b62f73ddef1a679168da21 @TooTallNate TooTallNate committed Oct 2, 2012
Showing with 34 additions and 8 deletions.
  1. +11 −0 test/ffi_tests.cc
  2. +23 −8 test/function.js
View
@@ -117,6 +117,16 @@ struct arst array_in_struct (struct arst input) {
return rtn;
}
+/*
+ * Tests for C function pointers.
+ */
+
+typedef int (*my_callback)(int);
+
+my_callback callback_func (my_callback cb) {
+ return cb;
+}
+
/*
* Hard-coded `strtoul` binding, for the benchmarks.
*
@@ -233,6 +243,7 @@ void Initialize(Handle<Object> target) {
target->Set(String::NewSymbol("add_boxes"), WrapPointer((char *)add_boxes));
target->Set(String::NewSymbol("int_array"), WrapPointer((char *)int_array));
target->Set(String::NewSymbol("array_in_struct"), WrapPointer((char *)array_in_struct));
+ target->Set(String::NewSymbol("callback_func"), WrapPointer((char *)callback_func));
}
} // anonymous namespace
View
@@ -2,6 +2,7 @@
var assert = require('assert')
, ref = require('ref')
, ffi = require('../')
+ , bindings = require('bindings')({ module_root: __dirname, bindings: 'ffi_tests' })
describe('Function "type"', function () {
@@ -11,19 +12,33 @@ describe('Function "type"', function () {
assert.equal('function', typeof ffi.Function)
})
+ var voidFn = ffi.Function('void', [])
+
it('should return a "type" object when invoked with a return type and array of arguments types', function () {
- var type = ffi.Function('void', [])
- assert(type)
- assert.equal('function', typeof type.get)
- assert.equal('function', typeof type.set)
+ assert(voidFn)
+ assert.equal('function', typeof voidFn.get)
+ assert.equal('function', typeof voidFn.set)
+ })
+
+ it('should be accepted as a return "type" to a ForeignFunction', function () {
+ var fn = ffi.ForeignFunction(ref.NULL, voidFn, []);
})
- it('should be accepted as a return "type"', function () {
- assert(0, 'implement')
+ it('should be accepted as an argument "type" to a ForeignFunction', function () {
+ var fn = ffi.ForeignFunction(ref.NULL, 'void', [ voidFn ]);
})
- it('should be accepted as a return "type"', function () {
- assert(0, 'implement')
+ it('should work as expected using the "callback_func" static bindings', function () {
+ var fn = ffi.Function('int', [ 'int' ])
+ var callback_func = ffi.ForeignFunction(bindings.callback_func, fn, [ fn ]);
+
+ var abs = callback_func(Math.abs)
+ assert.equal('function', typeof abs)
+
+ assert.equal(Math.abs(-5), abs(-5))
+ assert.equal(Math.abs(-9), abs(-9))
+ assert.equal(Math.abs(-69), abs(-69))
+ assert.equal(Math.abs(3), abs(3))
})
})

0 comments on commit 80b1ed7

Please sign in to comment.