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:
parent
395c228bee
commit
1c61fdf800
@ -145,6 +145,21 @@ add_executable(testcrypt
|
|||||||
target_link_libraries(testcrypt
|
target_link_libraries(testcrypt
|
||||||
keygen crypto utils ${platform_libraries})
|
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)
|
add_compile_definitions(HAVE_CMAKE_H)
|
||||||
|
|
||||||
foreach(subdir ${PLATFORM_SUBDIRS})
|
foreach(subdir ${PLATFORM_SUBDIRS})
|
||||||
|
@ -53,7 +53,7 @@ int main(void)
|
|||||||
#define TEST1(func, string, arg2, suffix, result) do \
|
#define TEST1(func, string, arg2, suffix, result) do \
|
||||||
{ \
|
{ \
|
||||||
const char *str = string; \
|
const char *str = string; \
|
||||||
unsigned ret = func(string, arg2) suffix; \
|
unsigned ret = func(str, arg2) suffix; \
|
||||||
if (ret == result) { \
|
if (ret == result) { \
|
||||||
passes++; \
|
passes++; \
|
||||||
} else { \
|
} else { \
|
||||||
|
105
utils/tree234.c
105
utils/tree234.c
@ -31,17 +31,16 @@
|
|||||||
|
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
#include "tree234.h"
|
#include "tree234.h"
|
||||||
|
#include "puttymem.h"
|
||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
#define LOG(x) (printf x)
|
static int verbose = 0;
|
||||||
#define snew(type) ((type *)malloc(sizeof(type)))
|
#define LOG(x) do \
|
||||||
#define snewn(n, type) ((type *)malloc((n) * sizeof(type)))
|
{ \
|
||||||
#define sresize(ptr, n, type) \
|
if (verbose > 2) \
|
||||||
((type *)realloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr), \
|
printf x; \
|
||||||
(n) * sizeof(type)))
|
} while (0)
|
||||||
#define sfree(ptr) free(ptr)
|
|
||||||
#else
|
#else
|
||||||
#include "puttymem.h"
|
|
||||||
#define LOG(x)
|
#define LOG(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1151,7 +1150,7 @@ int chknode(chkctx * ctx, int level, node234 * node,
|
|||||||
* nelems should be at least 1.
|
* nelems should be at least 1.
|
||||||
*/
|
*/
|
||||||
if (nelems == 0) {
|
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]);
|
(i + 1 == nelems ? highbound : node->elems[i + 1]);
|
||||||
if (lower && higher && cmp(lower, higher) >= 0) {
|
if (lower && higher && cmp(lower, higher) >= 0) {
|
||||||
error("node %p: kid comparison [%d=%s,%d=%s] failed",
|
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,9 +1222,10 @@ void verify(void)
|
|||||||
if (tree->root) {
|
if (tree->root) {
|
||||||
if (tree->root->parent != NULL)
|
if (tree->root->parent != NULL)
|
||||||
error("root->parent is %p should be null", tree->root->parent);
|
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);
|
||||||
}
|
}
|
||||||
printf("tree depth: %d\n", ctx->treedepth);
|
if (verbose)
|
||||||
|
printf("tree depth: %d\n", ctx->treedepth);
|
||||||
/*
|
/*
|
||||||
* Enumerate the tree and ensure it matches up to the array.
|
* 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);
|
error("tree contains more than %d elements", arraylen);
|
||||||
if (array[i] != p)
|
if (array[i] != p)
|
||||||
error("enum at position %d: array says %s, tree says %s",
|
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) {
|
if (ctx->elemcount != i) {
|
||||||
error("tree really contains %d elements, enum gave %d",
|
error("tree really contains %d elements, enum gave %d",
|
||||||
@ -1379,7 +1379,7 @@ char *strings[] = {
|
|||||||
|
|
||||||
#define NSTR lenof(strings)
|
#define NSTR lenof(strings)
|
||||||
|
|
||||||
int findtest(void)
|
void findtest(void)
|
||||||
{
|
{
|
||||||
const static int rels[] = {
|
const static int rels[] = {
|
||||||
REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT
|
REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT
|
||||||
@ -1444,17 +1444,16 @@ int findtest(void)
|
|||||||
p, relnames[j], realret, index, index, realret2);
|
p, relnames[j], realret, index, index, realret2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if 0
|
if (verbose)
|
||||||
printf("find(\"%s\",%s) gave %s(%d)\n", p, relnames[j],
|
printf("find(\"%s\",%s) gave %s(%d)\n", p, relnames[j],
|
||||||
realret, index);
|
realret, index);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
realret = findrelpos234(tree, NULL, NULL, REL234_GT, &index);
|
realret = findrelpos234(tree, NULL, NULL, REL234_GT, &index);
|
||||||
if (arraylen && (realret != array[0] || index != 0)) {
|
if (arraylen && (realret != array[0] || index != 0)) {
|
||||||
error("find(NULL,GT) gave %s(%d) should be %s(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)) {
|
} else if (!arraylen && (realret != NULL)) {
|
||||||
error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);
|
error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);
|
||||||
}
|
}
|
||||||
@ -1463,7 +1462,7 @@ int findtest(void)
|
|||||||
if (arraylen
|
if (arraylen
|
||||||
&& (realret != array[arraylen - 1] || index != arraylen - 1)) {
|
&& (realret != array[arraylen - 1] || index != arraylen - 1)) {
|
||||||
error("find(NULL,LT) gave %s(%d) should be %s(0)", realret, index,
|
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)) {
|
} else if (!arraylen && (realret != NULL)) {
|
||||||
error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);
|
error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);
|
||||||
}
|
}
|
||||||
@ -1483,9 +1482,10 @@ void searchtest_recurse(search234_state ss, int lo, int hi,
|
|||||||
error("search234(%s) gave index %d should be %d",
|
error("search234(%s) gave index %d should be %d",
|
||||||
directionbuf, ss.index, lo);
|
directionbuf, ss.index, lo);
|
||||||
} else {
|
} else {
|
||||||
printf("%*ssearch234(%s) gave NULL,%d\n",
|
if (verbose)
|
||||||
(int)(directionptr-directionbuf) * 2, "", directionbuf,
|
printf("%*ssearch234(%s) gave NULL,%d\n",
|
||||||
ss.index);
|
(int)(directionptr-directionbuf) * 2, "", directionbuf,
|
||||||
|
ss.index);
|
||||||
}
|
}
|
||||||
} else if (lo == hi) {
|
} else if (lo == hi) {
|
||||||
error("search234(%s) gave %s for empty interval [%d,%d)",
|
error("search234(%s) gave %s for empty interval [%d,%d)",
|
||||||
@ -1500,9 +1500,10 @@ void searchtest_recurse(search234_state ss, int lo, int hi,
|
|||||||
} else {
|
} else {
|
||||||
search234_state next;
|
search234_state next;
|
||||||
|
|
||||||
printf("%*ssearch234(%s) gave %s,%d\n",
|
if (verbose)
|
||||||
(int)(directionptr-directionbuf) * 2, "", directionbuf,
|
printf("%*ssearch234(%s) gave %s,%d\n",
|
||||||
(char *)ss.element, ss.index);
|
(int)(directionptr-directionbuf) * 2, "", directionbuf,
|
||||||
|
(char *)ss.element, ss.index);
|
||||||
|
|
||||||
next = ss;
|
next = ss;
|
||||||
search234_step(&next, -1);
|
search234_step(&next, -1);
|
||||||
@ -1525,23 +1526,42 @@ void searchtest(void)
|
|||||||
int n;
|
int n;
|
||||||
search234_state ss;
|
search234_state ss;
|
||||||
|
|
||||||
printf("beginning searchtest:");
|
if (verbose)
|
||||||
|
printf("beginning searchtest:");
|
||||||
for (n = 0; (p = index234(tree, n)) != NULL; n++) {
|
for (n = 0; (p = index234(tree, n)) != NULL; n++) {
|
||||||
expected[n] = p;
|
expected[n] = p;
|
||||||
printf(" %d=%s", n, p);
|
if (verbose)
|
||||||
|
printf(" %d=%s", n, p);
|
||||||
}
|
}
|
||||||
printf(" count=%d\n", n);
|
if (verbose)
|
||||||
|
printf(" count=%d\n", n);
|
||||||
|
|
||||||
search234_start(&ss, tree);
|
search234_start(&ss, tree);
|
||||||
searchtest_recurse(ss, 0, n, expected, directionbuf, directionbuf);
|
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 in[NSTR];
|
||||||
int i, j, k;
|
int i, j, k;
|
||||||
unsigned seed = 0;
|
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++)
|
for (i = 0; i < NSTR; i++)
|
||||||
in[i] = 0;
|
in[i] = 0;
|
||||||
array = NULL;
|
array = NULL;
|
||||||
@ -1554,13 +1574,16 @@ int main(void)
|
|||||||
for (i = 0; i < 10000; i++) {
|
for (i = 0; i < 10000; i++) {
|
||||||
j = randomnumber(&seed);
|
j = randomnumber(&seed);
|
||||||
j %= NSTR;
|
j %= NSTR;
|
||||||
printf("trial: %d\n", i);
|
if (verbose)
|
||||||
|
printf("trial: %d\n", i);
|
||||||
if (in[j]) {
|
if (in[j]) {
|
||||||
printf("deleting %s (%d)\n", strings[j], j);
|
if (verbose)
|
||||||
|
printf("deleting %s (%d)\n", strings[j], j);
|
||||||
deltest(strings[j]);
|
deltest(strings[j]);
|
||||||
in[j] = 0;
|
in[j] = 0;
|
||||||
} else {
|
} else {
|
||||||
printf("adding %s (%d)\n", strings[j], j);
|
if (verbose)
|
||||||
|
printf("adding %s (%d)\n", strings[j], j);
|
||||||
addtest(strings[j]);
|
addtest(strings[j]);
|
||||||
in[j] = 1;
|
in[j] = 1;
|
||||||
}
|
}
|
||||||
@ -1587,20 +1610,24 @@ int main(void)
|
|||||||
cmp = NULL;
|
cmp = NULL;
|
||||||
verify();
|
verify();
|
||||||
for (i = 0; i < 1000; i++) {
|
for (i = 0; i < 1000; i++) {
|
||||||
printf("trial: %d\n", i);
|
if (verbose)
|
||||||
|
printf("trial: %d\n", i);
|
||||||
j = randomnumber(&seed);
|
j = randomnumber(&seed);
|
||||||
j %= NSTR;
|
j %= NSTR;
|
||||||
k = randomnumber(&seed);
|
k = randomnumber(&seed);
|
||||||
k %= count234(tree) + 1;
|
k %= count234(tree) + 1;
|
||||||
printf("adding string %s at index %d\n", strings[j], k);
|
if (verbose)
|
||||||
|
printf("adding string %s at index %d\n", strings[j], k);
|
||||||
addpostest(strings[j], k);
|
addpostest(strings[j], k);
|
||||||
}
|
}
|
||||||
while (count234(tree) > 0) {
|
while (count234(tree) > 0) {
|
||||||
printf("cleanup: tree size %d\n", count234(tree));
|
if (verbose)
|
||||||
|
printf("cleanup: tree size %d\n", count234(tree));
|
||||||
j = randomnumber(&seed);
|
j = randomnumber(&seed);
|
||||||
j %= count234(tree);
|
j %= count234(tree);
|
||||||
printf("deleting string %s from index %d\n",
|
if (verbose)
|
||||||
(const char *)array[j], j);
|
printf("deleting string %s from index %d\n",
|
||||||
|
(const char *)array[j], j);
|
||||||
delpostest(j);
|
delpostest(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1608,4 +1635,4 @@ int main(void)
|
|||||||
return (n_errors != 0);
|
return (n_errors != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif /* TEST */
|
||||||
|
@ -344,7 +344,7 @@ bool wc_unescape(char *output, const char *wildcard)
|
|||||||
return true; /* it's clean */
|
return true; /* it's clean */
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TESTMODE
|
#ifdef TEST
|
||||||
|
|
||||||
struct test {
|
struct test {
|
||||||
const char *wildcard;
|
const char *wildcard;
|
||||||
@ -483,4 +483,4 @@ int main(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif /* TEST */
|
||||||
|
@ -167,3 +167,8 @@ set_target_properties(puttygen PROPERTIES
|
|||||||
WIN32_EXECUTABLE ON
|
WIN32_EXECUTABLE ON
|
||||||
LINK_FLAGS "${LFLAG_MANIFEST_NO}")
|
LINK_FLAGS "${LFLAG_MANIFEST_NO}")
|
||||||
installed_program(puttygen)
|
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})
|
||||||
|
@ -221,7 +221,7 @@ void split_into_argv(char *cmdline, int *argc, char ***argv,
|
|||||||
if (argstart) *argstart = outputargstart; else sfree(outputargstart);
|
if (argstart) *argstart = outputargstart; else sfree(outputargstart);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TESTMODE
|
#ifdef TEST
|
||||||
|
|
||||||
const struct argv_test {
|
const struct argv_test {
|
||||||
const char *cmdline;
|
const char *cmdline;
|
||||||
@ -323,6 +323,12 @@ const struct argv_test {
|
|||||||
{"\"a\\\\\\\\\"\"\"\"\"\"\"\"b c\" d", {"a\\\\\"\"\"b", "c d", NULL}},
|
{"\"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 main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
@ -429,7 +435,7 @@ int main(int argc, char **argv)
|
|||||||
int ac;
|
int ac;
|
||||||
char **av;
|
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++) {
|
for (j = 0; j < ac && argv_tests[i].argv[j]; j++) {
|
||||||
if (strcmp(av[j], argv_tests[i].argv[j])) {
|
if (strcmp(av[j], argv_tests[i].argv[j])) {
|
||||||
@ -456,4 +462,4 @@ int main(int argc, char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif /* TEST */
|
||||||
|
Loading…
Reference in New Issue
Block a user