test/json.c

changeset 26
dc36aa437249
child 28
e46f9f254fcd
equal deleted inserted replaced
25:0bb91d1f9bba 26:dc36aa437249
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBLIITY OF SUCH DAMAGE.
27 */
28
29 #include "json.h"
30
31 #include <stdbool.h>
32 #include <cx/buffer.h>
33
34
35 typedef struct Test1 {
36 char *str;
37 cxmutstr str2;
38 int8_t i8;
39 uint8_t u8;
40 int64_t i64;
41 int16_t i16;
42 int32_t i32;
43 uint32_t u32;
44 uint16_t u16;
45 uint64_t u64;
46 bool bt;
47 bool bf;
48 double d;
49 } Test1;
50
51 static DBUContext *ctx;
52 static DBUClass *test1_class;
53
54 int init_json_tests(void) {
55 ctx = dbuContextCreate();
56
57 test1_class = dbuRegisterClassWithoutPK(ctx, "test1", sizeof(Test1));
58 dbuClassAdd(test1_class, Test1, str);
59 dbuClassAdd(test1_class, Test1, str2);
60 dbuClassAdd(test1_class, Test1, i8);
61 dbuClassAdd(test1_class, Test1, u8);
62 dbuClassAdd(test1_class, Test1, i16);
63 dbuClassAdd(test1_class, Test1, u16);
64 dbuClassAdd(test1_class, Test1, i32);
65 dbuClassAdd(test1_class, Test1, u32);
66 dbuClassAdd(test1_class, Test1, i64);
67 dbuClassAdd(test1_class, Test1, u64);
68 dbuClassAdd(test1_class, Test1, bt);
69 dbuClassAdd(test1_class, Test1, bf);
70 dbuClassAdd(test1_class, Test1, d);
71 }
72
73 void cleanup_json_tests(void) {
74 dbuContextFree(ctx);
75 }
76
77 CX_TEST(testObjectToJsonSimple) {
78 Test1 test;
79 test.str = "hello str";
80 test.str2 = cx_mutstr("hello cx str");
81 test.i8 = 32;
82 test.u8 = 250;
83 test.i16 = -5533;
84 test.u16 = 8000;
85 test.i32 = -123456;
86 test.u32 = 4000000000;
87 test.i64 = -999999999999L;
88 test.u64 = 2501;
89 test.bt = true;
90 test.bf = false;
91 test.d = 431.15;
92
93 CX_TEST_DO {
94 CxJsonValue *value = dbuObjectToJson(test1_class, &test, NULL);
95
96 CX_TEST_ASSERT(value);
97 CX_TEST_ASSERT(value->type == CX_JSON_OBJECT);
98
99 CxJsonValue *str = cxJsonObjGet(value, "str");
100 CxJsonValue *str2 = cxJsonObjGet(value, "str2");
101 CxJsonValue *i8 = cxJsonObjGet(value, "i8");
102 CxJsonValue *u8 = cxJsonObjGet(value, "u8");
103 CxJsonValue *i16 = cxJsonObjGet(value, "i16");
104 CxJsonValue *u16 = cxJsonObjGet(value, "u16");
105 CxJsonValue *i32 = cxJsonObjGet(value, "i32");
106 CxJsonValue *u32 = cxJsonObjGet(value, "u32");
107 CxJsonValue *i64 = cxJsonObjGet(value, "i64");
108 CxJsonValue *u64 = cxJsonObjGet(value, "u64");
109 CxJsonValue *bt = cxJsonObjGet(value, "bt");
110 CxJsonValue *bf = cxJsonObjGet(value, "bf");
111 CxJsonValue *d = cxJsonObjGet(value, "d");
112
113 CX_TEST_ASSERT(str && str->type == CX_JSON_STRING);
114 CX_TEST_ASSERT(str2 && str2->type == CX_JSON_STRING);
115 CX_TEST_ASSERT(i8 && i8->type == CX_JSON_INTEGER);
116 CX_TEST_ASSERT(u8 && u8->type == CX_JSON_INTEGER);
117 CX_TEST_ASSERT(i16 && i16->type == CX_JSON_INTEGER);
118 CX_TEST_ASSERT(u16 && u16->type == CX_JSON_INTEGER);
119 CX_TEST_ASSERT(i32 && i32->type == CX_JSON_INTEGER);
120 CX_TEST_ASSERT(u32 && u32->type == CX_JSON_INTEGER);
121 CX_TEST_ASSERT(i64 && i64->type == CX_JSON_INTEGER);
122 CX_TEST_ASSERT(u64 && u64->type == CX_JSON_INTEGER);
123 CX_TEST_ASSERT(d && d->type == CX_JSON_NUMBER);
124
125 CX_TEST_ASSERT(!cx_strcmp(str->value.string, test.str));
126 CX_TEST_ASSERT(!cx_strcmp(str2->value.string, test.str2));
127 CX_TEST_ASSERT(i8->value.integer == test.i8);
128 CX_TEST_ASSERT(u8->value.integer == test.u8);
129 CX_TEST_ASSERT(i16->value.integer == test.i16);
130 CX_TEST_ASSERT(u16->value.integer == test.u16);
131 CX_TEST_ASSERT(i32->value.integer == test.i32);
132 CX_TEST_ASSERT(u32->value.integer == test.u32);
133 CX_TEST_ASSERT(i64->value.integer == test.i64);
134 CX_TEST_ASSERT(u64->value.integer == test.u64);
135 CX_TEST_ASSERT(d->value.number < test.d + 0.1 && d->value.number > test.d - 0.1);
136 }
137 }
138

mercurial