1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/unittests/tests-printf_float/tests-printf_float.c
Joakim Nohlgård b55975fc0a unittests: Fix printf float test BUFSIZE
Fixes the following error from GCC 7.2.0 with a recent newlib

In file included from /usr/arm-none-eabi/include/stdio.h:800:0,
                 from /home/jgn/work/src/riot/tests/unittests/tests-printf_float/tests-printf_float.c:21:
/home/jgn/work/src/riot/tests/unittests/tests-printf_float/tests-printf_float.c: In function ‘sfprintf_float’:
/home/jgn/work/src/riot/tests/unittests/tests-printf_float/tests-printf_float.c:39:28: error: ‘%f’ directive output truncated writing 11 bytes into a region of size 10 [-Werror=format-truncation=]
     snprintf(str, BUFSIZE, "%f", in0);
                            ^
/home/jgn/work/src/riot/tests/unittests/tests-printf_float/tests-printf_float.c:39:5: note: ‘__builtin_snprintf’ output 12 bytes into a destination of size 10
     snprintf(str, BUFSIZE, "%f", in0);
     ^
cc1: all warnings being treated as errors
2017-12-12 12:16:43 +01:00

79 lines
1.7 KiB
C

/*
* 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 <hauke.petersen@fu-berlin.de>
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "embUnit/embUnit.h"
#include "tests-printf_float.h"
#define BUFSIZE (12)
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.034900", 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());
}