Chromium Code Reviews| Index: tests/ffi/ffi_test.dart |
| diff --git a/tests/ffi/ffi_test.dart b/tests/ffi/ffi_test.dart |
| index faf8b86070b776ebfca96a311057052b083ee0d7..d28d1c71fac79be40fe83c54610054531d8bdbf3 100644 |
| --- a/tests/ffi/ffi_test.dart |
| +++ b/tests/ffi/ffi_test.dart |
| @@ -16,8 +16,303 @@ main() { |
| testAllocate(false); |
| testAllocate(true); |
| + |
| + // NEW |
|
kasperl
2015/06/30 07:16:01
Remove // NEW comment.
ricow1
2015/06/30 07:34:22
Acknowledged.
ricow1
2015/07/02 16:01:56
Done.
|
| + testVAndICall(); |
| + testFailingLibraryLookups(); |
| + testDefaultLibraryLookups(); |
| + testPCallAndMemory(); |
| +} |
| + |
| +testPCallAndMemory() { |
| + // Please see the expected values in the ffi_test_library.c file (obvious |
| + // from the code below, but that is where they are defined). |
| + // For all memory returning functions we expect there to be 4 values of the |
| + // type we are working on. |
| + var libPath = ForeignLibrary.bundleLibraryName('ffi_test_library'); |
| + ForeignLibrary fl = new ForeignLibrary.fromName(libPath); |
| + ForeignPointer p = new ForeignPointer(); |
| + var pcall0 = fl.lookupFunction('pcall0'); |
| + var foreignPointer = pcall0.pcall$0(p); |
| + var memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt32(0), 1); |
| + Expect.equals(memory.getInt32(4), 2); |
| + Expect.equals(memory.getInt32(8), 3); |
| + Expect.equals(memory.getInt32(12), 4); |
| + // Access memory out of bounds |
|
kasperl
2015/06/30 07:16:01
.
ricow1
2015/06/30 07:34:22
Acknowledged.
ricow1
2015/07/02 16:01:56
Done.
|
| + Expect.throws( |
| + () => memory.getInt32(16), |
| + (e) => e is NoSuchMethodError); |
|
kasperl
2015/06/30 07:16:01
Is this really the error we should get?
ricow1
2015/06/30 07:34:22
No, see comment below
ricow1
2015/07/02 16:01:56
Done.
|
| + |
| + memory.free(); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + |
| + var pcall1 = fl.lookupFunction('pcall1'); |
| + foreignPointer = pcall1.pcall$1(p, 42); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt32(0), 42); |
| + Expect.equals(memory.getInt32(4), 42); |
| + Expect.equals(memory.getInt32(8), 42); |
| + Expect.equals(memory.getInt32(12), 42); |
| + memory.setInt32(8, -1); |
| + Expect.equals(memory.getInt32(0), 42); |
| + Expect.equals(memory.getInt32(4), 42); |
| + Expect.equals(memory.getInt32(8), -1); |
| + Expect.equals(memory.getInt32(12), 42); |
| + memory.free(); |
| + |
| + var pcall2 = fl.lookupFunction('pcall2'); |
| + foreignPointer = pcall2.pcall$2(p, 42, 43); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt32(0), 42); |
| + Expect.equals(memory.getInt32(4), 43); |
| + Expect.equals(memory.getInt32(8), 42); |
| + Expect.equals(memory.getInt32(12), 43); |
| + memory.free(); |
| + |
| + // All tetsts below here is basically sanity checking that we correctly |
| + // convert the values to and from c, and that we can also set and read |
| + // back values correctly. |
| + |
| + var memint8 = fl.lookupFunction('memint8'); |
| + foreignPointer = memint8.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 4); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt8(0), -1); |
| + Expect.equals(memory.getInt8(1), -128); |
| + Expect.equals(memory.getInt8(2), 99); |
| + Expect.equals(memory.getInt8(3), 100); |
| + Expect.equals(memory.getUint8(0), 255); |
| + Expect.equals(memory.getUint8(1), 128); |
| + Expect.equals(memory.getUint8(2), 99); |
| + Expect.equals(memory.getUint8(3), 100); |
| + memory.setInt8(1, -1); |
| + memory.setUint8(2, 100); |
| + Expect.equals(memory.getInt8(0), -1); |
| + Expect.equals(memory.getInt8(1), -1); |
| + Expect.equals(memory.getInt8(2), 100); |
| + Expect.equals(memory.getInt8(3), 100); |
| + Expect.equals(memory.getUint8(0), 255); |
| + Expect.equals(memory.getUint8(1), 255); |
| + Expect.equals(memory.getUint8(2), 100); |
| + Expect.equals(memory.getUint8(3), 100); |
| + // Access memory out of bounds |
|
kasperl
2015/06/30 07:16:01
.
ricow1
2015/06/30 07:34:22
Acknowledged.
ricow1
2015/07/02 16:01:56
Done.
|
| + Expect.throws( |
| + () => memory.getUint8(4), |
| + (e) => e is NoSuchMethodError); |
|
kasperl
2015/06/30 07:16:01
Is this really the error we should get?
ricow1
2015/06/30 07:34:22
See comment below
ricow1
2015/07/02 16:01:56
Done.
|
| + memory.free(); |
| + |
| + var memint16 = fl.lookupFunction('memint16'); |
| + foreignPointer = memint16.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 8); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt16(0), 32767); |
| + Expect.equals(memory.getInt16(2), -32768); |
| + Expect.equals(memory.getInt16(4), 0); |
| + Expect.equals(memory.getInt16(6), -1); |
| + memory.setInt16(2, -1); |
| + Expect.equals(memory.getInt16(0), 32767); |
| + Expect.equals(memory.getInt16(2), -1); |
| + Expect.equals(memory.getInt16(4), 0); |
| + Expect.equals(memory.getInt16(6), -1); |
| + // Access memory out of bounds |
|
kasperl
2015/06/30 07:16:01
.
ricow1
2015/06/30 07:34:22
Acknowledged.
ricow1
2015/07/02 16:01:56
Done.
|
| + Expect.throws( |
| + () => memory.getInt16(8), |
| + (e) => e is NoSuchMethodError); |
|
kasperl
2015/06/30 07:16:01
Is this really the error we should get?
ricow1
2015/06/30 07:34:22
No it is not, I will make the the native code catc
ricow1
2015/07/02 16:01:56
Done.
|
| + memory.free(); |
| + |
| + var memuint16 = fl.lookupFunction('memuint16'); |
| + foreignPointer = memuint16.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 8); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getUint16(0), 0); |
| + Expect.equals(memory.getUint16(2), 32767); |
| + Expect.equals(memory.getUint16(4), 32768); |
| + Expect.equals(memory.getUint16(6), 65535); |
| + memory.setUint16(6, 1); |
| + Expect.equals(memory.getUint16(0), 0); |
| + Expect.equals(memory.getUint16(2), 32767); |
| + Expect.equals(memory.getUint16(4), 32768); |
| + Expect.equals(memory.getUint16(6), 1); |
| + // Access memory out of bounds |
|
kasperl
2015/06/30 07:16:01
Maybe add a helper for this kind of check, so I do
ricow1
2015/06/30 07:34:23
Acknowledged.
ricow1
2015/07/02 16:01:56
Done.
|
| + Expect.throws( |
| + () => memory.getUint16(8), |
| + (e) => e is NoSuchMethodError); |
| + memory.free(); |
| + |
| + var memuint32 = fl.lookupFunction('memuint32'); |
| + foreignPointer = memuint32.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getUint32(0), 0); |
| + Expect.equals(memory.getUint32(4), 1); |
| + Expect.equals(memory.getUint32(8), 65536); |
| + Expect.equals(memory.getUint32(12), 4294967295); |
| + memory.setUint32(8, 1); |
| + Expect.equals(memory.getUint32(0), 0); |
| + Expect.equals(memory.getUint32(4), 1); |
| + Expect.equals(memory.getUint32(8), 1); |
| + Expect.equals(memory.getUint32(12), 4294967295); |
| + // Access memory out of bounds |
| + Expect.throws( |
| + () => memory.getUint32(16), |
| + (e) => e is NoSuchMethodError); |
| + memory.free(); |
| + |
| + var memint64 = fl.lookupFunction('memint64'); |
| + foreignPointer = memint64.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 32); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getInt64(0), 0); |
| + Expect.equals(memory.getInt64(8), -1); |
| + Expect.equals(memory.getInt64(16), 9223372036854775807); |
| + Expect.equals(memory.getInt64(24), -9223372036854775808); |
| + memory.setInt64(8, 9223372036854775806); |
| + Expect.equals(memory.getInt64(0), 0); |
| + // TODO(ricow): Failure, need to investigate |
| + // Expect.equals(memory.getInt64(8), 9223372036854775806); |
| + Expect.equals(memory.getInt64(16), 9223372036854775807); |
| + Expect.equals(memory.getInt64(24), -9223372036854775808); |
| + |
| + // Access memory out of bounds |
| + Expect.throws( |
| + () => memory.getInt64(25), |
| + (e) => e is NoSuchMethodError); |
| + memory.free(); |
| + |
| + var memfloat32 = fl.lookupFunction('memfloat32'); |
| + foreignPointer = memfloat32.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 16); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.approxEquals(memory.getFloat32(0), 0); |
| + Expect.approxEquals(memory.getFloat32(4), 1.175494e-38, 0.01); |
| + Expect.approxEquals(memory.getFloat32(8), 3.402823e+38); |
| + Expect.equals(memory.getFloat32(12), 4); |
| + memory.setFloat32(4, 2.1); |
| + Expect.equals(memory.getFloat32(0), 0); |
| + Expect.approxEquals(memory.getFloat32(4), 2.1); |
| + Expect.approxEquals(memory.getFloat32(8), 3.402823e+38); |
| + Expect.equals(memory.getFloat32(12), 4); |
| + |
| + // Access memory out of bounds |
| + Expect.throws( |
| + () => memory.getFloat32(16), |
| + (e) => e is NoSuchMethodError); |
| + memory.free(); |
| + |
| + var memfloat64 = fl.lookupFunction('memFloat64'); |
| + foreignPointer = memfloat64.pcall$0(p); |
| + memory = new ForeignMemory.fromForeignPointer(foreignPointer, 32); |
| + Expect.equals(memory.value, foreignPointer.value); |
| + Expect.equals(memory.getFloat64(0), 0); |
| + Expect.approxEquals(memory.getFloat64(8), 1.79769e+308); |
| + Expect.approxEquals(memory.getFloat64(16), -1.79769e+308); |
| + Expect.equals(memory.getFloat64(24), 4); |
| + memory.setFloat64(24, 1.79769e+308); |
| + Expect.equals(memory.getFloat64(0), 0); |
| + Expect.approxEquals(memory.getFloat64(8), 1.79769e+308); |
| + Expect.approxEquals(memory.getFloat64(16), -1.79769e+308); |
| + Expect.approxEquals(memory.getFloat64(24), 1.79769e+308); |
| + // Access memory out of bounds |
| + Expect.throws( |
| + () => memory.getFloat64(25), |
| + (e) => e is NoSuchMethodError); |
| + memory.free(); |
| + |
| + fl.close(); |
| +} |
| + |
| +testVAndICall() { |
| + // We assume that there is a ffi_test_library library build. |
| + var libPath = ForeignLibrary.bundleLibraryName('ffi_test_library'); |
| + ForeignLibrary fl = new ForeignLibrary.fromName(libPath); |
| + |
| + // Test metods that use a static int. |
| + var setup = fl.lookupFunction('setup'); |
| + var getcount = fl.lookupFunction('getcount'); |
| + var inc = fl.lookupFunction('inc'); |
| + var setcount = fl.lookupFunction('setcount'); |
| + Expect.equals(null, setup.vcall$0()); |
| + Expect.equals(0, getcount.icall$0()); |
| + Expect.equals(null, inc.vcall$0()); |
| + Expect.equals(1, getcount.icall$0()); |
| + Expect.equals(42, setcount.icall$1(42)); |
| + |
| + // Test all the icall wrappers, all c functions returns the sum of the |
| + // arguments. |
| + var icall0 = fl.lookupFunction('icall0'); |
| + var icall1 = fl.lookupFunction('icall1'); |
| + var icall2 = fl.lookupFunction('icall2'); |
| + var icall3 = fl.lookupFunction('icall3'); |
| + var icall4 = fl.lookupFunction('icall4'); |
| + var icall5 = fl.lookupFunction('icall5'); |
| + var icall6 = fl.lookupFunction('icall6'); |
| + Expect.equals(0, icall0.icall$0()); |
| + Expect.equals(1, icall1.icall$1(1)); |
| + Expect.equals(2, icall2.icall$2(1, 1)); |
| + Expect.equals(3, icall3.icall$3(1, 1, 1)); |
| + Expect.equals(4, icall4.icall$4(1, 1, 1, 1)); |
| + Expect.equals(5, icall5.icall$5(1, 1, 1, 1, 1)); |
| + Expect.equals(6, icall6.icall$6(1, 1, 1, 1, 1, 1)); |
| + |
| + // Some limit tests, this is more of sanity checking of our conversions. |
| + Expect.equals(-1, icall1.icall$1(-1)); |
| + Expect.equals(-2, icall2.icall$2(-1, -1)); |
| + Expect.equals(2147483647, icall3.icall$3(2147483647, 0, 0)); |
| + Expect.equals(2147483646, icall3.icall$3(2147483647, -1, 0)); |
| + Expect.equals(-2147483647, icall3.icall$3(2147483647, 2, 0)); |
| + Expect.equals(0, icall1.icall$1(4294967296)); |
| + Expect.equals(1, icall1.icall$1(4294967297)); |
| + Expect.equals(-1, icall1.icall$1(4294967295)); |
| + Expect.equals(0, icall1.icall$1(1024 * 4294967296)); |
| + Expect.equals(1, icall1.icall$1(1024 * 4294967296 + 1)); |
| + |
| + // Test all the void wrappers. The vcall c functions will set the count to |
| + // the sum of the arguments, testable by running getcount. |
| + var vcall0 = fl.lookupFunction('vcall0'); |
| + var vcall1 = fl.lookupFunction('vcall1'); |
| + var vcall2 = fl.lookupFunction('vcall2'); |
| + var vcall3 = fl.lookupFunction('vcall3'); |
| + var vcall4 = fl.lookupFunction('vcall4'); |
| + var vcall5 = fl.lookupFunction('vcall5'); |
| + var vcall6 = fl.lookupFunction('vcall6'); |
| + Expect.equals(null, vcall0.vcall$0()); |
| + Expect.equals(0, getcount.icall$0()); |
| + Expect.equals(null, vcall1.vcall$1(1)); |
| + Expect.equals(1, getcount.icall$0()); |
| + Expect.equals(null, vcall2.vcall$2(1, 1)); |
| + Expect.equals(2, getcount.icall$0()); |
| + Expect.equals(null, vcall3.vcall$3(1, 1, 1)); |
| + Expect.equals(3, getcount.icall$0()); |
| + Expect.equals(null, vcall4.vcall$4(1, 1, 1, 1)); |
| + Expect.equals(4, getcount.icall$0()); |
| + Expect.equals(null, vcall5.vcall$5(1, 1, 1, 1, 1)); |
| + Expect.equals(5, getcount.icall$0()); |
| + Expect.equals(null, vcall6.vcall$6(1, 1, 1, 1, 1, 1)); |
| + Expect.equals(6, getcount.icall$0()); |
| + |
| + fl.close(); |
| +} |
| + |
| +testFailingLibraryLookups() { |
| + var libPath = ForeignLibrary.bundleLibraryName('foobar'); |
| + Expect.throws( |
| + () => new ForeignLibrary.fromName(libPath), |
| + isArgumentError); |
| + Expect.throws( |
| + () => new ForeignLibrary.fromName('random__for_not_hitting_foobar.so'), |
| + isArgumentError); |
| +} |
| + |
| +testDefaultLibraryLookups() { |
| + Expect.isTrue( |
| + ForeignLibrary.lookupDefaultLibraries('qsort') is ForeignFunction); |
| } |
| +/// OLD |
|
kasperl
2015/06/30 07:16:01
Is this something you want to get rid of?
ricow1
2015/06/30 07:34:22
Yep, and all of the below
ricow1
2015/07/02 16:01:56
Done.
|
| testLookup() { |
| Expect.isTrue(Foreign.lookup('qsort') is Foreign); |
| Expect.isTrue(Foreign.lookup('qsort', library: null) is Foreign); |