diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-06-01 21:29:39 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-06-01 21:29:39 +0000 |
commit | 0ac50c3165ee3f7084fe7fca8196aa439b56d4ab (patch) | |
tree | 6b23e3cd8e438e06f5476549cbed922ddf106b56 /regress/lib | |
parent | 0938d3bdca39acadf3e951275e680be03c644a8e (diff) |
Add detailed error messages and restructure to make the individual
test functions shorter and easier to understand. The total number
of lines remains unchanged.
Diffstat (limited to 'regress/lib')
-rw-r--r-- | regress/lib/libedit/readline/history.c | 230 |
1 files changed, 118 insertions, 112 deletions
diff --git a/regress/lib/libedit/readline/history.c b/regress/lib/libedit/readline/history.c index 8afbe5a022f..d1c677505c0 100644 --- a/regress/lib/libedit/readline/history.c +++ b/regress/lib/libedit/readline/history.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2016 Bastian Maerkisch <bmaerkisch@web.de> + * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> * * Permission to use, copy, modify, and distribute these tests for any * purpose with or without fee is hereby granted, provided that the above @@ -14,6 +15,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <err.h> +#include <stdarg.h> +#include <stdio.h> #include <string.h> #ifdef READLINE @@ -24,34 +27,98 @@ #endif +/* + * Test infrastructure function. + * At the beginning of each test, call as "msg(__func__);". + * Upon failure, call as "msg(fmt, ...);". + * At the end of each test, call as "return msg(NULL);". + */ +int +msg(const char *fmt, ...) +{ + static const char *testname = NULL; + static int failed = 0; + va_list ap; + + if (testname == NULL) { + using_history(); + unstifle_history(); + testname = fmt; + return 0; + } + + if (fmt == NULL) { + clear_history(); + unstifle_history(); + testname = NULL; + if (failed == 0) + return 0; + failed = 0; + return 1; + } + + fprintf(stderr, "%s: ", testname); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputc('\n', stderr); + failed = 1; + return 1; +} + +void +check_current(const char *descr, const char *want) +{ + HIST_ENTRY *he; + + he = current_history(); + + if (want == NULL) { + if (he != NULL) + msg("Failed to move beyond the newest entry."); + return; + } + + if (he == NULL) + msg("%s is NULL.", descr); + else if (strcmp(he->line, want) != 0) + msg("%s is \"%s\" instead of \"%s\".", descr, he->line, want); +} + +void +check_get(int idx, const char *want) +{ + HIST_ENTRY *he; + + if ((he = history_get(history_base + idx)) == NULL) + msg("Get %d+%d returned NULL.", history_base, idx); + else if (he->line == NULL) + msg("Get %d+%d returned line == NULL.", history_base, idx); + else if (strcmp(he->line, want) != 0) + msg("Get %d+%d returned \"%s\" instead of \"%s\".", + history_base, idx, he->line, want); +} + + /* Fails if previous and next are interchanged. */ int test_movement_direction(void) { - HIST_ENTRY *he; - int ok = 1; - - using_history(); + msg(__func__); add_history("111"); add_history("222"); - /* Move to the oldest entry. */ while (previous_history() != NULL); - he = current_history(); - if (he == NULL || strcmp(he->line, "111") != 0) - ok = 0; + check_current("Oldest entry", "111"); /* * Move to the most recent end of the history. * This moves past the newest entry. */ while (next_history() != NULL); - he = current_history(); - if (he != NULL) - ok = 0; + check_current(NULL, NULL); - clear_history(); - return ok; + return msg(NULL); } @@ -59,9 +126,9 @@ test_movement_direction(void) int test_where(void) { - int ok = 1; + int ret; - using_history(); + msg(__func__); /* * Adding four elements since set_pos(0) doesn't work @@ -74,11 +141,10 @@ test_where(void) /* Set the pointer to the element "222". */ history_set_pos(1); - if (where_history() != 1) - ok = 0; + if ((ret = where_history()) != 1) + msg("Where returned %d instead of 1.", ret); - clear_history(); - return ok; + return msg(NULL); } @@ -89,22 +155,16 @@ test_where(void) int test_get(void) { - HIST_ENTRY *he; - int ok = 1; - - using_history(); + msg(__func__); add_history("111"); add_history("222"); add_history("333"); add_history("444"); /* Try to retrieve second element. */ - he = history_get(history_base + 1); - if (he == NULL || he->line == NULL || strcmp(he->line, "222") != 0) - ok = 0; + check_get(1, "222"); - clear_history(); - return ok; + return msg(NULL); } @@ -112,29 +172,25 @@ test_get(void) int test_set_pos_return_values(void) { - int ok = 1; int ret; - using_history(); + msg(__func__); add_history("111"); add_history("222"); /* This should fail. */ - ret = history_set_pos(-1); - if (ret != 0) - ok = 0; + if ((ret = history_set_pos(-1)) != 0) + msg("Set_pos(-1) returned %d instead of 0.", ret); /* * This should succeed. * Note that we do not use the index 0 here, since that * actually fails for some versions of libedit. */ - ret = history_set_pos(1); - if (ret != 1) - ok = 0; + if ((ret = history_set_pos(1)) != 1) + msg("Set_pos(1) returned %d instead of 1.", ret); - clear_history(); - return ok; + return msg(NULL); } @@ -142,26 +198,18 @@ test_set_pos_return_values(void) int test_set_pos_index(void) { - HIST_ENTRY *he; - int ok = 1; - - using_history(); + msg(__func__); add_history("111"); add_history("222"); /* Do not test return value here since that might be broken, too. */ history_set_pos(0); - he = current_history(); - if (he == NULL || strcmp(he->line, "111") != 0) - ok = 0; + check_current("Entry 0", "111"); history_set_pos(1); - he = current_history(); - if (he == NULL || strcmp(he->line, "222") != 0) - ok = 0; + check_current("Entry 1", "222"); - clear_history(); - return ok; + return msg(NULL); } @@ -169,10 +217,7 @@ test_set_pos_index(void) int test_remove(void) { - HIST_ENTRY *he; - int ok = 1; - - using_history(); + msg(__func__); add_history("111"); add_history("222"); add_history("333"); @@ -185,27 +230,20 @@ test_remove(void) * Try to get the new second element using history_get. * The argument of get is based on history_base. */ - he = history_get(history_base + 1); - if (he == NULL || strcmp(he->line, "333") != 0) - ok = 0; + check_get(1, "333"); /* * Try to get the second element using set_pos/current. * The index is zero-based. */ history_set_pos(1); - he = current_history(); - if (he == NULL || strcmp(he->line, "333") != 0) - ok = 0; + check_current("Entry 1", "333"); /* Remove the new second item "333". */ remove_history(1); - he = history_get(history_base + 1); - if (he == NULL || strcmp(he->line, "444") != 0) - ok = 0; + check_get(1, "444"); - clear_history(); - return ok; + return msg(NULL); } @@ -213,10 +251,7 @@ test_remove(void) int test_stifle_size(void) { - int ok = 1; - - using_history(); - unstifle_history(); + msg(__func__); add_history("111"); add_history("222"); add_history("333"); @@ -224,11 +259,9 @@ test_stifle_size(void) /* Reduce the size of the history. */ stifle_history(2); if (history_length != 2) - ok = 0; + msg("Length is %d instead of 2.", history_length); - unstifle_history(); - clear_history(); - return ok; + return msg(NULL); } @@ -236,9 +269,7 @@ test_stifle_size(void) int test_stifle_base(void) { - int ok = 1; - - using_history(); + msg(__func__); stifle_history(2); /* Add one more than the maximum size. */ @@ -248,11 +279,9 @@ test_stifle_base(void) /* The history_base should have changed. */ if (history_base != 2) - ok = 0; + msg("Base is %d instead of 2.", history_base); - unstifle_history(); - clear_history(); - return ok; + return msg(NULL); } @@ -261,38 +290,15 @@ main(void) { int fail = 0; - if (!test_movement_direction()) { - warnx("previous or next move to the wrong entry."); - fail++; - } - if (!test_where()) { - warnx("where returns the wrong history number."); - fail++; - } - if (!test_get()) { - warnx("retrieving elements with history_get failed."); - fail++; - } - if (!test_set_pos_return_values()) { - warnx("set_pos returns the wrong history number."); - fail++; - } - if (!test_set_pos_index()) { - warnx("set_pos moves to the wrong entry."); - fail++; - } - if (!test_remove()) { - warnx("remove corrupts history numbers."); - fail++; - } - if (!test_stifle_size()) { - warnx("stifle sets the wrong history size."); - fail++; - } - if (!test_stifle_base()) { - warnx("add to a stifled history sets the wrong history_base."); - fail++; - } + fail += test_movement_direction(); + fail += test_where(); + fail += test_get(); + fail += test_set_pos_return_values(); + fail += test_set_pos_index(); + fail += test_remove(); + fail += test_stifle_size(); + fail += test_stifle_base(); + if (fail) errx(1, "%d test(s) failed.", fail); return 0; |