From 396bbd92c10cc50ab0e68cc201462bd87b8cde12 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 24 Jan 2022 21:52:12 +0500 Subject: [PATCH] Test ntoa assertions --- include/kernaux/ntoa.h | 1 - src/ntoa.c | 2 ++ tests/test_ntoa.c | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/include/kernaux/ntoa.h b/include/kernaux/ntoa.h index fd9dc71..6a2efce 100644 --- a/include/kernaux/ntoa.h +++ b/include/kernaux/ntoa.h @@ -28,7 +28,6 @@ extern "C" { // "-8000000000000000" #define KERNAUX_ITOA16_BUFFER_SIZE 18 -// TODO: add tests for assertion char *kernaux_utoa(uint64_t value, char *buffer, int base); char *kernaux_itoa(int64_t value, char *buffer, int base); diff --git a/src/ntoa.c b/src/ntoa.c index d8b45a0..13fd062 100644 --- a/src/ntoa.c +++ b/src/ntoa.c @@ -9,6 +9,8 @@ char *kernaux_utoa(uint64_t value, char *buffer, int base) { + KERNAUX_NOTNULL_RETVAL(buffer, NULL); + switch (base) { case 'b': case 'B': base = 2; break; case 'o': case 'O': base = 8; break; diff --git a/tests/test_ntoa.c b/tests/test_ntoa.c index 633ebcb..4598f19 100644 --- a/tests/test_ntoa.c +++ b/tests/test_ntoa.c @@ -2,6 +2,7 @@ #include "config.h" #endif +#include #include #include @@ -362,8 +363,69 @@ static const struct { { INT64_MIN, "-8000000000000000" }, }; +static unsigned int assert_count_exp = 0; +static unsigned int assert_count_ctr = 0; +static const char *assert_last_file = NULL; + +static void assert_cb( + const char *const file, + const int line __attribute__((unused)), + const char *const msg __attribute__((unused)) +) { + ++assert_count_ctr; + assert_last_file = file; +} + +static void test_utoa_assert(char *const buffer, const int base) +{ + assert(kernaux_utoa(0, buffer, base) == NULL); + assert(assert_count_ctr == ++assert_count_exp); + const char *pos = assert_last_file; + while (*pos) ++pos; + while (pos > assert_last_file) if (*(--pos) == '/') break; + while (pos > assert_last_file) if (*(--pos) == '/') break; + ++pos; + assert(strcmp(pos, "src/ntoa.c") == 0); +} + +static void test_itoa_assert(char *const buffer, const int base) +{ + assert(kernaux_itoa(0, buffer, base) == NULL); + assert(assert_count_ctr == ++assert_count_exp); + const char *pos = assert_last_file; + while (*pos) ++pos; + while (pos > assert_last_file) if (*(--pos) == '/') break; + while (pos > assert_last_file) if (*(--pos) == '/') break; + ++pos; + assert(strcmp(pos, "src/ntoa.c") == 0); +} + int main() { + kernaux_assert_cb = assert_cb; + + { + char buffer[KERNAUX_UTOA_BUFFER_SIZE]; + + test_utoa_assert(NULL, 'd'); + test_utoa_assert(buffer, 0); + test_utoa_assert(buffer, 1); + test_utoa_assert(buffer, -1); + test_utoa_assert(buffer, 37); + test_utoa_assert(buffer, -37); + } + + { + char buffer[KERNAUX_ITOA_BUFFER_SIZE]; + + test_itoa_assert(NULL, 'd'); + test_itoa_assert(buffer, 0); + test_itoa_assert(buffer, 1); + test_itoa_assert(buffer, -1); + test_itoa_assert(buffer, 37); + test_itoa_assert(buffer, -37); + } + { char buffer[KERNAUX_UTOA_BUFFER_SIZE];