xref: /freebsd/contrib/libucl/tests/test_generate.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
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.
11  *
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.
22  */
23 
24 #include <stdio.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include "ucl.h"
28 
29 static void
30 ud_dtor (void *ptr)
31 {
32 	assert (ptr == NULL);
33 }
34 
35 static const char *
36 ud_emit (void *ptr)
37 {
38 	return "test userdata emit";
39 }
40 
41 int
42 main (int argc, char **argv)
43 {
44 	ucl_object_t *obj, *cur, *ar, *ar1, *ref, *test_obj, *comments;
45 	ucl_object_iter_t it;
46 	const ucl_object_t *found, *it_obj, *test;
47 	struct ucl_emitter_functions *fn;
48 	FILE *out;
49 	unsigned char *emitted;
50 	const char *fname_out = NULL;
51 	struct ucl_parser *parser;
52 	int ret = 0;
53 
54 	switch (argc) {
55 	case 2:
56 		fname_out = argv[1];
57 		break;
58 	}
59 
60 
61 	if (fname_out != NULL) {
62 		out = fopen (fname_out, "w");
63 		if (out == NULL) {
64 			exit (-errno);
65 		}
66 	}
67 	else {
68 		out = stdout;
69 	}
70 
71 	obj = ucl_object_typed_new (UCL_OBJECT);
72 
73 	/* Keys replacing */
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));
78 
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);
98 
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);
108 /*
109  * This is usually 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);
114 */
115 	cur = ucl_object_fromstring ("��"); /* everybody likes emoji in the code */
116 	ucl_array_prepend (ar1, cur);
117 
118 	ucl_object_array_sort (ar1, ucl_object_compare_qsort);
119 
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);
136 
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);
142 	/* Empty strings */
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);
167 
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));
202 
203 	/* Comments */
204 
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);
214 
215 	/* Array replace */
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);
222 
223 	/* Try to find using path */
224 	/* Should exist */
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);
230 	/* moar dots... */
231 	found = ucl_object_lookup_path (obj, ".key4........1...");
232 	assert (found != NULL && ucl_object_toint (found) == 10);
233 	/* No such index */
234 	found = ucl_object_lookup_path (obj, ".key4.3");
235 	assert (found == NULL);
236 	/* No such key */
237 	found = ucl_object_lookup_path (obj, "key9..key1");
238 	assert (found == NULL);
239 
240 	/* Test iteration */
241 	it = ucl_object_iterate_new (obj);
242 	it_obj = ucl_object_iterate_safe (it, true);
243 	assert (!ucl_object_iter_chk_excpn (it));
244 	/* key0 = 0.1 */
245 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
246 	it_obj = ucl_object_iterate_safe (it, true);
247 	assert (!ucl_object_iter_chk_excpn (it));
248 	/* key1 = "" */
249 	assert (ucl_object_type (it_obj) == UCL_STRING);
250 	it_obj = ucl_object_iterate_safe (it, true);
251 	assert (!ucl_object_iter_chk_excpn (it));
252 	/* key2 = "" */
253 	assert (ucl_object_type (it_obj) == UCL_STRING);
254 	it_obj = ucl_object_iterate_safe (it, true);
255 	assert (!ucl_object_iter_chk_excpn (it));
256 	/* key3 = "" */
257 	assert (ucl_object_type (it_obj) == UCL_STRING);
258 	it_obj = ucl_object_iterate_safe (it, true);
259 	assert (!ucl_object_iter_chk_excpn (it));
260 	/* key4 = ([float, int, float], boolean) */
261 	ucl_object_iterate_reset (it, it_obj);
262 	it_obj = ucl_object_iterate_safe (it, true);
263 	assert (!ucl_object_iter_chk_excpn (it));
264 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
265 	it_obj = ucl_object_iterate_safe (it, true);
266 	assert (!ucl_object_iter_chk_excpn (it));
267 	assert (ucl_object_type (it_obj) == UCL_INT);
268 	it_obj = ucl_object_iterate_safe (it, true);
269 	assert (!ucl_object_iter_chk_excpn (it));
270 	assert (ucl_object_type (it_obj) == UCL_FLOAT);
271 	it_obj = ucl_object_iterate_safe (it, true);
272 	assert (!ucl_object_iter_chk_excpn (it));
273 	assert (ucl_object_type (it_obj) == UCL_BOOLEAN);
274 	ucl_object_iterate_free (it);
275 
276 	fn = ucl_object_emit_memory_funcs ((void **)&emitted);
277 	assert (ucl_object_emit_full (obj, UCL_EMIT_CONFIG, fn, comments));
278 	fprintf (out, "%s\n", emitted);
279 	ucl_object_emit_funcs_free (fn);
280 	ucl_object_unref (obj);
281 	ucl_object_unref (comments);
282 
283 	parser = ucl_parser_new (UCL_PARSER_NO_IMPLICIT_ARRAYS);
284 
285 	if (ucl_parser_add_chunk_full (parser, emitted, strlen (emitted),
286 			3, UCL_DUPLICATE_ERROR, UCL_PARSE_UCL)) {
287 		/* Should fail due to duplicate */
288 		assert (0);
289 	}
290 	else {
291 		assert (ucl_parser_get_error (parser) != NULL);
292 		ucl_parser_clear_error (parser);
293 		ucl_parser_free (parser);
294 		parser = ucl_parser_new (0);
295 		ucl_parser_add_chunk_full (parser, emitted, strlen (emitted),
296 					3, UCL_DUPLICATE_MERGE, UCL_PARSE_UCL);
297 	}
298 
299 	assert (ucl_parser_get_column (parser) == 0);
300 	assert (ucl_parser_get_linenum (parser) != 0);
301 	ucl_parser_clear_error (parser);
302 	assert (ucl_parser_get_error_code (parser) == 0);
303 	obj = ucl_parser_get_object (parser);
304 	ucl_parser_free (parser);
305 	ucl_object_unref (obj);
306 
307 	if (emitted != NULL) {
308 		free (emitted);
309 	}
310 	fclose (out);
311 
312 	/* Ref should still be accessible */
313 	ref->value.iv = 100500;
314 	ucl_object_unref (ref);
315 
316 	return ret;
317 }
318