mirror of
https://github.com/tailix/libshmemq.git
synced 2025-02-10 15:36:43 -05:00
Add test "prod_jumps_to_buffer_start_and_pushes_too_long_message"
This commit is contained in:
parent
21b2f57bd7
commit
b3ac161fc6
5 changed files with 80 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -39,5 +39,6 @@
|
|||
/tests/test_main
|
||||
/tests/test_cons_pops_buffer_start
|
||||
/tests/test_cons_reaches_queue_end
|
||||
/tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message
|
||||
/tests/test_prod_jumps_to_buffer_start_and_reaches_cons
|
||||
/tests/test_prod_reaches_buffer_end
|
||||
|
|
|
@ -12,6 +12,7 @@ TESTS = \
|
|||
tests/test_main \
|
||||
tests/test_cons_pops_buffer_start \
|
||||
tests/test_cons_reaches_queue_end \
|
||||
tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message \
|
||||
tests/test_prod_jumps_to_buffer_start_and_reaches_cons \
|
||||
tests/test_prod_reaches_buffer_end
|
||||
|
||||
|
@ -42,6 +43,10 @@ tests_test_cons_reaches_queue_end_SOURCES = \
|
|||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_cons_reaches_queue_end.c
|
||||
|
||||
tests_test_prod_jumps_to_buffer_start_and_pushes_too_long_message_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_prod_jumps_to_buffer_start_and_pushes_too_long_message.c
|
||||
|
||||
tests_test_prod_jumps_to_buffer_start_and_reaches_cons_SOURCES = \
|
||||
$(libshmemq_a_SOURCES) \
|
||||
tests/test_prod_jumps_to_buffer_start_and_reaches_cons.c
|
||||
|
|
|
@ -29,6 +29,7 @@ typedef enum ShmemqError {
|
|||
// Bugs in user code.
|
||||
SHMEMQ_ERROR_BUG_POP_END_ON_EMPTY_QUEUE = 50,
|
||||
SHMEMQ_ERROR_BUG_PUSH_END_ON_FULL_QUEUE = 51,
|
||||
SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW = 52,
|
||||
|
||||
// Failed system calls.
|
||||
SHMEMQ_ERROR_FAILED_MALLOC = 100,
|
||||
|
|
14
src/main.c
14
src/main.c
|
@ -217,9 +217,21 @@ void shmemq_push_end(
|
|||
header_and_data_size / SHMEMQ_FRAME_SIZE + 1;
|
||||
}
|
||||
|
||||
shmemq->buffer->header.write_frame_index =
|
||||
const size_t new_write_frame_index =
|
||||
shmemq->buffer->header.write_frame_index +
|
||||
frame->header.message_frames_count;
|
||||
|
||||
if (
|
||||
shmemq->buffer->header.write_frame_index <
|
||||
shmemq->buffer->header.read_frame_index &&
|
||||
new_write_frame_index >=
|
||||
shmemq->buffer->header.read_frame_index
|
||||
) {
|
||||
if (error_ptr) *error_ptr = SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW;
|
||||
return;
|
||||
}
|
||||
|
||||
shmemq->buffer->header.write_frame_index = new_write_frame_index;
|
||||
}
|
||||
|
||||
ShmemqFrame shmemq_pop_start(const Shmemq shmemq)
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
#include <shmemq.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static const char name[] = "/foobar";
|
||||
|
||||
int main()
|
||||
{
|
||||
ShmemqError error;
|
||||
|
||||
const Shmemq consumer = shmemq_new(name, true, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
const Shmemq producer = shmemq_new(name, false, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
for (unsigned i = 0; i < 100; ++i) {
|
||||
const ShmemqFrame frame = shmemq_push_start(producer);
|
||||
assert(frame != NULL);
|
||||
|
||||
*(unsigned*)frame->data = i;
|
||||
|
||||
shmemq_push_end(producer, sizeof(unsigned), &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < 10; ++i) {
|
||||
const ShmemqFrame frame = shmemq_pop_start(consumer);
|
||||
assert(frame != NULL);
|
||||
|
||||
assert(*(unsigned*)frame->data == i);
|
||||
|
||||
shmemq_pop_end(consumer, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < 8; ++i) {
|
||||
const ShmemqFrame frame = shmemq_push_start(producer);
|
||||
assert(frame != NULL);
|
||||
|
||||
*(unsigned*)frame->data = i;
|
||||
|
||||
shmemq_push_end(producer, sizeof(unsigned), &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
}
|
||||
|
||||
const ShmemqFrame frame = shmemq_push_start(producer);
|
||||
assert(frame != NULL);
|
||||
|
||||
shmemq_push_end(producer, 9, &error);
|
||||
assert(error == SHMEMQ_ERROR_BUG_PUSH_END_OVERFLOW);
|
||||
|
||||
shmemq_delete(consumer, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
shmemq_delete(producer, &error);
|
||||
assert(error == SHMEMQ_ERROR_NONE);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue