~akkartik/basic-test

basic-test/test.c -rw-r--r-- 1.1 KiB View raw
3a8f345c — Kartik Agaram work around sr.ht 11 months ago
                                                                                
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "test.h"

#include "function_list"  // list of function prototypes auto-generated in 'build'
const test_fn Tests[] = {
  #include "test_list"  // list of test function names auto-generated in 'build'
};

bool Passed = true;
int Num_failures = 0;

int run_tests(void) {
  for (size_t i = 0;  i < sizeof(Tests)/sizeof(Tests[0]);  ++i)
    run_test(i);
  fprintf(stderr, "\n");
  if (Num_failures > 0) {
    fprintf(stderr, "%d failure%s\n", Num_failures, (Num_failures > 1 ? "s" : ""));
    return 1;
  }
  return 0;
}

void run_test(int i) {
  Passed = true;
  reset();
  (*Tests[i])();
  if (Passed) fprintf(stderr, ".");
  else ++Num_failures;
}

const char* Test_names[] = {
  #include "test_name_list"  // auto-generated; see 'build*' scripts
};

int run_single_test(const char* test_name) {
  for (size_t i=0;  i < sizeof(Tests)/sizeof(Tests[0]);  ++i) {
    if (strcmp(Test_names[i], test_name) != 0) continue;
    run_test(i);
    if (Passed) fprintf(stderr, "\n");
    return !Passed;
  }
  return 1;  // no test found
}