Skip to content

Commit 076937b

Browse files
Convert errors to strings
1 parent 128cd8b commit 076937b

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
/tests/test_alternatively_push_and_pop_one_frame_messages_with_jump
4040
/tests/test_cons_pops_buffer_start
4141
/tests/test_cons_reaches_queue_end
42+
/tests/test_error_str
4243
/tests/test_fork
4344
/tests/test_main
4445
/tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message

Makefile.am

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TESTS = \
1212
tests/test_alternatively_push_and_pop_one_frame_messages_with_jump \
1313
tests/test_cons_pops_buffer_start \
1414
tests/test_cons_reaches_queue_end \
15+
tests/test_error_str \
1516
tests/test_fork \
1617
tests/test_main \
1718
tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message \
@@ -47,6 +48,10 @@ tests_test_cons_reaches_queue_end_SOURCES = \
4748
$(libshmemq_a_SOURCES) \
4849
tests/test_cons_reaches_queue_end.c
4950

51+
tests_test_error_str_SOURCES = \
52+
$(libshmemq_a_SOURCES) \
53+
tests/test_error_str.c
54+
5055
tests_test_fork_SOURCES = \
5156
$(libshmemq_a_SOURCES) \
5257
tests/test_fork.c

examples/raw_receiver.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ int main()
6969
shmemq_pop_end(shmemq, &shmemq_error);
7070

7171
if (shmemq_error != SHMEMQ_ERROR_NONE) {
72-
printf("Error: %u.\n", shmemq_error);
72+
printf(
73+
"Error: %u (SHMEMQ_ERROR_%s).\n",
74+
shmemq_error,
75+
shmemq_error_str(shmemq_error)
76+
);
7377
break;
7478
}
7579
}

examples/raw_sender.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ int main()
8888
}
8989

9090
finalize:
91-
if (shmemq_error != SHMEMQ_ERROR_NONE) printf("Error: %u.\n", shmemq_error);
91+
if (shmemq_error != SHMEMQ_ERROR_NONE) {
92+
printf(
93+
"Error: %u (SHMEMQ_ERROR_%s).\n",
94+
shmemq_error,
95+
shmemq_error_str(shmemq_error)
96+
);
97+
}
9298

9399
printf("Destroy queue.\n");
94100

include/shmemq.h

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ typedef struct Shmemq {
7878
struct ShmemqBuffer *buffer;
7979
} *Shmemq;
8080

81+
const char *shmemq_error_str(ShmemqError error);
82+
8183
Shmemq shmemq_new(const char *name, bool is_consumer, ShmemqError *error_ptr);
8284

8385
void shmemq_init(

src/main.c

+34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,40 @@
99
#include <sys/stat.h>
1010
#include <unistd.h>
1111

12+
const char *shmemq_error_str(const ShmemqError error)
13+
{
14+
switch (error) {
15+
case SHMEMQ_ERROR_NONE:
16+
return "NONE";
17+
case SHMEMQ_ERROR_INVALID_NAME:
18+
return "INVALID_NAME";
19+
case SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE:
20+
return "BUG_POP_END_ON_EMPTY_QUEUE";
21+
case SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE:
22+
return "BUG_PUSH_END_ON_FULL_QUEUE";
23+
case SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW:
24+
return "BUG_PUSH_END_OVERFLOW";
25+
case SHMEMQ_ERROR_FAILED_MALLOC:
26+
return "FAILED_MALLOC";
27+
case SHMEMQ_ERROR_FAILED_SHM_OPEN:
28+
return "FAILED_SHM_OPEN";
29+
case SHMEMQ_ERROR_FAILED_FTRUNCATE:
30+
return "FAILED_FTRUNCATE";
31+
case SHMEMQ_ERROR_FAILED_MMAP:
32+
return "FAILED_MMAP";
33+
case SHMEMQ_ERROR_FAILED_MUNMAP:
34+
return "FAILED_MUNMAP";
35+
case SHMEMQ_ERROR_FAILED_CLOSE:
36+
return "FAILED_CLOSE";
37+
case SHMEMQ_ERROR_FAILED_SHM_UNLINK:
38+
return "FAILED_SHM_UNLINK";
39+
case SHMEMQ_ERROR_FAILED_SEM_INIT:
40+
return "FAILED_SEM_INIT";
41+
default:
42+
return "UNKNOWN";
43+
}
44+
}
45+
1246
void shmemq_delete(const Shmemq shmemq, ShmemqError *const error_ptr)
1347
{
1448
shmemq_finish(shmemq, error_ptr);

tests/test_error_str.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <shmemq.h>
2+
3+
#include <assert.h>
4+
#include <string.h>
5+
6+
static void test(const ShmemqError shmemq_error, const char *const expected)
7+
{
8+
assert(strcmp(shmemq_error_str(shmemq_error), expected) == 0);
9+
}
10+
11+
int main()
12+
{
13+
test(49, "UNKNOWN");
14+
test(99, "UNKNOWN");
15+
test(149, "UNKNOWN");
16+
17+
test(SHMEMQ_ERROR_NONE, "NONE");
18+
test(SHMEMQ_ERROR_INVALID_NAME, "INVALID_NAME");
19+
test(SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE, "BUG_POP_END_ON_EMPTY_QUEUE");
20+
test(SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE, "BUG_PUSH_END_ON_FULL_QUEUE");
21+
test(SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW, "BUG_PUSH_END_OVERFLOW");
22+
test(SHMEMQ_ERROR_FAILED_MALLOC, "FAILED_MALLOC");
23+
test(SHMEMQ_ERROR_FAILED_SHM_OPEN, "FAILED_SHM_OPEN");
24+
test(SHMEMQ_ERROR_FAILED_FTRUNCATE, "FAILED_FTRUNCATE");
25+
test(SHMEMQ_ERROR_FAILED_MMAP, "FAILED_MMAP");
26+
test(SHMEMQ_ERROR_FAILED_MUNMAP, "FAILED_MUNMAP");
27+
test(SHMEMQ_ERROR_FAILED_CLOSE, "FAILED_CLOSE");
28+
test(SHMEMQ_ERROR_FAILED_SHM_UNLINK, "FAILED_SHM_UNLINK");
29+
test(SHMEMQ_ERROR_FAILED_SEM_INIT, "FAILED_SEM_INIT");
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)