1 /* Copyright (c) 2013, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 return "test userdata emit";
42 main (int argc, char **argv)
44 ucl_object_t *obj, *cur, *ar, *ar1, *ref, *test_obj, *comments;
46 const ucl_object_t *found, *it_obj, *test;
47 struct ucl_emitter_functions *fn;
49 unsigned char *emitted;
50 const char *fname_out = NULL;
51 struct ucl_parser *parser;
61 if (fname_out != NULL) {
62 out = fopen (fname_out, "w");
71 obj = ucl_object_typed_new (UCL_OBJECT);
74 cur = ucl_object_fromstring_common ("value1", 0, UCL_STRING_TRIM);
75 ucl_object_insert_key (obj, cur, "key0", 0, false);
76 cur = ucl_object_fromdouble (0.1);
77 assert (ucl_object_replace_key (obj, cur, "key0", 0, false));
79 /* Create some strings */
80 cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM);
81 ucl_object_insert_key (obj, cur, "key1", 0, false);
82 cur = ucl_object_fromstring_common (" test \nstring\n\r\n\b\t\f\\\" ", 0,
83 UCL_STRING_TRIM | UCL_STRING_ESCAPE);
84 ucl_object_insert_key (obj, cur, "key2", 0, false);
85 cur = ucl_object_fromstring_common (" test string \n", 0, 0);
86 ucl_object_insert_key (obj, cur, "key3", 0, false);
87 /* Array of numbers */
88 ar = ucl_object_typed_new (UCL_ARRAY);
89 cur = ucl_object_fromint (10);
90 ucl_array_append (ar, cur);
91 assert (ucl_array_index_of (ar, cur) == 0);
92 cur = ucl_object_fromdouble (10.1);
93 ucl_array_append (ar, cur);
94 assert (ucl_array_index_of (ar, cur) == 1);
95 cur = ucl_object_fromdouble (9.999);
96 ucl_array_prepend (ar, cur);
97 assert (ucl_array_index_of (ar, cur) == 0);
99 ar1 = ucl_object_copy (ar);
100 cur = ucl_object_fromstring ("abc");
101 ucl_array_prepend (ar1, cur);
102 cur = ucl_object_fromstring ("cde");
103 ucl_array_prepend (ar1, cur);
104 cur = ucl_object_fromstring ("абв"); /* UTF8 */
105 ucl_array_prepend (ar1, cur);
106 cur = ucl_object_fromstring ("Ебв"); /* UTF8 */
107 ucl_array_prepend (ar1, cur);
109 * This is ususally broken or fragile as utf collate is far from perfect
110 cur = ucl_object_fromstring ("ёбв");
111 ucl_array_prepend (ar1, cur);
112 cur = ucl_object_fromstring ("Ёбв"); // hello to @bapt
113 ucl_array_prepend (ar1, cur);
115 cur = ucl_object_fromstring ("😎"); /* everybody likes emoji in the code */
116 ucl_array_prepend (ar1, cur);
118 ucl_object_array_sort (ar1, ucl_object_compare_qsort);
120 /* Removing from an array */
121 cur = ucl_object_fromdouble (1.0);
122 ucl_array_append (ar, cur);
123 cur = ucl_array_delete (ar, cur);
124 assert (ucl_object_todouble (cur) == 1.0);
125 ucl_object_unref (cur);
126 cur = ucl_object_fromdouble (2.0);
127 ucl_array_append (ar, cur);
128 cur = ucl_array_pop_last (ar);
129 assert (ucl_object_todouble (cur) == 2.0);
130 ucl_object_unref (cur);
131 cur = ucl_object_fromdouble (3.0);
132 ucl_array_prepend (ar, cur);
133 cur = ucl_array_pop_first (ar);
134 assert (ucl_object_todouble (cur) == 3.0);
135 ucl_object_unref (cur);
137 ucl_object_insert_key (obj, ar, "key4", 0, false);
138 cur = ucl_object_frombool (true);
139 /* Ref object to test refcounts */
140 ref = ucl_object_ref (cur);
141 ucl_object_insert_key (obj, cur, "key4", 0, false);
143 cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM);
144 ucl_object_insert_key (obj, cur, "key5", 0, false);
145 cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE);
146 ucl_object_insert_key (obj, cur, "key6", 0, false);
147 cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE);
148 ucl_object_insert_key (obj, cur, "key7", 0, false);
149 /* Numbers and booleans */
150 cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE);
151 ucl_object_insert_key (obj, cur, "key8", 0, false);
152 cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE);
153 ucl_object_insert_key (obj, cur, "key9", 0, false);
154 cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE);
155 ucl_object_insert_key (obj, cur, "key10", 0, false);
156 cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM);
157 ucl_object_insert_key (obj, cur, "key11", 0, false);
158 cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT);
159 ucl_object_insert_key (obj, cur, "key12", 0, false);
160 cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT);
161 ucl_object_insert_key (obj, cur, "key13", 0, false);
162 cur = ucl_object_frombool (true);
163 ucl_object_insert_key (obj, cur, "k=3", 0, false);
164 ucl_object_insert_key (obj, ar1, "key14", 0, false);
165 cur = ucl_object_new_userdata (ud_dtor, ud_emit, NULL);
166 ucl_object_insert_key (obj, cur, "key15", 0, false);
168 /* More tests for keys */
169 cur = ucl_object_fromlstring ("test", 3);
170 ucl_object_insert_key (obj, cur, "key16", 0, false);
171 test = ucl_object_lookup_any (obj, "key100", "key200", "key300", "key16", NULL);
172 assert (test == cur);
173 test = ucl_object_lookup_len (obj, "key160", 5);
174 assert (test == cur);
175 cur = ucl_object_pop_key (obj, "key16");
176 assert (test == cur);
177 test = ucl_object_pop_key (obj, "key16");
178 assert (test == NULL);
179 test = ucl_object_lookup_len (obj, "key160", 5);
180 assert (test == NULL);
181 /* Objects merging tests */
182 test_obj = ucl_object_new_full (UCL_OBJECT, 2);
183 ucl_object_insert_key (test_obj, cur, "key16", 0, true);
184 ucl_object_merge (obj, test_obj, true);
185 ucl_object_unref (test_obj);
186 /* Array merging test */
187 test_obj = ucl_object_new_full (UCL_ARRAY, 3);
188 ucl_array_append (test_obj, ucl_object_fromstring ("test"));
189 ucl_array_merge (test_obj, ar1, false);
190 ucl_object_insert_key (obj, test_obj, "key17", 0, true);
191 /* Object deletion */
192 cur = ucl_object_fromstring ("test");
193 ucl_object_insert_key (obj, cur, "key18", 0, true);
194 assert (ucl_object_delete_key (obj, "key18"));
195 assert (!ucl_object_delete_key (obj, "key18"));
196 cur = ucl_object_fromlstring ("test", 4);
197 ucl_object_insert_key (obj, cur, "key18\0\0", 7, true);
198 assert (ucl_object_lookup_len (obj, "key18\0\0", 7) == cur);
199 assert (ucl_object_lookup (obj, "key18") == NULL);
200 assert (ucl_object_lookup_len (obj, "key18\0\1", 7) == NULL);
201 assert (ucl_object_delete_keyl (obj, "key18\0\0", 7));
205 comments = ucl_object_typed_new (UCL_OBJECT);
206 found = ucl_object_lookup (obj, "key17");
207 test = ucl_object_lookup (obj, "key16");
208 ucl_comments_add (comments, found, "# test comment");
209 assert (ucl_comments_find (comments, found) != NULL);
210 assert (ucl_comments_find (comments, test) == NULL);
211 ucl_comments_move (comments, found, test);
212 assert (ucl_comments_find (comments, found) == NULL);
213 assert (ucl_comments_find (comments, test) != NULL);
216 ar1 = ucl_object_typed_new (UCL_ARRAY);
217 cur = ucl_object_fromstring ("test");
218 cur = ucl_elt_append (cur, ucl_object_fromstring ("test1"));
219 ucl_array_append (ar1, cur);
220 test = ucl_array_replace_index (ar1, ucl_object_fromstring ("test2"), 0);
221 assert (test == cur);
223 /* Try to find using path */
225 found = ucl_object_lookup_path (obj, "key4.1");
226 assert (found != NULL && ucl_object_toint (found) == 10);
227 /* . should be ignored */
228 found = ucl_object_lookup_path (obj, ".key4.1");
229 assert (found != NULL && ucl_object_toint (found) == 10);
231 found = ucl_object_lookup_path (obj, ".key4........1...");
232 assert (found != NULL && ucl_object_toint (found) == 10);
234 found = ucl_object_lookup_path (obj, ".key4.3");
235 assert (found == NULL);
237 found = ucl_object_lookup_path (obj, "key9..key1");
238 assert (found == NULL);
241 it = ucl_object_iterate_new (obj);
242 it_obj = ucl_object_iterate_safe (it, true);
244 assert (ucl_object_type (it_obj) == UCL_FLOAT);
245 it_obj = ucl_object_iterate_safe (it, true);
247 assert (ucl_object_type (it_obj) == UCL_STRING);
248 it_obj = ucl_object_iterate_safe (it, true);
250 assert (ucl_object_type (it_obj) == UCL_STRING);
251 it_obj = ucl_object_iterate_safe (it, true);
253 assert (ucl_object_type (it_obj) == UCL_STRING);
254 it_obj = ucl_object_iterate_safe (it, true);
255 /* key4 = ([float, int, float], boolean) */
256 ucl_object_iterate_reset (it, it_obj);
257 it_obj = ucl_object_iterate_safe (it, true);
258 assert (ucl_object_type (it_obj) == UCL_FLOAT);
259 it_obj = ucl_object_iterate_safe (it, true);
260 assert (ucl_object_type (it_obj) == UCL_INT);
261 it_obj = ucl_object_iterate_safe (it, true);
262 assert (ucl_object_type (it_obj) == UCL_FLOAT);
263 it_obj = ucl_object_iterate_safe (it, true);
264 assert (ucl_object_type (it_obj) == UCL_BOOLEAN);
265 ucl_object_iterate_free (it);
267 fn = ucl_object_emit_memory_funcs (&emitted);
268 assert (ucl_object_emit_full (obj, UCL_EMIT_CONFIG, fn, comments));
269 fprintf (out, "%s\n", emitted);
270 ucl_object_emit_funcs_free (fn);
271 ucl_object_unref (obj);
272 ucl_object_unref (comments);
274 parser = ucl_parser_new (UCL_PARSER_NO_IMPLICIT_ARRAYS);
276 if (ucl_parser_add_chunk_full (parser, emitted, strlen (emitted),
277 3, UCL_DUPLICATE_ERROR, UCL_PARSE_UCL)) {
278 /* Should fail due to duplicate */
282 assert (ucl_parser_get_error (parser) != NULL);
283 ucl_parser_clear_error (parser);
284 ucl_parser_free (parser);
285 parser = ucl_parser_new (0);
286 ucl_parser_add_chunk_full (parser, emitted, strlen (emitted),
287 3, UCL_DUPLICATE_MERGE, UCL_PARSE_UCL);
290 assert (ucl_parser_get_column (parser) == 0);
291 assert (ucl_parser_get_linenum (parser) != 0);
292 ucl_parser_clear_error (parser);
293 assert (ucl_parser_get_error_code (parser) == 0);
294 obj = ucl_parser_get_object (parser);
295 ucl_parser_free (parser);
296 ucl_object_free (obj);
298 if (emitted != NULL) {
303 /* Ref should still be accessible */
304 ref->value.iv = 100500;
305 ucl_object_unref (ref);