aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2018-11-20 03:01:04 -0600
committerGanesh Viswanathan <dev@genotrance.com>2018-11-20 03:01:04 -0600
commit9787797d15d281ce1dd792d247fac043c72dc769 (patch)
tree6588ca41871c8dc8ed2782898f4b52605fae2f61 /tests
downloadnimterop-9787797d15d281ce1dd792d247fac043c72dc769.tar.gz
nimterop-9787797d15d281ce1dd792d247fac043c72dc769.zip
Initial version
Diffstat (limited to 'tests')
-rw-r--r--tests/include/test.c33
-rw-r--r--tests/include/test.h36
-rw-r--r--tests/tnimterop.nim39
3 files changed, 108 insertions, 0 deletions
diff --git a/tests/include/test.c b/tests/include/test.c
new file mode 100644
index 0000000..8d801b7
--- /dev/null
+++ b/tests/include/test.c
@@ -0,0 +1,33 @@
+#include "test.h"
+
+int test_call_int() {
+ return 5;
+}
+
+struct STRUCT1 test_call_int_param(int param1) {
+ struct STRUCT1 s;
+
+ s.field1 = param1;
+
+ return s;
+}
+
+STRUCT2 test_call_int_param2(int param1, STRUCT2 param2) {
+ STRUCT2 s;
+
+ s.field1 = param1 + param2.field1;
+
+ return s;
+}
+
+STRUCT2 test_call_int_param3(int param1, struct STRUCT1 param2) {
+ STRUCT2 s;
+
+ s.field1 = param1 + param2.field1;
+
+ return s;
+}
+
+ENUM2 test_call_int_param4(enum ENUM param1) {
+ return enum4;
+} \ No newline at end of file
diff --git a/tests/include/test.h b/tests/include/test.h
new file mode 100644
index 0000000..60d1660
--- /dev/null
+++ b/tests/include/test.h
@@ -0,0 +1,36 @@
+#include <stdint.h>
+
+#define TEST_INT 512
+#define TEST_FLOAT 5.12
+#define TEST_HEX 0x512
+
+typedef uint8_t PRIMTYPE;
+typedef PRIMTYPE CUSTTYPE;
+
+struct STRUCT1 {
+ int field1;
+};
+
+typedef struct STRUCT1 STRUCT2;
+
+typedef struct {
+ int field1;
+} STRUCT3;
+
+enum ENUM {
+ enum1,
+ enum2,
+ enum3
+};
+
+typedef enum {
+ enum4 = 3,
+ enum5,
+ enum6
+} ENUM2;
+
+int test_call_int();
+struct STRUCT1 test_call_int_param(int param1);
+STRUCT2 test_call_int_param2(int param1, STRUCT2 param2);
+STRUCT2 test_call_int_param3(int param1, struct STRUCT1 param2);
+ENUM2 test_call_int_param4(enum ENUM param1); \ No newline at end of file
diff --git a/tests/tnimterop.nim b/tests/tnimterop.nim
new file mode 100644
index 0000000..77419ea
--- /dev/null
+++ b/tests/tnimterop.nim
@@ -0,0 +1,39 @@
+import nimterop/cimport
+import macros
+
+cDebug()
+
+cIncludeDir("include")
+cCompile("test.c")
+cImport("test.h")
+
+assert TEST_INT == 512
+assert TEST_FLOAT == 5.12
+assert TEST_HEX == 0x512
+
+var
+ pt: PRIMTYPE
+ ct: CUSTTYPE
+
+ s: STRUCT1
+ s2: STRUCT2
+ s3: STRUCT3
+
+ e: ENUM
+ e2: ENUM2 = enum5
+
+pt = 3
+ct = 4
+
+s.field1 = 5
+s2.field1 = 6
+s3.field1 = 7
+
+e = enum1
+e2 = enum4
+
+assert test_call_int() == 5
+assert test_call_int_param(5).field1 == 5
+assert test_call_int_param2(5, s2).field1 == 11
+assert test_call_int_param3(5, s).field1 == 10
+assert test_call_int_param4(e) == e2 \ No newline at end of file