1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Build various unit-test main() programs in utils.

I found these while going through the code, and decided if we're going
to have them then we should compile them. They didn't all compile
first time, proving my point :-)

I've enhanced the tree234 test so that it has a verbose option, which
by default is off.
This commit is contained in:
Simon Tatham 2021-04-17 16:39:31 +01:00
parent 395c228bee
commit 1c61fdf800
6 changed files with 98 additions and 45 deletions

View File

@ -145,6 +145,21 @@ add_executable(testcrypt
target_link_libraries(testcrypt
keygen crypto utils ${platform_libraries})
add_executable(test_host_strfoo
utils/host_strchr_internal.c)
target_compile_definitions(test_host_strfoo PRIVATE TEST)
target_link_libraries(test_host_strfoo utils ${platform_libraries})
add_executable(test_tree234
utils/tree234.c)
target_compile_definitions(test_tree234 PRIVATE TEST)
target_link_libraries(test_tree234 utils ${platform_libraries})
add_executable(test_wildcard
utils/wildcard.c)
target_compile_definitions(test_wildcard PRIVATE TEST)
target_link_libraries(test_wildcard utils ${platform_libraries})
add_compile_definitions(HAVE_CMAKE_H)
foreach(subdir ${PLATFORM_SUBDIRS})

View File

@ -53,7 +53,7 @@ int main(void)
#define TEST1(func, string, arg2, suffix, result) do \
{ \
const char *str = string; \
unsigned ret = func(string, arg2) suffix; \
unsigned ret = func(str, arg2) suffix; \
if (ret == result) { \
passes++; \
} else { \

View File

@ -31,17 +31,16 @@
#include "defs.h"
#include "tree234.h"
#include "puttymem.h"
#ifdef TEST
#define LOG(x) (printf x)
#define snew(type) ((type *)malloc(sizeof(type)))
#define snewn(n, type) ((type *)malloc((n) * sizeof(type)))
#define sresize(ptr, n, type) \
((type *)realloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr), \
(n) * sizeof(type)))
#define sfree(ptr) free(ptr)
static int verbose = 0;
#define LOG(x) do \
{ \
if (verbose > 2) \
printf x; \
} while (0)
#else
#include "puttymem.h"
#define LOG(x)
#endif
@ -1151,7 +1150,7 @@ int chknode(chkctx * ctx, int level, node234 * node,
* nelems should be at least 1.
*/
if (nelems == 0) {
error("node %p: no elems", node, nkids);
error("node %p: no elems", node);
}
/*
@ -1173,7 +1172,7 @@ int chknode(chkctx * ctx, int level, node234 * node,
(i + 1 == nelems ? highbound : node->elems[i + 1]);
if (lower && higher && cmp(lower, higher) >= 0) {
error("node %p: kid comparison [%d=%s,%d=%s] failed",
node, i, lower, i + 1, higher);
node, i, (char *)lower, i + 1, (char *)higher);
}
}
}
@ -1223,8 +1222,9 @@ void verify(void)
if (tree->root) {
if (tree->root->parent != NULL)
error("root->parent is %p should be null", tree->root->parent);
chknode(&ctx, 0, tree->root, NULL, NULL);
chknode(ctx, 0, tree->root, NULL, NULL);
}
if (verbose)
printf("tree depth: %d\n", ctx->treedepth);
/*
* Enumerate the tree and ensure it matches up to the array.
@ -1234,7 +1234,7 @@ void verify(void)
error("tree contains more than %d elements", arraylen);
if (array[i] != p)
error("enum at position %d: array says %s, tree says %s",
i, array[i], p);
i, (char *)array[i], (char *)p);
}
if (ctx->elemcount != i) {
error("tree really contains %d elements, enum gave %d",
@ -1379,7 +1379,7 @@ char *strings[] = {
#define NSTR lenof(strings)
int findtest(void)
void findtest(void)
{
const static int rels[] = {
REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT
@ -1444,17 +1444,16 @@ int findtest(void)
p, relnames[j], realret, index, index, realret2);
}
}
#if 0
if (verbose)
printf("find(\"%s\",%s) gave %s(%d)\n", p, relnames[j],
realret, index);
#endif
}
}
realret = findrelpos234(tree, NULL, NULL, REL234_GT, &index);
if (arraylen && (realret != array[0] || index != 0)) {
error("find(NULL,GT) gave %s(%d) should be %s(0)",
realret, index, array[0]);
realret, index, (char *)array[0]);
} else if (!arraylen && (realret != NULL)) {
error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);
}
@ -1463,7 +1462,7 @@ int findtest(void)
if (arraylen
&& (realret != array[arraylen - 1] || index != arraylen - 1)) {
error("find(NULL,LT) gave %s(%d) should be %s(0)", realret, index,
array[arraylen - 1]);
(char *)array[arraylen - 1]);
} else if (!arraylen && (realret != NULL)) {
error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);
}
@ -1483,6 +1482,7 @@ void searchtest_recurse(search234_state ss, int lo, int hi,
error("search234(%s) gave index %d should be %d",
directionbuf, ss.index, lo);
} else {
if (verbose)
printf("%*ssearch234(%s) gave NULL,%d\n",
(int)(directionptr-directionbuf) * 2, "", directionbuf,
ss.index);
@ -1500,6 +1500,7 @@ void searchtest_recurse(search234_state ss, int lo, int hi,
} else {
search234_state next;
if (verbose)
printf("%*ssearch234(%s) gave %s,%d\n",
(int)(directionptr-directionbuf) * 2, "", directionbuf,
(char *)ss.element, ss.index);
@ -1525,23 +1526,42 @@ void searchtest(void)
int n;
search234_state ss;
if (verbose)
printf("beginning searchtest:");
for (n = 0; (p = index234(tree, n)) != NULL; n++) {
expected[n] = p;
if (verbose)
printf(" %d=%s", n, p);
}
if (verbose)
printf(" count=%d\n", n);
search234_start(&ss, tree);
searchtest_recurse(ss, 0, n, expected, directionbuf, directionbuf);
}
int main(void)
void out_of_memory(void)
{
fprintf(stderr, "out of memory!\n");
exit(2);
}
int main(int argc, char **argv)
{
int in[NSTR];
int i, j, k;
unsigned seed = 0;
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (!strcmp(arg, "-v")) {
verbose++;
} else {
fprintf(stderr, "unrecognised option '%s'\n", arg);
return 1;
}
}
for (i = 0; i < NSTR; i++)
in[i] = 0;
array = NULL;
@ -1554,12 +1574,15 @@ int main(void)
for (i = 0; i < 10000; i++) {
j = randomnumber(&seed);
j %= NSTR;
if (verbose)
printf("trial: %d\n", i);
if (in[j]) {
if (verbose)
printf("deleting %s (%d)\n", strings[j], j);
deltest(strings[j]);
in[j] = 0;
} else {
if (verbose)
printf("adding %s (%d)\n", strings[j], j);
addtest(strings[j]);
in[j] = 1;
@ -1587,18 +1610,22 @@ int main(void)
cmp = NULL;
verify();
for (i = 0; i < 1000; i++) {
if (verbose)
printf("trial: %d\n", i);
j = randomnumber(&seed);
j %= NSTR;
k = randomnumber(&seed);
k %= count234(tree) + 1;
if (verbose)
printf("adding string %s at index %d\n", strings[j], k);
addpostest(strings[j], k);
}
while (count234(tree) > 0) {
if (verbose)
printf("cleanup: tree size %d\n", count234(tree));
j = randomnumber(&seed);
j %= count234(tree);
if (verbose)
printf("deleting string %s from index %d\n",
(const char *)array[j], j);
delpostest(j);
@ -1608,4 +1635,4 @@ int main(void)
return (n_errors != 0);
}
#endif
#endif /* TEST */

View File

@ -344,7 +344,7 @@ bool wc_unescape(char *output, const char *wildcard)
return true; /* it's clean */
}
#ifdef TESTMODE
#ifdef TEST
struct test {
const char *wildcard;
@ -483,4 +483,4 @@ int main(void)
return 0;
}
#endif
#endif /* TEST */

View File

@ -167,3 +167,8 @@ set_target_properties(puttygen PROPERTIES
WIN32_EXECUTABLE ON
LINK_FLAGS "${LFLAG_MANIFEST_NO}")
installed_program(puttygen)
add_executable(test_split_into_argv
utils/split_into_argv.c)
target_compile_definitions(test_split_into_argv PRIVATE TEST)
target_link_libraries(test_split_into_argv utils ${platform_libraries})

View File

@ -221,7 +221,7 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
if (argstart) *argstart = outputargstart; else sfree(outputargstart);
}
#ifdef TESTMODE
#ifdef TEST
const struct argv_test {
const char *cmdline;
@ -323,6 +323,12 @@ const struct argv_test {
{"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL}},
};
void out_of_memory(void)
{
fprintf(stderr, "out of memory!\n");
exit(2);
}
int main(int argc, char **argv)
{
int i, j;
@ -429,7 +435,7 @@ int main(int argc, char **argv)
int ac;
char **av;
split_into_argv(argv_tests[i].cmdline, &ac, &av);
split_into_argv((char *)argv_tests[i].cmdline, &ac, &av, NULL);
for (j = 0; j < ac && argv_tests[i].argv[j]; j++) {
if (strcmp(av[j], argv_tests[i].argv[j])) {
@ -456,4 +462,4 @@ int main(int argc, char **argv)
return 0;
}
#endif
#endif /* TEST */