2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-08-25 11:23:25 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2021-02-03 14:22:51 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
|
|
|
#include "libc/errno.h"
|
2021-02-03 14:22:51 +00:00
|
|
|
#include "libc/fmt/fmt.h"
|
|
|
|
#include "libc/log/check.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/process.h"
|
2021-02-03 14:22:51 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/runtime/symbols.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
2021-02-03 14:22:51 +00:00
|
|
|
#include "libc/x/x.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-05-03 08:59:27 +00:00
|
|
|
static int x;
|
2021-08-18 21:21:30 +00:00
|
|
|
char g_testlib_olddir[PATH_MAX];
|
|
|
|
char g_testlib_tmpdir[PATH_MAX];
|
2021-05-03 08:59:27 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
void testlib_finish(void) {
|
|
|
|
if (g_testlib_failed) {
|
|
|
|
fprintf(stderr, "%u / %u %s\n", g_testlib_failed, g_testlib_ran,
|
|
|
|
"tests failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 20:20:41 +00:00
|
|
|
wontreturn void testlib_abort(void) {
|
2020-06-15 14:18:57 +00:00
|
|
|
testlib_finish();
|
2020-10-11 04:18:53 +00:00
|
|
|
exit(MIN(255, g_testlib_failed));
|
2020-06-15 14:18:57 +00:00
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
|
2021-05-03 08:59:27 +00:00
|
|
|
static void SetupTmpDir(void) {
|
2021-08-18 21:21:30 +00:00
|
|
|
snprintf(g_testlib_tmpdir, sizeof(g_testlib_tmpdir), "o/tmp/%s.%d.%d",
|
|
|
|
program_invocation_short_name, getpid(), x++);
|
|
|
|
CHECK_NE(-1, makedirs(g_testlib_tmpdir, 0755), "%s", g_testlib_tmpdir);
|
|
|
|
CHECK_EQ(1, isdirectory(g_testlib_tmpdir), "%s", g_testlib_tmpdir);
|
|
|
|
CHECK_NOTNULL(realpath(g_testlib_tmpdir, g_testlib_tmpdir), "%`'s",
|
|
|
|
g_testlib_tmpdir);
|
|
|
|
CHECK_NE(-1, chdir(g_testlib_tmpdir), "%s", g_testlib_tmpdir);
|
2021-05-03 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTmpDir(void) {
|
2021-08-18 21:21:30 +00:00
|
|
|
CHECK_NE(-1, chdir(g_testlib_olddir));
|
|
|
|
CHECK_NE(-1, rmrf(g_testlib_tmpdir));
|
2021-05-03 08:59:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Runs all test case functions in sorted order.
|
|
|
|
*/
|
|
|
|
testonly void testlib_runtestcases(testfn_t *start, testfn_t *end,
|
|
|
|
testfn_t warmup) {
|
2020-12-26 10:09:07 +00:00
|
|
|
/*
|
2020-06-15 14:18:57 +00:00
|
|
|
* getpid() calls are inserted to help visually see tests in traces
|
|
|
|
* which can be performed on Linux, FreeBSD, OpenBSD, and XNU:
|
|
|
|
*
|
|
|
|
* strace -f o/default/test.com |& less
|
|
|
|
* truss o/default/test.com |& less
|
|
|
|
* ktrace -f trace o/default/test.com </dev/null; kdump -f trace | less
|
|
|
|
* dtruss o/default/test.com |& less
|
|
|
|
*
|
|
|
|
* Test cases are iterable via a decentralized section. Your TEST()
|
|
|
|
* macro inserts .testcase.SUITENAME sections into the binary which
|
|
|
|
* the linker sorts into an array.
|
|
|
|
*
|
|
|
|
* @see ape/ape.lds
|
|
|
|
*/
|
|
|
|
const testfn_t *fn;
|
2021-08-18 21:21:30 +00:00
|
|
|
CHECK_NOTNULL(getcwd(g_testlib_olddir, sizeof(g_testlib_olddir)));
|
2021-05-03 08:59:27 +00:00
|
|
|
if (weaken(testlib_enable_tmp_setup_teardown_once)) SetupTmpDir();
|
|
|
|
if (weaken(SetUpOnce)) weaken(SetUpOnce)();
|
2021-02-03 14:22:51 +00:00
|
|
|
for (x = 0, fn = start; fn != end; ++fn) {
|
2021-05-03 08:59:27 +00:00
|
|
|
if (weaken(testlib_enable_tmp_setup_teardown)) SetupTmpDir();
|
2020-06-15 14:18:57 +00:00
|
|
|
if (weaken(SetUp)) weaken(SetUp)();
|
|
|
|
errno = 0;
|
|
|
|
SetLastError(0);
|
2021-02-04 03:35:29 +00:00
|
|
|
sys_getpid();
|
2020-06-15 14:18:57 +00:00
|
|
|
if (warmup) warmup();
|
2020-08-25 11:23:25 +00:00
|
|
|
testlib_clearxmmregisters();
|
2020-06-15 14:18:57 +00:00
|
|
|
(*fn)();
|
2021-02-04 03:35:29 +00:00
|
|
|
sys_getpid();
|
2020-06-15 14:18:57 +00:00
|
|
|
if (weaken(TearDown)) weaken(TearDown)();
|
2021-05-03 08:59:27 +00:00
|
|
|
if (weaken(testlib_enable_tmp_setup_teardown)) TearDownTmpDir();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-05-03 08:59:27 +00:00
|
|
|
if (weaken(TearDownOnce)) weaken(TearDownOnce)();
|
|
|
|
if (weaken(testlib_enable_tmp_setup_teardown_once)) TearDownTmpDir();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|