Skip to content

Commit

Permalink
Add macro to define self-tests in debug builds
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Jan 7, 2024
1 parent d453ab1 commit 88ade5f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/lib/lwan-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ bool parse_bool(const char *value, bool default_value)
return parse_int(value, default_value);
}

#ifndef NDEBUG
__attribute__((constructor))
static void test_parse_bool(void)
LWAN_SELF_TEST(parse_bool)
{
assert(parse_bool("true", false) == true);
assert(parse_bool("on", false) == true);
Expand All @@ -229,7 +227,6 @@ static void test_parse_bool(void)
assert(parse_bool("abacate", true) == true);
assert(parse_bool("abacate", false) == false);
}
#endif

bool config_error(struct config *conf, const char *fmt, ...)
{
Expand Down
4 changes: 1 addition & 3 deletions src/lib/lwan-coro.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define ALLOCATE_STACK_WITH_MMAP
#endif

#ifndef NDEBUG
__attribute__((constructor)) static void assert_sizes_are_sane(void)
LWAN_SELF_TEST(sizes_are_same)
{
/* This is done in runtime rather than during compilation time because
* in Glibc >= 2.34, SIGSTKSZ is defined as sysconf(_SC_MINSIGSTKSZ). */
Expand All @@ -86,7 +85,6 @@ __attribute__((constructor)) static void assert_sizes_are_sane(void)
assert((CORO_STACK_SIZE >= PAGE_SIZE));
#endif
}
#endif

typedef void (*defer1_func)(void *data);
typedef void (*defer2_func)(void *data1, void *data2);
Expand Down
12 changes: 5 additions & 7 deletions src/lib/lwan-tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ void lwan_tables_init(void)
"text/javascript"));
assert(streq(lwan_determine_mime_type_for_file_name(".BZ2"),
"application/x-bzip2"));
}

#ifndef NDEBUG
LWAN_SELF_TEST(status_codes)
{
#define ASSERT_STATUS(id, code, short, long) \
do { \
const char *status = lwan_http_status_as_string_with_code(HTTP_##id); \
Expand All @@ -115,9 +117,8 @@ void lwan_tables_init(void)
const char *descr = lwan_http_status_as_descriptive_string(HTTP_##id); \
assert(!strcmp(descr, long)); \
} while (0);
FOR_EACH_HTTP_STATUS(ASSERT_STATUS)
FOR_EACH_HTTP_STATUS(ASSERT_STATUS)
#undef ASSERT_STATUS
#endif
}

static int
Expand Down Expand Up @@ -292,10 +293,8 @@ ALWAYS_INLINE uint8_t lwan_char_isalnum(char ch)
return char_prop_tbl[(unsigned char)ch] & (CHAR_PROP_ALPHA | CHAR_PROP_DIG);
}

#ifndef NDEBUG
#include <ctype.h>
__attribute__((constructor))
static void test_lwan_char_tables(void)
LWAN_SELF_TEST(compare_with_ctype)
{
for (int i = 0; i < 256; i++) {
assert(!!isxdigit((char)i) == !!lwan_char_isxdigit((char)i));
Expand All @@ -306,4 +305,3 @@ static void test_lwan_char_tables(void)
/* isspace() and lwan_char_isspace() differs on purpose */
}
}
#endif
4 changes: 1 addition & 3 deletions src/lib/missing.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ ALWAYS_INLINE bool strcaseequal_neutral(const char *a, const char *b)
return strcaseequal_neutral_len(a, b, SIZE_MAX);
}

#ifndef NDEBUG
__attribute__((constructor)) static void test_strcaseequal_neutral(void)
LWAN_SELF_TEST(strcaseequal_neutral)
{
assert(strcaseequal_neutral("LWAN", "lwan") == true);
assert(strcaseequal_neutral("LwAn", "lWaN") == true);
Expand All @@ -746,7 +745,6 @@ __attribute__((constructor)) static void test_strcaseequal_neutral(void)
static_assert('0' == 48, "ASCII character set");
static_assert('a' == 97, "ASCII character set");
}
#endif

#ifndef LWAN_HAVE_STPCPY
char *stpncpy(char *restrict dst, const char *restrict src, size_t sz)
Expand Down
10 changes: 10 additions & 0 deletions src/lib/missing/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@
#endif
#endif

/* Macro to enable self-test on startup in debug builds.
* Details: https://tia.mat.br/posts/2023/12/11/self-test.html */
#if defined(NDEBUG)
#define LWAN_SELF_TEST(name) \
__attribute__((unused)) static void self_test_##name(void)
#else
#define LWAN_SELF_TEST(name) \
__attribute__((constructor)) static void self_test_##name(void)
#endif

#endif /* MISSING_ASSERT_H */

0 comments on commit 88ade5f

Please sign in to comment.