diff --git a/ct/ct.c b/ct/ct.c index 2e726072..e7b4e15a 100644 --- a/ct/ct.c +++ b/ct/ct.c @@ -1,4 +1,4 @@ -// CT - simple-minded unit testing for C +/* CT - simple-minded unit testing for C */ #include #include @@ -14,18 +14,20 @@ #include #include #include +#include #include "internal.h" #include "ct.h" static char *curdir; static int rjobfd = -1, wjobfd = -1; +int fail = 0; /* bool */ static int64 bstart, bdur; -static int btiming; // bool +static int btiming; /* bool */ static int64 bbytes; -static const int64 Second = 1000 * 1000 * 1000; -static const int64 BenchTime = Second; -static const int MaxN = 1000 * 1000 * 1000; +enum { Second = 1000 * 1000 * 1000 }; +enum { BenchTime = Second }; +enum { MaxN = 1000 * 1000 * 1000 }; @@ -39,6 +41,7 @@ nstime() } #else +# include static int64 nstime() @@ -51,7 +54,7 @@ nstime() #endif void -ctlogpn(char *p, int n, char *fmt, ...) +ctlogpn(const char *p, int n, const char *fmt, ...) { va_list arg; @@ -66,8 +69,14 @@ ctlogpn(char *p, int n, char *fmt, ...) void ctfail(void) { - fflush(stdout); - fflush(stderr); + fail = 1; +} + + +void +ctfailnow(void) +{ + fflush(NULL); abort(); } @@ -75,7 +84,6 @@ ctfail(void) char * ctdir(void) { - mkdir(curdir, 0700); return curdir; } @@ -116,7 +124,7 @@ ctsetbytes(int n) static void -die(int code, int err, char *msg) +die(int code, int err, const char *msg) { putc('\n', stderr); @@ -182,7 +190,10 @@ start(Test *t) { t->fd = tmpfd(); strcpy(t->dir, TmpDirPat); - mktemp(t->dir); + if (mkdtemp(t->dir) == NULL) { + die(1, errno, "mkdtemp"); + } + fflush(NULL); t->pid = fork(); if (t->pid < 0) { die(1, errno, "fork"); @@ -199,7 +210,10 @@ start(Test *t) } curdir = t->dir; t->f(); - _exit(0); + if (fail) { + ctfailnow(); + } + exit(0); } setpgid(t->pid, t->pid); } @@ -228,7 +242,7 @@ static void copyfd(FILE *out, int in) { ssize_t n; - char buf[1024]; // arbitrary size + char buf[1024]; /* arbitrary size */ while ((n = read(in, buf, sizeof(buf))) != 0) { if (fwrite(buf, 1, n, out) != (size_t)n) { @@ -238,15 +252,17 @@ copyfd(FILE *out, int in) } -// Removes path and all of its children. -// Writes errors to stderr and keeps going. -// If path doesn't exist, rmtree returns silently. +/* +Removes path and all of its children. +Writes errors to stderr and keeps going. +If path doesn't exist, rmtree returns silently. +*/ static void rmtree(char *path) { int r = unlink(path); if (r == 0 || errno == ENOENT) { - return; // success + return; /* success */ } int unlinkerr = errno; @@ -285,7 +301,10 @@ runbenchn(Benchmark *b, int n) int outfd = tmpfd(); int durfd = tmpfd(); strcpy(b->dir, TmpDirPat); - mktemp(b->dir); + if (mkdtemp(b->dir) == NULL) { + die(1, errno, "mkdtemp"); + } + fflush(NULL); int pid = fork(); if (pid < 0) { die(1, errno, "fork"); @@ -306,7 +325,7 @@ runbenchn(Benchmark *b, int n) ctstoptimer(); write(durfd, &bdur, sizeof bdur); write(durfd, &bbytes, sizeof bbytes); - _exit(0); + exit(0); } setpgid(pid, pid); @@ -337,17 +356,17 @@ runbenchn(Benchmark *b, int n) } -// rounddown10 rounds a number down to the nearest power of 10. +/* rounddown10 rounds a number down to the nearest power of 10. */ static int rounddown10(int n) { int tens = 0; - // tens = floor(log_10(n)) + /* tens = floor(log_10(n)) */ while (n >= 10) { n = n / 10; tens++; } - // result = 10**tens + /* result = 10**tens */ int i, result = 1; for (i = 0; i < tens; i++) { result *= 10; @@ -356,7 +375,7 @@ rounddown10(int n) } -// roundup rounds n up to a number of the form [1eX, 2eX, 5eX]. +/* roundup rounds n up to a number of the form [1eX, 2eX, 5eX]. */ static int roundup(int n) { @@ -400,23 +419,23 @@ runbench(Benchmark *b) runbenchn(b, n); while (b->status == 0 && b->dur < BenchTime && n < MaxN) { int last = n; - // Predict iterations/sec. + /* Predict iterations/sec. */ int nsop = b->dur / n; if (nsop == 0) { n = MaxN; } else { n = BenchTime / nsop; } - // Run more iterations than we think we'll need for a second (1.5x). - // Don't grow too fast in case we had timing errors previously. - // Be sure to run at least one more than last time. + /* Run more iterations than we think we'll need for a second (1.5x). + Don't grow too fast in case we had timing errors previously. + Be sure to run at least one more than last time. */ n = max(min(n+n/2, 100*last), last+1); - // Round up to something easy to read. + /* Round up to something easy to read. */ n = roundup(n); runbenchn(b, n); } if (b->status == 0) { - printf("%8d\t%10lld ns/op", n, b->dur/n); + printf("%8d\t%10" PRId64 " ns/op", n, b->dur/n); if (b->bytes > 0) { double mbs = 0; if (b->dur > 0) { @@ -495,14 +514,14 @@ report(Test *t) } -int +static int readtokens() { int n = 1; char c, *s; if ((s = strstr(getenv("MAKEFLAGS"), " --jobserver-fds="))) { - rjobfd = (int)strtol(s+17, &s, 10); // skip " --jobserver-fds=" - wjobfd = (int)strtol(s+1, NULL, 10); // skip comma + rjobfd = (int)strtol(s+17, &s, 10); /* skip " --jobserver-fds=" */ + wjobfd = (int)strtol(s+1, NULL, 10); /* skip comma */ } if (rjobfd >= 0) { fcntl(rjobfd, F_SETFL, fcntl(rjobfd, F_GETFL)|O_NONBLOCK); @@ -514,14 +533,14 @@ readtokens() } -void +static void writetokens(int n) { char c = '+'; if (wjobfd >= 0) { fcntl(wjobfd, F_SETFL, fcntl(wjobfd, F_GETFL)|O_NONBLOCK); for (; n>1; n--) { - write(wjobfd, &c, 1); // ignore error; nothing we can do anyway + write(wjobfd, &c, 1); /* ignore error; nothing we can do anyway */ } } } diff --git a/ct/ct.h b/ct/ct.h index adaf8b90..b645fe44 100644 --- a/ct/ct.h +++ b/ct/ct.h @@ -1,17 +1,18 @@ char *ctdir(void); void ctfail(void); +void ctfailnow(void); void ctresettimer(void); void ctstarttimer(void); void ctstoptimer(void); void ctsetbytes(int); -void ctlogpn(char*, int, char*, ...) __attribute__((format(printf, 3, 4))); +void ctlogpn(const char*, int, const char*, ...) __attribute__((format(printf, 3, 4))); #define ctlog(...) ctlogpn(__FILE__, __LINE__, __VA_ARGS__) #define assert(x) do if (!(x)) {\ ctlog("%s", "test: " #x);\ - ctfail();\ + ctfailnow();\ } while (0) #define assertf(x, ...) do if (!(x)) {\ ctlog("%s", "test: " #x);\ ctlog(__VA_ARGS__);\ - ctfail();\ + ctfailnow();\ } while (0) diff --git a/ct/internal.h b/ct/internal.h index 777e97c1..18dcf5cf 100644 --- a/ct/internal.h +++ b/ct/internal.h @@ -1,4 +1,4 @@ -// include +/* include */ #define TmpDirPat "/tmp/ct.XXXXXX" @@ -8,7 +8,7 @@ typedef struct Benchmark Benchmark; struct Test { void (*f)(void); - char *name; + const char *name; int status; int fd; int pid; @@ -17,7 +17,7 @@ struct Test { struct Benchmark { void (*f)(int); - char *name; + const char *name; int status; int64 dur; int64 bytes;