From 076937b5b588288bc0131ea1a074f6580e68a6be Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 17 Aug 2021 14:14:01 +0500 Subject: [PATCH] Convert errors to strings --- .gitignore | 1 + Makefile.am | 5 +++++ examples/raw_receiver.c | 6 +++++- examples/raw_sender.c | 8 +++++++- include/shmemq.h | 2 ++ src/main.c | 34 ++++++++++++++++++++++++++++++++++ tests/test_error_str.c | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/test_error_str.c diff --git a/.gitignore b/.gitignore index 82a6391..d52634a 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,7 @@ /tests/test_alternatively_push_and_pop_one_frame_messages_with_jump /tests/test_cons_pops_buffer_start /tests/test_cons_reaches_queue_end +/tests/test_error_str /tests/test_fork /tests/test_main /tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message diff --git a/Makefile.am b/Makefile.am index 1334e1a..b8d9217 100644 --- a/Makefile.am +++ b/Makefile.am @@ -12,6 +12,7 @@ TESTS = \ tests/test_alternatively_push_and_pop_one_frame_messages_with_jump \ tests/test_cons_pops_buffer_start \ tests/test_cons_reaches_queue_end \ + tests/test_error_str \ tests/test_fork \ tests/test_main \ tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message \ @@ -47,6 +48,10 @@ tests_test_cons_reaches_queue_end_SOURCES = \ $(libshmemq_a_SOURCES) \ tests/test_cons_reaches_queue_end.c +tests_test_error_str_SOURCES = \ + $(libshmemq_a_SOURCES) \ + tests/test_error_str.c + tests_test_fork_SOURCES = \ $(libshmemq_a_SOURCES) \ tests/test_fork.c diff --git a/examples/raw_receiver.c b/examples/raw_receiver.c index ce8140f..6f17e74 100644 --- a/examples/raw_receiver.c +++ b/examples/raw_receiver.c @@ -69,7 +69,11 @@ int main() shmemq_pop_end(shmemq, &shmemq_error); if (shmemq_error != SHMEMQ_ERROR_NONE) { - printf("Error: %u.\n", shmemq_error); + printf( + "Error: %u (SHMEMQ_ERROR_%s).\n", + shmemq_error, + shmemq_error_str(shmemq_error) + ); break; } } diff --git a/examples/raw_sender.c b/examples/raw_sender.c index 4ba6535..fff4b91 100644 --- a/examples/raw_sender.c +++ b/examples/raw_sender.c @@ -88,7 +88,13 @@ int main() } finalize: - if (shmemq_error != SHMEMQ_ERROR_NONE) printf("Error: %u.\n", shmemq_error); + if (shmemq_error != SHMEMQ_ERROR_NONE) { + printf( + "Error: %u (SHMEMQ_ERROR_%s).\n", + shmemq_error, + shmemq_error_str(shmemq_error) + ); + } printf("Destroy queue.\n"); diff --git a/include/shmemq.h b/include/shmemq.h index 01be0f6..8eae41b 100644 --- a/include/shmemq.h +++ b/include/shmemq.h @@ -78,6 +78,8 @@ typedef struct Shmemq { struct ShmemqBuffer *buffer; } *Shmemq; +const char *shmemq_error_str(ShmemqError error); + Shmemq shmemq_new(const char *name, bool is_consumer, ShmemqError *error_ptr); void shmemq_init( diff --git a/src/main.c b/src/main.c index 05c1cfd..ca23a79 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,40 @@ #include #include +const char *shmemq_error_str(const ShmemqError error) +{ + switch (error) { + case SHMEMQ_ERROR_NONE: + return "NONE"; + case SHMEMQ_ERROR_INVALID_NAME: + return "INVALID_NAME"; + case SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE: + return "BUG_POP_END_ON_EMPTY_QUEUE"; + case SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE: + return "BUG_PUSH_END_ON_FULL_QUEUE"; + case SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW: + return "BUG_PUSH_END_OVERFLOW"; + case SHMEMQ_ERROR_FAILED_MALLOC: + return "FAILED_MALLOC"; + case SHMEMQ_ERROR_FAILED_SHM_OPEN: + return "FAILED_SHM_OPEN"; + case SHMEMQ_ERROR_FAILED_FTRUNCATE: + return "FAILED_FTRUNCATE"; + case SHMEMQ_ERROR_FAILED_MMAP: + return "FAILED_MMAP"; + case SHMEMQ_ERROR_FAILED_MUNMAP: + return "FAILED_MUNMAP"; + case SHMEMQ_ERROR_FAILED_CLOSE: + return "FAILED_CLOSE"; + case SHMEMQ_ERROR_FAILED_SHM_UNLINK: + return "FAILED_SHM_UNLINK"; + case SHMEMQ_ERROR_FAILED_SEM_INIT: + return "FAILED_SEM_INIT"; + default: + return "UNKNOWN"; + } +} + void shmemq_delete(const Shmemq shmemq, ShmemqError *const error_ptr) { shmemq_finish(shmemq, error_ptr); diff --git a/tests/test_error_str.c b/tests/test_error_str.c new file mode 100644 index 0000000..af3c305 --- /dev/null +++ b/tests/test_error_str.c @@ -0,0 +1,32 @@ +#include + +#include +#include + +static void test(const ShmemqError shmemq_error, const char *const expected) +{ + assert(strcmp(shmemq_error_str(shmemq_error), expected) == 0); +} + +int main() +{ + test(49, "UNKNOWN"); + test(99, "UNKNOWN"); + test(149, "UNKNOWN"); + + test(SHMEMQ_ERROR_NONE, "NONE"); + test(SHMEMQ_ERROR_INVALID_NAME, "INVALID_NAME"); + test(SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE, "BUG_POP_END_ON_EMPTY_QUEUE"); + test(SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE, "BUG_PUSH_END_ON_FULL_QUEUE"); + test(SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW, "BUG_PUSH_END_OVERFLOW"); + test(SHMEMQ_ERROR_FAILED_MALLOC, "FAILED_MALLOC"); + test(SHMEMQ_ERROR_FAILED_SHM_OPEN, "FAILED_SHM_OPEN"); + test(SHMEMQ_ERROR_FAILED_FTRUNCATE, "FAILED_FTRUNCATE"); + test(SHMEMQ_ERROR_FAILED_MMAP, "FAILED_MMAP"); + test(SHMEMQ_ERROR_FAILED_MUNMAP, "FAILED_MUNMAP"); + test(SHMEMQ_ERROR_FAILED_CLOSE, "FAILED_CLOSE"); + test(SHMEMQ_ERROR_FAILED_SHM_UNLINK, "FAILED_SHM_UNLINK"); + test(SHMEMQ_ERROR_FAILED_SEM_INIT, "FAILED_SEM_INIT"); + + return 0; +}