chiark / gitweb /
Commit 2.4.5-5 as unpacked
[innduct.git] / tests / libtest.c
1 /*  $Id: libtest.c 5955 2002-12-08 09:28:32Z rra $
2 **
3 **  Some utility routines for writing tests.
4 **
5 **  Herein are a variety of utility routines for writing tests.  All
6 **  routines of the form ok*() take a test number and some number of
7 **  appropriate arguments, check to be sure the results match the expected
8 **  output using the arguments, and print out something appropriate for that
9 **  test number.  Other utility routines help in constructing more complex
10 **  tests.
11 */
12
13 #include "config.h"
14 #include "clibrary.h"
15
16 #include "inn/messages.h"
17 #include "libinn.h"
18 #include "libtest.h"
19
20 /* A global buffer into which message_log_buffer stores error messages. */
21 char *errors = NULL;
22
23
24 /*
25 **  Takes a boolean success value and assumes the test passes if that value
26 **  is true and fails if that value is false.
27 */
28 void
29 ok(int n, int success)
30 {
31     printf("%sok %d\n", success ? "" : "not ", n);
32 }
33
34
35 /*
36 **  Takes an expected integer and a seen integer and assumes the test passes
37 **  if those two numbers match.
38 */
39 void
40 ok_int(int n, int wanted, int seen)
41 {
42     if (wanted == seen)
43         printf("ok %d\n", n);
44     else
45         printf("not ok %d\n  wanted: %d\n    seen: %d\n", n, wanted, seen);
46 }
47
48
49 /*
50 **  Takes a string and what the string should be, and assumes the test
51 **  passes if those strings match (using strcmp).
52 */
53 void
54 ok_string(int n, const char *wanted, const char *seen)
55 {
56     if (wanted == NULL)
57         wanted = "(null)";
58     if (seen == NULL)
59         seen = "(null)";
60     if (strcmp(wanted, seen) != 0)
61         printf("not ok %d\n  wanted: %s\n    seen: %s\n", n, wanted, seen);
62     else
63         printf("ok %d\n", n);
64 }
65
66
67 /*
68 **  An error handler that appends all errors to the errors global.  Used by
69 **  error_capture.
70 */
71 static void
72 message_log_buffer(int len, const char *fmt, va_list args, int error UNUSED)
73 {
74     char *message;
75
76     message = xmalloc(len + 1);
77     vsnprintf(message, len + 1, fmt, args);
78     if (errors == NULL) {
79         errors = concat(message, "\n", (char *) 0);
80     } else {
81         char *new_errors;
82
83         new_errors = concat(errors, message, "\n", (char *) 0);
84         free(errors);
85         errors = new_errors;
86     }
87     free(message);
88 }
89
90
91 /*
92 **  Turn on the capturing of errors.  Errors will be stored in the global
93 **  errors variable where they can be checked by the test suite.  Capturing is
94 **  turned off with errors_uncapture.
95 */
96 void
97 errors_capture(void)
98 {
99     if (errors != NULL) {
100         free(errors);
101         errors = NULL;
102     }
103     message_handlers_warn(1, message_log_buffer);
104 }
105
106
107 /*
108 **  Turn off the capturing of errors again.
109 */
110 void
111 errors_uncapture(void)
112 {
113     message_handlers_warn(1, message_log_stderr);
114 }