Blame common/main.c

nsz 54c268
#define _POSIX_C_SOURCE 200809L
nsz 54c268
#include <stdlib.h>
nsz 9087bf
#include <stdio.h>
nsz 9087bf
#include <stdarg.h>
nsz 54c268
#include <string.h>
nsz 54c268
#include <errno.h>
nsz 54c268
#include <sys/types.h>
nsz 54c268
#include <sys/wait.h>
nsz 54c268
#include <unistd.h>
nsz 7308b3
#include "test.h"
nsz 7308b3
nsz a520c1
#define B(t)
nsz 7308b3
#define T(t) void t();
nsz 7308b3
#include "main.h"
nsz 7308b3
#undef T
nsz 7308b3
nsz 9087bf
static int failed;
nsz 9087bf
static const char *name;
nsz 7308b3
nsz 7308b3
static int verbose;
nsz 7308b3
static int count;
nsz 7308b3
static int nfailed;
nsz 7308b3
nsz a520c1
static void errtimer() { error("use *_timer in benchmarks only\n"); }
nsz a520c1
int N = 0;
nsz a520c1
void start_timer() { errtimer(); }
nsz a520c1
void stop_timer() { errtimer(); }
nsz a520c1
void reset_timer() { errtimer(); }
nsz a520c1
nsz 9087bf
void error__(const char *n, int l, const char *s, ...) {
nsz 9087bf
	va_list ap;
nsz 9087bf
nsz 9087bf
	failed = 1;
nsz 9087bf
	fprintf(stderr, "- ERROR %s at %s:%d: ", name, n, l);
nsz 9087bf
	va_start(ap, s);
nsz 9087bf
	vfprintf(stderr, s, ap);
nsz 9087bf
	va_end(ap);
nsz 9087bf
}
nsz 9087bf
nsz 7308b3
static void run(const char *n, void (*f)()) {
nsz 54c268
	pid_t pid;
nsz 9087bf
	int s;
nsz 54c268
nsz 7308b3
	count++;
nsz 9087bf
	failed = 0;
nsz 9087bf
	name = n;
nsz 7308b3
	if (verbose)
nsz 9087bf
		fprintf(stderr, "running %s:\n", name);
nsz 54c268
nsz 54c268
	pid = fork();
nsz 54c268
	if (pid == 0) {
nsz 54c268
		/* run test in a child process */
nsz 54c268
		f();
nsz 9087bf
		exit(failed);
nsz 54c268
	}
nsz 54c268
nsz 54c268
	if (pid == -1)
nsz 54c268
		error("fork failed: %s\n", strerror(errno));
nsz 54c268
	else {
nsz 9087bf
		if (waitpid(pid, &s, 0) == -1)
nsz 54c268
			error("waitpid failed: %s\n", strerror(errno));
nsz 9087bf
		else if (!WIFEXITED(s))
nsz 9087bf
			error("abnormal exit: %s\n", WIFSIGNALED(s) ? strsignal(WTERMSIG(s)) : "(unknown)");
nsz 54c268
		else
nsz 9087bf
			failed = !!WEXITSTATUS(s);
nsz 54c268
	}
nsz 54c268
nsz 9087bf
	if (failed) {
nsz 7308b3
		nfailed++;
nsz 9087bf
		fprintf(stderr, "FAILED %s\n", name);
nsz 7308b3
	} else if (verbose)
nsz 9087bf
		fprintf(stderr, "PASSED %s\n", name);
nsz 7308b3
}
nsz 7308b3
nsz 7308b3
static int summary() {
nsz 7308b3
	fprintf(stderr, "PASS:%d FAIL:%d\n", count-nfailed, nfailed);
nsz 7308b3
	return !!nfailed;
nsz 7308b3
}
nsz 7308b3
nsz 7308b3
int main() {
nsz 7308b3
#define T(t) run(#t, t);
nsz 7308b3
#include "main.h"
nsz 7308b3
	return summary();
nsz 7308b3
}