diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-10-22 17:04:20 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-10-22 17:04:20 +0100 |
commit | b7a0873dac545a79778133152a9b6d9b4e47cc42 (patch) | |
tree | d389696b986920f8e8f66df01519f6f6d55a02b5 /src/ucl/tests | |
parent | 41e269801406c374a041da0fd0c6b4eff6ba4f3d (diff) | |
download | rspamd-b7a0873dac545a79778133152a9b6d9b4e47cc42.tar.gz rspamd-b7a0873dac545a79778133152a9b6d9b4e47cc42.zip |
Replace RCL to UCL from libucl to avoid duplicity in the code.
Diffstat (limited to 'src/ucl/tests')
-rw-r--r-- | src/ucl/tests/1.in | 11 | ||||
-rw-r--r-- | src/ucl/tests/1.res | 12 | ||||
-rw-r--r-- | src/ucl/tests/2.in | 18 | ||||
-rw-r--r-- | src/ucl/tests/2.res | 45 | ||||
-rw-r--r-- | src/ucl/tests/3.in | 31 | ||||
-rw-r--r-- | src/ucl/tests/3.res | 27 | ||||
-rw-r--r-- | src/ucl/tests/4.in | 47 | ||||
-rw-r--r-- | src/ucl/tests/4.res | 36 | ||||
-rw-r--r-- | src/ucl/tests/generate.res | 18 | ||||
-rw-r--r-- | src/ucl/tests/rcl_test.json.xz | bin | 0 -> 1853872 bytes | |||
-rwxr-xr-x | src/ucl/tests/run_tests.sh | 57 | ||||
-rw-r--r-- | src/ucl/tests/test_basic.c | 113 | ||||
-rw-r--r-- | src/ucl/tests/test_generate.c | 98 | ||||
-rw-r--r-- | src/ucl/tests/test_speed.c | 128 |
14 files changed, 641 insertions, 0 deletions
diff --git a/src/ucl/tests/1.in b/src/ucl/tests/1.in new file mode 100644 index 000000000..41b0cfb3c --- /dev/null +++ b/src/ucl/tests/1.in @@ -0,0 +1,11 @@ +{ +"key1": value; +"key1": value2; +"key1": "value;" +"key1": 1.0, +"key1": -1e-10, +"key1": 1 +"key1": true +"key1": no +"key1": yes +} diff --git a/src/ucl/tests/1.res b/src/ucl/tests/1.res new file mode 100644 index 000000000..789c82769 --- /dev/null +++ b/src/ucl/tests/1.res @@ -0,0 +1,12 @@ +key1 [ + "value", + "value2", + "value;", + 1.0, + -1e-10, + 1, + true, + false, + true, +] + diff --git a/src/ucl/tests/2.in b/src/ucl/tests/2.in new file mode 100644 index 000000000..59a4d0c38 --- /dev/null +++ b/src/ucl/tests/2.in @@ -0,0 +1,18 @@ +section1 { param1 = value; param2 = value, +section3 {param = value; param2 = value, param3 = ["value1", value2, 100500]}} +section2 { param1 = {key = value}, param1 = ["key"]} + +# Numbers +key1 = 1s +key2 = 1min +key3 = 1kb +key4 = 5M +key5 = 10mS +key6 = 10y + +# Strings +key1 = "some string"; +key2 = /some/path; +key3 = 111some, +key4: s1, +"key5": "\n\r123" diff --git a/src/ucl/tests/2.res b/src/ucl/tests/2.res new file mode 100644 index 000000000..3a8966f65 --- /dev/null +++ b/src/ucl/tests/2.res @@ -0,0 +1,45 @@ +section1 { + param1 = "value"; + param2 = "value"; + section3 { + param = "value"; + param2 = "value"; + param3 [ + "value1", + "value2", + 100500, + ] + } +} +section2 { + param1 [ + { + key = "value"; + }, + [ + "key", + ], + ] +} +key1 [ + 1.0, + "some string", +] +key2 [ + 60.0, + "/some/path", +] +key3 [ + 1024, + "111some", +] +key4 [ + 5000000, + "s1", +] +key5 [ + 0.010000, + "\n\r123", +] +key6 = 2207520000.000000; + diff --git a/src/ucl/tests/3.in b/src/ucl/tests/3.in new file mode 100644 index 000000000..b3e369670 --- /dev/null +++ b/src/ucl/tests/3.in @@ -0,0 +1,31 @@ +/* + * Pkg conf + */ + +#packagesite http//pkg.freebsd.org/freebsd-9-amd64/latest +#packagesite http//pkg.freebsd.org/freebsd-9-amd64/latest +packagesite: http://pkg-test.freebsd.org/pkg-test/${ABI}/latest +squaretest: some[]value +ALIAS : { + all-depends: query %dn-%dv, + annotations: info -A, + build-depends: info -qd, + download: fetch, + iinfo: info -i -g -x, + isearch: search -i -g -x, + leaf: query -e '%a == 0' '%n-%v', + leaf: query -e '%a == 0' '%n-%v', + list: info -ql, + origin: info -qo, + provided-depends: info -qb, + raw: info -R, + required-depends: info -qr, + shared-depends: info -qB, + show: info -f -k, + size: info -sq, + } + +repo_dirs : [ + /home/bapt, + /usr/local/etc +] diff --git a/src/ucl/tests/3.res b/src/ucl/tests/3.res new file mode 100644 index 000000000..2f84ed69e --- /dev/null +++ b/src/ucl/tests/3.res @@ -0,0 +1,27 @@ +packagesite = "http://pkg-test.freebsd.org/pkg-test/${ABI}/latest"; +squaretest = "some[]value"; +alias { + all-depends = "query %dn-%dv"; + annotations = "info -A"; + build-depends = "info -qd"; + download = "fetch"; + iinfo = "info -i -g -x"; + isearch = "search -i -g -x"; + leaf [ + "query -e '%a == 0' '%n-%v'", + "query -e '%a == 0' '%n-%v'", + ] + list = "info -ql"; + origin = "info -qo"; + provided-depends = "info -qb"; + raw = "info -R"; + required-depends = "info -qr"; + shared-depends = "info -qB"; + show = "info -f -k"; + size = "info -sq"; +} +repo_dirs [ + "/home/bapt", + "/usr/local/etc", +] + diff --git a/src/ucl/tests/4.in b/src/ucl/tests/4.in new file mode 100644 index 000000000..2b296efff --- /dev/null +++ b/src/ucl/tests/4.in @@ -0,0 +1,47 @@ +name : "pkgconf" +version : "0.9.3" +origin : "devel/pkgconf" +comment : "Utility to help to configure compiler and linker flags" +arch : "freebsd:9:x86:64" +maintainer : "bapt@FreeBSD.org" +prefix : "/usr/local" +licenselogic : "single" +licenses : [ + "BSD", +] +flatsize : 60523 +desc : "pkgconf is a program which helps to configure compiler and linker flags for\ndevelopment frameworks. It is similar to pkg-config, but was written from\nscratch in Summer of 2011 to replace pkg-config, which now needs itself to build\nitself.\n\nWWW: https://github.com/pkgconf/pkgconf" +categories : [ + "devel", +] +files : { + /usr/local/bin/pkg-config : "-", + /usr/local/bin/pkgconf : "4a0fc53e5ad64e8085da2e61652d61c50b192a086421d865703f1de9f724da38", + /usr/local/share/aclocal/pkg.m4 : "cffab33d659adfe36497ec57665eec36fa6fb7b007e578e6ac2434cc28be8820", + /usr/local/share/licenses/pkgconf-0.9.3/BSD : "85e7a53b5e2d3e350e2d084fed2f94b7f63005f8e1168740e1e84aa9fa5d48ce", + /usr/local/share/licenses/pkgconf-0.9.3/LICENSE : "d9cce0db43502eb1bd8fbef7e960cfaa43b5647186f7f7379923b336209fd77b", + /usr/local/share/licenses/pkgconf-0.9.3/catalog.mk : "e7b131acce7c3d3c61f2214607b11b34526e03b05afe89a608f50586a898e2ef", +} +directories : { + /usr/local/share/licenses/pkgconf-0.9.3/ : false, + /usr/local/share/licenses/ : true, +} +scripts : { + post-install : "cd /usr/local\nn", + pre-deinstall : "cd /usr/local\nn", + post-deinstall : "cd /usr/local\nn", +} +multiline-key : <<EOD +test +test +test\n +/* comment like */ +# Some invalid endings + EOD +EOD +EOF +# Valid ending + empty string + +EOD + +normal-key : <<EODnot diff --git a/src/ucl/tests/4.res b/src/ucl/tests/4.res new file mode 100644 index 000000000..58c359943 --- /dev/null +++ b/src/ucl/tests/4.res @@ -0,0 +1,36 @@ +name = "pkgconf"; +version = "0.9.3"; +origin = "devel/pkgconf"; +comment = "Utility to help to configure compiler and linker flags"; +arch = "freebsd:9:x86:64"; +maintainer = "bapt@FreeBSD.org"; +prefix = "/usr/local"; +licenselogic = "single"; +licenses [ + "BSD", +] +flatsize = 60523; +desc = "pkgconf is a program which helps to configure compiler and linker flags for\ndevelopment frameworks. It is similar to pkg-config, but was written from\nscratch in Summer of 2011 to replace pkg-config, which now needs itself to build\nitself.\n\nWWW: https://github.com/pkgconf/pkgconf"; +categories [ + "devel", +] +files { + /usr/local/bin/pkg-config = "-"; + /usr/local/bin/pkgconf = "4a0fc53e5ad64e8085da2e61652d61c50b192a086421d865703f1de9f724da38"; + /usr/local/share/aclocal/pkg.m4 = "cffab33d659adfe36497ec57665eec36fa6fb7b007e578e6ac2434cc28be8820"; + /usr/local/share/licenses/pkgconf-0.9.3/bsd = "85e7a53b5e2d3e350e2d084fed2f94b7f63005f8e1168740e1e84aa9fa5d48ce"; + /usr/local/share/licenses/pkgconf-0.9.3/license = "d9cce0db43502eb1bd8fbef7e960cfaa43b5647186f7f7379923b336209fd77b"; + /usr/local/share/licenses/pkgconf-0.9.3/catalog.mk = "e7b131acce7c3d3c61f2214607b11b34526e03b05afe89a608f50586a898e2ef"; +} +directories { + /usr/local/share/licenses/pkgconf-0.9.3/ = false; + /usr/local/share/licenses/ = true; +} +scripts { + post-install = "cd /usr/local\nn"; + pre-deinstall = "cd /usr/local\nn"; + post-deinstall = "cd /usr/local\nn"; +} +multiline-key = "test\ntest\ntest\\n\n/* comment like */\n# Some invalid endings\n EOD\nEOD \nEOF\n# Valid ending + empty string\n"; +normal-key = "<<EODnot"; + diff --git a/src/ucl/tests/generate.res b/src/ucl/tests/generate.res new file mode 100644 index 000000000..e7659ec3a --- /dev/null +++ b/src/ucl/tests/generate.res @@ -0,0 +1,18 @@ +key1 = "test string"; +key2 = "test \\nstring"; +key3 = " test string \n"; +key4 [ + [ + 10, + 10.100000, + ], + true, +] +key5 = ""; +key6 = ""; +key7 = " \\n"; +key8 = 1048576; +key9 = 3.140000; +key10 = true; +key11 = false; + diff --git a/src/ucl/tests/rcl_test.json.xz b/src/ucl/tests/rcl_test.json.xz Binary files differnew file mode 100644 index 000000000..98c3559ad --- /dev/null +++ b/src/ucl/tests/rcl_test.json.xz diff --git a/src/ucl/tests/run_tests.sh b/src/ucl/tests/run_tests.sh new file mode 100755 index 000000000..71e6f1b08 --- /dev/null +++ b/src/ucl/tests/run_tests.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +if [ $# -lt 1 ] ; then + echo 'Specify binary to run as the first argument' + exit 1 +fi + + +for _tin in ${TEST_DIR}/*.in ; do + _t=`echo $_tin | sed -e 's/.in$//'` + $1 $_t.in $_t.out + if [ $? -ne 0 ] ; then + echo "Test: $_t failed, output:" + cat $_t.out + rm $_t.out + exit 1 + fi + if [ -f $_t.res ] ; then + diff -s $_t.out $_t.res -u 2>/dev/null + if [ $? -ne 0 ] ; then + rm $_t.out + echo "Test: $_t output missmatch" + exit 1 + fi + fi + rm $_t.out +done + +if [ $# -gt 2 ] ; then + $3 ${TEST_DIR}/generate.out + diff -s ${TEST_DIR}/generate.out ${TEST_DIR}/generate.res -u 2>/dev/null + if [ $? -ne 0 ] ; then + rm ${TEST_DIR}/generate.out + echo "Test: generate.res output missmatch" + exit 1 + fi + rm ${TEST_DIR}/generate.out +fi + +if [ $# -gt 1 -a -x "/usr/bin/xz" ] ; then + echo 'Running speed tests' + for _tin in ${TEST_DIR}/*.xz ; do + echo "Unpacking $_tin..." + xz -cd < $_tin > ${TEST_DIR}/test_file + # Preread file to cheat benchmark! + cat ${TEST_DIR}/test_file > /dev/null + echo "Starting benchmarking for $_tin..." + $2 ${TEST_DIR}/test_file + if [ $? -ne 0 ] ; then + echo "Test: $_tin failed" + rm ${TEST_DIR}/test_file + exit 1 + fi + rm ${TEST_DIR}/test_file + done +fi + diff --git a/src/ucl/tests/test_basic.c b/src/ucl/tests/test_basic.c new file mode 100644 index 000000000..d4d4786d6 --- /dev/null +++ b/src/ucl/tests/test_basic.c @@ -0,0 +1,113 @@ +/* Copyright (c) 2013, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <errno.h> +#include "ucl.h" + +int +main (int argc, char **argv) +{ + char inbuf[8192]; + struct ucl_parser *parser, *parser2; + ucl_object_t *obj; + FILE *in, *out; + UT_string *err = NULL; + unsigned char *emitted; + const char *fname_in = NULL, *fname_out = NULL; + int ret = 0; + + switch (argc) { + case 2: + fname_in = argv[1]; + break; + case 3: + fname_in = argv[1]; + fname_out = argv[2]; + break; + } + + if (fname_in != NULL) { + in = fopen (fname_in, "r"); + if (in == NULL) { + exit (-errno); + } + } + else { + in = stdin; + } + parser = ucl_parser_new (UCL_FLAG_KEY_LOWERCASE); + + while (!feof (in)) { + fread (inbuf, sizeof (inbuf), 1, in); + ucl_parser_add_chunk (parser, inbuf, strlen (inbuf), &err); + } + fclose (in); + + if (fname_out != NULL) { + out = fopen (fname_out, "w"); + if (out == NULL) { + exit (-errno); + } + } + else { + out = stdout; + } + if (err != NULL) { + fprintf (out, "Error occurred: %s\n", err->d); + ret = 1; + goto end; + } + obj = ucl_parser_get_object (parser, &err); + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + ucl_parser_free (parser); + ucl_object_unref (obj); + parser2 = ucl_parser_new (UCL_FLAG_KEY_LOWERCASE); + ucl_parser_add_chunk (parser2, emitted, strlen (emitted), &err); + + if (err != NULL) { + fprintf (out, "Error occurred: %s\n", err->d); + fprintf (out, "%s\n", emitted); + ret = 1; + goto end; + } + if (emitted != NULL) { + free (emitted); + } + obj = ucl_parser_get_object (parser2, &err); + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + + fprintf (out, "%s\n", emitted); + ucl_object_unref (obj); + +end: + if (emitted != NULL) { + free (emitted); + } + if (parser2 != NULL) { + ucl_parser_free (parser2); + } + fclose (out); + + return ret; +} diff --git a/src/ucl/tests/test_generate.c b/src/ucl/tests/test_generate.c new file mode 100644 index 000000000..a61b35396 --- /dev/null +++ b/src/ucl/tests/test_generate.c @@ -0,0 +1,98 @@ +/* Copyright (c) 2013, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <errno.h> +#include "ucl.h" + +int +main (int argc, char **argv) +{ + ucl_object_t *obj, *cur, *ar; + FILE *out; + unsigned char *emitted; + const char *fname_out = NULL; + int ret = 0; + + switch (argc) { + case 2: + fname_out = argv[1]; + break; + } + + + if (fname_out != NULL) { + out = fopen (fname_out, "w"); + if (out == NULL) { + exit (-errno); + } + } + else { + out = stdout; + } + + obj = ucl_object_new (); + /* Create some strings */ + cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); + obj = ucl_object_insert_key (obj, cur, "key1", 0, false); + cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); + obj = ucl_object_insert_key (obj, cur, "key2", 0, false); + cur = ucl_object_fromstring_common (" test string \n", 0, 0); + obj = ucl_object_insert_key (obj, cur, "key3", 0, false); + /* Array of numbers */ + cur = ucl_object_fromint (10); + ar = ucl_array_append (NULL, cur); + cur = ucl_object_fromdouble (10.1); + ar = ucl_array_append (ar, cur); + obj = ucl_object_insert_key (obj, ar, "key4", 0, false); + cur = ucl_object_frombool (true); + obj = ucl_object_insert_key (obj, cur, "key4", 0, false); + /* Empty strings */ + cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); + obj = ucl_object_insert_key (obj, cur, "key5", 0, false); + cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); + obj = ucl_object_insert_key (obj, cur, "key6", 0, false); + cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); + obj = ucl_object_insert_key (obj, cur, "key7", 0, false); + /* Numbers and booleans */ + cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); + obj = ucl_object_insert_key (obj, cur, "key8", 0, false); + cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); + obj = ucl_object_insert_key (obj, cur, "key9", 0, false); + cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); + obj = ucl_object_insert_key (obj, cur, "key10", 0, false); + cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); + obj = ucl_object_insert_key (obj, cur, "key11", 0, false); + + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + + fprintf (out, "%s\n", emitted); + ucl_object_unref (obj); + + if (emitted != NULL) { + free (emitted); + } + fclose (out); + + return ret; +} diff --git a/src/ucl/tests/test_speed.c b/src/ucl/tests/test_speed.c new file mode 100644 index 000000000..92a972aa5 --- /dev/null +++ b/src/ucl/tests/test_speed.c @@ -0,0 +1,128 @@ +/* Copyright (c) 2013, Vsevolod Stakhov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <time.h> + +#include "ucl.h" + +int +main (int argc, char **argv) +{ + void *map; + struct ucl_parser *parser; + ucl_object_t *obj; + int fin; + UT_string *err = NULL; + unsigned char *emitted; + struct stat st; + const char *fname_in = NULL; + int ret = 0; + struct timespec start, end; + double seconds; + + switch (argc) { + case 2: + fname_in = argv[1]; + break; + } + + fin = open (fname_in, O_RDONLY); + if (fin == -1) { + perror ("open failed"); + exit (EXIT_FAILURE); + } + parser = ucl_parser_new (UCL_FLAG_ZEROCOPY); + + (void)fstat (fin, &st); + map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fin, 0); + if (map == MAP_FAILED) { + perror ("mmap failed"); + exit (EXIT_FAILURE); + } + + close (fin); + + clock_gettime (CLOCK_MONOTONIC, &start); + ucl_parser_add_chunk (parser, map, st.st_size, &err); + + obj = ucl_parser_get_object (parser, &err); + clock_gettime (CLOCK_MONOTONIC, &end); + + seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.; + printf ("ucl: parsed input in %.4f seconds\n", seconds); + if (err != NULL) { + printf ("Error occurred: %s\n", err->d); + ret = 1; + } + + clock_gettime (CLOCK_MONOTONIC, &start); + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + clock_gettime (CLOCK_MONOTONIC, &end); + + seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.; + printf ("ucl: emitted config in %.4f seconds\n", seconds); + + free (emitted); + + clock_gettime (CLOCK_MONOTONIC, &start); + emitted = ucl_object_emit (obj, UCL_EMIT_JSON); + clock_gettime (CLOCK_MONOTONIC, &end); + + seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.; + printf ("ucl: emitted json in %.4f seconds\n", seconds); + + free (emitted); + + clock_gettime (CLOCK_MONOTONIC, &start); + emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); + clock_gettime (CLOCK_MONOTONIC, &end); + + seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.; + printf ("ucl: emitted compact json in %.4f seconds\n", seconds); + + free (emitted); + + clock_gettime (CLOCK_MONOTONIC, &start); + emitted = ucl_object_emit (obj, UCL_EMIT_YAML); + clock_gettime (CLOCK_MONOTONIC, &end); + + seconds = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.; + printf ("ucl: emitted yaml in %.4f seconds\n", seconds); + + free (emitted); + + ucl_parser_free (parser); + ucl_object_unref (obj); + + munmap (map, st.st_size); + + return ret; +} |