summaryrefslogtreecommitdiff
path: root/src/libnm-glib-aux/tests/test-json-aux.c
blob: 5f33f4fe39e5a7c5cade9a9b6025e29c96fef5fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "libnm-glib-aux/nm-default-glib-i18n-prog.h"

#include <jansson.h>

#include "libnm-glib-aux/nm-json-aux.h"

#include "libnm-glib-aux/nm-test-utils.h"

/*****************************************************************************/

static void
test_jansson(void)
{
    const NMJsonVt *    vt;
    nm_auto_decref_json nm_json_t *js1 = NULL;
    nm_auto_decref_json nm_json_t *js2 = NULL;

#define _ASSERT_FIELD(type1, type2, field)                                                        \
    G_STMT_START                                                                                  \
    {                                                                                             \
        G_STATIC_ASSERT_EXPR(sizeof(((type1 *) NULL)->field) == sizeof(((type2 *) NULL)->field)); \
        G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(type1, field) == G_STRUCT_OFFSET(type2, field));     \
    }                                                                                             \
    G_STMT_END

    G_STATIC_ASSERT_EXPR(NM_JSON_REJECT_DUPLICATES == JSON_REJECT_DUPLICATES);

    G_STATIC_ASSERT_EXPR(sizeof(nm_json_type) == sizeof(json_type));

    G_STATIC_ASSERT_EXPR((int) NM_JSON_OBJECT == JSON_OBJECT);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_ARRAY == JSON_ARRAY);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_STRING == JSON_STRING);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_INTEGER == JSON_INTEGER);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_REAL == JSON_REAL);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_TRUE == JSON_TRUE);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_FALSE == JSON_FALSE);
    G_STATIC_ASSERT_EXPR((int) NM_JSON_NULL == JSON_NULL);

    G_STATIC_ASSERT_EXPR(sizeof(nm_json_int_t) == sizeof(json_int_t));

    G_STATIC_ASSERT_EXPR(sizeof(nm_json_t) == sizeof(json_t));
    _ASSERT_FIELD(nm_json_t, json_t, refcount);
    _ASSERT_FIELD(nm_json_t, json_t, type);

    G_STATIC_ASSERT_EXPR(NM_JSON_ERROR_TEXT_LENGTH == JSON_ERROR_TEXT_LENGTH);
    G_STATIC_ASSERT_EXPR(NM_JSON_ERROR_SOURCE_LENGTH == JSON_ERROR_SOURCE_LENGTH);

    G_STATIC_ASSERT_EXPR(sizeof(nm_json_error_t) == sizeof(json_error_t));
    _ASSERT_FIELD(nm_json_error_t, json_error_t, line);
    _ASSERT_FIELD(nm_json_error_t, json_error_t, column);
    _ASSERT_FIELD(nm_json_error_t, json_error_t, position);
    _ASSERT_FIELD(nm_json_error_t, json_error_t, source);
    _ASSERT_FIELD(nm_json_error_t, json_error_t, text);

    vt = nm_json_vt();

    g_assert(vt);
    g_assert(vt->loaded);

    js1 = vt->nm_json_loads("{ \"a\": 5 }", 0, NULL);
    g_assert(js1);
    nm_json_decref(vt, g_steal_pointer(&js1));

    js2 = vt->nm_json_loads("{ \"a\": 6 }", 0, NULL);
    g_assert(js2);

