From 08a2aed46db3b07ace76960ab658b6ee6cb3c5af Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 21 Apr 2016 17:54:32 +0200 Subject: [PATCH 1/2] unittests: added test for printf with floats --- tests/unittests/tests-printf_float/Makefile | 1 + .../tests-printf_float/Makefile.include | 1 + .../tests-printf_float/tests-printf_float.c | 78 +++++++++++++++++++ .../tests-printf_float/tests-printf_float.h | 38 +++++++++ 4 files changed, 118 insertions(+) create mode 100644 tests/unittests/tests-printf_float/Makefile create mode 100644 tests/unittests/tests-printf_float/Makefile.include create mode 100644 tests/unittests/tests-printf_float/tests-printf_float.c create mode 100644 tests/unittests/tests-printf_float/tests-printf_float.h diff --git a/tests/unittests/tests-printf_float/Makefile b/tests/unittests/tests-printf_float/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-printf_float/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-printf_float/Makefile.include b/tests/unittests/tests-printf_float/Makefile.include new file mode 100644 index 0000000000..4f383954c9 --- /dev/null +++ b/tests/unittests/tests-printf_float/Makefile.include @@ -0,0 +1 @@ +USEMODULE += printf_float diff --git a/tests/unittests/tests-printf_float/tests-printf_float.c b/tests/unittests/tests-printf_float/tests-printf_float.c new file mode 100644 index 0000000000..dbcb0074aa --- /dev/null +++ b/tests/unittests/tests-printf_float/tests-printf_float.c @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2016 Freie Universität Berlin + * + * 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 Implementations of unit tests for printing floating point numbers + * + * @author Hauke Petersen + * @} + */ + +#include +#include +#include + +#include "embUnit/embUnit.h" + +#include "tests-printf_float.h" + +#define BUFSIZE (10) + +static const double in0 = 2016.0349; +static const double in1 = 123.4567; +static const double in2 = 0.0; + +static void sfprintf_float(void) +{ + char tmp[BUFSIZE]; + char *str = tmp; + + snprintf(str, BUFSIZE, "%f", in0); + TEST_ASSERT_EQUAL_STRING("2016.0349", str); + + snprintf(str, BUFSIZE, "%.2f", in0); + TEST_ASSERT_EQUAL_STRING("2016.03", str); + + snprintf(str, BUFSIZE, "%.f", in0); + TEST_ASSERT_EQUAL_STRING("2016", str); + + snprintf(str, BUFSIZE, "%.4f", in1); + TEST_ASSERT_EQUAL_STRING("123.4567", str); + + snprintf(str, BUFSIZE, "%.2f", in1); + TEST_ASSERT_EQUAL_STRING("123.46", str); + + snprintf(str, BUFSIZE, "%4.f", in2); + TEST_ASSERT_EQUAL_STRING(" 0", str); + + snprintf(str, BUFSIZE, "%.3f", in2); + TEST_ASSERT_EQUAL_STRING("0.000", str); + + snprintf(str, BUFSIZE, "%2.04f", in2); + TEST_ASSERT_EQUAL_STRING("0.0000", str); +} + +Test *tests_printf_float_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(sfprintf_float) + }; + + EMB_UNIT_TESTCALLER(pkt_tests, NULL, NULL, fixtures); + + return (Test *)&pkt_tests; +} + +void tests_printf_float(void) +{ + TESTS_RUN(tests_printf_float_tests()); +} diff --git a/tests/unittests/tests-printf_float/tests-printf_float.h b/tests/unittests/tests-printf_float/tests-printf_float.h new file mode 100644 index 0000000000..01afa14dd6 --- /dev/null +++ b/tests/unittests/tests-printf_float/tests-printf_float.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2016 Freie Universität Berlin + * + * 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 Unit tests for printing floating point numbers + * + * @author Hauke Petersen + */ + +#ifndef TESTS_PRINTF_FLOAT_H_ +#define TESTS_PRINTF_FLOAT_H_ + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite + */ +void tests_printf_float(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_PRINTF_FLOAT_H_ */ +/** @} */ From 260a8ab7ad88b31d5179162a527691d77d0a978d Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Thu, 21 Apr 2016 17:55:33 +0200 Subject: [PATCH 2/2] tests: removed printf_float test application this test is covered by the unittests and not longer needed --- tests/printf_float/Makefile | 6 ----- tests/printf_float/main.c | 51 ------------------------------------- 2 files changed, 57 deletions(-) delete mode 100644 tests/printf_float/Makefile delete mode 100644 tests/printf_float/main.c diff --git a/tests/printf_float/Makefile b/tests/printf_float/Makefile deleted file mode 100644 index fa6c04a5d2..0000000000 --- a/tests/printf_float/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -APPLICATION = test_printf_float -include ../Makefile.tests_common - -USEMODULE += printf_float - -include $(RIOTBASE)/Makefile.include diff --git a/tests/printf_float/main.c b/tests/printf_float/main.c deleted file mode 100644 index 471e11bdef..0000000000 --- a/tests/printf_float/main.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2016 Alexandre Abadie - * - * 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. - */ - -/** - * @ingroup tests - * @{ - * - * @file - * @brief print floating point values test application - * - * This test is supposed to check that floating point values can be - * displayed with printf function. - * - * @author Alexandre Abadie - * - * @} - */ - -#include -#include -#include - -static const char * expected_result = "2016.0"; -static const double floating_point_value = 2016.0317; - -int main(void) -{ - const uint8_t str_len = strlen(expected_result); - char result[str_len]; - snprintf(result, str_len + 1, - "%.1f", floating_point_value); - - printf("Value displayed: %s\n", result); - - if (strcmp(result, expected_result) == 0) { - printf("[OK]\n"); - } - else { - printf("[FAILED] Values are not equal:\n" - "actual: %s\n" - "expected: %s\n", result, expected_result); - return 1; - } - - return 0; -}