Skip to content

Commit 14175fb

Browse files
sytsereitsmaSytse Reitsma
authored andcommitted
Added unit test for floating point formatting
And added a bonus test for string padding
1 parent 84e0d5d commit 14175fb

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

platform/tests/UNITTESTS/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
add_subdirectory(doubles)
55
add_subdirectory(ATCmdParser)
66
add_subdirectory(CircularBuffer)
7+
add_subdirectory(minimal-printf)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2021 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
set(TEST_NAME minimal-printf-unittest)
5+
6+
add_executable(${TEST_NAME})
7+
8+
target_sources(${TEST_NAME}
9+
PRIVATE
10+
${mbed-os_SOURCE_DIR}/platform/source/minimal-printf/mbed_printf_implementation.c
11+
test_minimal-printf-implementation.cpp
12+
)
13+
14+
target_include_directories(mbed-headers-platform
15+
INTERFACE
16+
${mbed-os_SOURCE_DIR}/platform/source/minimal-printf
17+
)
18+
19+
target_link_libraries(${TEST_NAME}
20+
PRIVATE
21+
mbed-stubs-platform
22+
gmock_main
23+
)
24+
25+
add_test(NAME "${TEST_NAME}" COMMAND ${TEST_NAME})
26+
27+
set_tests_properties(${TEST_NAME} PROPERTIES LABELS "platform")
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <cmath>
19+
#include <cstdarg>
20+
#include "gtest/gtest.h"
21+
22+
/* "mbed_printf_implementation.h" does not declare it as extern "C", so pull it in manually */
23+
extern "C" int mbed_minimal_formatted_string(char *buffer, size_t length, const char *format, va_list arguments, FILE *stream);
24+
25+
namespace
26+
{
27+
std::string format(const char *fmt, ...)
28+
{
29+
va_list arguments;
30+
va_start(arguments, fmt);
31+
32+
constexpr int buf_size = 128;
33+
char buffer[buf_size];
34+
int n = mbed_minimal_formatted_string(buffer, buf_size, fmt, arguments, nullptr);
35+
va_end(arguments);
36+
37+
if (n >= buf_size) {
38+
return "overflow";
39+
}
40+
41+
return std::string(buffer);
42+
}
43+
}
44+
45+
TEST(minimal_printf, floats)
46+
{
47+
/* std::nextafter is used for the values below to make sure the numbers are
48+
floating point representations of at least the value we want to test.
49+
This hopefully mitigates false failures due to different floating point
50+
implementations */
51+
52+
/* Positive numbers */
53+
float value = std::nextafter(1.5f, 0.0f); /* Something like 1.499999, i.e just below 1.5 */
54+
EXPECT_EQ("1", format("%.0f", value));
55+
EXPECT_EQ("1.5", format("%.1f", value));
56+
EXPECT_EQ("1.50", format("%.2f", value));
57+
EXPECT_EQ("1.500", format("%.3f", value));
58+
EXPECT_EQ("1.5000", format("%.4f", value));
59+
EXPECT_EQ("1.50000", format("%.5f", value));
60+
EXPECT_EQ("1.500000", format("%.6f", value));
61+
62+
value = std::nextafter(1.5f, 2.0f); /* Something like 1.500001, i.e just above 1.5 */
63+
EXPECT_EQ("2", format("%.0f", value));
64+
EXPECT_EQ("1.5", format("%.1f", value));
65+
EXPECT_EQ("1.50", format("%.2f", value));
66+
EXPECT_EQ("1.500", format("%.3f", value));
67+
EXPECT_EQ("1.5000", format("%.4f", value));
68+
EXPECT_EQ("1.50000", format("%.5f", value));
69+
EXPECT_EQ("1.500000", format("%.6f", value));
70+
71+
value = std::nextafter(2.0f, 0.0f); /* Something like 1.999999, i.e just below 2.0 */
72+
EXPECT_EQ("2", format("%.0f", value));
73+
EXPECT_EQ("2.0", format("%.1f", value));
74+
EXPECT_EQ("2.00", format("%.2f", value));
75+
EXPECT_EQ("2.000", format("%.3f", value));
76+
EXPECT_EQ("2.0000", format("%.4f", value));
77+
EXPECT_EQ("2.00000", format("%.5f", value));
78+
EXPECT_EQ("2.000000", format("%.6f", value));
79+
80+
/* Negative numbers */
81+
value = std::nextafter(-1.5f, 0.0f); /* Something like -1.499999, i.e just above -1.5 */
82+
EXPECT_EQ("-1", format("%.0f", value));
83+
EXPECT_EQ("-1.5", format("%.1f", value));
84+
EXPECT_EQ("-1.50", format("%.2f", value));
85+
EXPECT_EQ("-1.500", format("%.3f", value));
86+
EXPECT_EQ("-1.5000", format("%.4f", value));
87+
EXPECT_EQ("-1.50000", format("%.5f", value));
88+
EXPECT_EQ("-1.500000", format("%.6f", value));
89+
90+
value = std::nextafter(-1.5f, -2.0f); /* Something like -1.500001, i.e just below -1.5 */
91+
EXPECT_EQ("-2", format("%.0f", value));
92+
EXPECT_EQ("-1.5", format("%.1f", value));
93+
EXPECT_EQ("-1.50", format("%.2f", value));
94+
EXPECT_EQ("-1.500", format("%.3f", value));
95+
EXPECT_EQ("-1.5000", format("%.4f", value));
96+
EXPECT_EQ("-1.50000", format("%.5f", value));
97+
EXPECT_EQ("-1.500000", format("%.6f", value));
98+
99+
value = std::nextafter(-2.0f, 0.0f); /* Something like -1.999999, i.e just above -2.0 */
100+
EXPECT_EQ("-2", format("%.0f", value));
101+
EXPECT_EQ("-2.0", format("%.1f", value));
102+
EXPECT_EQ("-2.00", format("%.2f", value));
103+
EXPECT_EQ("-2.000", format("%.3f", value));
104+
EXPECT_EQ("-2.0000", format("%.4f", value));
105+
EXPECT_EQ("-2.00000", format("%.5f", value));
106+
EXPECT_EQ("-2.000000", format("%.6f", value));
107+
108+
// And some near zero stuff
109+
EXPECT_EQ("-0", format("%.0f", std::nextafter(0.0f, -1.0f)));
110+
EXPECT_EQ("0", format("%.0f", 0.0));
111+
EXPECT_EQ("0", format("%.0f", std::nextafter(0.0f, 1.0f)));
112+
}
113+
114+
TEST(minimal_printf, padding)
115+
{
116+
// Right aligned integers
117+
EXPECT_EQ("12", format("%1d", 12));
118+
EXPECT_EQ("12", format("%2d", 12));
119+
EXPECT_EQ(" 12", format("%3d", 12));
120+
EXPECT_EQ(" 12", format("%4d", 12));
121+
122+
EXPECT_EQ("-12", format("%1d", -12));
123+
EXPECT_EQ("-12", format("%3d", -12));
124+
EXPECT_EQ(" -12", format("%4d", -12));
125+
EXPECT_EQ(" -12", format("%5d", -12));
126+
127+
EXPECT_EQ("0012", format("%04d", 12));
128+
EXPECT_EQ("-012", format("%04d", -12));
129+
130+
// Right aligned floats
131+
EXPECT_EQ("12.3", format("%1.1f", 12.345f));
132+
EXPECT_EQ("12.3", format("%4.1f", 12.345));
133+
EXPECT_EQ(" 12.3", format("%5.1f", 12.345));
134+
EXPECT_EQ(" 12.3", format("%6.1f", 12.345));
135+
136+
EXPECT_EQ("-12.3", format("%1.1f", -12.345f));
137+
EXPECT_EQ("-12.3", format("%4.1f", -12.345));
138+
EXPECT_EQ(" -12.3", format("%6.1f", -12.345));
139+
EXPECT_EQ(" -12.3", format("%7.1f", -12.345));
140+
141+
EXPECT_EQ("012.3", format("%05.1f", 12.345));
142+
EXPECT_EQ("-012.3", format("%06.1f", -12.345));
143+
}
144+

0 commit comments

Comments
 (0)