test_generate.c (263019) | test_generate.c (268896) |
---|---|
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 --- 15 unchanged lines hidden (view full) --- 24#include <stdio.h> 25#include <errno.h> 26#include <assert.h> 27#include "ucl.h" 28 29int 30main (int argc, char **argv) 31{ | 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 --- 15 unchanged lines hidden (view full) --- 24#include <stdio.h> 25#include <errno.h> 26#include <assert.h> 27#include "ucl.h" 28 29int 30main (int argc, char **argv) 31{ |
32 ucl_object_t *obj, *cur, *ar; | 32 ucl_object_t *obj, *cur, *ar, *ref; 33 const ucl_object_t *found; |
33 FILE *out; 34 unsigned char *emitted; 35 const char *fname_out = NULL; 36 int ret = 0; 37 38 switch (argc) { 39 case 2: 40 fname_out = argv[1]; --- 9 unchanged lines hidden (view full) --- 50 } 51 else { 52 out = stdout; 53 } 54 55 obj = ucl_object_typed_new (UCL_OBJECT); 56 /* Create some strings */ 57 cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); | 34 FILE *out; 35 unsigned char *emitted; 36 const char *fname_out = NULL; 37 int ret = 0; 38 39 switch (argc) { 40 case 2: 41 fname_out = argv[1]; --- 9 unchanged lines hidden (view full) --- 51 } 52 else { 53 out = stdout; 54 } 55 56 obj = ucl_object_typed_new (UCL_OBJECT); 57 /* Create some strings */ 58 cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); |
58 obj = ucl_object_insert_key (obj, cur, "key1", 0, false); | 59 ucl_object_insert_key (obj, cur, "key1", 0, false); |
59 cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); | 60 cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); |
60 obj = ucl_object_insert_key (obj, cur, "key2", 0, false); | 61 ucl_object_insert_key (obj, cur, "key2", 0, false); |
61 cur = ucl_object_fromstring_common (" test string \n", 0, 0); | 62 cur = ucl_object_fromstring_common (" test string \n", 0, 0); |
62 obj = ucl_object_insert_key (obj, cur, "key3", 0, false); | 63 ucl_object_insert_key (obj, cur, "key3", 0, false); |
63 /* Array of numbers */ | 64 /* Array of numbers */ |
65 ar = ucl_object_typed_new (UCL_ARRAY); |
|
64 cur = ucl_object_fromint (10); | 66 cur = ucl_object_fromint (10); |
65 ar = ucl_array_append (NULL, cur); | 67 ucl_array_append (ar, cur); |
66 cur = ucl_object_fromdouble (10.1); | 68 cur = ucl_object_fromdouble (10.1); |
67 ar = ucl_array_append (ar, cur); | 69 ucl_array_append (ar, cur); |
68 cur = ucl_object_fromdouble (9.999); | 70 cur = ucl_object_fromdouble (9.999); |
69 ar = ucl_array_prepend (ar, cur); | 71 ucl_array_prepend (ar, cur); |
70 71 /* Removing from an array */ 72 cur = ucl_object_fromdouble (1.0); | 72 73 /* Removing from an array */ 74 cur = ucl_object_fromdouble (1.0); |
73 ar = ucl_array_append (ar, cur); | 75 ucl_array_append (ar, cur); |
74 cur = ucl_array_delete (ar, cur); 75 assert (ucl_object_todouble (cur) == 1.0); 76 ucl_object_unref (cur); 77 cur = ucl_object_fromdouble (2.0); | 76 cur = ucl_array_delete (ar, cur); 77 assert (ucl_object_todouble (cur) == 1.0); 78 ucl_object_unref (cur); 79 cur = ucl_object_fromdouble (2.0); |
78 ar = ucl_array_append (ar, cur); | 80 ucl_array_append (ar, cur); |
79 cur = ucl_array_pop_last (ar); 80 assert (ucl_object_todouble (cur) == 2.0); 81 ucl_object_unref (cur); 82 cur = ucl_object_fromdouble (3.0); | 81 cur = ucl_array_pop_last (ar); 82 assert (ucl_object_todouble (cur) == 2.0); 83 ucl_object_unref (cur); 84 cur = ucl_object_fromdouble (3.0); |
83 ar = ucl_array_prepend (ar, cur); | 85 ucl_array_prepend (ar, cur); |
84 cur = ucl_array_pop_first (ar); 85 assert (ucl_object_todouble (cur) == 3.0); 86 ucl_object_unref (cur); 87 | 86 cur = ucl_array_pop_first (ar); 87 assert (ucl_object_todouble (cur) == 3.0); 88 ucl_object_unref (cur); 89 |
88 obj = ucl_object_insert_key (obj, ar, "key4", 0, false); | 90 ucl_object_insert_key (obj, ar, "key4", 0, false); |
89 cur = ucl_object_frombool (true); | 91 cur = ucl_object_frombool (true); |
90 obj = ucl_object_insert_key (obj, cur, "key4", 0, false); | 92 /* Ref object to test refcounts */ 93 ref = ucl_object_ref (cur); 94 ucl_object_insert_key (obj, cur, "key4", 0, false); |
91 /* Empty strings */ 92 cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); | 95 /* Empty strings */ 96 cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); |
93 obj = ucl_object_insert_key (obj, cur, "key5", 0, false); | 97 ucl_object_insert_key (obj, cur, "key5", 0, false); |
94 cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); | 98 cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); |
95 obj = ucl_object_insert_key (obj, cur, "key6", 0, false); | 99 ucl_object_insert_key (obj, cur, "key6", 0, false); |
96 cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); | 100 cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); |
97 obj = ucl_object_insert_key (obj, cur, "key7", 0, false); | 101 ucl_object_insert_key (obj, cur, "key7", 0, false); |
98 /* Numbers and booleans */ 99 cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); | 102 /* Numbers and booleans */ 103 cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); |
100 obj = ucl_object_insert_key (obj, cur, "key8", 0, false); | 104 ucl_object_insert_key (obj, cur, "key8", 0, false); |
101 cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); | 105 cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); |
102 obj = ucl_object_insert_key (obj, cur, "key9", 0, false); | 106 ucl_object_insert_key (obj, cur, "key9", 0, false); |
103 cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); | 107 cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); |
104 obj = ucl_object_insert_key (obj, cur, "key10", 0, false); | 108 ucl_object_insert_key (obj, cur, "key10", 0, false); |
105 cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); | 109 cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); |
106 obj = ucl_object_insert_key (obj, cur, "key11", 0, false); | 110 ucl_object_insert_key (obj, cur, "key11", 0, false); |
107 cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); | 111 cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); |
108 obj = ucl_object_insert_key (obj, cur, "key12", 0, false); | 112 ucl_object_insert_key (obj, cur, "key12", 0, false); |
109 cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); | 113 cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); |
110 obj = ucl_object_insert_key (obj, cur, "key13", 0, false); | 114 ucl_object_insert_key (obj, cur, "key13", 0, false); |
111 cur = ucl_object_frombool (true); | 115 cur = ucl_object_frombool (true); |
112 obj = ucl_object_insert_key (obj, cur, "k=3", 0, false); | 116 ucl_object_insert_key (obj, cur, "k=3", 0, false); |
113 | 117 |
118 /* Try to find using path */ 119 /* Should exist */ 120 found = ucl_lookup_path (obj, "key4.1"); 121 assert (found != NULL && ucl_object_toint (found) == 10); 122 /* . should be ignored */ 123 found = ucl_lookup_path (obj, ".key4.1"); 124 assert (found != NULL && ucl_object_toint (found) == 10); 125 /* moar dots... */ 126 found = ucl_lookup_path (obj, ".key4........1..."); 127 assert (found != NULL && ucl_object_toint (found) == 10); 128 /* No such index */ 129 found = ucl_lookup_path (obj, ".key4.3"); 130 assert (found == NULL); 131 /* No such key */ 132 found = ucl_lookup_path (obj, "key9..key1"); 133 assert (found == NULL); |
|
114 115 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 116 117 fprintf (out, "%s\n", emitted); 118 ucl_object_unref (obj); 119 120 if (emitted != NULL) { 121 free (emitted); 122 } 123 fclose (out); 124 | 134 135 emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 136 137 fprintf (out, "%s\n", emitted); 138 ucl_object_unref (obj); 139 140 if (emitted != NULL) { 141 free (emitted); 142 } 143 fclose (out); 144 |
145 /* Ref should still be accessible */ 146 ref->value.iv = 100500; 147 ucl_object_unref (ref); 148 |
|
125 return ret; 126} | 149 return ret; 150} |