Skip to content

Commit

Permalink
chore: refactor and cleanup header file
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Jan 23, 2025
1 parent 711d335 commit ea3517e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 171 deletions.
2 changes: 1 addition & 1 deletion Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ FMT ?= clang-format
VALGRIND ?= valgrind

LIBNAME := libtap
LIBVERS := 0.0.8
LIBVERS := 0.0.9
15 changes: 4 additions & 11 deletions clib.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
{
"name": "libtap",
"version": "0.0.8",
"version": "0.0.9",
"author": "Matthew Zito",
"repo": "exbotanical/libtap",
"license": "MIT",
"description": "An implementation of TAP testing",
"keywords": [
"test anything protocol",
"testing",
"unit test"
],
"src": [
"include/libtap.h",
"src/tap.c"
],
"keywords": ["test anything protocol", "testing", "unit test"],
"src": ["include/libtap.h", "src/tap.c"],
"development": {
"exbotanical/print-assert": "0.0.1"
},
"dependencies": {
"strdup": "0.1.5"
}
}
}
38 changes: 23 additions & 15 deletions deps/print-assert/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ static int stderr_p[2];

// TODO: move to libutil/io
static void
write_buf (int fd, const void *buf, size_t sz)
{
write_buf (int fd, const void *buf, size_t sz) {
while (sz) {
ssize_t done;

Expand All @@ -35,8 +34,7 @@ write_buf (int fd, const void *buf, size_t sz)
}

static unsigned int
match (int fd, const char *pattern)
{
match (int fd, const char *pattern) {
char buf[PIPE_BUF + 1];

int r = read(fd, buf, sizeof(buf) - 1);
Expand All @@ -61,8 +59,7 @@ match (int fd, const char *pattern)
}

void
pa_printerr (const char *fmt, ...)
{
pa_printerr (const char *fmt, ...) {
char buf[PRINT_BUFFER_SZ];
va_list ap;

Expand All @@ -74,9 +71,11 @@ pa_printerr (const char *fmt, ...)
write_buf(stderrfd, "\n", 1);
}

int setup = 0;

void
pa_setup (void)
{
pa_setup (void) {
setup = 1;
if (is_running) {
pa_printerr("setup has already been called");
_exit(EXIT_FAILURE);
Expand Down Expand Up @@ -109,15 +108,18 @@ pa_setup (void)
_exit(EXIT_FAILURE);
}

if (dup2(stderr_p[1], STDERR_FILENO) < 0 || dup2(stdout_p[1], STDOUT_FILENO) < 0) {
if (dup2(stderr_p[1], STDERR_FILENO) < 0
|| dup2(stdout_p[1], STDOUT_FILENO) < 0) {
pa_printerr("failed to dup2 file descriptors");
_exit(EXIT_FAILURE);
}
}

void
pa_teardown (void)
{
pa_teardown (void) {
if (setup == 0) {
return;
}
close(stderr_p[0]);
close(stderr_p[1]);
close(stdout_p[0]);
Expand All @@ -138,13 +140,19 @@ pa_teardown (void)
}

unsigned int
pa_match_stdout (const char *pattern)
{
pa_match_stdout (const char *pattern) {
if (setup == 0) {
return 1;
}

return match(stdout_p[0], pattern);
}

unsigned int
pa_match_stderr (const char *pattern)
{
pa_match_stderr (const char *pattern) {
if (setup == 0) {
return 1;
}

return match(stderr_p[0], pattern);
}
162 changes: 38 additions & 124 deletions include/libtap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIBTAP_H

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -15,39 +16,16 @@ extern "C" {
#endif

// TODO: parser for gh actions, etc

static inline char*
__tap_s_fmt__ (char* fmt, ...) {
va_list args, args_cp;
va_start(args, fmt);
va_copy(args_cp, args);

// Pass length of zero first to determine number of bytes needed
int n = vsnprintf(NULL, 0, fmt, args) + 1;
char* buf = (char*)malloc(n);
if (!buf) {
return NULL;
}

vsnprintf(buf, n, fmt, args_cp);

va_end(args);
va_end(args_cp);

return buf;
}

unsigned int __tap_ok(
unsigned int ok,
const char* fn_name,
const char* file,
const unsigned int line,
char* msg
const char* fmt,
...
);

void __tap_skip(unsigned int num_skips, char* msg);

int __tap_write_shared_mem(int status);
void __tap_skip(unsigned int num_skips, const char* msg);
int __tap_write_shared_mem(int status);

#define __tap_lives_or_dies(wants_death, code, ...) \
do { \
Expand Down Expand Up @@ -83,7 +61,7 @@ int __tap_write_shared_mem(int status);
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
__VA_ARGS__ \
); \
} while (0)

Expand All @@ -96,108 +74,44 @@ void plan(unsigned int num_tests);
unsigned int exit_status(void);
unsigned int bail_out(const char* fmt, ...);

#define ok(test, ...) \
__tap_ok( \
test ? 1 : 0, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)

#define eq_num(a, b, ...) \
__tap_ok( \
a == b ? 1 : 0, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)

#define neq_num(a, b, ...) \
__tap_ok( \
a != b ? 1 : 0, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)

#define eq_str(a, b, ...) \
__tap_ok( \
strcmp(a, b) == 0 ? 1 : 0, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)

#define neq_str(a, b, ...) \
__tap_ok( \
strcmp(a, b) == 0 ? 0 : 1, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)

#define eq_null(a, ...) \
__tap_ok(a == NULL, __func__, __FILE__, __LINE__, __tap_s_fmt__(__VA_ARGS__))

#define neq_null(a, ...) \
__tap_ok(a != NULL, __func__, __FILE__, __LINE__, __tap_s_fmt__(__VA_ARGS__))

#define is(actual, expected, ...) \
__tap_ok( \
!(actual == expected ? 0 \
: !actual ? -1 \
: !expected ? 1 \
: strcmp(actual, expected)), \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
);

#define skip_start(cond, num_skips, ...) \
do { \
if (cond) { \
__tap_skip(num_skips, __tap_s_fmt__(__VA_ARGS__)); \
break; \
}

#define skip_end() \
} \
while (0)

#define skip(test, ...) __tap_skip(1, __tap_s_fmt__(__VA_ARGS__));

#define done_testing() return exit_status()

#define lives(...) __tap_lives_or_dies(0, __VA_ARGS__)
#define dies(...) __tap_lives_or_dies(1, __VA_ARGS__)
#define done_testing() return exit_status()
// clang-format off
#define ok(test, ...) __tap_ok(test ? 1 : 0, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define eq_num(a, b, ...) __tap_ok(a == b ? 1 : 0, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define neq_num(a, b, ...) __tap_ok(a != b ? 1 : 0, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define eq_str(a, b, ...) __tap_ok(strcmp(a, b) == 0 ? 1 : 0, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define neq_str(a, b, ...) __tap_ok(strcmp(a, b) == 0 ? 0 : 1, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define eq_null(a, ...) __tap_ok(a == NULL, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define neq_null(a, ...) __tap_ok(a != NULL, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define lives(...) __tap_lives_or_dies(0, __VA_ARGS__)
#define dies(...) __tap_lives_or_dies(1, __VA_ARGS__)
#define pass(...) ok(1, __VA_ARGS__)
#define fail(...) ok(0, __VA_ARGS__)

#define skip_start(cond, num_skips, msg) do { if (cond) { __tap_skip(num_skips, msg); break; }
#define skip_end() } while (0)
#define skip(test, msg) __tap_skip(1, msg);

#ifdef TAP_WANT_PCRE
unsigned int __tap_match(
const char* string,
const char* pattern,
const char* fn_name,
const char* file,

unsigned int
__tap_match (
const char* string,
const char* pattern,
bool want_match,
const char* fn_name,
const char* file,
const unsigned int line,
char* msg
const char* fmt,
...
);

# define match_str(string, pattern, ...) \
__tap_match( \
string, \
pattern, \
__func__, \
__FILE__, \
__LINE__, \
__tap_s_fmt__(__VA_ARGS__) \
)
#endif
#define match_str(string, pattern, ...) __tap_match(string, pattern, true, __func__, __FILE__, __LINE__, __VA_ARGS__)
#define nomatch_str(string, pattern, ...) __tap_match(string, pattern, false, __func__, __FILE__, __LINE__, __VA_ARGS__)

#endif /* TAP_WANT_PCRE */

// clang-format on
#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit ea3517e

Please sign in to comment.