From 567d1c932284bdd53c60937ff6f4ce4a63d104bf Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 15:56:41 +0000 Subject: test(extern): call functions from a user C object Co-Authored-By: Claude Sonnet 5.5 --- tests/i/extern-c-object/case.json | 5 +++++ tests/i/extern-c-object/expected.txt | 6 ++++++ tests/i/extern-c-object/lib.c | 10 ++++++++++ tests/i/extern-c-object/notes.md | 7 +++++++ tests/i/extern-c-object/program.cps | 13 +++++++++++++ 5 files changed, 41 insertions(+) create mode 100644 tests/i/extern-c-object/case.json create mode 100644 tests/i/extern-c-object/expected.txt create mode 100644 tests/i/extern-c-object/lib.c create mode 100644 tests/i/extern-c-object/notes.md create mode 100644 tests/i/extern-c-object/program.cps (limited to 'tests/i/extern-c-object') diff --git a/tests/i/extern-c-object/case.json b/tests/i/extern-c-object/case.json new file mode 100644 index 0000000..1ce5970 --- /dev/null +++ b/tests/i/extern-c-object/case.json @@ -0,0 +1,5 @@ +{ + "objects": [ + {"source": "lib.c", "output": "lib.o"} + ] +} diff --git a/tests/i/extern-c-object/expected.txt b/tests/i/extern-c-object/expected.txt new file mode 100644 index 0000000..58ebb6d --- /dev/null +++ b/tests/i/extern-c-object/expected.txt @@ -0,0 +1,6 @@ +6 +10.000000 +9 +1 +2 +from lib.c diff --git a/tests/i/extern-c-object/lib.c b/tests/i/extern-c-object/lib.c new file mode 100644 index 0000000..568133d --- /dev/null +++ b/tests/i/extern-c-object/lib.c @@ -0,0 +1,10 @@ +#include +#include + +static int calls; + +int add3(int a, int b, int c) { return a + b + c; } +double scale(double x, int k) { return x * k; } +int name_length(const char *s) { return (int)strlen(s); } +int next_call(void) { return ++calls; } +void say(const char *s) { puts(s); } diff --git a/tests/i/extern-c-object/notes.md b/tests/i/extern-c-object/notes.md new file mode 100644 index 0000000..51af8cb --- /dev/null +++ b/tests/i/extern-c-object/notes.md @@ -0,0 +1,7 @@ +# extern-c-object + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); C semantics. + +Trace: the harness compiles `lib.c` with the host C compiler (`objects` in `case.json`). add3(1,2,3)=6. scale(2.5,4)=10.0, printed like other C doubles as `10.000000`. name_length("cambridge")=9. next_call increments a static counter in the object, giving 1 then 2. say calls puts, which prints the string and a newline. diff --git a/tests/i/extern-c-object/program.cps b/tests/i/extern-c-object/program.cps new file mode 100644 index 0000000..5ac34ec --- /dev/null +++ b/tests/i/extern-c-object/program.cps @@ -0,0 +1,13 @@ +// Functions from a user C object built with the host C compiler. +INCLUDE_OBJ "lib.o" +EXTERN "C" EXTERN_FUNCTION add3(C_INT, C_INT, C_INT) RETURNS C_INT +EXTERN "C" EXTERN_FUNCTION scale(C_DOUBLE, C_INT) RETURNS C_DOUBLE +EXTERN "C" EXTERN_FUNCTION name_length(C_STRING) RETURNS C_INT +EXTERN "C" EXTERN_FUNCTION next_call() RETURNS C_INT +EXTERN "C" EXTERN_PROCEDURE say(C_STRING) +OUTPUT add3((C_INT)1, (C_INT)2, (C_INT)3) +OUTPUT scale((C_DOUBLE)2.5, (C_INT)4) +OUTPUT name_length((C_STRING)"cambridge") +OUTPUT next_call() +OUTPUT next_call() +CALL say((C_STRING)"from lib.c") -- cgit v1.3.1-4-g156e