summaryrefslogtreecommitdiff
path: root/tests/test.c
blob: 4686369e486ed2f86d6916bf4cf9ae2332b6d343 (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
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

/* System */
#include <stdarg.h>
#include <stdio.h>

/* Opentracker */
#include "test.h"

unsigned long      test_checks;
unsigned long      test_failures;

static const char *current_module = "";
static const char *current_case   = "";

void               test_module(const char *name) {
  current_module = name;
  current_case   = "";
  printf("%s\n", name);
}

void test_case(const char *name) { current_case = name; }

static void test_where(const char *file, int line) {
  ++test_failures;
  printf("  FAIL %s/%s (%s:%d)\n    ", current_module, current_case, file, line);
}

void test_failf(const char *file, int line, const char *fmt, ...) {
  va_list ap;
  test_where(file, line);
  va_start(ap, fmt);
  vprintf(fmt, ap);
  va_end(ap);
  printf("\n");
}

void test_failmem(const char *file, int line, const char *what, const void *got, const void *want, size_t len) {
  const unsigned char *g = got, *w = want;
  size_t               i;

  test_where(file, line);
  printf("%s: %zu bytes differ\n      got  ", what, len);
  for (i = 0; i < len; ++i)
    printf("%02x", g[i]);
  printf("\n      want ");
  for (i = 0; i < len; ++i)
    printf("%02x", w[i]);
  printf("\n");
}

int test_summary(void) {
  printf("\n%lu checks, %lu failures\n", test_checks, test_failures);
  return test_failures != 0;
}