Add a simple FIFO unit test that test wrap around and push, pop and
peek for both fifo8 and fifo32.
Signed-off-by: Octavian Purdila <tavip@google.com>
---
tests/unit/meson.build | 1 +
tests/unit/test-fifo.c | 98 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 99 insertions(+)
create mode 100644 tests/unit/test-fifo.c
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 26c109c968..397f2503f8 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..1686f8bd59
--- /dev/null
+++ b/tests/unit/test-fifo.c
@@ -0,0 +1,98 @@
+/*
+ * QEMU FIFO testing
+ *
+ * Copyright (C) 2024 Google LLC
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qemu/fifo8.h"
+#include "qemu/fifo32.h"
+
+typedef struct {
+ Fifo8 fifo8;
+ Fifo32 fifo32;
+} TestFixture;
+
+#define FIFO_SIZE 13
+
+/*
+ * Test fixture initialization.
+ */
+static void set_up(TestFixture *f, gconstpointer data)
+{
+ int n = (uintptr_t) data;
+
+ fifo8_create(&f->fifo8, n);
+ fifo32_create(&f->fifo32, n);
+}
+
+static void tear_down(TestFixture *f, gconstpointer user_data)
+{
+ fifo8_destroy(&f->fifo8);
+ fifo32_destroy(&f->fifo32);
+}
+
+static void test_push_pop_batch(TestFixture *f, int n)
+{
+ uint8_t i;
+
+ /* push and check peek */
+ for (i = 0; i < n; i++) {
+ uint8_t val8 = i;
+ uint32_t val32 = i | ((i + 1) << 8) | ((i + 2) << 16) | ((i + 3) << 24);
+
+ fifo8_push(&f->fifo8, val8);
+ if (i == 0) {
+ g_assert(*fifo8_peek_buf(&f->fifo8, 1, NULL) == val8);
+ }
+
+ fifo32_push(&f->fifo32, val32);
+ if (i == 0) {
+ g_assert(fifo32_peek(&f->fifo32) == val32);
+ }
+ }
+
+ /* check peek and pop */
+ for (i = 0; i < n; i++) {
+ uint8_t val8 = i;
+ uint32_t val32 = i | ((i + 1) << 8) | ((i + 2) << 16) | ((i + 3) << 24);
+
+ g_assert(*fifo8_peek_buf(&f->fifo8, 1, NULL) == val8);
+ g_assert(fifo8_pop(&f->fifo8) == val8);
+
+ g_assert(fifo32_peek(&f->fifo32) == val32);
+ g_assert(fifo32_pop(&f->fifo32) == val32);
+ }
+}
+
+/* max n should be less then 256 - 3 */
+static void wrap_around_test(TestFixture *f, gconstpointer user_data)
+{
+ int n = (uintptr_t) user_data;
+ const int cycles = 3;
+ int i;
+
+ for (i = 0; i < cycles; i++) {
+ test_push_pop_batch(f, n / 2 + 1);
+ }
+}
+
+/* mock-ups */
+void *vmstate_info_buffer;
+uint32_t vmstate_info_uint32;
+
+int main(int argc, char **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add("/fifo/wrap-around", TestFixture, (gconstpointer)FIFO_SIZE,
+ set_up, wrap_around_test, tear_down);
+
+ return g_test_run();
+}
--
2.46.0.rc2.264.g509ed76dc8-goog