From f1e742dcf4e8fb14f49e791705d7e02053cc8dba Mon Sep 17 00:00:00 2001
From: Kaspar Schleiser <kaspar@schleiser.de>
Date: Thu, 8 Oct 2015 13:45:21 +0200
Subject: [PATCH] tests: unittests: add unittests for div.h

---
 tests/unittests/tests-div/Makefile    |   1 +
 tests/unittests/tests-div/tests-div.c | 120 ++++++++++++++++++++++++++
 tests/unittests/tests-div/tests-div.h |  43 +++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 tests/unittests/tests-div/Makefile
 create mode 100644 tests/unittests/tests-div/tests-div.c
 create mode 100644 tests/unittests/tests-div/tests-div.h

diff --git a/tests/unittests/tests-div/Makefile b/tests/unittests/tests-div/Makefile
new file mode 100644
index 0000000000..48422e909a
--- /dev/null
+++ b/tests/unittests/tests-div/Makefile
@@ -0,0 +1 @@
+include $(RIOTBASE)/Makefile.base
diff --git a/tests/unittests/tests-div/tests-div.c b/tests/unittests/tests-div/tests-div.c
new file mode 100644
index 0000000000..824d1952a3
--- /dev/null
+++ b/tests/unittests/tests-div/tests-div.c
@@ -0,0 +1,120 @@
+/*
+* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
+*
+* This file is subject to the terms and conditions of the GNU Lesser
+* General Public License v2.1. See the file LICENSE in the top level
+* directory for more details.
+*/
+
+#include <string.h>
+#include "embUnit.h"
+#include "tests-div.h"
+
+#include "div.h"
+
+#define ENABLE_DEBUG (0)
+#include "debug.h"
+
+static uint32_t u32_test_values[] = {
+    0,
+    1,
+    10,
+    32,
+    15625,
+    15625LU*5,
+    (15625LU*5)+1,
+    0xffff,
+    0xffff<<10,
+    0xffffffff
+};
+
+static uint64_t u64_test_values[] = {
+    0xffffffffULL+1
+};
+
+#define N_U32_VALS (sizeof(u32_test_values)/sizeof(uint32_t))
+#define N_U64_VALS (sizeof(u64_test_values)/sizeof(uint64_t))
+
+static void test_div_u64_by_15625(void)
+{
+    for (unsigned i = 0; i < N_U32_VALS; i++) {
+        DEBUG("Dividing %"PRIu32" by 15625...\n", u32_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u64_by_15625(u32_test_values[i]),
+                u32_test_values[i]/15625);
+    }
+
+    for (unsigned i = 0; i < N_U64_VALS; i++) {
+        DEBUG("Dividing %"PRIu64" by 15625...\n", u64_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u64_by_15625(u64_test_values[i]),
+                u64_test_values[i]/15625);
+    }
+}
+
+static void test_div_u32_by_15625div512(void)
+{
+    for (unsigned i = 0; i < N_U32_VALS; i++) {
+        DEBUG("Dividing %"PRIu32" by (15625/512)...\n", u32_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u32_by_15625div512(u32_test_values[i]),
+                (uint64_t)u32_test_values[i]*512LU/15625);
+    }
+}
+
+static void test_div_u64_by_1000000(void)
+{
+    for (unsigned i = 0; i < N_U32_VALS; i++) {
+        DEBUG("Dividing %"PRIu32" by 1000000...\n", u32_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u64_by_1000000(u32_test_values[i]),
+                u32_test_values[i]/1000000);
+    }
+
+    for (unsigned i = 0; i < N_U64_VALS; i++) {
+        DEBUG("Dividing %"PRIu64" by 1000000...\n", u64_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u64_by_1000000(u64_test_values[i]),
+                u64_test_values[i]/1000000U);
+    }
+}
+
+static void test_div_u32_by_10(void)
+{
+    for (unsigned i = 0; i < N_U32_VALS; i++) {
+        DEBUG("Dividing %"PRIu32" by 10...\n", u32_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u32_by_10(u32_test_values[i]),
+                u32_test_values[i]/10);
+    }
+}
+
+static void test_div_u32_mod_10(void)
+{
+    for (unsigned i = 0; i < N_U32_VALS; i++) {
+        DEBUG("Calculating %"PRIu32" % 10...\n", u32_test_values[i]);
+        TEST_ASSERT_EQUAL_INT(
+                div_u32_mod_10(u32_test_values[i]),
+                u32_test_values[i]%10);
+    }
+}
+
+Test *tests_div_tests(void)
+{
+    EMB_UNIT_TESTFIXTURES(fixtures) {
+        new_TestFixture(test_div_u64_by_15625),
+        new_TestFixture(test_div_u32_by_15625div512),
+        new_TestFixture(test_div_u64_by_1000000),
+        new_TestFixture(test_div_u32_by_10),
+        new_TestFixture(test_div_u32_mod_10),
+    };
+
+    EMB_UNIT_TESTCALLER(div_tests, NULL, NULL, fixtures);
+
+    return (Test *)&div_tests;
+}
+
+void tests_div(void)
+{
+    TESTS_RUN(tests_div_tests());
+}
diff --git a/tests/unittests/tests-div/tests-div.h b/tests/unittests/tests-div/tests-div.h
new file mode 100644
index 0000000000..037ee6b021
--- /dev/null
+++ b/tests/unittests/tests-div/tests-div.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @addtogroup  unittests
+ * @{
+ *
+ * @file
+ * @brief       Unittests for the ``div`` header
+ *
+ * @author      Kaspar Schleiser <kaspar@schleiser.de>
+ */
+#ifndef TESTS_DIV_H_
+#define TESTS_DIV_H_
+#include "embUnit/embUnit.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+*  @brief   The entry point of this test suite.
+*/
+void tests_div(void);
+
+/**
+ * @brief   Generates tests for div
+ *
+ * @return  embUnit tests if successful, NULL if not.
+ */
+Test *tests_div_tests(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TESTS_DIV_H_ */
+/** @} */
-- 
GitLab