00001 #include <u/libu.h>
00002
00003 int test_suite_json_register (u_test_t *t);
00004
00005 static int test_codec (u_test_case_t *tc);
00006 static int test_build_simple_object (u_test_case_t *tc);
00007 static int test_build_nested_object (u_test_case_t *tc);
00008 static int test_build_simple_array (u_test_case_t *tc);
00009 static int test_iterators (u_test_case_t *tc);
00010 static int test_max_nesting (u_test_case_t *tc);
00011
00012 static int test_codec (u_test_case_t *tc)
00013 {
00014 size_t i;
00015 char *s = NULL;
00016 u_json_t *jo = NULL;
00017
00018 const char *tv[] = {
00019
00020 "{ }",
00021
00022 "[ ]",
00023
00024 "[ { }, { }, [ [ ], { } ] ]",
00025
00026 "{ \"ascii\": \"This is an ASCII string.\" }",
00027
00028 "{ \"unicode\": \"This is a \\uDEAD\\uBEEF.\" }",
00029
00030 "{ \"int\": 12439084123 }",
00031
00032 "{ \"exp\": -12439084123E+1423 }",
00033
00034 "{ \"frac\": 12439084123.999e-1423 }",
00035
00036 "[ true, false ]",
00037
00038 "{ \"NullMatrix\": [ [ null, null ], [ null, null ] ] }",
00039 NULL
00040 };
00041
00042 for (i = 0; tv[i] != NULL; i++)
00043 {
00044 u_test_err_if (u_json_decode(tv[i], &jo));
00045 u_test_err_if (u_json_encode(jo, &s));
00046 u_test_err_ifm (strcmp(s, tv[i]), "%s and %s differ !", tv[i], s);
00047
00048 u_free(s), s = NULL;
00049 u_json_free(jo), jo = NULL;
00050 }
00051
00052 return U_TEST_SUCCESS;
00053 err:
00054 if (s)
00055 u_free(s);
00056 if (jo)
00057 u_json_free(jo);
00058
00059 return U_TEST_FAILURE;
00060 }
00061
00062 static int test_build_simple_array (u_test_case_t *tc)
00063 {
00064 long l;
00065 char *s = NULL;
00066 u_json_t *root = NULL, *tmp = NULL;
00067 const char *ex = "[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]";
00068
00069
00070 u_test_err_if (u_json_new_array(NULL, &root));
00071
00072 for (l = 0; l < 10 ; l++)
00073 {
00074
00075 u_test_err_if (u_json_new_int(NULL, l, &tmp));
00076 u_test_err_if (u_json_add(root, tmp));
00077 tmp = NULL;
00078 }
00079
00080 u_test_err_if (u_json_encode(root, &s));
00081
00082 u_test_err_ifm (strcmp(ex, s), "expecting \'%s\', got \'%s\'", ex, s);
00083
00084 u_json_free(root);
00085 u_free(s);
00086
00087 return U_TEST_SUCCESS;
00088 err:
00089 if (root)
00090 u_json_free(root);
00091 if (tmp)
00092 u_json_free(tmp);
00093 if (s)
00094 u_free(s);
00095
00096 return U_TEST_FAILURE;
00097 }
00098
00099 static int test_build_simple_object (u_test_case_t *tc)
00100 {
00101 char *s = NULL;
00102 u_json_t *root = NULL, *tmp = NULL;
00103 const char *ex =
00104 "{ \"num\": 999, \"string\": \".\", \"null\": null, \"bool\": true }";
00105
00106
00107 u_test_err_if (u_json_new_object(NULL, &root));
00108
00109
00110 u_test_err_if (u_json_new_int("num", 999, &tmp));
00111 u_test_err_if (u_json_add(root, tmp));
00112 tmp = NULL;
00113
00114
00115 u_test_err_if (u_json_new_string("string", ".", &tmp));
00116 u_test_err_if (u_json_add(root, tmp));
00117 tmp = NULL;
00118
00119
00120 u_test_err_if (u_json_new_null("null", &tmp));
00121 u_test_err_if (u_json_add(root, tmp));
00122 tmp = NULL;
00123
00124
00125 u_test_err_if (u_json_new_bool("bool", 1, &tmp));
00126 u_test_err_if (u_json_add(root, tmp));
00127 tmp = NULL;
00128
00129 u_test_err_if (u_json_encode(root, &s));
00130
00131 u_test_err_ifm (strcmp(ex, s), "expecting \'%s\', got \'%s\'", ex, s);
00132
00133 u_json_free(root);
00134 u_free(s);
00135
00136 return U_TEST_SUCCESS;
00137 err:
00138 if (root)
00139 u_json_free(root);
00140 if (tmp)
00141 u_json_free(tmp);
00142 if (s)
00143 u_free(s);
00144
00145 return U_TEST_FAILURE;
00146 }
00147
00148 static int test_build_nested_object (u_test_case_t *tc)
00149 {
00150 int i;
00151 char *s = NULL;
00152 u_json_t *array = NULL, *root = NULL, *tmp = NULL;
00153 const char *ex = "{ \"array\": [ null, null, null ] }";
00154
00155
00156 u_test_err_if (u_json_new_array("array", &array));
00157
00158 for (i= 0; i < 3 ; i++)
00159 {
00160 u_test_err_if (u_json_new_null(NULL, &tmp));
00161 u_test_err_if (u_json_add(array, tmp));
00162 tmp = NULL;
00163 }
00164
00165
00166
00167
00168 u_test_err_if (u_json_new_object(NULL, &root));
00169 u_test_err_if (u_json_add(root, array));
00170 array = NULL;
00171
00172 u_test_err_if (u_json_encode(root, &s));
00173
00174 u_test_err_ifm (strcmp(ex, s), "expecting \'%s\', got \'%s\'", ex, s);
00175
00176 u_json_free(root);
00177 u_free(s);
00178
00179 return U_TEST_SUCCESS;
00180 err:
00181 if (root)
00182 u_json_free(root);
00183 if (array)
00184 u_json_free(array);
00185 if (tmp)
00186 u_json_free(tmp);
00187 if (s)
00188 u_free(s);
00189
00190 return U_TEST_FAILURE;
00191 }
00192
00193 static int test_iterators (u_test_case_t *tc)
00194 {
00195 long e, i;
00196 u_json_it_t jit;
00197 u_json_t *jo = NULL, *cur;
00198 const char *s = "[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]";
00199
00200 u_test_err_if (u_json_decode(s, &jo));
00201
00202
00203 u_test_err_if (u_json_it(u_json_child_first(jo), &jit));
00204 for (i = 1; (cur = u_json_it_next(&jit)) != NULL; i++)
00205 {
00206 u_test_err_if (u_json_get_int(cur, &e));
00207 u_test_err_ifm (e != i, "expecting \'%d\', got \'%d\'", e, i);
00208 }
00209
00210
00211 u_test_err_if (u_json_it(u_json_child_last(jo), &jit));
00212 for (i = 10; (cur = u_json_it_prev(&jit)) != NULL; i--)
00213 {
00214 u_test_err_if (u_json_get_int(cur, &e));
00215 u_test_err_ifm (e != i, "expecting \'%d\', got \'%d\'", e, i);
00216 }
00217
00218 u_json_free(jo), jo = NULL;
00219
00220 return U_TEST_SUCCESS;
00221 err:
00222 return U_TEST_FAILURE;
00223 }
00224
00225 static int test_max_nesting (u_test_case_t *tc)
00226 {
00227 u_json_t *jo = NULL;
00228 char ns[(U_JSON_MAX_DEPTH * 2) + 2 + 1];
00229
00230
00231
00232
00233
00234 memset(ns, '[', U_JSON_MAX_DEPTH + 1);
00235 memset(ns + U_JSON_MAX_DEPTH + 1, ']', U_JSON_MAX_DEPTH + 1);
00236 ns[(U_JSON_MAX_DEPTH * 2) + 2] = '\0';
00237
00238
00239 u_test_err_ifm (u_json_decode(ns, &jo) == 0,
00240 "expecting parser rejection because of excessive nesting");
00241
00242 u_json_free(jo);
00243
00244 return U_TEST_SUCCESS;
00245 err:
00246 if (jo)
00247 u_json_free(jo);
00248
00249 return U_TEST_FAILURE;
00250 }
00251
00252 int test_suite_json_register (u_test_t *t)
00253 {
00254 u_test_suite_t *ts = NULL;
00255
00256 con_err_if (u_test_suite_new("JSON", &ts));
00257
00258 con_err_if (u_test_case_register("Encode-Decode", test_codec, ts));
00259 con_err_if (u_test_case_register("Builder (simple object)",
00260 test_build_simple_object, ts));
00261 con_err_if (u_test_case_register("Builder (simple array)",
00262 test_build_simple_array, ts));
00263 con_err_if (u_test_case_register("Builder (nested object)",
00264 test_build_nested_object, ts));
00265 con_err_if (u_test_case_register("Iterators", test_iterators, ts));
00266 con_err_if (u_test_case_register("Nesting", test_max_nesting, ts));
00267
00268
00269 con_err_if (u_test_suite_dep_register("Lexer", ts));
00270 con_err_if (u_test_suite_dep_register("Hash Map", ts));
00271
00272 return u_test_suite_add(ts, t);
00273 err:
00274 u_test_suite_free(ts);
00275 return ~0;
00276 }