]> git.donarmstrong.com Git - lilypond.git/blob - guile18/test-suite/tests/c-api/testlib.c
New upstream version 2.19.65
[lilypond.git] / guile18 / test-suite / tests / c-api / testlib.c
1 /* testlib.c --- reporting test results
2    Jim Blandy <jimb@red-bean.com> --- August 1999 */
3
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include "testlib.h"
8
9
10 \f
11 /* Dying.  */
12
13 static void
14 fatal (char *message)
15 {
16   fprintf (stderr, "%s\n", message);
17   exit (1);
18 }
19
20 \f
21 /* Contexts.  */
22
23 /* If it gets deeper than this, that's probably an error, right?  */
24 #define MAX_NESTING 10
25
26 int depth = 0;
27 char *context_name_stack[MAX_NESTING];
28 int marker;
29 int context_marker_stack[MAX_NESTING];
30
31 test_context_t
32 test_enter_context (char *name)
33 {
34   if (depth >= MAX_NESTING)
35     fatal ("test contexts nested too deeply");
36
37   /* Generate a unique marker value for this context.  */
38   marker++;
39
40   context_name_stack[depth] = name;
41   context_marker_stack[depth] = marker;
42
43   depth++;
44
45   return marker;
46 }
47
48 void
49 test_restore_context (test_context_t context)
50 {
51   if (depth <= 0)
52     fatal ("attempt to leave outermost context");
53
54   depth--;
55
56   /* Make sure that we're exiting the same context we last entered.  */
57   if (context_marker_stack[depth] != context)
58     fatal ("contexts not nested properly");
59 }
60
61 \f
62 /* Reporting results.  */
63
64 int count_passes, count_fails;
65
66 static void
67 print_test_name (char *name)
68 {
69   int i;
70
71   for (i = 0; i < depth; i++)
72     printf ("%s: ", context_name_stack[i]);
73
74   printf ("%s", name);
75 }
76
77 static void
78 print_result (char *result, char *name)
79 {
80   printf ("%s: ", result);
81   print_test_name (name);
82   putchar ('\n');
83 }
84
85 void
86 test_pass (char *name)
87 {
88   print_result ("PASS", name);
89   count_passes++;
90 }
91
92 void
93 test_fail (char *name)
94 {
95   print_result ("FAIL", name);
96   count_fails++;
97 }
98
99 void
100 test_pass_if (char *name, int condition)
101 {
102   (condition ? test_pass : test_fail) (name);
103 }
104
105 \f
106 /* Printing a summary.  */
107
108 /* Print a summary of the reported test results.  Return zero if
109    no failures occurred, one otherwise.  */
110
111 int
112 test_summarize ()
113 {
114   putchar ('\n');
115
116   printf ("passes:      %d\n", count_passes);
117   printf ("failures:    %d\n", count_fails);
118   printf ("total tests: %d\n", count_passes + count_fails);
119
120   return (count_fails != 0);
121 }