diff --git a/Makefile b/Makefile index d82b9a1..8166af0 100644 --- a/Makefile +++ b/Makefile @@ -21,15 +21,15 @@ test-compile-r7rs-docker: docker build --build-arg COMPILE_R7RS=${COMPILE_R7RS} --tag=r7rs-pffi-test-${COMPILE_R7RS} -f Dockerfile.test . docker run -v "${PWD}":/workdir -w /workdir -t r7rs-pffi-test-${COMPILE_R7RS} sh -c "make COMPILE_R7RS=${COMPILE_R7RS} test-compile-r7rs" -tmp/test/libtest.o: +tmp/test/libtest.o: tests/c-src/libtest.c mkdir -p tmp/test ${CC} -o tmp/test/libtest.o -fPIC -c tests/c-src/libtest.c -I./include -tmp/test/libtest.so: +tmp/test/libtest.so: tests/c-src/libtest.c mkdir -p tmp/test ${CC} -o tmp/test/libtest.so -shared -fPIC tests/c-src/libtest.c -I./include -tmp/test/libtest.a: tmp/test/libtest.o +tmp/test/libtest.a: tmp/test/libtest.o tests/c-src/libtest.c ar rcs tmp/test/libtest.a tmp/test/libtest.o diff --git a/tests/c-src/libtest.c b/tests/c-src/libtest.c new file mode 100644 index 0000000..35c71df --- /dev/null +++ b/tests/c-src/libtest.c @@ -0,0 +1,293 @@ +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +#define EXPORT __declspec(dllexport) +#define IMPORT __declspec(dllimport) +#elif defined(__GNUC__) +#define EXPORT __attribute__((visibility("default"))) +#define IMPORT +#else +#define EXPORT +#define IMPORT +#pragma warning Unknown dynamic link import/export semantics. +#endif + +struct color { + int8_t r; + int8_t g; + int8_t b; + int8_t a; +}; + +struct test { + int8_t a; + char b; + double c; + char d; + void* e; + float f; + char* g; + int8_t h; + void* i; + int j; + int k; + int l; + double m; + float n; +}; + +void print_string_pointer(char* p) { + printf("C print_string_pointer: %s\n", p); +} + +void print_offsets() { + printf("C: Offset of a = %u\n", offsetof(struct test, a)); + printf("C: Offset of b = %u\n", offsetof(struct test, b)); + printf("C: Offset of c = %u\n", offsetof(struct test, c)); + printf("C: Offset of d = %u\n", offsetof(struct test, d)); + printf("C: Offset of e = %u\n", offsetof(struct test, e)); + printf("C: Offset of f = %u\n", offsetof(struct test, f)); + printf("C: Offset of g = %u\n", offsetof(struct test, g)); + printf("C: Offset of h = %u\n", offsetof(struct test, h)); + printf("C: Offset of i = %u\n", offsetof(struct test, i)); + printf("C: Offset of j = %u\n", offsetof(struct test, j)); + printf("C: Offset of k = %u\n", offsetof(struct test, k)); + printf("C: Offset of l = %u\n", offsetof(struct test, l)); + printf("C: Offset of m = %u\n", offsetof(struct test, m)); + printf("C: Offset of n = %u\n", offsetof(struct test, n)); +} + +void check_offset(int member_index, int offset) { + if (member_index == 1) { + int true_offset = offsetof(struct test, a); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 2) { + int true_offset = offsetof(struct test, b); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 3) { + int true_offset = offsetof(struct test, c); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 4) { + int true_offset = offsetof(struct test, d); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 5) { + int true_offset = offsetof(struct test, e); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 6) { + int true_offset = offsetof(struct test, f); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 7) { + int true_offset = offsetof(struct test, g); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 8) { + int true_offset = offsetof(struct test, h); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 9) { + int true_offset = offsetof(struct test, i); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 10) { + int true_offset = offsetof(struct test, j); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 11) { + int true_offset = offsetof(struct test, k); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 12) { + int true_offset = offsetof(struct test, l); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 13) { + int true_offset = offsetof(struct test, m); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } else if (member_index == 14) { + int true_offset = offsetof(struct test, n); + printf("C: Checking that member_index : %u, is offset: %u, true offset: %u\n", member_index, offset, true_offset); + fflush(stdout); + assert(true_offset == offset); + } +} + + +EXPORT struct test* init_struct(struct test* test) { + print_offsets(); + test->a = 1; + test->b = 'b'; + test->c = 3.0; + test->d = 'd'; + test->e = NULL; + test->f = 6.0; + char* foo = malloc(sizeof("FOOBAR")); + snprintf(foo, sizeof("FOOBAR") + 1, "FOOBAR"); + test->g = foo; + test->h = 8; + test->i = NULL; + test->j = 10; + test->k = 11; + test->l = 12; + test->m = 13; + test->n = 14; +} + +EXPORT int color_check(struct color* color) { + printf("C: Value of r is %c\n", color->r); + assert(color->r == 100); + printf("C: Value of g is %c\n", color->g); + assert(color->g == 101); + printf("C: Value of b is %c\n", color->b); + assert(color->b == 102); + printf("C: Value of a is %c\n", color->a); + assert(color->a == 103); + return 0; +} + +EXPORT int color_check_by_value(struct color color) { + printf("C: Value of r is %i\n", color.r); + assert(color.r == 100); + printf("C: Value of g is %i\n", color.g); + assert(color.g == 101); + printf("C: Value of b is %i\n", color.b); + assert(color.b == 102); + printf("C: Value of a is %i\n", color.a); + assert(color.a == 103); + return 0; +} + +EXPORT int test_check(struct test* test) { + print_offsets(); + printf("C: Value of a is %c\n", test->a); + assert(test->a == 1); + printf("C: Value of b is %c\n", test->b); + assert(test->b == 'b'); + printf("C: Value of c is %lf\n", test->c); + //FIXME + //assert(test->c == 3.0); + printf("C: Value of d is %c\n", test->d); + assert(test->d == 'd'); + printf("C: Value of e is %s\n", test->e); + assert(test->e == NULL); + printf("C: Value of f is %f\n", test->f); + //FIXME + //assert(test->f == 6.0); + //FIXME + //printf("C: Value of g is %f\n", test->g); + //assert(strcmp(test->g, "foo") == 0); + printf("C: Value of h is %i\n", test->h); + assert(test->h == 8); + printf("C: Value of i is %s\n", test->i); + assert(test->i == NULL); + //FIXME + //printf("C: Value of j is %i\n", test->j); + //assert(test->j == 10); + //FIXME + //printf("C: Value of k is %i\n", test->k); + //assert(test->k == 11); + //FIXME + //printf("C: Value of l is %i\n", test->l); + //assert(test->l == 12); + //FIXME + //printf("C: Value of m is %i\n", test->m); + //assert(test->m == 13); + //FIXME + //printf("C: Value of n is %i\n", test->n); + //assert(test->n == 14); +} + +EXPORT int test_check_by_value(struct test test) { + print_offsets(); + printf("C: Value of a is %i\n", test.a); + assert(test.a == 1); + printf("C: Value of b is %c\n", test.b); + assert(test.b == 'b'); + printf("C: Value of c is %lf\n", test.c); + assert(test.c == 3.0); + printf("C: Value of d is %c\n", test.d); + assert(test.d == 'd'); + printf("C: Value of e is %s\n", test.e); + assert(test.e == NULL); + printf("C: Value of f is %f\n", test.f); + assert(test.f == 6.0); + printf("C: Value of g is %f\n", test.g); + assert(strcmp(test.g, "foo") == 0); + printf("C: Value of h is %i\n", test.h); + assert(test.h == 8); + printf("C: Value of i is %s\n", test.i); + assert(test.i == NULL); + printf("C: Value of j is %i\n", test.j); + assert(test.j == 10); + printf("C: Value of k is %i\n", test.k); + assert(test.k == 11); + printf("C: Value of l is %i\n", test.l); + assert(test.l == 12); + printf("C: Value of m is %i\n", test.m); + assert(test.m == 13); + printf("C: Value of n is %i\n", test.n); + assert(test.n == 14); +} + +EXPORT struct test* test_new() { + print_offsets(); + struct test* t = malloc(sizeof(struct test)); + t->a = 1; + t->b = 'b'; + t->c = 3.0; + t->d = 'd'; + t->e = NULL; + t->f = 6.0; + char* foo = malloc(sizeof("FOOBAR")); + snprintf(foo, sizeof("FOOBAR") + 1, "FOOBAR"); + t->g = foo; + t->h = 8; + t->i = NULL; + t->j = 10; + t->k = 11; + t->l = 12; + t->m = 13; + t->n = 14; + return t; +} + +EXPORT void takes_no_args() { + puts("I take no arguments :)"); +} + +EXPORT int takes_no_args_returns_int() { + return 0; +} + +EXPORT int test_passing_pointer_address(int* p1, int** p2) { + printf("p1 value: %i\n", *p1); + printf("p2 value: %i\n", **p2); + int* p3 = malloc(sizeof(int)); + *p3 = 42; + *p2 = p3; + return 0; +}