Skip to content

Commit

Permalink
tweaks for c++ usage and cleaner output
Browse files Browse the repository at this point in the history
  • Loading branch information
gfaster committed Sep 18, 2023
1 parent effe6f1 commit 38b4d4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
6 changes: 6 additions & 0 deletions example.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#define _GNU_SOURCE
#include "gtest.h"

#include <signal.h>

int
main(void)
{
gtest("test that will segfault") {
raise(SIGSEGV);
}

gtest("Test that will pass") {
gassert(1 + 1 == 2);
}
Expand Down
56 changes: 30 additions & 26 deletions gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ for (int gtest__igtest = (!gtest_ignore) ? gtest__inner(name) : gtest__ignore(na
struct gtest__failed_test {
int outfd;
int errfd;
char *name;
const char *name;
};

static int gtest_ignore = 0;
Expand All @@ -52,15 +52,15 @@ static struct gtest__failed_test *gtest__failures = NULL;
static unsigned int gtest__failures_cap = 0;

static void
gtest__handle_fail(int outfd, int errfd, char *name)
gtest__handle_fail(int outfd, int errfd, const char *name)
{
if (gtest__failures_cap == gtest__failcnt) {
if (gtest__failures_cap) {
gtest__failures_cap *= 2;
} else {
gtest__failures_cap = 8;
}
gtest__failures = realloc(gtest__failures,
gtest__failures = (struct gtest__failed_test *) realloc(gtest__failures,
gtest__failures_cap *
sizeof(struct gtest__failed_test));
}
Expand All @@ -74,7 +74,7 @@ gtest__handle_fail(int outfd, int errfd, char *name)
}

static int
gtest__ignore(char *name)
gtest__ignore(const char *name)
{
printf(IGN_STR " %s\n", name);
gtest__igncnt += 1;
Expand All @@ -83,7 +83,7 @@ gtest__ignore(char *name)
}

static int
gtest__inner(char* name)
gtest__inner(const char* name)
{
int ingtest;
int outfd = memfd_create("test_stdout", 0);
Expand Down Expand Up @@ -130,9 +130,9 @@ static int
gtest_prntres(void)
{
unsigned int i;
int has_out, has_err;
ssize_t e;
struct gtest__failed_test fail;
char buf[4096];


printf("\n%u tests were run:\n", gtest__failcnt + gtest__passcnt);
Expand All @@ -146,36 +146,40 @@ gtest_prntres(void)
i = 0;
for (; i < gtest__failcnt; i++) {
fail = gtest__failures[i];
printf("\n\033[1m--------- stdout of %s ---------\033[0m\n", fail.name);
fflush(NULL);
lseek(fail.outfd, 0, SEEK_SET);
while ((e = read(fail.outfd, buf, 4096))) {
if (e == -1) {
err(1, "reading test stdout failed");
}
if (write(STDOUT_FILENO, buf, e) != e) {
err(1, "incomplete write");
if ((has_out = (lseek(fail.outfd, 0, SEEK_END) > 0))) {
printf("\n\033[1m---------\033[0m stdout of %s \033[1m---------\033[0m\n", fail.name);
fflush(NULL);
lseek(fail.outfd, 0, SEEK_SET);
while ((e = sendfile(STDERR_FILENO, fail.outfd, NULL, 1 << 20))) {
if (e == -1) {
warn("reading test stdout failed");
break;
}
}
puts("");
}
if (close(fail.outfd)) {
warn("could not close test stdout fd %i", fail.outfd);
}

printf("\n\033[1m--------- stderr of %s ---------\033[0m\n", fail.name);
fflush(NULL);

lseek(fail.errfd, 0, SEEK_SET);
while ((e = read(fail.errfd, buf, 4096))) {
if (e == -1) {
err(1, "reading test stderr failed");
}
if (write(STDERR_FILENO, buf, e) != e) {
err(1, "incomplete write");
if ((has_err = (lseek(fail.errfd, 0, SEEK_END) > 0))) {
printf("\n\033[1m---------\033[0m stderr of %s \033[1m---------\033[0m\n", fail.name);
fflush(NULL);

lseek(fail.errfd, 0, SEEK_SET);
while ((e = sendfile(STDERR_FILENO, fail.errfd, NULL, 1 << 20))) {
if (e == -1) {
warn("reading test stderr failed");
break;
}
}
puts("");
}
if (close(fail.errfd)) {
warn("could not close test stderr fd %i", fail.errfd);
}
if (!has_out && !has_err) {
printf("\n\033[1m[NO OUTPUT]\033[0m %s\n", fail.name);
}
}
if (gtest__failcnt) {
free(gtest__failures);
Expand Down

0 comments on commit 38b4d4f

Please sign in to comment.