From 13dd2132310fb4dc5aa3dd217ed62f2e8301b981 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Thu, 25 Jun 2026 14:00:54 +0200 Subject: all: migrate to yyjson --- tools/server/test_json.c | 121 ++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 60 deletions(-) (limited to 'tools/server/test_json.c') diff --git a/tools/server/test_json.c b/tools/server/test_json.c index 76a0f45..22b95c8 100644 --- a/tools/server/test_json.c +++ b/tools/server/test_json.c @@ -11,6 +11,10 @@ static const char *TEST_QWEN3_QUERY_INSTRUCT = "Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery:"; +/* Parse a JSON literal into an immutable document so its root can be passed to + * the read-only validators (encoding_from_root, text_type_is_query). */ +static yyjson_doc *parse_obj(const char *json) { return yyjson_read(json, strlen(json), 0); } + static void test_json_error_body_escaping(void) { size_t len = 0; char *body = json_error_body("line\nquote\" tab\t ctrl\001 slash\\", "bad\ntype", &len); @@ -20,24 +24,26 @@ static void test_json_error_body_escaping(void) { TEST_ASSERT(len == strlen(body)); TEST_ASSERT(strchr(body, '\n') == NULL); TEST_ASSERT(strchr(body, '\t') == NULL); - cJSON *root = cJSON_Parse(body); + yyjson_doc *doc = yyjson_read(body, strlen(body), 0); + yyjson_val *root = yyjson_doc_get_root(doc); TEST_ASSERT(root != NULL); if (!root) { + yyjson_doc_free(doc); free(body); return; } - cJSON *err = cJSON_GetObjectItemCaseSensitive(root, "error"); - cJSON *msg = err ? cJSON_GetObjectItemCaseSensitive(err, "message") : NULL; - cJSON *type = err ? cJSON_GetObjectItemCaseSensitive(err, "type") : NULL; - if (!msg || !type || !cJSON_IsString(msg) || !cJSON_IsString(type)) { + yyjson_val *err = yyjson_obj_get(root, "error"); + yyjson_val *msg = err ? yyjson_obj_get(err, "message") : NULL; + yyjson_val *type = err ? yyjson_obj_get(err, "type") : NULL; + if (!msg || !type || !yyjson_is_str(msg) || !yyjson_is_str(type)) { TEST_ASSERT(0 && "error body fields must be strings"); - cJSON_Delete(root); + yyjson_doc_free(doc); free(body); return; } - TEST_ASSERT(strcmp(msg->valuestring, "line\nquote\" tab\t ctrl\001 slash\\") == 0); - TEST_ASSERT(strcmp(type->valuestring, "bad\ntype") == 0); - cJSON_Delete(root); + TEST_ASSERT(strcmp(yyjson_get_str(msg), "line\nquote\" tab\t ctrl\001 slash\\") == 0); + TEST_ASSERT(strcmp(yyjson_get_str(type), "bad\ntype") == 0); + yyjson_doc_free(doc); free(body); } @@ -48,7 +54,7 @@ static void test_job_set_422_replaces_response(void) { j.response_len = strlen(j.response); j.status = 500; - cJSON *detail = cJSON_CreateArray(); + yyjson_mut_doc *detail = ve_new(); ve_add(detail, "[\"body\",\"field\"]", "bad value", "value_error"); job_set_422(&j, detail); TEST_ASSERT(j.status == 422); @@ -58,80 +64,75 @@ static void test_job_set_422_replaces_response(void) { TEST_ASSERT(strstr(j.response, "bad value") != NULL); } - cJSON_Delete(detail); + yyjson_mut_doc_free(detail); free(j.response); } static void test_encoding_from_root_family(void) { - cJSON *detail = cJSON_CreateArray(); + yyjson_mut_doc *detail = ve_new(); /* Default encoding follows the explicit serving API. */ - cJSON *empty = cJSON_CreateObject(); - TEST_ASSERT(strcmp(encoding_from_root(empty, detail, FFWD_API_OPENAI), "float") == 0); - TEST_ASSERT(strcmp(encoding_from_root(empty, detail, FFWD_API_PERPLEXITY), "base64_int8") == + yyjson_doc *empty = parse_obj("{}"); + yyjson_val *empty_root = yyjson_doc_get_root(empty); + TEST_ASSERT(strcmp(encoding_from_root(empty_root, detail, FFWD_API_OPENAI), "float") == 0); + TEST_ASSERT(strcmp(encoding_from_root(empty_root, detail, FFWD_API_PERPLEXITY), "base64_int8") == 0); - cJSON_Delete(empty); - TEST_ASSERT(cJSON_GetArraySize(detail) == 0); + yyjson_doc_free(empty); + TEST_ASSERT(ve_count(detail) == 0); /* OpenAI rejects int8 formats; Perplexity rejects the OpenAI base64. */ - cJSON *o = cJSON_CreateObject(); - cJSON_AddStringToObject(o, "encoding_format", "base64_int8"); - encoding_from_root(o, detail, FFWD_API_OPENAI); - TEST_ASSERT(cJSON_GetArraySize(detail) == 1); - cJSON_Delete(o); - - cJSON *p = cJSON_CreateObject(); - cJSON_AddStringToObject(p, "encoding_format", "base64"); - encoding_from_root(p, detail, FFWD_API_PERPLEXITY); - TEST_ASSERT(cJSON_GetArraySize(detail) == 2); - cJSON_Delete(p); + yyjson_doc *o = parse_obj("{\"encoding_format\":\"base64_int8\"}"); + encoding_from_root(yyjson_doc_get_root(o), detail, FFWD_API_OPENAI); + TEST_ASSERT(ve_count(detail) == 1); + yyjson_doc_free(o); + + yyjson_doc *p = parse_obj("{\"encoding_format\":\"base64\"}"); + encoding_from_root(yyjson_doc_get_root(p), detail, FFWD_API_PERPLEXITY); + TEST_ASSERT(ve_count(detail) == 2); + yyjson_doc_free(p); /* An unknown format is rejected. */ - cJSON *bad = cJSON_CreateObject(); - cJSON_AddStringToObject(bad, "encoding_format", "base64_fp16"); - encoding_from_root(bad, detail, FFWD_API_OPENAI); - TEST_ASSERT(cJSON_GetArraySize(detail) == 3); - cJSON_Delete(bad); + yyjson_doc *bad = parse_obj("{\"encoding_format\":\"base64_fp16\"}"); + encoding_from_root(yyjson_doc_get_root(bad), detail, FFWD_API_OPENAI); + TEST_ASSERT(ve_count(detail) == 3); + yyjson_doc_free(bad); - cJSON_Delete(detail); + yyjson_mut_doc_free(detail); } static void test_text_type(void) { - cJSON *detail = cJSON_CreateArray(); + yyjson_mut_doc *detail = ve_new(); /* Absent: no-op for both model types, no error. */ - cJSON *empty = cJSON_CreateObject(); - TEST_ASSERT(text_type_is_query(empty, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); - TEST_ASSERT(text_type_is_query(empty, detail, NULL) == 0); - cJSON_Delete(empty); - TEST_ASSERT(cJSON_GetArraySize(detail) == 0); + yyjson_doc *empty = parse_obj("{}"); + yyjson_val *empty_root = yyjson_doc_get_root(empty); + TEST_ASSERT(text_type_is_query(empty_root, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); + TEST_ASSERT(text_type_is_query(empty_root, detail, NULL) == 0); + yyjson_doc_free(empty); + TEST_ASSERT(ve_count(detail) == 0); /* Qwen3: query -> 1, document -> 0, both accepted. */ - cJSON *q = cJSON_CreateObject(); - cJSON_AddStringToObject(q, "text_type", "query"); - TEST_ASSERT(text_type_is_query(q, detail, TEST_QWEN3_QUERY_INSTRUCT) == 1); - cJSON_Delete(q); - cJSON *d = cJSON_CreateObject(); - cJSON_AddStringToObject(d, "text_type", "document"); - TEST_ASSERT(text_type_is_query(d, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); - cJSON_Delete(d); - TEST_ASSERT(cJSON_GetArraySize(detail) == 0); + yyjson_doc *q = parse_obj("{\"text_type\":\"query\"}"); + TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(q), detail, TEST_QWEN3_QUERY_INSTRUCT) == 1); + yyjson_doc_free(q); + yyjson_doc *d = parse_obj("{\"text_type\":\"document\"}"); + TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(d), detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); + yyjson_doc_free(d); + TEST_ASSERT(ve_count(detail) == 0); /* Invalid enum value on Qwen3 is reported. */ - cJSON *bad = cJSON_CreateObject(); - cJSON_AddStringToObject(bad, "text_type", "passage"); - TEST_ASSERT(text_type_is_query(bad, detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); - TEST_ASSERT(cJSON_GetArraySize(detail) == 1); - cJSON_Delete(bad); + yyjson_doc *bad = parse_obj("{\"text_type\":\"passage\"}"); + TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(bad), detail, TEST_QWEN3_QUERY_INSTRUCT) == 0); + TEST_ASSERT(ve_count(detail) == 1); + yyjson_doc_free(bad); /* text_type on a model with no query instruction is rejected. */ - cJSON *p = cJSON_CreateObject(); - cJSON_AddStringToObject(p, "text_type", "query"); - TEST_ASSERT(text_type_is_query(p, detail, NULL) == 0); - TEST_ASSERT(cJSON_GetArraySize(detail) == 2); - cJSON_Delete(p); + yyjson_doc *p = parse_obj("{\"text_type\":\"query\"}"); + TEST_ASSERT(text_type_is_query(yyjson_doc_get_root(p), detail, NULL) == 0); + TEST_ASSERT(ve_count(detail) == 2); + yyjson_doc_free(p); - cJSON_Delete(detail); + yyjson_mut_doc_free(detail); } int main(void) { -- cgit v1.3.1