1
0
Fork 0
mirror of https://github.com/tailix/libshmemq.git synced 2024-11-20 11:08:35 -05:00

Convert errors to strings

This commit is contained in:
Alex Kotov 2021-08-17 14:14:01 +05:00
parent 128cd8bbf8
commit 076937b5b5
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
7 changed files with 86 additions and 2 deletions

1
.gitignore vendored
View file

@ -39,6 +39,7 @@
/tests/test_alternatively_push_and_pop_one_frame_messages_with_jump /tests/test_alternatively_push_and_pop_one_frame_messages_with_jump
/tests/test_cons_pops_buffer_start /tests/test_cons_pops_buffer_start
/tests/test_cons_reaches_queue_end /tests/test_cons_reaches_queue_end
/tests/test_error_str
/tests/test_fork /tests/test_fork
/tests/test_main /tests/test_main
/tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message /tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message

View file

@ -12,6 +12,7 @@ TESTS = \
tests/test_alternatively_push_and_pop_one_frame_messages_with_jump \ tests/test_alternatively_push_and_pop_one_frame_messages_with_jump \
tests/test_cons_pops_buffer_start \ tests/test_cons_pops_buffer_start \
tests/test_cons_reaches_queue_end \ tests/test_cons_reaches_queue_end \
tests/test_error_str \
tests/test_fork \ tests/test_fork \
tests/test_main \ tests/test_main \
tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message \ 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) \ $(libshmemq_a_SOURCES) \
tests/test_cons_reaches_queue_end.c tests/test_cons_reaches_queue_end.c
tests_test_error_str_SOURCES = \
$(libshmemq_a_SOURCES) \
tests/test_error_str.c
tests_test_fork_SOURCES = \ tests_test_fork_SOURCES = \
$(libshmemq_a_SOURCES) \ $(libshmemq_a_SOURCES) \
tests/test_fork.c tests/test_fork.c

View file

@ -69,7 +69,11 @@ int main()
shmemq_pop_end(shmemq, &shmemq_error); shmemq_pop_end(shmemq, &shmemq_error);
if (shmemq_error != SHMEMQ_ERROR_NONE) { 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; break;
} }
} }

View file

@ -88,7 +88,13 @@ int main()
} }
finalize: 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"); printf("Destroy queue.\n");

View file

@ -78,6 +78,8 @@ typedef struct Shmemq {
struct ShmemqBuffer *buffer; struct ShmemqBuffer *buffer;
} *Shmemq; } *Shmemq;
const char *shmemq_error_str(ShmemqError error);
Shmemq shmemq_new(const char *name, bool is_consumer, ShmemqError *error_ptr); Shmemq shmemq_new(const char *name, bool is_consumer, ShmemqError *error_ptr);
void shmemq_init( void shmemq_init(

View file

@ -9,6 +9,40 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
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) void shmemq_delete(const Shmemq shmemq, ShmemqError *const error_ptr)
{ {
shmemq_finish(shmemq, error_ptr); shmemq_finish(shmemq, error_ptr);

32
tests/test_error_str.c Normal file
View file

@ -0,0 +1,32 @@
#include <shmemq.h>
#include <assert.h>
#include <string.h>
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;
}