summaryrefslogtreecommitdiff
path: root/test-suite/tests/c-api/testlib.c
blob: 21fff249262e0d0f077fff500528889099446b25 (plain)
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
/* testlib.c --- reporting test results
   Jim Blandy <jimb@red-bean.com> --- August 1999 */

#include <stdlib.h>
#include <stdio.h>

#include "testlib.h"



/* Dying.  */

static void
fatal (char *message)
{
  fprintf (stderr, "%s\n", message);
  exit (1);
}


/* Contexts.  */

/* If it gets deeper than this, that's probably an error, right?  */
#define MAX_NESTING 10

int depth = 0;
char *context_name_stack[MAX_NESTING];
int marker;
int context_marker_stack[MAX_NESTING];

test_context_t
test_enter_context (char *name)
{
  if (depth >= MAX_NESTING)
    fatal ("test contexts nested too deeply");

  /* Generate a unique marker value for this context.  */
  marker++;

  context_name_stack[depth] = name;
  context_marker_stack[depth] = marker;

  depth++;

  return marker;
}

void
test_restore_context (test_context_t context)
{
  if (depth <= 0)
    fatal ("attempt to leave outermost context");

  depth--;

  /* Make sure that we're exiting the same context we last entered.  */
  if (context_marker_stack[depth] != context)
    fatal ("contexts not nested properly");
}


/* Reporting results.  */

int count_passes, count_fails;

static void
print_test_name (char *name)
{
  int i;

  for (i = 0; i < depth; i++)
    printf ("%s: ", context_name_stack[i]);

  printf ("%s", name);
}

static void
print_result (char *result, char *name)
{
  printf ("%s: ", result);
  print_test_name (name);
  putchar ('\n');
}

void
test_pass (char *name)
{
  print_result ("PASS", name);
  count_passes++;
}

void
test_fail (char *name)
{
  print_result ("FAIL", name);
  count_fails++;
}

void
test_pass_if (char *name, int condition)
{
  (condition ? test_pass : test_fail) (name);
}


/* Printing a summary.  */

/* Print a summary of the reported test results.  Return zero if
   no failures occurred, one otherwise.  */

int
test_summarize ()
{
  putchar ('\n');

  printf ("passes:      %d\n", count_passes);
  printf ("failures:    %d\n", count_fails);
  printf ("total tests: %d\n", count_passes + count_fails);

  return (count_fails != 0);
}