]> git.donarmstrong.com Git - lilypond.git/blobdiff - guile18/test-suite/tests/c-api/testlib.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / test-suite / tests / c-api / testlib.c
diff --git a/guile18/test-suite/tests/c-api/testlib.c b/guile18/test-suite/tests/c-api/testlib.c
new file mode 100644 (file)
index 0000000..21fff24
--- /dev/null
@@ -0,0 +1,121 @@
+/* testlib.c --- reporting test results
+   Jim Blandy <jimb@red-bean.com> --- August 1999 */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "testlib.h"
+
+
+\f
+/* Dying.  */
+
+static void
+fatal (char *message)
+{
+  fprintf (stderr, "%s\n", message);
+  exit (1);
+}
+
+\f
+/* 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");
+}
+
+\f
+/* 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);
+}
+
+\f
+/* 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);
+}