1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/* $Id: libtest.c 8972 2010-02-08 21:07:38Z iulius $
**
** Some utility routines for writing tests.
**
** Herein are a variety of utility routines for writing tests. All
** routines of the form ok*() take a test number and some number of
** appropriate arguments, check to be sure the results match the expected
** output using the arguments, and print out something appropriate for that
** test number. Other utility routines help in constructing more complex
** tests.
*/
#include "config.h"
#include "clibrary.h"
#include "inn/messages.h"
#include "inn/libinn.h"
#include "libtest.h"
/* A global buffer into which message_log_buffer stores error messages. */
char *errors = NULL;
/*
** Initialize things. Turns on line buffering on stdout and then prints out
** the number of tests in the test suite.
*/
void
test_init(int count)
{
if (setvbuf(stdout, NULL, _IOLBF, BUFSIZ) != 0)
syswarn("cannot set stdout to line buffered");
printf("%d\n", count);
}
/*
** Takes a boolean success value and assumes the test passes if that value
** is true and fails if that value is false.
*/
void
ok(int n, int success)
{
printf("%sok %d\n", success ? "" : "not ", n);
}
/*
** Takes an expected integer and a seen integer and assumes the test passes
** if those two numbers match.
*/
void
ok_int(int n, int wanted, int seen)
{
if (wanted == seen)
printf("ok %d\n", n);
else
printf("not ok %d\n wanted: %d\n seen: %d\n", n, wanted, seen);
}
/*
** Takes a string and what the string should be, and assumes the test
** passes if those strings match (using strcmp).
*/
void
ok_string(int n, const char *wanted, const char *seen)
{
if (wanted == NULL)
wanted = "(null)";
if (seen == NULL)
seen = "(null)";
if (strcmp(wanted, seen) != 0)
printf("not ok %d\n wanted: %s\n seen: %s\n", n, wanted, seen);
else
printf("ok %d\n", n);
}
/*
** Takes an expected double and a seen double and assumes the test passes
** if those two numbers match.
*/
void
ok_double(int n, double wanted, double seen)
{
if (wanted == seen)
printf("ok %d\n", n);
else
printf("not ok %d\n wanted: %g\n seen: %g\n", n, wanted, seen);
}
/*
** Skip a test.
*/
void
skip(int n, const char *reason)
{
printf("ok %d # skip", n);
if (reason != NULL)
printf(" - %s", reason);
putchar('\n');
}
/*
** Report the same status on the next count tests.
*/
void
ok_block(int n, int count, int status)
{
int i;
for (i = 0; i < count; i++)
ok(n++, status);
}
/*
** Skip the next count tests.
*/
void
skip_block(int n, int count, const char *reason)
{
int i;
for (i = 0; i < count; i++)
skip(n++, reason);
}
/*
** An error handler that appends all errors to the errors global. Used by
** error_capture.
*/
static void
message_log_buffer(int len, const char *fmt, va_list args, int error UNUSED)
{
char *message;
message = xmalloc(len + 1);
vsnprintf(message, len + 1, fmt, args);
if (errors == NULL) {
errors = concat(message, "\n", (char *) 0);
} else {
char *new_errors;
new_errors = concat(errors, message, "\n", (char *) 0);
free(errors);
errors = new_errors;
}
free(message);
}
/*
** Turn on the capturing of errors. Errors will be stored in the global
** errors variable where they can be checked by the test suite. Capturing is
** turned off with errors_uncapture.
*/
void
errors_capture(void)
{
if (errors != NULL) {
free(errors);
errors = NULL;
}
message_handlers_warn(1, message_log_buffer);
message_handlers_notice(1, message_log_buffer);
}
/*
** Turn off the capturing of errors again.
*/
void
errors_uncapture(void)
{
message_handlers_warn(1, message_log_stderr);
message_handlers_notice(1, message_log_stdout);
}
|