#define CHECK_FCN(vt, fcn, nm_type, js_type)             \
    G_STMT_START                                         \
    {                                                    \
        const NMJsonVt *const _vt     = (vt);            \
        _nm_unused            nm_type = (_vt->nm_##fcn); \
        _nm_unused            js_type = (fcn);           \
                                                         \
        g_assert(_vt->nm_##fcn);                         \
        g_assert(_f_nm);                                 \
        g_assert(_f_js);                                 \
        g_assert(_f_nm == _vt->nm_##fcn);                \
    }                                                    \
    G_STMT_END

    CHECK_FCN(vt, json_array, nm_json_t * (*_f_nm)(void), json_t * (*_f_js)(void) );
    CHECK_FCN(vt,
              json_array_append_new,
              int (*_f_nm)(nm_json_t *, nm_json_t *),
              int (*_f_js)(json_t *, json_t *));
    CHECK_FCN(vt,
              json_array_get,
              nm_json_t * (*_f_nm)(const nm_json_t *, gsize),
              json_t * (*_f_js)(const json_t *, size_t));
    CHECK_FCN(vt,
              json_array_size,
              gsize(*_f_nm)(const nm_json_t *),
              size_t(*_f_js)(const json_t *));
    CHECK_FCN(vt, json_delete, void (*_f_nm)(nm_json_t *), void (*_f_js)(json_t *));
    CHECK_FCN(vt,
              json_dumps,
              char *(*_f_nm)(const nm_json_t *, gsize),
              char *(*_f_js)(const json_t *, size_t));
    CHECK_FCN(vt, json_false, nm_json_t * (*_f_nm)(void), json_t * (*_f_js)(void) );
    CHECK_FCN(vt, json_integer, nm_json_t * (*_f_nm)(nm_json_int_t), json_t * (*_f_js)(json_int_t));
    CHECK_FCN(vt,
              json_integer_value,
              nm_json_int_t(*_f_nm)(const nm_json_t *),
              json_int_t(*_f_js)(const json_t *));
    CHECK_FCN(vt,
              json_loads,
              nm_json_t * (*_f_nm)(const char *, gsize, nm_json_error_t *),
              json_t * (*_f_js)(const char *, size_t, json_error_t *) );
    CHECK_FCN(vt, json_object, nm_json_t * (*_f_nm)(void), json_t * (*_f_js)(void) );
    CHECK_FCN(vt,
              json_object_del,
              int (*_f_nm)(nm_json_t *, const char *),
              int (*_f_js)(json_t *, const char *));
    CHECK_FCN(vt,
              json_object_get,
              nm_json_t * (*_f_nm)(const nm_json_t *, const char *),
              json_t * (*_f_js)(const json_t *, const char *) );
    CHECK_FCN(vt, json_object_iter, void *(*_f_nm)(nm_json_t *), void *(*_f_js)(json_t *) );
    CHECK_FCN(vt,
              json_object_iter_key,
              const char *(*_f_nm)(void *),
              const char *(*_f_js)(void *) );
    CHECK_FCN(vt,
              json_object_iter_next,
              void *(*_f_nm)(nm_json_t *, void *),
              void *(*_f_js)(json_t *, void *) );
    CHECK_FCN(vt, json_object_iter_value, nm_json_t * (*_f_nm)(void *), json_t * (*_f_js)(void *) );
    CHECK_FCN(vt,
              json_object_key_to_iter,
              void *(*_f_nm)(const char *),
              void *(*_f_js)(const char *) );
    CHECK_FCN(vt,
              json_object_set_new,
              int (*_f_nm)(nm_json_t *, const char *, nm_json_t *),
              int (*_f_js)(json_t *, const char *, json_t *));
    CHECK_FCN(vt,
              json_object_size,
              gsize(*_f_nm)(const nm_json_t *),
              size_t(*_f_js)(const json_t *));
    CHECK_FCN(vt,
              json_string,
              nm_json_t * (*_f_nm)(const char *),
              json_t * (*_f_js)(const char *) );
    CHECK_FCN(vt,
              json_string_value,
              const char *(*_f_nm)(const nm_json_t *),
              const char *(*_f_js)(const json_t *) );
    CHECK_FCN(vt, json_true, nm_json_t * (*_f_nm)(void), json_t * (*_f_js)(void) );
}

/*****************************************************************************/

NMTST_DEFINE();

int
main(int argc, char **argv)
{
    nmtst_init(&argc, &argv, TRUE);

    g_test_add_func("/general/test_jansson", test_jansson);

    return g_test_run();
}