From faf9bb5073f1a07b75e9d49b9774241a1c0d5de5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= <rene.kijewski@fu-berlin.de>
Date: Wed, 9 Jul 2014 18:09:37 +0200
Subject: [PATCH] tests: add ringbuffer unittest

---
 tests/unittests/tests-lib/Makefile            |   1 +
 tests/unittests/tests-lib/Makefile.include    |   1 +
 .../tests-lib/tests-lib-ringbuffer.c          | 127 ++++++++++++++++++
 tests/unittests/tests-lib/tests-lib.c         |  14 ++
 tests/unittests/tests-lib/tests-lib.h         |  37 +++++
 5 files changed, 180 insertions(+)
 create mode 100644 tests/unittests/tests-lib/Makefile
 create mode 100644 tests/unittests/tests-lib/Makefile.include
 create mode 100644 tests/unittests/tests-lib/tests-lib-ringbuffer.c
 create mode 100644 tests/unittests/tests-lib/tests-lib.c
 create mode 100644 tests/unittests/tests-lib/tests-lib.h

diff --git a/tests/unittests/tests-lib/Makefile b/tests/unittests/tests-lib/Makefile
new file mode 100644
index 0000000000..48422e909a
--- /dev/null
+++ b/tests/unittests/tests-lib/Makefile
@@ -0,0 +1 @@
+include $(RIOTBASE)/Makefile.base
diff --git a/tests/unittests/tests-lib/Makefile.include b/tests/unittests/tests-lib/Makefile.include
new file mode 100644
index 0000000000..6ba533d1b3
--- /dev/null
+++ b/tests/unittests/tests-lib/Makefile.include
@@ -0,0 +1 @@
+USEMODULE += lib
diff --git a/tests/unittests/tests-lib/tests-lib-ringbuffer.c b/tests/unittests/tests-lib/tests-lib-ringbuffer.c
new file mode 100644
index 0000000000..b4a42e28da
--- /dev/null
+++ b/tests/unittests/tests-lib/tests-lib-ringbuffer.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2014  RenĂŠ Kijewski  <rene.kijewski@fu-berlin.de>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "thread.h"
+#include "flags.h"
+#include "kernel.h"
+#include "ringbuffer.h"
+#include "mutex.h"
+
+#include "tests-lib.h"
+
+/* (ITERATIONS * (BUF_SIZE + 1)) needs to be <= 127! Otherwise `char` overflows. */
+#define ITERATIONS 15
+#define BUF_SIZE 7
+
+static char stack_get[KERNEL_CONF_STACKSIZE_DEFAULT];
+
+static char rb_buf[BUF_SIZE];
+static ringbuffer_t rb = RINGBUFFER_INIT(rb_buf);
+static mutex_t mutex;
+static unsigned pid_add, pid_get;
+
+static void assert_avail(unsigned assumed)
+{
+    TEST_ASSERT_EQUAL_INT(assumed, rb.avail);
+}
+
+static void assert_add_one(char to_add, int assumed_result)
+{
+    int actual_result = ringbuffer_add_one(&rb, to_add);
+    TEST_ASSERT_EQUAL_INT(assumed_result, actual_result);
+}
+
+static void assert_get_one(int assumed_result)
+{
+    int actual_result = ringbuffer_get_one(&rb);
+    TEST_ASSERT_EQUAL_INT(assumed_result, actual_result);
+}
+
+static void run_add(void)
+{
+    char next = 0;
+    for (unsigned iteration = 0; iteration < ITERATIONS; ++iteration) {
+        mutex_lock(&mutex);
+
+        for (unsigned i = 0; i < BUF_SIZE; ++i) {
+            assert_avail(i);
+            assert_add_one(next, -1);
+            assert_avail(i + 1);
+            ++next;
+        }
+
+        /* Overwrite oldest element. It should be returned to us. */
+        assert_avail(BUF_SIZE);
+        assert_add_one(next, next - BUF_SIZE);
+        assert_avail(BUF_SIZE);
+        ++next;
+
+        thread_wakeup(pid_get);
+        mutex_unlock_and_sleep(&mutex);
+    }
+
+    thread_wakeup(pid_get);
+}
+
+static void *run_get(void *arg)
+{
+    (void) arg;
+
+    char next = 0;
+    for (unsigned iteration = 0; iteration < ITERATIONS; ++iteration) {
+        ++next; /* the first element of a stride is always overwritten */
+
+        mutex_lock(&mutex);
+
+        for (unsigned i = BUF_SIZE; i > 0; --i) {
+            assert_avail(i);
+            assert_get_one(next);
+            assert_avail(i - 1);
+            ++next;
+        }
+
+        assert_avail(0);
+        assert_get_one(-1);
+        assert_avail(0);
+
+        thread_wakeup(pid_add);
+        mutex_unlock_and_sleep(&mutex);
+    }
+
+    return NULL;
+}
+
+static void tests_lib_ringbuffer(void)
+{
+    pid_add = sched_active_pid;
+    pid_get = thread_create(stack_get, sizeof (stack_get),
+                            PRIORITY_MAIN, CREATE_SLEEPING | CREATE_STACKTEST,
+                            run_get, NULL, "get");
+    run_add();
+}
+
+Test *tests_lib_ringbuffer_tests(void)
+{
+    EMB_UNIT_TESTFIXTURES(fixtures) {
+        new_TestFixture(tests_lib_ringbuffer),
+    };
+
+    EMB_UNIT_TESTCALLER(ringbuffer_tests, NULL, NULL, fixtures);
+
+    return (Test *)&ringbuffer_tests;
+}
diff --git a/tests/unittests/tests-lib/tests-lib.c b/tests/unittests/tests-lib/tests-lib.c
new file mode 100644
index 0000000000..0fb20872a4
--- /dev/null
+++ b/tests/unittests/tests-lib/tests-lib.c
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) 2014 RenĂŠ Kijewski
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License. See the file LICENSE in the top level directory for more
+ * details.
+ */
+
+#include "tests-lib.h"
+
+void tests_lib(void)
+{
+    TESTS_RUN(tests_lib_ringbuffer_tests());
+}
diff --git a/tests/unittests/tests-lib/tests-lib.h b/tests/unittests/tests-lib/tests-lib.h
new file mode 100644
index 0000000000..0644e49a42
--- /dev/null
+++ b/tests/unittests/tests-lib/tests-lib.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2014 RenĂŠ Kijewski
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License. See the file LICENSE in the top level directory for more
+ * details.
+ */
+
+/**
+ * @addtogroup  unittests
+ * @{
+ *
+ * @file        tests-lib.h
+ * @brief       Unittests for the ``lib`` sysmodule
+ *
+ * @author      Freie Universität Berlin, Computer Systems & Telematics
+ * @author      RenĂŠ Kijewski <rene.kijewski@fu-berlin.de>
+ */
+#ifndef __TESTS_CORE_H_
+#define __TESTS_CORE_H_
+
+#include "../unittests.h"
+
+/**
+ * @brief   The entry point of this test suite.
+ */
+void tests_lib(void);
+
+/**
+ * @brief   Generates tests ringbuffer.h
+ *
+ * @return  embUnit tests if successful, NULL if not.
+ */
+Test *tests_lib_ringbuffer_tests(void);
+
+#endif /* __TESTS_CORE_H_ */
+/** @} */
-- 
GitLab