diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/1.in | 14 | ||||
-rw-r--r-- | tests/1.res | 13 | ||||
-rw-r--r-- | tests/2.in | 24 | ||||
-rw-r--r-- | tests/2.res | 37 | ||||
-rw-r--r-- | tests/3.in | 31 | ||||
-rw-r--r-- | tests/3.res | 25 | ||||
-rw-r--r-- | tests/4.in | 47 | ||||
-rw-r--r-- | tests/4.res | 36 | ||||
-rw-r--r-- | tests/5.in | 1 | ||||
-rw-r--r-- | tests/5.res | 1 | ||||
-rw-r--r-- | tests/6.in | 4 | ||||
-rw-r--r-- | tests/6.res | 2 | ||||
-rw-r--r-- | tests/7.in | 0 | ||||
-rw-r--r-- | tests/7.res | 1 | ||||
-rw-r--r-- | tests/8.in | 21 | ||||
-rw-r--r-- | tests/8.res | 36 | ||||
-rw-r--r-- | tests/9-comment.inc | 1 | ||||
-rw-r--r-- | tests/9-empty.inc | 0 | ||||
-rw-r--r-- | tests/9.in | 13 | ||||
-rw-r--r-- | tests/9.inc | 1 | ||||
-rw-r--r-- | tests/9.res | 5 | ||||
-rw-r--r-- | tests/generate.res | 20 | ||||
-rwxr-xr-x | tests/run_tests.sh | 58 | ||||
-rw-r--r-- | tests/test_basic.c | 151 | ||||
-rw-r--r-- | tests/test_generate.c | 126 | ||||
-rw-r--r-- | tests/test_speed.c | 149 |
26 files changed, 817 insertions, 0 deletions
diff --git a/tests/1.in b/tests/1.in new file mode 100644 index 0000000..9c14df6 --- /dev/null +++ b/tests/1.in @@ -0,0 +1,14 @@ +{ +"key1": value; +"key1": value2; +"key1": "value;" +"key1": 1.0, +"key1": -0xdeadbeef +"key1": 0xdeadbeef.1 +"key1": 0xreadbeef +"key1": -1e-10, +"key1": 1 +"key1": true +"key1": no +"key1": yes +} diff --git a/tests/1.res b/tests/1.res new file mode 100644 index 0000000..660af9f --- /dev/null +++ b/tests/1.res @@ -0,0 +1,13 @@ +key1 = "value"; +key1 = "value2"; +key1 = "value;"; +key1 = 1.0; +key1 = -3735928559; +key1 = "0xdeadbeef.1"; +key1 = "0xreadbeef"; +key1 = -1e-10; +key1 = 1; +key1 = true; +key1 = false; +key1 = true; + diff --git a/tests/2.in b/tests/2.in new file mode 100644 index 0000000..a4419a4 --- /dev/null +++ b/tests/2.in @@ -0,0 +1,24 @@ +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" + +# Variables +keyvar = "$ABItest"; +keyvar = "${ABI}$ABI${ABI}${$ABI}"; +keyvar = "${some}$no${}$$test$$$$$$$"; +keyvar = "$ABI$$ABI$$$ABI$$$$"; diff --git a/tests/2.res b/tests/2.res new file mode 100644 index 0000000..72e9570 --- /dev/null +++ b/tests/2.res @@ -0,0 +1,37 @@ +section1 { + param1 = "value"; + param2 = "value"; + section3 { + param = "value"; + param2 = "value"; + param3 [ + "value1", + "value2", + 100500, + ] + } +} +section2 { + param1 { + key = "value"; + } + param1 [ + "key", + ] +} +key1 = 1.0; +key1 = "some string"; +key2 = 60.0; +key2 = "/some/path"; +key3 = 1024; +key3 = "111some"; +key4 = 5000000; +key4 = "s1"; +key5 = 0.010000; +key5 = "\n\r123"; +key6 = 2207520000.000000; +keyvar = "unknowntest"; +keyvar = "unknownunknownunknown${unknown}"; +keyvar = "${some}$no${}$$test$$$$$$$"; +keyvar = "unknown$ABI$unknown$$"; + diff --git a/tests/3.in b/tests/3.in new file mode 100644 index 0000000..b3e3696 --- /dev/null +++ b/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/tests/3.res b/tests/3.res new file mode 100644 index 0000000..bb18457 --- /dev/null +++ b/tests/3.res @@ -0,0 +1,25 @@ +packagesite = "http://pkg-test.freebsd.org/pkg-test/unknown/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/tests/4.in b/tests/4.in new file mode 100644 index 0000000..2b296ef --- /dev/null +++ b/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/tests/4.res b/tests/4.res new file mode 100644 index 0000000..58c3599 --- /dev/null +++ b/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/tests/5.in b/tests/5.in new file mode 100644 index 0000000..83c831f --- /dev/null +++ b/tests/5.in @@ -0,0 +1 @@ +# test diff --git a/tests/5.res b/tests/5.res new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/5.res @@ -0,0 +1 @@ + diff --git a/tests/6.in b/tests/6.in new file mode 100644 index 0000000..5b46088 --- /dev/null +++ b/tests/6.in @@ -0,0 +1,4 @@ + +# test +# +key = value diff --git a/tests/6.res b/tests/6.res new file mode 100644 index 0000000..4b17c4b --- /dev/null +++ b/tests/6.res @@ -0,0 +1,2 @@ +key = "value"; + diff --git a/tests/7.in b/tests/7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/7.in diff --git a/tests/7.res b/tests/7.res new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/7.res @@ -0,0 +1 @@ + diff --git a/tests/8.in b/tests/8.in new file mode 100644 index 0000000..5559b3c --- /dev/null +++ b/tests/8.in @@ -0,0 +1,21 @@ +section blah { # test + param = "value" +} +section test { + key = test; + subsection testsub { + flag on; + subsubsection testsubsub1 testsubsub2 { + key = [1, 2, 3]; + } + } +} + +section test { + /* Empty */ +} + + +section foo { # test + param = 123.2; +} diff --git a/tests/8.res b/tests/8.res new file mode 100644 index 0000000..36eb39a --- /dev/null +++ b/tests/8.res @@ -0,0 +1,36 @@ +section { + blah { + param = "value"; + } +} +section { + test { + key = "test"; + subsection { + testsub { + flag = true; + subsubsection { + testsubsub1 { + testsubsub2 { + key [ + 1, + 2, + 3, + ] + } + } + } + } + } + } +} +section { + test { + } +} +section { + foo { + param = 123.200000; + } +} + diff --git a/tests/9-comment.inc b/tests/9-comment.inc new file mode 100644 index 0000000..8939db1 --- /dev/null +++ b/tests/9-comment.inc @@ -0,0 +1 @@ +#key = value diff --git a/tests/9-empty.inc b/tests/9-empty.inc new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/9-empty.inc diff --git a/tests/9.in b/tests/9.in new file mode 100644 index 0000000..da04e40 --- /dev/null +++ b/tests/9.in @@ -0,0 +1,13 @@ +.include "$CURDIR/9.inc" +.include "$CURDIR/9-empty.inc" +.include "$CURDIR/9-comment.inc" +#.include "$CURDIR/9.inc" +.include "$CURDIR/9.inc" + +key = value; +.include "$CURDIR/9.inc" + +.try_include "/non/existent" +#.try_include "$CURDIR/9.incorrect.inc" +# 9.incorrect.inc contains '{}}' +#key = value; diff --git a/tests/9.inc b/tests/9.inc new file mode 100644 index 0000000..c5592b8 --- /dev/null +++ b/tests/9.inc @@ -0,0 +1 @@ +key1 = value diff --git a/tests/9.res b/tests/9.res new file mode 100644 index 0000000..ec3f014 --- /dev/null +++ b/tests/9.res @@ -0,0 +1,5 @@ +key1 = "value"; +key1 = "value"; +key1 = "value"; +key = "value"; + diff --git a/tests/generate.res b/tests/generate.res new file mode 100644 index 0000000..1d39478 --- /dev/null +++ b/tests/generate.res @@ -0,0 +1,20 @@ +key1 = "test string"; +key2 = "test \\nstring"; +key3 = " test string \n"; +key4 [ + 9.999000, + 10, + 10.100000, +] +key4 = true; +key5 = ""; +key6 = ""; +key7 = " \\n"; +key8 = 1048576; +key9 = 3.140000; +key10 = true; +key11 = false; +key12 = "gslin@gslin.org"; +key13 = "#test"; +"k=3" = true; + diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..6c7751e --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,58 @@ +#!/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 + +sh -c "xz -c < /dev/null > /dev/null" +if [ $? -eq 0 -a $# -gt 1 ] ; 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/tests/test_basic.c b/tests/test_basic.c new file mode 100644 index 0000000..641679f --- /dev/null +++ b/tests/test_basic.c @@ -0,0 +1,151 @@ +/* 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 <unistd.h> +#include "ucl.h" + +int +main (int argc, char **argv) +{ + char inbuf[8192], *test_in = NULL; + struct ucl_parser *parser = NULL, *parser2 = NULL; + ucl_object_t *obj; + FILE *in, *out; + unsigned char *emitted = NULL; + const char *fname_in = NULL, *fname_out = NULL; + int ret = 0, inlen, opt, json = 0; + + while ((opt = getopt(argc, argv, "j")) != -1) { + switch (opt) { + case 'j': + json = 1; + break; + default: /* '?' */ + fprintf (stderr, "Usage: %s [-j] [in] [out]\n", + argv[0]); + exit (EXIT_FAILURE); + } + } + + argc -= optind; + argv += optind; + + switch (argc) { + case 1: + fname_in = argv[0]; + break; + case 2: + fname_in = argv[0]; + fname_out = argv[1]; + break; + } + + if (fname_in != NULL) { + in = fopen (fname_in, "r"); + if (in == NULL) { + exit (-errno); + } + } + else { + in = stdin; + } + parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); + ucl_parser_register_variable (parser, "ABI", "unknown"); + + if (fname_in != NULL) { + ucl_parser_set_filevars (parser, fname_in, true); + } + + while (!feof (in)) { + memset (inbuf, 0, sizeof (inbuf)); + (void)fread (inbuf, sizeof (inbuf) - 1, 1, in); + inlen = strlen (inbuf); + test_in = malloc (inlen); + memcpy (test_in, inbuf, inlen); + ucl_parser_add_chunk (parser, test_in, inlen); + } + fclose (in); + + if (fname_out != NULL) { + out = fopen (fname_out, "w"); + if (out == NULL) { + exit (-errno); + } + } + else { + out = stdout; + } + if (ucl_parser_get_error(parser) != NULL) { + fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser)); + ret = 1; + goto end; + } + obj = ucl_parser_get_object (parser); + if (json) { + emitted = ucl_object_emit (obj, UCL_EMIT_JSON); + } + else { + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + } + ucl_parser_free (parser); + ucl_object_unref (obj); + parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); + ucl_parser_add_chunk (parser2, emitted, strlen (emitted)); + + if (ucl_parser_get_error(parser2) != NULL) { + fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2)); + fprintf (out, "%s\n", emitted); + ret = 1; + goto end; + } + if (emitted != NULL) { + free (emitted); + } + obj = ucl_parser_get_object (parser2); + if (json) { + emitted = ucl_object_emit (obj, UCL_EMIT_JSON); + } + else { + 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); + } + if (test_in != NULL) { + free (test_in); + } + + fclose (out); + + return ret; +} diff --git a/tests/test_generate.c b/tests/test_generate.c new file mode 100644 index 0000000..b2081ba --- /dev/null +++ b/tests/test_generate.c @@ -0,0 +1,126 @@ +/* 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 <assert.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_typed_new (UCL_OBJECT); + /* 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); + cur = ucl_object_fromdouble (9.999); + ar = ucl_array_prepend (ar, cur); + + /* Removing from an array */ + cur = ucl_object_fromdouble (1.0); + ar = ucl_array_append (ar, cur); + cur = ucl_array_delete (ar, cur); + assert (ucl_object_todouble (cur) == 1.0); + ucl_object_unref (cur); + cur = ucl_object_fromdouble (2.0); + ar = ucl_array_append (ar, cur); + cur = ucl_array_pop_last (ar); + assert (ucl_object_todouble (cur) == 2.0); + ucl_object_unref (cur); + cur = ucl_object_fromdouble (3.0); + ar = ucl_array_prepend (ar, cur); + cur = ucl_array_pop_first (ar); + assert (ucl_object_todouble (cur) == 3.0); + ucl_object_unref (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); + cur = ucl_object_fromstring_common ("gslin@gslin.org", 0, UCL_STRING_PARSE_INT); + obj = ucl_object_insert_key (obj, cur, "key12", 0, false); + cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); + obj = ucl_object_insert_key (obj, cur, "key13", 0, false); + cur = ucl_object_frombool (true); + obj = ucl_object_insert_key (obj, cur, "k=3", 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/tests/test_speed.c b/tests/test_speed.c new file mode 100644 index 0000000..cbf290c --- /dev/null +++ b/tests/test_speed.c @@ -0,0 +1,149 @@ +/* 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> + +#ifdef __APPLE__ +#include <mach/mach_time.h> +#endif + +#include "ucl.h" + +static double +get_ticks (void) +{ + double res; + +#ifdef __APPLE__ + res = mach_absolute_time () / 1000000000.; +#else + struct timespec ts; + clock_gettime (CLOCK_MONOTONIC, &ts); + + res = (double)ts.tv_sec + ts.tv_nsec / 1000000000.; +#endif + + return res; +} + +int +main (int argc, char **argv) +{ + void *map; + struct ucl_parser *parser; + ucl_object_t *obj; + int fin; + unsigned char *emitted; + struct stat st; + const char *fname_in = NULL; + int ret = 0; + double start, end, 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_PARSER_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); + + start = get_ticks (); + ucl_parser_add_chunk (parser, map, st.st_size); + + obj = ucl_parser_get_object (parser); + end = get_ticks (); + + seconds = end - start; + printf ("ucl: parsed input in %.4f seconds\n", seconds); + if (ucl_parser_get_error(parser)) { + printf ("Error occurred: %s\n", ucl_parser_get_error(parser)); + ret = 1; + goto err; + } + + start = get_ticks (); + emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); + end = get_ticks (); + + seconds = end - start; + printf ("ucl: emitted config in %.4f seconds\n", seconds); + + free (emitted); + + start = get_ticks (); + emitted = ucl_object_emit (obj, UCL_EMIT_JSON); + end = get_ticks (); + + seconds = end - start; + printf ("ucl: emitted json in %.4f seconds\n", seconds); + + free (emitted); + + start = get_ticks (); + emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); + end = get_ticks (); + + seconds = end - start; + printf ("ucl: emitted compact json in %.4f seconds\n", seconds); + + free (emitted); + + start = get_ticks (); + emitted = ucl_object_emit (obj, UCL_EMIT_YAML); + end = get_ticks (); + + seconds = end - start; + printf ("ucl: emitted yaml in %.4f seconds\n", seconds); + + free (emitted); + + ucl_parser_free (parser); + ucl_object_unref (obj); + +err: + munmap (map, st.st_size); + + return ret; +} |