Message ID | 20240828122258.928947-10-mark.cave-ayland@ilande.co.uk |
---|---|
State | New |
Headers | show |
Series | fifo8: add fifo8_peek(), fifo8_peek_buf() and tests | expand |
On Wed, Aug 28, 2024 at 10:25 PM Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote: > > This tests the Fifo8 implementation for basic operations as well as testing for > the correct *_bufptr() including handling wraparound of the internal FIFO buffer. > > Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> Acked-by: Alistair Francis <alistair.francis@wdc.com> Alistair > --- > tests/unit/meson.build | 1 + > tests/unit/test-fifo.c | 256 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 257 insertions(+) > create mode 100644 tests/unit/test-fifo.c > > diff --git a/tests/unit/meson.build b/tests/unit/meson.build > index 490ab8182d..89f9633cd6 100644 > --- a/tests/unit/meson.build > +++ b/tests/unit/meson.build > @@ -47,6 +47,7 @@ tests = { > 'test-logging': [], > 'test-qapi-util': [], > 'test-interval-tree': [], > + 'test-fifo': [], > } > > if have_system or have_tools > diff --git a/tests/unit/test-fifo.c b/tests/unit/test-fifo.c > new file mode 100644 > index 0000000000..1e54cde871 > --- /dev/null > +++ b/tests/unit/test-fifo.c > @@ -0,0 +1,256 @@ > +/* > + * Fifo8 tests > + * > + * Copyright 2024 Mark Cave-Ayland > + * > + * Authors: > + * Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or later. > + * See the COPYING.LIB file in the top-level directory. > + */ > + > +#include "qemu/osdep.h" > +#include "migration/vmstate.h" > +#include "qemu/fifo8.h" > + > +const VMStateInfo vmstate_info_uint32; > +const VMStateInfo vmstate_info_buffer; > + > + > +static void test_fifo8_pop_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + buf = fifo8_pop_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + > + buf = fifo8_peek_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0xff && data_out[1] == 0xff && > + data_out[2] == 0xff && data_out[3] == 0xff); > + > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_peek(&fifo); > + g_assert(c == 0x1); > + fifo8_pop(&fifo); > + c = fifo8_peek(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 1); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pushpop(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_pop(&fifo); > + g_assert(c == 0x1); > + c = fifo8_pop(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +int main(int argc, char *argv[]) > +{ > + g_test_init(&argc, &argv, NULL); > + g_test_add_func("/fifo8/pushpop", test_fifo8_pushpop); > + g_test_add_func("/fifo8/peek", test_fifo8_peek); > + g_test_add_func("/fifo8/peek_buf", test_fifo8_peek_buf); > + g_test_add_func("/fifo8/peek_buf_wrap", test_fifo8_peek_buf_wrap); > + g_test_add_func("/fifo8/pop_buf", test_fifo8_pop_buf); > + g_test_add_func("/fifo8/pop_buf_wrap", test_fifo8_pop_buf_wrap); > + g_test_add_func("/fifo8/peek_bufptr", test_fifo8_peek_bufptr); > + g_test_add_func("/fifo8/peek_bufptr_wrap", test_fifo8_peek_bufptr_wrap); > + g_test_add_func("/fifo8/pop_bufptr", test_fifo8_pop_bufptr); > + g_test_add_func("/fifo8/pop_bufptr_wrap", test_fifo8_pop_bufptr_wrap); > + return g_test_run(); > +} > -- > 2.39.2 > >
On Wed, Aug 28, 2024 at 5:23 AM Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> wrote: > > This tests the Fifo8 implementation for basic operations as well as testing for > the correct *_bufptr() including handling wraparound of the internal FIFO buffer. > > Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> Reviewed-by: Octavian Purdila <tavip@google.com> > --- > tests/unit/meson.build | 1 + > tests/unit/test-fifo.c | 256 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 257 insertions(+) > create mode 100644 tests/unit/test-fifo.c > > diff --git a/tests/unit/meson.build b/tests/unit/meson.build > index 490ab8182d..89f9633cd6 100644 > --- a/tests/unit/meson.build > +++ b/tests/unit/meson.build > @@ -47,6 +47,7 @@ tests = { > 'test-logging': [], > 'test-qapi-util': [], > 'test-interval-tree': [], > + 'test-fifo': [], > } > > if have_system or have_tools > diff --git a/tests/unit/test-fifo.c b/tests/unit/test-fifo.c > new file mode 100644 > index 0000000000..1e54cde871 > --- /dev/null > +++ b/tests/unit/test-fifo.c > @@ -0,0 +1,256 @@ > +/* > + * Fifo8 tests > + * > + * Copyright 2024 Mark Cave-Ayland > + * > + * Authors: > + * Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or later. > + * See the COPYING.LIB file in the top-level directory. > + */ > + > +#include "qemu/osdep.h" > +#include "migration/vmstate.h" > +#include "qemu/fifo8.h" > + > +const VMStateInfo vmstate_info_uint32; > +const VMStateInfo vmstate_info_buffer; > + > + > +static void test_fifo8_pop_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + buf = fifo8_pop_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + > + buf = fifo8_peek_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > +with known values. > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; Initialize data_out. > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; Initialize data_out. > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0xff && data_out[1] == 0xff && > + data_out[2] == 0xff && data_out[3] == 0xff); > + > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_peek(&fifo); > + g_assert(c == 0x1); > + fifo8_pop(&fifo); > + c = fifo8_peek(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 1); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pushpop(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_pop(&fifo); > + g_assert(c == 0x1); > + c = fifo8_pop(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +int main(int argc, char *argv[]) > +{ > + g_test_init(&argc, &argv, NULL); > + g_test_add_func("/fifo8/pushpop", test_fifo8_pushpop); > + g_test_add_func("/fifo8/peek", test_fifo8_peek); > + g_test_add_func("/fifo8/peek_buf", test_fifo8_peek_buf); > + g_test_add_func("/fifo8/peek_buf_wrap", test_fifo8_peek_buf_wrap); > + g_test_add_func("/fifo8/pop_buf", test_fifo8_pop_buf); > + g_test_add_func("/fifo8/pop_buf_wrap", test_fifo8_pop_buf_wrap); > + g_test_add_func("/fifo8/peek_bufptr", test_fifo8_peek_bufptr); > + g_test_add_func("/fifo8/peek_bufptr_wrap", test_fifo8_peek_bufptr_wrap); > + g_test_add_func("/fifo8/pop_bufptr", test_fifo8_pop_bufptr); > + g_test_add_func("/fifo8/pop_bufptr_wrap", test_fifo8_pop_bufptr_wrap); > + return g_test_run(); > +} > -- > 2.39.2 >
On 28/08/2024 13:22, Mark Cave-Ayland wrote: > This tests the Fifo8 implementation for basic operations as well as testing for > the correct *_bufptr() including handling wraparound of the internal FIFO buffer. Hmmm this doesn't quite read correctly either - I think perhaps something like: This tests the Fifo8 implementation basic operations as well as testing the *_bufptr() in-place buffer functions and the newer *_buf() functions that also handle wraparound of the internal FIFO buffer. > Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> > --- > tests/unit/meson.build | 1 + > tests/unit/test-fifo.c | 256 +++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 257 insertions(+) > create mode 100644 tests/unit/test-fifo.c > > diff --git a/tests/unit/meson.build b/tests/unit/meson.build > index 490ab8182d..89f9633cd6 100644 > --- a/tests/unit/meson.build > +++ b/tests/unit/meson.build > @@ -47,6 +47,7 @@ tests = { > 'test-logging': [], > 'test-qapi-util': [], > 'test-interval-tree': [], > + 'test-fifo': [], > } > > if have_system or have_tools > diff --git a/tests/unit/test-fifo.c b/tests/unit/test-fifo.c > new file mode 100644 > index 0000000000..1e54cde871 > --- /dev/null > +++ b/tests/unit/test-fifo.c > @@ -0,0 +1,256 @@ > +/* > + * Fifo8 tests > + * > + * Copyright 2024 Mark Cave-Ayland > + * > + * Authors: > + * Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> > + * > + * This work is licensed under the terms of the GNU LGPL, version 2 or later. > + * See the COPYING.LIB file in the top-level directory. Regarding your comment on using SPDX is it also worth replacing the above two lines with: * SPDX-License-Identifier: GPL-2.0-or-later ? > + */ > + > +#include "qemu/osdep.h" > +#include "migration/vmstate.h" > +#include "qemu/fifo8.h" > + > +const VMStateInfo vmstate_info_uint32; > +const VMStateInfo vmstate_info_buffer; > + > + > +static void test_fifo8_pop_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + buf = fifo8_pop_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 2); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + buf = fifo8_pop_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + > + buf = fifo8_peek_bufptr(&fifo, 8, &count); > + g_assert(count == 6); > + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && > + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_bufptr(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + const uint8_t *buf; > + uint32_t count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + buf = fifo8_peek_bufptr(&fifo, 2, &count); > + g_assert(count == 2); > + g_assert(buf[0] == 0x1 && buf[1] == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pop_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_pop_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_pop_buf(&fifo, data_out, 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf_wrap(void) > +{ > + Fifo8 fifo; > + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[4]; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); > + fifo8_pop_buf(&fifo, NULL, 4); > + > + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && > + data_out[2] == 0x7 && data_out[3] == 0x8); > + > + g_assert(fifo8_num_used(&fifo) == 8); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek_buf(void) > +{ > + Fifo8 fifo; > + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; > + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; > + int count; > + > + fifo8_create(&fifo, 8); > + > + fifo8_push_all(&fifo, data_in, sizeof(data_in)); > + count = fifo8_peek_buf(&fifo, NULL, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0xff && data_out[1] == 0xff && > + data_out[2] == 0xff && data_out[3] == 0xff); > + > + count = fifo8_peek_buf(&fifo, data_out, 4); > + g_assert(count == 4); > + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && > + data_out[2] == 0x3 && data_out[3] == 0x4); > + > + g_assert(fifo8_num_used(&fifo) == 4); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_peek(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_peek(&fifo); > + g_assert(c == 0x1); > + fifo8_pop(&fifo); > + c = fifo8_peek(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 1); > + fifo8_destroy(&fifo); > +} > + > +static void test_fifo8_pushpop(void) > +{ > + Fifo8 fifo; > + uint8_t c; > + > + fifo8_create(&fifo, 8); > + fifo8_push(&fifo, 0x1); > + fifo8_push(&fifo, 0x2); > + > + c = fifo8_pop(&fifo); > + g_assert(c == 0x1); > + c = fifo8_pop(&fifo); > + g_assert(c == 0x2); > + > + g_assert(fifo8_num_used(&fifo) == 0); > + fifo8_destroy(&fifo); > +} > + > +int main(int argc, char *argv[]) > +{ > + g_test_init(&argc, &argv, NULL); > + g_test_add_func("/fifo8/pushpop", test_fifo8_pushpop); > + g_test_add_func("/fifo8/peek", test_fifo8_peek); > + g_test_add_func("/fifo8/peek_buf", test_fifo8_peek_buf); > + g_test_add_func("/fifo8/peek_buf_wrap", test_fifo8_peek_buf_wrap); > + g_test_add_func("/fifo8/pop_buf", test_fifo8_pop_buf); > + g_test_add_func("/fifo8/pop_buf_wrap", test_fifo8_pop_buf_wrap); > + g_test_add_func("/fifo8/peek_bufptr", test_fifo8_peek_bufptr); > + g_test_add_func("/fifo8/peek_bufptr_wrap", test_fifo8_peek_bufptr_wrap); > + g_test_add_func("/fifo8/pop_bufptr", test_fifo8_pop_bufptr); > + g_test_add_func("/fifo8/pop_bufptr_wrap", test_fifo8_pop_bufptr_wrap); > + return g_test_run(); > +} ATB, Mark.
diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 490ab8182d..89f9633cd6 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -47,6 +47,7 @@ tests = { 'test-logging': [], 'test-qapi-util': [], 'test-interval-tree': [], + 'test-fifo': [], } if have_system or have_tools diff --git a/tests/unit/test-fifo.c b/tests/unit/test-fifo.c new file mode 100644 index 0000000000..1e54cde871 --- /dev/null +++ b/tests/unit/test-fifo.c @@ -0,0 +1,256 @@ +/* + * Fifo8 tests + * + * Copyright 2024 Mark Cave-Ayland + * + * Authors: + * Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> + * + * This work is licensed under the terms of the GNU LGPL, version 2 or later. + * See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "migration/vmstate.h" +#include "qemu/fifo8.h" + +const VMStateInfo vmstate_info_uint32; +const VMStateInfo vmstate_info_buffer; + + +static void test_fifo8_pop_bufptr_wrap(void) +{ + Fifo8 fifo; + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; + const uint8_t *buf; + uint32_t count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); + buf = fifo8_pop_bufptr(&fifo, 2, &count); + g_assert(count == 2); + g_assert(buf[0] == 0x1 && buf[1] == 0x2); + + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); + buf = fifo8_pop_bufptr(&fifo, 8, &count); + g_assert(count == 6); + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); + + g_assert(fifo8_num_used(&fifo) == 2); + fifo8_destroy(&fifo); +} + +static void test_fifo8_pop_bufptr(void) +{ + Fifo8 fifo; + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; + const uint8_t *buf; + uint32_t count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in, sizeof(data_in)); + buf = fifo8_pop_bufptr(&fifo, 2, &count); + g_assert(count == 2); + g_assert(buf[0] == 0x1 && buf[1] == 0x2); + + g_assert(fifo8_num_used(&fifo) == 2); + fifo8_destroy(&fifo); +} + +static void test_fifo8_peek_bufptr_wrap(void) +{ + Fifo8 fifo; + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2 }; + const uint8_t *buf; + uint32_t count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); + buf = fifo8_peek_bufptr(&fifo, 2, &count); + g_assert(count == 2); + g_assert(buf[0] == 0x1 && buf[1] == 0x2); + + buf = fifo8_pop_bufptr(&fifo, 2, &count); + g_assert(count == 2); + g_assert(buf[0] == 0x1 && buf[1] == 0x2); + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); + + buf = fifo8_peek_bufptr(&fifo, 8, &count); + g_assert(count == 6); + g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 && + buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8); + + g_assert(fifo8_num_used(&fifo) == 8); + fifo8_destroy(&fifo); +} + +static void test_fifo8_peek_bufptr(void) +{ + Fifo8 fifo; + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; + const uint8_t *buf; + uint32_t count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in, sizeof(data_in)); + buf = fifo8_peek_bufptr(&fifo, 2, &count); + g_assert(count == 2); + g_assert(buf[0] == 0x1 && buf[1] == 0x2); + + g_assert(fifo8_num_used(&fifo) == 4); + fifo8_destroy(&fifo); +} + +static void test_fifo8_pop_buf_wrap(void) +{ + Fifo8 fifo; + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_out[4]; + int count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); + fifo8_pop_buf(&fifo, NULL, 4); + + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); + count = fifo8_pop_buf(&fifo, NULL, 4); + g_assert(count == 4); + count = fifo8_pop_buf(&fifo, data_out, 4); + g_assert(count == 4); + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && + data_out[2] == 0x3 && data_out[3] == 0x4); + + g_assert(fifo8_num_used(&fifo) == 0); + fifo8_destroy(&fifo); +} + +static void test_fifo8_pop_buf(void) +{ + Fifo8 fifo; + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 }; + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; + int count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in, sizeof(data_in)); + count = fifo8_pop_buf(&fifo, NULL, 4); + g_assert(count == 4); + count = fifo8_pop_buf(&fifo, data_out, 4); + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && + data_out[2] == 0x7 && data_out[3] == 0x8); + + g_assert(fifo8_num_used(&fifo) == 0); + fifo8_destroy(&fifo); +} + +static void test_fifo8_peek_buf_wrap(void) +{ + Fifo8 fifo; + uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_out[4]; + int count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in1, sizeof(data_in1)); + fifo8_pop_buf(&fifo, NULL, 4); + + fifo8_push_all(&fifo, data_in2, sizeof(data_in2)); + count = fifo8_peek_buf(&fifo, NULL, 4); + g_assert(count == 4); + count = fifo8_peek_buf(&fifo, data_out, 4); + g_assert(count == 4); + g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 && + data_out[2] == 0x7 && data_out[3] == 0x8); + + g_assert(fifo8_num_used(&fifo) == 8); + fifo8_destroy(&fifo); +} + +static void test_fifo8_peek_buf(void) +{ + Fifo8 fifo; + uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 }; + uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff }; + int count; + + fifo8_create(&fifo, 8); + + fifo8_push_all(&fifo, data_in, sizeof(data_in)); + count = fifo8_peek_buf(&fifo, NULL, 4); + g_assert(count == 4); + g_assert(data_out[0] == 0xff && data_out[1] == 0xff && + data_out[2] == 0xff && data_out[3] == 0xff); + + count = fifo8_peek_buf(&fifo, data_out, 4); + g_assert(count == 4); + g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 && + data_out[2] == 0x3 && data_out[3] == 0x4); + + g_assert(fifo8_num_used(&fifo) == 4); + fifo8_destroy(&fifo); +} + +static void test_fifo8_peek(void) +{ + Fifo8 fifo; + uint8_t c; + + fifo8_create(&fifo, 8); + fifo8_push(&fifo, 0x1); + fifo8_push(&fifo, 0x2); + + c = fifo8_peek(&fifo); + g_assert(c == 0x1); + fifo8_pop(&fifo); + c = fifo8_peek(&fifo); + g_assert(c == 0x2); + + g_assert(fifo8_num_used(&fifo) == 1); + fifo8_destroy(&fifo); +} + +static void test_fifo8_pushpop(void) +{ + Fifo8 fifo; + uint8_t c; + + fifo8_create(&fifo, 8); + fifo8_push(&fifo, 0x1); + fifo8_push(&fifo, 0x2); + + c = fifo8_pop(&fifo); + g_assert(c == 0x1); + c = fifo8_pop(&fifo); + g_assert(c == 0x2); + + g_assert(fifo8_num_used(&fifo) == 0); + fifo8_destroy(&fifo); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + g_test_add_func("/fifo8/pushpop", test_fifo8_pushpop); + g_test_add_func("/fifo8/peek", test_fifo8_peek); + g_test_add_func("/fifo8/peek_buf", test_fifo8_peek_buf); + g_test_add_func("/fifo8/peek_buf_wrap", test_fifo8_peek_buf_wrap); + g_test_add_func("/fifo8/pop_buf", test_fifo8_pop_buf); + g_test_add_func("/fifo8/pop_buf_wrap", test_fifo8_pop_buf_wrap); + g_test_add_func("/fifo8/peek_bufptr", test_fifo8_peek_bufptr); + g_test_add_func("/fifo8/peek_bufptr_wrap", test_fifo8_peek_bufptr_wrap); + g_test_add_func("/fifo8/pop_bufptr", test_fifo8_pop_bufptr); + g_test_add_func("/fifo8/pop_bufptr_wrap", test_fifo8_pop_bufptr_wrap); + return g_test_run(); +}
This tests the Fifo8 implementation for basic operations as well as testing for the correct *_bufptr() including handling wraparound of the internal FIFO buffer. Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk> --- tests/unit/meson.build | 1 + tests/unit/test-fifo.c | 256 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 tests/unit/test-fifo.c