diff -r 0bb91d1f9bba -r dc36aa437249 test/json.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/json.c Tue Dec 09 18:24:48 2025 +0100 @@ -0,0 +1,138 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBLIITY OF SUCH DAMAGE. + */ + +#include "json.h" + +#include +#include + + +typedef struct Test1 { + char *str; + cxmutstr str2; + int8_t i8; + uint8_t u8; + int64_t i64; + int16_t i16; + int32_t i32; + uint32_t u32; + uint16_t u16; + uint64_t u64; + bool bt; + bool bf; + double d; +} Test1; + +static DBUContext *ctx; +static DBUClass *test1_class; + +int init_json_tests(void) { + ctx = dbuContextCreate(); + + test1_class = dbuRegisterClassWithoutPK(ctx, "test1", sizeof(Test1)); + dbuClassAdd(test1_class, Test1, str); + dbuClassAdd(test1_class, Test1, str2); + dbuClassAdd(test1_class, Test1, i8); + dbuClassAdd(test1_class, Test1, u8); + dbuClassAdd(test1_class, Test1, i16); + dbuClassAdd(test1_class, Test1, u16); + dbuClassAdd(test1_class, Test1, i32); + dbuClassAdd(test1_class, Test1, u32); + dbuClassAdd(test1_class, Test1, i64); + dbuClassAdd(test1_class, Test1, u64); + dbuClassAdd(test1_class, Test1, bt); + dbuClassAdd(test1_class, Test1, bf); + dbuClassAdd(test1_class, Test1, d); +} + +void cleanup_json_tests(void) { + dbuContextFree(ctx); +} + +CX_TEST(testObjectToJsonSimple) { + Test1 test; + test.str = "hello str"; + test.str2 = cx_mutstr("hello cx str"); + test.i8 = 32; + test.u8 = 250; + test.i16 = -5533; + test.u16 = 8000; + test.i32 = -123456; + test.u32 = 4000000000; + test.i64 = -999999999999L; + test.u64 = 2501; + test.bt = true; + test.bf = false; + test.d = 431.15; + + CX_TEST_DO { + CxJsonValue *value = dbuObjectToJson(test1_class, &test, NULL); + + CX_TEST_ASSERT(value); + CX_TEST_ASSERT(value->type == CX_JSON_OBJECT); + + CxJsonValue *str = cxJsonObjGet(value, "str"); + CxJsonValue *str2 = cxJsonObjGet(value, "str2"); + CxJsonValue *i8 = cxJsonObjGet(value, "i8"); + CxJsonValue *u8 = cxJsonObjGet(value, "u8"); + CxJsonValue *i16 = cxJsonObjGet(value, "i16"); + CxJsonValue *u16 = cxJsonObjGet(value, "u16"); + CxJsonValue *i32 = cxJsonObjGet(value, "i32"); + CxJsonValue *u32 = cxJsonObjGet(value, "u32"); + CxJsonValue *i64 = cxJsonObjGet(value, "i64"); + CxJsonValue *u64 = cxJsonObjGet(value, "u64"); + CxJsonValue *bt = cxJsonObjGet(value, "bt"); + CxJsonValue *bf = cxJsonObjGet(value, "bf"); + CxJsonValue *d = cxJsonObjGet(value, "d"); + + CX_TEST_ASSERT(str && str->type == CX_JSON_STRING); + CX_TEST_ASSERT(str2 && str2->type == CX_JSON_STRING); + CX_TEST_ASSERT(i8 && i8->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(u8 && u8->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(i16 && i16->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(u16 && u16->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(i32 && i32->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(u32 && u32->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(i64 && i64->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(u64 && u64->type == CX_JSON_INTEGER); + CX_TEST_ASSERT(d && d->type == CX_JSON_NUMBER); + + CX_TEST_ASSERT(!cx_strcmp(str->value.string, test.str)); + CX_TEST_ASSERT(!cx_strcmp(str2->value.string, test.str2)); + CX_TEST_ASSERT(i8->value.integer == test.i8); + CX_TEST_ASSERT(u8->value.integer == test.u8); + CX_TEST_ASSERT(i16->value.integer == test.i16); + CX_TEST_ASSERT(u16->value.integer == test.u16); + CX_TEST_ASSERT(i32->value.integer == test.i32); + CX_TEST_ASSERT(u32->value.integer == test.u32); + CX_TEST_ASSERT(i64->value.integer == test.i64); + CX_TEST_ASSERT(u64->value.integer == test.u64); + CX_TEST_ASSERT(d->value.number < test.d + 0.1 && d->value.number > test.d - 0.1); + } +} +