#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef READLINE
#include <readline/history.h>
#else
#include <readline/readline.h>
#endif
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);
}
int
test_movement_direction(void)
{
msg(__func__);
add_history("111");
add_history("222");
while (previous_history() != NULL);
check_current("Oldest entry", "111");
while (next_history() != NULL);
check_current(NULL, NULL);
return msg(NULL);
}
int
test_where(void)
{
int ret;
msg(__func__);
add_history("111");
add_history("222");
add_history("333");
add_history("444");
history_set_pos(1);
if ((ret = where_history()) != 1)
msg("Where returned %d instead of 1.", ret);
return msg(NULL);
}
int
test_get(void)
{
msg(__func__);
add_history("111");
add_history("222");
add_history("333");
add_history("444");
check_get(1, "222");
return msg(NULL);
}
int
test_set_pos_return_values(void)
{
int ret;
msg(__func__);
add_history("111");
add_history("222");
if ((ret = history_set_pos(-1)) != 0)
msg("Set_pos(-1) returned %d instead of 0.", ret);
if ((ret = history_set_pos(1)) != 1)
msg("Set_pos(1) returned %d instead of 1.", ret);
return msg(NULL);
}
int
test_set_pos_index(void)
{
msg(__func__);
add_history("111");
add_history("222");
history_set_pos(0);
check_current("Entry 0", "111");
history_set_pos(1);
check_current("Entry 1", "222");
return msg(NULL);
}
int
test_remove(void)
{
msg(__func__);
add_history("111");
add_history("222");
add_history("333");
add_history("444");
remove_history(1);
check_get(1, "333");
history_set_pos(1);
check_current("Entry 1", "333");
remove_history(1);
check_get(1, "444");
return msg(NULL);
}
int
test_stifle_size(void)
{
msg(__func__);
add_history("111");
add_history("222");
add_history("333");
stifle_history(2);
if (history_length != 2)
msg("Length is %d instead of 2.", history_length);
return msg(NULL);
}
int
test_stifle_base(void)
{
msg(__func__);
stifle_history(2);
add_history("111");
add_history("222");
add_history("333");
if (history_base != 2)
msg("Base is %d instead of 2.", history_base);
return msg(NULL);
}
int
main(void)
{
int fail = 0;
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;
}