diff options
Diffstat (limited to 'contrib/libucl/tests')
-rwxr-xr-x | contrib/libucl/tests/basic.test | 12 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/18.in | 21 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/18.res | 15 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/2.res | 2 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/9.in | 13 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/9.res | 29 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/escapes.in | 2 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/escapes.res | 2 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/load.in | 14 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/load.inc | 2 | ||||
-rw-r--r-- | contrib/libucl/tests/basic/load.res | 19 | ||||
-rw-r--r-- | contrib/libucl/tests/generate.res | 26 | ||||
-rwxr-xr-x | contrib/libucl/tests/schema.test | 15 | ||||
-rw-r--r-- | contrib/libucl/tests/schema/definitions.json | 4 | ||||
-rw-r--r-- | contrib/libucl/tests/schema/ref.json | 4 | ||||
-rw-r--r-- | contrib/libucl/tests/schema/refRemote.json | 10 | ||||
-rw-r--r-- | contrib/libucl/tests/test_basic.c | 208 | ||||
-rw-r--r-- | contrib/libucl/tests/test_generate.c | 143 | ||||
-rw-r--r-- | contrib/libucl/tests/test_msgpack.c | 143 | ||||
-rw-r--r-- | contrib/libucl/tests/test_schema.c | 16 |
20 files changed, 609 insertions, 91 deletions
diff --git a/contrib/libucl/tests/basic.test b/contrib/libucl/tests/basic.test index a4a07eb13ad9..174cc8c3f0f1 100755 --- a/contrib/libucl/tests/basic.test +++ b/contrib/libucl/tests/basic.test @@ -21,6 +21,18 @@ for _tin in ${TEST_DIR}/basic/*.in ; do fi fi rm $_out + # Use FD interface + $PROG -f $_t.in > /dev/null + # JSON output + $PROG -j $_t.in > /dev/null + $PROG -c -j $_t.in > /dev/null + # YAML output + $PROG -y $_t.in > /dev/null + # Save comments mode + $PROG -C $_t.in > /dev/null + # Save macro mode + $PROG -M $_t.in > /dev/null + $PROG -M -C $_t.in > /dev/null done diff --git a/contrib/libucl/tests/basic/18.in b/contrib/libucl/tests/basic/18.in index bd12c019859a..9b5655afc25a 100644 --- a/contrib/libucl/tests/basic/18.in +++ b/contrib/libucl/tests/basic/18.in @@ -7,4 +7,25 @@ defaults { mything { .inherit "defaults" key = "newval" + key = "newval1" +} +mything { + .inherit "mything" + key = "newval" +} +.priority 3 + +defaults { + key = "val1" + foo = "bar1" + many = "values here" +} +mything1 { + key2 = "wtf??" + .priority 1 + .inherit "defaults" + .inherit "mything" + .inherit "mything1" + key1 = "newval" + key2 = "OMG" # low priority } diff --git a/contrib/libucl/tests/basic/18.res b/contrib/libucl/tests/basic/18.res index a6272d508ac5..92bf0f8e8840 100644 --- a/contrib/libucl/tests/basic/18.res +++ b/contrib/libucl/tests/basic/18.res @@ -1,5 +1,11 @@ defaults { - key = "val"; + key = "val1"; + foo = "bar1"; + many = "values here"; +} +mything { + key = "newval"; + key = "newval1"; foo = "bar"; many = "values here"; } @@ -8,4 +14,11 @@ mything { foo = "bar"; many = "values here"; } +mything1 { + key2 = "wtf??"; + key = "val1"; + foo = "bar1"; + many = "values here"; + key1 = "newval"; +} diff --git a/contrib/libucl/tests/basic/2.res b/contrib/libucl/tests/basic/2.res index 72e95705ec46..cac7a243ccc6 100644 --- a/contrib/libucl/tests/basic/2.res +++ b/contrib/libucl/tests/basic/2.res @@ -29,7 +29,7 @@ key4 = 5000000; key4 = "s1"; key5 = 0.010000; key5 = "\n\r123"; -key6 = 2207520000.000000; +key6 = 315360000.0; keyvar = "unknowntest"; keyvar = "unknownunknownunknown${unknown}"; keyvar = "${some}$no${}$$test$$$$$$$"; diff --git a/contrib/libucl/tests/basic/9.in b/contrib/libucl/tests/basic/9.in index 4c06e39dcaeb..2341445edbf0 100644 --- a/contrib/libucl/tests/basic/9.in +++ b/contrib/libucl/tests/basic/9.in @@ -6,7 +6,18 @@ key = value; .include "$CURDIR/9.inc" -.try_include "/non/existent" #.try_include "$CURDIR/9.incorrect.inc" # 9.incorrect.inc contains '{}}' #key = value; +prefix1 = { + key = value +} +array = [10] +array1 = [10] +.include(prefix=true; key="prefix") "$CURDIR/9.inc" +.include(prefix=true; key="prefix2"; target="array"; glob=true) "$CURDIR/9.inc" +.include(prefix=true; key="prefix1"; target="array"; glob=true) "$CURDIR/9.inc" +.include(prefix=true; key="array"; target="array"; glob=true) "$CURDIR/9.inc" +.include(prefix=true; key="array1"; glob=true) "$CURDIR/9.inc" +.include(prefix=true; key="prefix"; glob=true) "$CURDIR/9.inc" +.try_include "/non/existent" diff --git a/contrib/libucl/tests/basic/9.res b/contrib/libucl/tests/basic/9.res index ec3f014e60d4..0ed36e84422a 100644 --- a/contrib/libucl/tests/basic/9.res +++ b/contrib/libucl/tests/basic/9.res @@ -2,4 +2,33 @@ key1 = "value"; key1 = "value"; key1 = "value"; key = "value"; +prefix1 [ + { + key = "value"; + } + { + key1 = "value"; + } +] +array [ + 10, + { + key1 = "value"; + } +] +array1 [ + 10, + { + key1 = "value"; + } +] +prefix { + key1 = "value"; + key1 = "value"; +} +prefix2 [ + { + key1 = "value"; + } +] diff --git a/contrib/libucl/tests/basic/escapes.in b/contrib/libucl/tests/basic/escapes.in new file mode 100644 index 000000000000..d9da67a3fd53 --- /dev/null +++ b/contrib/libucl/tests/basic/escapes.in @@ -0,0 +1,2 @@ +# Checks for escapes in strings +str = "\r\n\b\t\f\\\"\u03B4\u0B90\u1F640\uFFFFsome text" diff --git a/contrib/libucl/tests/basic/escapes.res b/contrib/libucl/tests/basic/escapes.res new file mode 100644 index 000000000000..e914e778ff7d --- /dev/null +++ b/contrib/libucl/tests/basic/escapes.res @@ -0,0 +1,2 @@ +str = "\r\n\b\t\f\\\"δஐὤ0some text"; + diff --git a/contrib/libucl/tests/basic/load.in b/contrib/libucl/tests/basic/load.in new file mode 100644 index 000000000000..3ed4cee9a0d9 --- /dev/null +++ b/contrib/libucl/tests/basic/load.in @@ -0,0 +1,14 @@ +# Load macro tests +section { + +.load(try=false, multiline=false, trim=false, escape=false, key="key1", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=true, trim=false, escape=false, key="key2", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=true, trim=true, escape=false, key="key3", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=true, trim=true, escape=true, key="key4", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=false, trim=true, escape=false, key="key5", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=false, trim=false, escape=true, key="key6", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=false, trim=true, escape=true, key="key7", target="string", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=false, trim=false, escape=false, key="key8", target="int", priority=1) "${CURDIR}/load.inc" +.load(try=false, multiline=false, trim=false, escape=false, key="key9", target="int", priority=4) "${CURDIR}/load.inc" +.load(try=true, multiline=false, trim=false, escape=false, key="key10", target="string", priority=1) "load_bad.inc" +}; diff --git a/contrib/libucl/tests/basic/load.inc b/contrib/libucl/tests/basic/load.inc new file mode 100644 index 000000000000..cbd9a13b82a2 --- /dev/null +++ b/contrib/libucl/tests/basic/load.inc @@ -0,0 +1,2 @@ + 123 + 321\n diff --git a/contrib/libucl/tests/basic/load.res b/contrib/libucl/tests/basic/load.res new file mode 100644 index 000000000000..9ab1eac6b178 --- /dev/null +++ b/contrib/libucl/tests/basic/load.res @@ -0,0 +1,19 @@ +section { + key1 = " 123\n 321\\n \n"; + key2 = <<EOD + 123 + 321\n + +EOD; + key3 = <<EOD +123 + 321\n +EOD; + key4 = "123\\n 321\\\\n"; + key5 = "123\n 321\\n"; + key6 = " 123\\n 321\\\\n \\n"; + key7 = "123\\n 321\\\\n"; + key8 = 123; + key9 = 123; +} + diff --git a/contrib/libucl/tests/generate.res b/contrib/libucl/tests/generate.res index 970b3125bae6..65db3b57acfb 100644 --- a/contrib/libucl/tests/generate.res +++ b/contrib/libucl/tests/generate.res @@ -1,6 +1,6 @@ key0 = 0.100000; key1 = "test string"; -key2 = "test \\nstring"; +key2 = "test \\nstring\\n\\r\\n\\b\\t\\f\\\\\\\""; key3 = " test string \n"; key4 [ 9.999000, @@ -18,4 +18,28 @@ key11 = false; key12 = "gslin@gslin.org"; key13 = "#test"; "k=3" = true; +key14 [ + 10, + 9.999000, + 10.100000, + "abc", + "cde", + "😎", + "Ебв", + "абв", +] +key15 = "test userdata emit"; +# test comment +key16 = "tes"; +key17 [ + "test", + 10, + 9.999000, + 10.100000, + "abc", + "cde", + "😎", + "Ебв", + "абв", +] diff --git a/contrib/libucl/tests/schema.test b/contrib/libucl/tests/schema.test index fd26804fa295..bad3073679f1 100755 --- a/contrib/libucl/tests/schema.test +++ b/contrib/libucl/tests/schema.test @@ -2,8 +2,21 @@ PROG=${TEST_BINARY_DIR}/test_schema rm /tmp/_ucl_test_schema.out ||true +_succeed=0 +_tests=0 for i in ${TEST_DIR}/schema/*.json ; do _name=`basename $i` printf "running schema test suite $_name... " - $PROG >> /tmp/_ucl_test_schema.out < $i && ( echo "OK" ) || ( echo "Fail" ; exit 1 ) + $PROG >> /tmp/_ucl_test_schema.out < $i + if [ $? -eq 0 ] ; then + echo "OK" + _succeed=$(($_succeed + 1)) + else + echo "Fail" + fi + _tests=$(($_tests + 1)) done + +if [ $_tests -ne $_succeed ] ; then + exit 1 +fi diff --git a/contrib/libucl/tests/schema/definitions.json b/contrib/libucl/tests/schema/definitions.json index cf935a321532..1ab9b2163c44 100644 --- a/contrib/libucl/tests/schema/definitions.json +++ b/contrib/libucl/tests/schema/definitions.json @@ -1,7 +1,7 @@ [ { "description": "valid definition", - "schema": {"$ref": "http://json-schema.org/draft-04/schema#"}, + "schema": {"$ref": "http://highsecure.ru/ucl-schema/schema#"}, "tests": [ { "description": "valid definition schema", @@ -16,7 +16,7 @@ }, { "description": "invalid definition", - "schema": {"$ref": "http://json-schema.org/draft-04/schema#"}, + "schema": {"$ref": "http://highsecure.ru/ucl-schema/schema#"}, "tests": [ { "description": "invalid definition schema", diff --git a/contrib/libucl/tests/schema/ref.json b/contrib/libucl/tests/schema/ref.json index 1b88b27d6faf..1767769cd845 100644 --- a/contrib/libucl/tests/schema/ref.json +++ b/contrib/libucl/tests/schema/ref.json @@ -125,10 +125,9 @@ } ] }, -/* { "description": "remote ref, containing refs itself", - "schema": {"$ref": "http://json-schema.org/draft-04/schema#"}, + "schema": {"$ref": "http://highsecure.ru/ucl-schema/schema#"}, "tests": [ { "description": "remote ref valid", @@ -142,5 +141,4 @@ } ] } -*/ ] diff --git a/contrib/libucl/tests/schema/refRemote.json b/contrib/libucl/tests/schema/refRemote.json index 4ca804732c97..0ba9adb94ca1 100644 --- a/contrib/libucl/tests/schema/refRemote.json +++ b/contrib/libucl/tests/schema/refRemote.json @@ -1,7 +1,7 @@ [ { "description": "remote ref", - "schema": {"$ref": "http://localhost:1234/integer.json"}, + "schema": {"$ref": "http://highsecure.ru/ucl-schema/remotes/integer.json"}, "tests": [ { "description": "remote ref valid", @@ -17,7 +17,7 @@ }, { "description": "fragment within remote ref", - "schema": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}, + "schema": {"$ref": "http://highsecure.ru/ucl-schema/remotes/subSchemas.json#/integer"}, "tests": [ { "description": "remote fragment valid", @@ -34,7 +34,7 @@ { "description": "ref within remote ref", "schema": { - "$ref": "http://localhost:1234/subSchemas.json#/refToInteger" + "$ref": "http://highsecure.ru/ucl-schema/remotes/subSchemas.json#/refToInteger" }, "tests": [ { @@ -49,10 +49,11 @@ } ] }, +/* { "description": "change resolution scope", "schema": { - "id": "http://localhost:1234/", + "id": "http://highsecure.ru/ucl-schema/remotes/", "items": { "id": "folder/", "items": {"$ref": "folderInteger.json"} @@ -71,4 +72,5 @@ } ] } +*/ ] diff --git a/contrib/libucl/tests/test_basic.c b/contrib/libucl/tests/test_basic.c index 45a9c8b2bc6a..b7acc73b4a69 100644 --- a/contrib/libucl/tests/test_basic.c +++ b/contrib/libucl/tests/test_basic.c @@ -23,20 +23,27 @@ #include "ucl.h" #include "ucl_internal.h" +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + int main (int argc, char **argv) { - char *inbuf; + char *inbuf = NULL; struct ucl_parser *parser = NULL, *parser2 = NULL; - ucl_object_t *obj; + ucl_object_t *obj, *comments = NULL; ssize_t bufsize, r; FILE *in, *out; unsigned char *emitted = NULL; const char *fname_in = NULL, *fname_out = NULL; - int ret = 0, opt, json = 0, compact = 0, yaml = 0; + int ret = 0, opt, json = 0, compact = 0, yaml = 0, + save_comments = 0, skip_macro = 0, + flags, fd_out, fd_in, use_fd = 0; + struct ucl_emitter_functions *func; - while ((opt = getopt(argc, argv, "jcy")) != -1) { + while ((opt = getopt(argc, argv, "fjcyCM")) != -1) { switch (opt) { case 'j': json = 1; @@ -44,11 +51,20 @@ main (int argc, char **argv) case 'c': compact = 1; break; + case 'C': + save_comments = 1; + break; case 'y': yaml = 1; break; + case 'M': + skip_macro = true; + break; + case 'f': + use_fd = true; + break; default: /* '?' */ - fprintf (stderr, "Usage: %s [-jcy] [in] [out]\n", + fprintf (stderr, "Usage: %s [-jcy] [-CM] [-f] [in] [out]\n", argv[0]); exit (EXIT_FAILURE); } @@ -67,64 +83,113 @@ main (int argc, char **argv) break; } - if (fname_in != NULL) { - in = fopen (fname_in, "r"); - if (in == NULL) { - exit (-errno); + if (!use_fd) { + if (fname_in != NULL) { + in = fopen (fname_in, "r"); + if (in == NULL) { + exit (-errno); + } + } + else { + in = stdin; } } else { - in = stdin; + if (fname_in != NULL) { + fd_in = open (fname_in, O_RDONLY); + if (fd_in == -1) { + exit (-errno); + } + } + else { + fd_in = STDIN_FILENO; + } } - parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); + + flags = UCL_PARSER_KEY_LOWERCASE; + + if (save_comments) { + flags |= UCL_PARSER_SAVE_COMMENTS; + } + + if (skip_macro) { + flags |= UCL_PARSER_DISABLE_MACRO; + } + + parser = ucl_parser_new (flags); ucl_parser_register_variable (parser, "ABI", "unknown"); if (fname_in != NULL) { ucl_parser_set_filevars (parser, fname_in, true); } - inbuf = malloc (BUFSIZ); - bufsize = BUFSIZ; - r = 0; + if (!use_fd) { + inbuf = malloc (BUFSIZ); + bufsize = BUFSIZ; + r = 0; - while (!feof (in) && !ferror (in)) { - if (r == bufsize) { - inbuf = realloc (inbuf, bufsize * 2); - bufsize *= 2; - if (inbuf == NULL) { - perror ("realloc"); - exit (EXIT_FAILURE); + while (!feof (in) && !ferror (in)) { + if (r == bufsize) { + inbuf = realloc (inbuf, bufsize * 2); + bufsize *= 2; + if (inbuf == NULL) { + perror ("realloc"); + exit (EXIT_FAILURE); + } } + r += fread (inbuf + r, 1, bufsize - r, in); } - r += fread (inbuf + r, 1, bufsize - r, in); - } - if (ferror (in)) { - fprintf (stderr, "Failed to read the input file.\n"); - exit (EXIT_FAILURE); - } + if (ferror (in)) { + fprintf (stderr, "Failed to read the input file.\n"); + exit (EXIT_FAILURE); + } - ucl_parser_add_chunk (parser, (const unsigned char *)inbuf, r); - fclose (in); + ucl_parser_add_chunk (parser, (const unsigned char *)inbuf, r); + fclose (in); + } + else { + ucl_parser_add_fd (parser, fd_in); + close (fd_in); + } - if (fname_out != NULL) { - out = fopen (fname_out, "w"); - if (out == NULL) { - exit (-errno); + if (!use_fd) { + if (fname_out != NULL) { + out = fopen (fname_out, "w"); + if (out == NULL) { + exit (-errno); + } + } + else { + out = stdout; } } else { - out = stdout; + if (fname_out != NULL) { + fd_out = open (fname_out, O_WRONLY | O_CREAT, 00644); + if (fd_out == -1) { + exit (-errno); + } + } + else { + fd_out = STDOUT_FILENO; + } } + if (ucl_parser_get_error (parser) != NULL) { - fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser)); + fprintf (out, "Error occurred (phase 1): %s\n", + ucl_parser_get_error(parser)); ret = 1; goto end; } obj = ucl_parser_get_object (parser); + if (save_comments) { + comments = ucl_object_ref (ucl_parser_get_comments (parser)); + } + if (json) { if (compact) { emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); @@ -137,16 +202,27 @@ main (int argc, char **argv) emitted = ucl_object_emit (obj, UCL_EMIT_YAML); } else { - emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + emitted = NULL; + func = ucl_object_emit_memory_funcs ((void **)&emitted); + + if (func != NULL) { + ucl_object_emit_full (obj, UCL_EMIT_CONFIG, func, comments); + ucl_object_emit_funcs_free (func); + } } +#if 0 + fprintf (out, "%s\n****\n", emitted); +#endif + ucl_parser_free (parser); ucl_object_unref (obj); - parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); + parser2 = ucl_parser_new (flags); ucl_parser_add_string (parser2, (const char *)emitted, 0); if (ucl_parser_get_error(parser2) != NULL) { - fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2)); + fprintf (out, "Error occurred (phase 2): %s\n", + ucl_parser_get_error(parser2)); fprintf (out, "%s\n", emitted); ret = 1; goto end; @@ -155,38 +231,68 @@ main (int argc, char **argv) if (emitted != NULL) { free (emitted); } + if (comments) { + ucl_object_unref (comments); + comments = NULL; + } + + if (save_comments) { + comments = ucl_object_ref (ucl_parser_get_comments (parser2)); + } obj = ucl_parser_get_object (parser2); - if (json) { - if (compact) { - emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); + + if (!use_fd) { + func = ucl_object_emit_file_funcs (out); + } + else { + func = ucl_object_emit_fd_funcs (fd_out); + } + + if (func != NULL) { + if (json) { + if (compact) { + ucl_object_emit_full (obj, UCL_EMIT_JSON_COMPACT, + func, comments); + } + else { + ucl_object_emit_full (obj, UCL_EMIT_JSON, + func, comments); + } + } + else if (yaml) { + ucl_object_emit_full (obj, UCL_EMIT_YAML, + func, comments); } else { - emitted = ucl_object_emit (obj, UCL_EMIT_JSON); + ucl_object_emit_full (obj, UCL_EMIT_CONFIG, + func, comments); } + + ucl_object_emit_funcs_free (func); } - else if (yaml) { - emitted = ucl_object_emit (obj, UCL_EMIT_YAML); + + if (!use_fd) { + fprintf (out, "\n"); + fclose (out); } else { - emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + write (fd_out, "\n", 1); + close (fd_out); } - fprintf (out, "%s\n", emitted); ucl_object_unref (obj); end: - if (emitted != NULL) { - free (emitted); - } if (parser2 != NULL) { ucl_parser_free (parser2); } + if (comments) { + ucl_object_unref (comments); + } if (inbuf != NULL) { free (inbuf); } - fclose (out); - return ret; } diff --git a/contrib/libucl/tests/test_generate.c b/contrib/libucl/tests/test_generate.c index f09eabb839fe..a8dc2fafc2bb 100644 --- a/contrib/libucl/tests/test_generate.c +++ b/contrib/libucl/tests/test_generate.c @@ -26,15 +26,29 @@ #include <assert.h> #include "ucl.h" +static void +ud_dtor (void *ptr) +{ + assert (ptr == NULL); +} + +static const char * +ud_emit (void *ptr) +{ + return "test userdata emit"; +} + int main (int argc, char **argv) { - ucl_object_t *obj, *cur, *ar, *ref; + ucl_object_t *obj, *cur, *ar, *ar1, *ref, *test_obj, *comments; ucl_object_iter_t it; - const ucl_object_t *found, *it_obj; + const ucl_object_t *found, *it_obj, *test; + struct ucl_emitter_functions *fn; FILE *out; unsigned char *emitted; const char *fname_out = NULL; + struct ucl_parser *parser; int ret = 0; switch (argc) { @@ -65,7 +79,8 @@ main (int argc, char **argv) /* Create some strings */ cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key1", 0, false); - cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); + cur = ucl_object_fromstring_common (" test \nstring\n\r\n\b\t\f\\\" ", 0, + UCL_STRING_TRIM | UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key2", 0, false); cur = ucl_object_fromstring_common (" test string \n", 0, 0); ucl_object_insert_key (obj, cur, "key3", 0, false); @@ -73,10 +88,34 @@ main (int argc, char **argv) ar = ucl_object_typed_new (UCL_ARRAY); cur = ucl_object_fromint (10); ucl_array_append (ar, cur); + assert (ucl_array_index_of (ar, cur) == 0); cur = ucl_object_fromdouble (10.1); ucl_array_append (ar, cur); + assert (ucl_array_index_of (ar, cur) == 1); cur = ucl_object_fromdouble (9.999); ucl_array_prepend (ar, cur); + assert (ucl_array_index_of (ar, cur) == 0); + + ar1 = ucl_object_copy (ar); + cur = ucl_object_fromstring ("abc"); + ucl_array_prepend (ar1, cur); + cur = ucl_object_fromstring ("cde"); + ucl_array_prepend (ar1, cur); + cur = ucl_object_fromstring ("абв"); /* UTF8 */ + ucl_array_prepend (ar1, cur); + cur = ucl_object_fromstring ("Ебв"); /* UTF8 */ + ucl_array_prepend (ar1, cur); +/* + * This is ususally broken or fragile as utf collate is far from perfect + cur = ucl_object_fromstring ("ёбв"); + ucl_array_prepend (ar1, cur); + cur = ucl_object_fromstring ("Ёбв"); // hello to @bapt + ucl_array_prepend (ar1, cur); +*/ + cur = ucl_object_fromstring ("😎"); /* everybody likes emoji in the code */ + ucl_array_prepend (ar1, cur); + + ucl_object_array_sort (ar1, ucl_object_compare_qsort); /* Removing from an array */ cur = ucl_object_fromdouble (1.0); @@ -122,22 +161,80 @@ main (int argc, char **argv) ucl_object_insert_key (obj, cur, "key13", 0, false); cur = ucl_object_frombool (true); ucl_object_insert_key (obj, cur, "k=3", 0, false); + ucl_object_insert_key (obj, ar1, "key14", 0, false); + cur = ucl_object_new_userdata (ud_dtor, ud_emit, NULL); + ucl_object_insert_key (obj, cur, "key15", 0, false); + + /* More tests for keys */ + cur = ucl_object_fromlstring ("test", 3); + ucl_object_insert_key (obj, cur, "key16", 0, false); + test = ucl_object_lookup_any (obj, "key100", "key200", "key300", "key16", NULL); + assert (test == cur); + test = ucl_object_lookup_len (obj, "key160", 5); + assert (test == cur); + cur = ucl_object_pop_key (obj, "key16"); + assert (test == cur); + test = ucl_object_pop_key (obj, "key16"); + assert (test == NULL); + test = ucl_object_lookup_len (obj, "key160", 5); + assert (test == NULL); + /* Objects merging tests */ + test_obj = ucl_object_new_full (UCL_OBJECT, 2); + ucl_object_insert_key (test_obj, cur, "key16", 0, true); + ucl_object_merge (obj, test_obj, true); + ucl_object_unref (test_obj); + /* Array merging test */ + test_obj = ucl_object_new_full (UCL_ARRAY, 3); + ucl_array_append (test_obj, ucl_object_fromstring ("test")); + ucl_array_merge (test_obj, ar1, false); + ucl_object_insert_key (obj, test_obj, "key17", 0, true); + /* Object deletion */ + cur = ucl_object_fromstring ("test"); + ucl_object_insert_key (obj, cur, "key18", 0, true); + assert (ucl_object_delete_key (obj, "key18")); + assert (!ucl_object_delete_key (obj, "key18")); + cur = ucl_object_fromlstring ("test", 4); + ucl_object_insert_key (obj, cur, "key18\0\0", 7, true); + assert (ucl_object_lookup_len (obj, "key18\0\0", 7) == cur); + assert (ucl_object_lookup (obj, "key18") == NULL); + assert (ucl_object_lookup_len (obj, "key18\0\1", 7) == NULL); + assert (ucl_object_delete_keyl (obj, "key18\0\0", 7)); + + /* Comments */ + + comments = ucl_object_typed_new (UCL_OBJECT); + found = ucl_object_lookup (obj, "key17"); + test = ucl_object_lookup (obj, "key16"); + ucl_comments_add (comments, found, "# test comment"); + assert (ucl_comments_find (comments, found) != NULL); + assert (ucl_comments_find (comments, test) == NULL); + ucl_comments_move (comments, found, test); + assert (ucl_comments_find (comments, found) == NULL); + assert (ucl_comments_find (comments, test) != NULL); + + /* Array replace */ + ar1 = ucl_object_typed_new (UCL_ARRAY); + cur = ucl_object_fromstring ("test"); + cur = ucl_elt_append (cur, ucl_object_fromstring ("test1")); + ucl_array_append (ar1, cur); + test = ucl_array_replace_index (ar1, ucl_object_fromstring ("test2"), 0); + assert (test == cur); /* Try to find using path */ /* Should exist */ - found = ucl_lookup_path (obj, "key4.1"); + found = ucl_object_lookup_path (obj, "key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* . should be ignored */ - found = ucl_lookup_path (obj, ".key4.1"); + found = ucl_object_lookup_path (obj, ".key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* moar dots... */ - found = ucl_lookup_path (obj, ".key4........1..."); + found = ucl_object_lookup_path (obj, ".key4........1..."); assert (found != NULL && ucl_object_toint (found) == 10); /* No such index */ - found = ucl_lookup_path (obj, ".key4.3"); + found = ucl_object_lookup_path (obj, ".key4.3"); assert (found == NULL); /* No such key */ - found = ucl_lookup_path (obj, "key9..key1"); + found = ucl_object_lookup_path (obj, "key9..key1"); assert (found == NULL); /* Test iteration */ @@ -167,10 +264,36 @@ main (int argc, char **argv) assert (ucl_object_type (it_obj) == UCL_BOOLEAN); ucl_object_iterate_free (it); - emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); - + fn = ucl_object_emit_memory_funcs (&emitted); + assert (ucl_object_emit_full (obj, UCL_EMIT_CONFIG, fn, comments)); fprintf (out, "%s\n", emitted); + ucl_object_emit_funcs_free (fn); ucl_object_unref (obj); + ucl_object_unref (comments); + + parser = ucl_parser_new (UCL_PARSER_NO_IMPLICIT_ARRAYS); + + if (ucl_parser_add_chunk_full (parser, emitted, strlen (emitted), + 3, UCL_DUPLICATE_ERROR, UCL_PARSE_UCL)) { + /* Should fail due to duplicate */ + assert (0); + } + else { + assert (ucl_parser_get_error (parser) != NULL); + ucl_parser_clear_error (parser); + ucl_parser_free (parser); + parser = ucl_parser_new (0); + ucl_parser_add_chunk_full (parser, emitted, strlen (emitted), + 3, UCL_DUPLICATE_MERGE, UCL_PARSE_UCL); + } + + assert (ucl_parser_get_column (parser) == 0); + assert (ucl_parser_get_linenum (parser) != 0); + ucl_parser_clear_error (parser); + assert (ucl_parser_get_error_code (parser) == 0); + obj = ucl_parser_get_object (parser); + ucl_parser_free (parser); + ucl_object_free (obj); if (emitted != NULL) { free (emitted); diff --git a/contrib/libucl/tests/test_msgpack.c b/contrib/libucl/tests/test_msgpack.c index dd5860e9828b..00f804a0a07e 100644 --- a/contrib/libucl/tests/test_msgpack.c +++ b/contrib/libucl/tests/test_msgpack.c @@ -26,9 +26,9 @@ #include "ucl_internal.h" #include <ctype.h> -static const int niter = 1000; -static const int ntests = 100; -static const int nelt = 10; +static const int niter = 20; +static const int ntests = 10; +static const int nelt = 20; static int recursion = 0; @@ -39,6 +39,10 @@ static ucl_object_t* ucl_test_string (void); static ucl_object_t* ucl_test_boolean (void); static ucl_object_t* ucl_test_map (void); static ucl_object_t* ucl_test_array (void); +static ucl_object_t* ucl_test_large_map (void); +static ucl_object_t* ucl_test_large_array (void); +static ucl_object_t* ucl_test_large_string (void); +static ucl_object_t* ucl_test_null (void); ucl_msgpack_test tests[] = { ucl_test_integer, @@ -46,6 +50,7 @@ ucl_msgpack_test tests[] = { ucl_test_boolean, ucl_test_map, ucl_test_array, + ucl_test_null }; #define NTESTS (sizeof(tests) / sizeof(tests[0])) @@ -144,6 +149,18 @@ main (int argc, char **argv) ucl_object_insert_key (obj, elt, key, klen, true); } + key = random_key (&klen); + elt = ucl_test_large_array (); + ucl_object_insert_key (obj, elt, key, klen, true); + + key = random_key (&klen); + elt = ucl_test_large_map (); + ucl_object_insert_key (obj, elt, key, klen, true); + + key = random_key (&klen); + elt = ucl_test_large_string (); + ucl_object_insert_key (obj, elt, key, klen, true); + emitted = ucl_object_emit_len (obj, UCL_EMIT_MSGPACK, &elen); assert (emitted != NULL); @@ -189,6 +206,7 @@ ucl_test_integer (void) ucl_object_t *res; int count, i; uint64_t cur; + double curf; res = ucl_object_typed_new (UCL_ARRAY); count = pcg32_random () % nelt; @@ -196,13 +214,32 @@ ucl_test_integer (void) for (i = 0; i < count; i ++) { cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); ucl_array_append (res, ucl_object_fromint (cur % 128)); + ucl_array_append (res, ucl_object_fromint (-(cur % 128))); + cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); + ucl_array_append (res, ucl_object_fromint (cur % UINT16_MAX)); + ucl_array_append (res, ucl_object_fromint (-(cur % INT16_MAX))); + cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); + ucl_array_append (res, ucl_object_fromint (cur % UINT32_MAX)); + ucl_array_append (res, ucl_object_fromint (-(cur % INT32_MAX))); + cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); + ucl_array_append (res, ucl_object_fromint (cur)); + ucl_array_append (res, ucl_object_fromint (-cur)); + /* Double version */ + cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); + curf = (cur % 128) / 19 * 16; + ucl_array_append (res, ucl_object_fromdouble (curf)); cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); - ucl_array_append (res, ucl_object_fromint (-cur % 128)); + curf = -(cur % 128) / 19 * 16; + ucl_array_append (res, ucl_object_fromdouble (curf)); cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); - ucl_array_append (res, ucl_object_fromint (cur % 65536)); + curf = (cur % 65536) / 19 * 16; + ucl_array_append (res, ucl_object_fromdouble (curf)); + ucl_array_append (res, ucl_object_fromdouble (-curf)); cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); - ucl_array_append (res, ucl_object_fromint (cur % INT32_MAX)); + curf = (cur % INT32_MAX) / 19 * 16; + ucl_array_append (res, ucl_object_fromdouble (curf)); cur = ((uint64_t)pcg32_random ()) << 32 | pcg32_random (); + memcpy (&curf, &cur, sizeof (curf)); ucl_array_append (res, ucl_object_fromint (cur)); } @@ -251,6 +288,14 @@ ucl_test_string (void) free (str); } + /* One large string */ + str = malloc (65537); + elt = ucl_object_fromstring_common (str, 65537, + UCL_STRING_RAW); + elt->flags |= UCL_OBJECT_BINARY; + ucl_array_append (res, elt); + free (str); + return res; } @@ -287,7 +332,13 @@ ucl_test_map (void) for (i = 0; i < count; i ++) { if (recursion > 10) { - sel = pcg32_random () % (NTESTS - 2); + for (;;) { + sel = pcg32_random () % NTESTS; + if (tests[sel] != ucl_test_map && + tests[sel] != ucl_test_array) { + break; + } + } } else { sel = pcg32_random () % NTESTS; @@ -311,6 +362,32 @@ ucl_test_map (void) } static ucl_object_t* +ucl_test_large_map (void) +{ + ucl_object_t *res, *cur; + int count, i; + uint32_t cur_len; + size_t klen; + const char *key; + + res = ucl_object_typed_new (UCL_OBJECT); + count = 65537; + + recursion ++; + + for (i = 0; i < count; i ++) { + key = random_key (&klen); + cur = ucl_test_boolean (); + assert (cur != NULL); + assert (klen != 0); + + ucl_object_insert_key (res, cur, key, klen, true); + } + + return res; +} + +static ucl_object_t* ucl_test_array (void) { ucl_object_t *res, *cur; @@ -324,7 +401,13 @@ ucl_test_array (void) for (i = 0; i < count; i ++) { if (recursion > 10) { - sel = pcg32_random () % (NTESTS - 2); + for (;;) { + sel = pcg32_random () % NTESTS; + if (tests[sel] != ucl_test_map && + tests[sel] != ucl_test_array) { + break; + } + } } else { sel = pcg32_random () % NTESTS; @@ -338,3 +421,47 @@ ucl_test_array (void) return res; } + +static ucl_object_t* +ucl_test_large_array (void) +{ + ucl_object_t *res, *cur; + int count, i; + uint32_t cur_len; + + res = ucl_object_typed_new (UCL_ARRAY); + count = 65537; + + recursion ++; + + for (i = 0; i < count; i ++) { + cur = ucl_test_boolean (); + assert (cur != NULL); + + ucl_array_append (res, cur); + } + + return res; +} + +static ucl_object_t* +ucl_test_large_string (void) +{ + ucl_object_t *res; + char *str; + uint32_t cur_len; + + while ((cur_len = pcg32_random ()) % 100000 == 0); + str = malloc (cur_len % 100000); + res = ucl_object_fromstring_common (str, cur_len % 100000, + UCL_STRING_RAW); + res->flags |= UCL_OBJECT_BINARY; + + return res; +} + +static ucl_object_t* +ucl_test_null (void) +{ + return ucl_object_typed_new (UCL_NULL); +} diff --git a/contrib/libucl/tests/test_schema.c b/contrib/libucl/tests/test_schema.c index 39eb7f399125..675764925507 100644 --- a/contrib/libucl/tests/test_schema.c +++ b/contrib/libucl/tests/test_schema.c @@ -68,9 +68,9 @@ perform_test (const ucl_object_t *schema, const ucl_object_t *obj, const ucl_object_t *valid, *data, *description; bool match; - data = ucl_object_find_key (obj, "data"); - description = ucl_object_find_key (obj, "description"); - valid = ucl_object_find_key (obj, "valid"); + data = ucl_object_lookup (obj, "data"); + description = ucl_object_lookup (obj, "description"); + valid = ucl_object_lookup (obj, "valid"); if (data == NULL || description == NULL || valid == NULL) { fprintf (stdout, "Bad test case\n"); @@ -103,9 +103,9 @@ perform_tests (const ucl_object_t *obj) return EXIT_FAILURE; } - schema = ucl_object_find_key (obj, "schema"); - tests = ucl_object_find_key (obj, "tests"); - description = ucl_object_find_key (obj, "description"); + schema = ucl_object_lookup (obj, "schema"); + tests = ucl_object_lookup (obj, "tests"); + description = ucl_object_lookup (obj, "description"); if (schema == NULL || tests == NULL || description == NULL) { fprintf (stdout, "Bad test case\n"); @@ -114,7 +114,7 @@ perform_tests (const ucl_object_t *obj) memset (&err, 0, sizeof (err)); - while ((test = ucl_iterate_object (tests, &iter, true)) != NULL) { + while ((test = ucl_object_iterate (tests, &iter, true)) != NULL) { if (!perform_test (schema, test, &err)) { fprintf (stdout, "Test suite '%s' failed\n", ucl_object_tostring (description)); @@ -151,7 +151,7 @@ main (int argc, char **argv) obj = ucl_parser_get_object (parser); ucl_parser_free (parser); - while ((elt = ucl_iterate_object (obj, &iter, true)) != NULL) { + while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) { ret = perform_tests (elt); if (ret != 0) { break; |