Index: ossp-pkg/var/var_test.c RCS File: /v/ossp/cvs/ossp-pkg/var/var_test.c,v rcsdiff -q -kk '-r1.26' '-r1.27' -u '/v/ossp/cvs/ossp-pkg/var/var_test.c,v' 2>/dev/null --- var_test.c 2001/12/14 13:47:01 1.26 +++ var_test.c 2001/12/14 13:49:40 1.27 @@ -1,6 +1,36 @@ +/* +** VAR - OSSP variable expression library. +** Copyright (c) 2001 The OSSP Project (http://www.ossp.org/) +** Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/) +** +** This file is part of OSSP VAR, an extensible data serialization +** library which can be found at http://www.ossp.org/pkg/var/. +** +** Permission to use, copy, modify, and distribute this software for +** any purpose with or without fee is hereby granted, provided that +** the above copyright notice and this permission notice appear in all +** copies. +** +** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR +** CONTRIBUTORS 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. +** +** var_test.c: VAR library regression test. +*/ + #include #include #include + #include "var.h" struct variable { @@ -9,39 +39,35 @@ const char *data; }; -static int var_lookup(void *context, +static int var_lookup( + void *context, const char *varname, size_t name_len, int idx, const char **data, size_t *data_len, size_t *buffer_size) { - const struct variable* vars = context; + const struct variable *vars = context; size_t i, counter, length; static char buf[((sizeof(int)*8)/3)+10]; /* sufficient size: <#bits> x log_10(2) + safety */ - if (idx >= 0) - { - for (i = 0; vars[i].name; ++i) - { - if (strncmp(varname, vars[i].name, name_len) == 0 && vars[i].idx == idx) - { + if (idx >= 0) { + for (i = 0; vars[i].name; ++i) { + if (strncmp(varname, vars[i].name, name_len) == 0 && vars[i].idx == idx) { *data = vars[i].data; *data_len = strlen(*data); *buffer_size = 0; return 1; - } } } - else - { - for (i = 0; vars[i].name; ++i) - { - if (strncmp(varname, vars[i].name, name_len) == 0) - { + } + else { + for (i = 0; vars[i].name; ++i) { + if (strncmp(varname, vars[i].name, name_len) == 0) { printf("Found variable at index %d.\n", i); counter = 1; length = strlen(vars[i].data); - while (vars[i + counter].data && strncmp(varname, vars[i + counter].name, name_len) == 0) - ++counter; + while ( vars[i + counter].data + && strncmp(varname, vars[i + counter].name, name_len) == 0) + counter++; if (counter == 1) sprintf(buf, "%d", length); else @@ -50,9 +76,9 @@ *data_len = strlen(buf); *buffer_size = 0; return 1; - } } } + } return 0; } @@ -173,7 +199,7 @@ for (i = 0; i < sizeof(tests) / sizeof(struct test_case); ++i) { #ifdef DEBUG printf("Test case #%02d: Original input is '%s'.\n", i, - tests[i].input); + tests[i].input); #endif rc = var_unescape(tests[i].input, strlen(tests[i].input), buffer, 0); if (rc != VAR_OK) { @@ -194,14 +220,15 @@ #ifdef DEBUG printf("Test case #%02d: Expanded output is '%s'.\n", i, tmp); #endif - if (tmp_len != strlen(tests[i].expected) || tmp == NULL - || memcmp(tests[i].expected, tmp, tmp_len) != 0) { + if ( tmp_len != strlen(tests[i].expected) + || tmp == NULL + || memcmp(tests[i].expected, tmp, tmp_len) != 0) { printf("Test case #%d: Expected result '%s' but got '%s'.\n", i, tests[i].expected, tmp); return 1; } - free(tmp); + free(tmp); } - return 0; } +