From 0610c2091476d5488a2345d000ff907d01de3af9 Mon Sep 17 00:00:00 2001
From: Hauke Petersen <hauke.petersen@fu-berlin.de>
Date: Tue, 29 Mar 2016 15:20:37 +0200
Subject: [PATCH] tests: added unittests for fmt_s16_[dfp|dec]()

---
 tests/unittests/tests-fmt/tests-fmt.c | 91 +++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/tests/unittests/tests-fmt/tests-fmt.c b/tests/unittests/tests-fmt/tests-fmt.c
index 6c88b114a7..6e9a4f1f9b 100644
--- a/tests/unittests/tests-fmt/tests-fmt.c
+++ b/tests/unittests/tests-fmt/tests-fmt.c
@@ -120,6 +120,95 @@ static void test_fmt_s32_dec(void)
     TEST_ASSERT_EQUAL_STRING("-9876", (char *) out);
 }
 
+static void test_rmt_s16_dec(void)
+{
+    char out[7] = "-------";
+    int16_t val;
+    size_t len;
+
+    val = 0;
+    len = fmt_s16_dec(out, val);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(1, len);
+    TEST_ASSERT_EQUAL_STRING("0", (char *)out);
+
+    val = -32000;
+    len = fmt_s16_dec(out, val);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(6, len);
+    TEST_ASSERT_EQUAL_STRING("-32000", (char *)out);
+
+    val = 12345;
+    len = fmt_s16_dec(out, val);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(5, len);
+    TEST_ASSERT_EQUAL_STRING("12345", (char *)out);
+}
+
+static void test_rmt_s16_dfp(void)
+{
+    char out[8] = "--------";
+    int16_t val;
+    unsigned fpp;
+    size_t len;
+
+    val = 0;
+    fpp = 3;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(5, len);
+    TEST_ASSERT_EQUAL_STRING("0.000", (char *)out);
+
+    val = 12345;
+    fpp = 4;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(6, len);
+    TEST_ASSERT_EQUAL_STRING("1.2345", (char *)out);
+
+    val = 12030;
+    fpp = 3;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(6, len);
+    TEST_ASSERT_EQUAL_STRING("12.030", (char *)out);
+
+    val = -3548;
+    fpp = 2;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(6, len);
+    TEST_ASSERT_EQUAL_STRING("-35.48", (char *)out);
+
+    val = -23;
+    fpp = 4;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(7, len);
+    TEST_ASSERT_EQUAL_STRING("-0.0023", (char *)out);
+
+    val = 50;
+    fpp = 3;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(5, len);
+    TEST_ASSERT_EQUAL_STRING("0.050", (char *)out);
+
+    val = -12345;
+    fpp = 0;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(6, len);
+    TEST_ASSERT_EQUAL_STRING("-12345", (char *)out);
+
+    val = 31987;
+    fpp = 5;
+    len = fmt_s16_dfp(out, val, fpp);
+    out[len] = '\0';
+    TEST_ASSERT_EQUAL_INT(0, len);
+    TEST_ASSERT_EQUAL_STRING("", (char *)out);
+}
+
 static void test_fmt_strlen(void)
 {
     const char *empty_str = "";
@@ -160,6 +249,8 @@ Test *tests_fmt_tests(void)
         new_TestFixture(test_fmt_u32_dec),
         new_TestFixture(test_fmt_u16_dec),
         new_TestFixture(test_fmt_s32_dec),
+        new_TestFixture(test_rmt_s16_dec),
+        new_TestFixture(test_rmt_s16_dfp),
         new_TestFixture(test_fmt_strlen),
         new_TestFixture(test_fmt_str),
         new_TestFixture(test_scn_u32_dec),
-- 
GitLab