// testJson.c -- exercises the JSON library: parse, stringify, round-trip, types, escapes, // nesting, and error handling, driven from a Lua context. #define _POSIX_C_SOURCE 200809L #include "calog.h" #include "calogJson.h" #include #include #include #define CHECK(cond, msg) checkImpl((cond), (msg), __FILE__, __LINE__) #define PUMP_LIMIT 4000 #define RESULT_SLOTS 12 static CalogT *calog = NULL; static _Atomic int64_t results[RESULT_SLOTS]; static _Atomic int32_t doneCount = 0; static _Atomic int32_t errorCount = 0; static int32_t testsRun = 0; static int32_t testsFailed = 0; static int64_t asInt(const CalogValueT *value); static void checkImpl(bool condition, const char *message, const char *file, int32_t line); static int32_t nativeDone(CalogValueT *args, int32_t argCount, CalogValueT *result, void *userData); static int32_t nativeReport(CalogValueT *args, int32_t argCount, CalogValueT *result, void *userData); static void onError(uint64_t contextId, const char *message, void *userData); static void pumpUntilDone(int32_t target); static int64_t asInt(const CalogValueT *value) { if (value->type == calogIntE) { return value->as.i; } return (int64_t)value->as.r; } static void checkImpl(bool condition, const char *message, const char *file, int32_t line) { testsRun++; if (!condition) { testsFailed++; printf("FAIL %s:%d %s\n", file, line, message); } } static int32_t nativeDone(CalogValueT *args, int32_t argCount, CalogValueT *result, void *userData) { (void)args; (void)argCount; (void)userData; atomic_fetch_add(&doneCount, 1); calogValueNil(result); return calogOkE; } static int32_t nativeReport(CalogValueT *args, int32_t argCount, CalogValueT *result, void *userData) { int64_t tag; (void)userData; calogValueNil(result); if (argCount != 2 || (args[0].type != calogIntE && args[0].type != calogRealE)) { return calogFail(result, calogErrArgE, "report expects (tag, value)"); } tag = asInt(&args[0]); if (tag < 0 || tag >= RESULT_SLOTS) { return calogFail(result, calogErrArgE, "report: tag out of range"); } atomic_store(&results[tag], asInt(&args[1])); return calogOkE; } static void onError(uint64_t contextId, const char *message, void *userData) { (void)contextId; (void)userData; fprintf(stderr, " [script error] %s\n", (message != NULL) ? message : "(null)"); atomic_fetch_add(&errorCount, 1); } static void pumpUntilDone(int32_t target) { struct timespec ts = { 0, 500000 }; int i; for (i = 0; i < PUMP_LIMIT; i++) { calogPump(calog); if (atomic_load(&doneCount) >= target) { calogPump(calog); return; } nanosleep(&ts, NULL); } } int main(void) { CalogContextT *ctx; int32_t i; calog = calogCreate(); if (calog == NULL) { printf("calog create failed\n"); return 1; } calogSetErrorHandler(calog, onError, NULL); calogRegister(calog, "report", nativeReport, NULL); calogRegister(calog, "done", nativeDone, NULL); calogJsonRegister(calog); for (i = 0; i < RESULT_SLOTS; i++) { atomic_store(&results[i], -1); } ctx = calogContextOpen(calog, &calogLuaEngine); calogContextEval(ctx, "local o = jsonParse('{\"a\":1,\"b\":2,\"nested\":{\"c\":3}}')\n" "report(1, o.a + o.b + o.nested.c)\n" // 6, nested object "local arr = jsonParse('[10,20,30]')\n" "report(2, arr[1] + arr[2] + arr[3])\n" // 60, array "local o2 = jsonParse(jsonStringify({x=5, y=7}))\n" "report(3, o2.x + o2.y)\n" // 12, round-trip a map "local back = jsonParse(jsonStringify('a\\nb\"c'))\n" "report(4, back == 'a\\nb\"c' and 1 or 0)\n" // 1, escape round-trip "report(5, jsonParse('3.5') * 2)\n" // 7, real "report(6, (jsonParse('true') and jsonParse('null') == nil) and 1 or 0)\n" // 1 "local ok = pcall(function() jsonParse('{not valid}') end)\n" "report(7, ok and 0 or 1)\n" // 1, parse error is catchable "local oks = pcall(function() jsonParse([[\"\\uDC00\"]]) end)\n" "report(8, oks and 0 or 1)\n" // 1, a lone low surrogate is rejected "report(9, math.type(jsonParse('99999999999999999999')) == 'float' and 1 or 0)\n" // 1, an int that overflows int64 becomes a real "done()"); pumpUntilDone(1); CHECK(atomic_load(&results[1]) == 6, "parse a nested object"); CHECK(atomic_load(&results[2]) == 60, "parse an array"); CHECK(atomic_load(&results[3]) == 12, "round-trip a map through stringify + parse"); CHECK(atomic_load(&results[4]) == 1, "strings with escapes round-trip"); CHECK(atomic_load(&results[5]) == 7, "parse a real number"); CHECK(atomic_load(&results[6]) == 1, "parse true and null"); CHECK(atomic_load(&results[7]) == 1, "invalid JSON raises a catchable error"); CHECK(atomic_load(&results[8]) == 1, "an unpaired low surrogate is rejected"); CHECK(atomic_load(&results[9]) == 1, "an integer literal overflowing int64 parses as a real"); CHECK(atomic_load(&errorCount) == 0, "no uncaught errors"); calogContextClose(ctx); calogDestroy(calog); printf("\n%d checks, %d failed\n", testsRun, testsFailed); fflush(stdout); if (testsFailed != 0) { return 1; } return 0; }