diff options
author | Jeremy Bettis <jbettis@google.com> | 2022-08-15 09:51:44 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-08-16 19:28:11 +0000 |
commit | 7523f099fc2efef7b7303ea80a6c369e3b39f8e4 (patch) | |
tree | 5909562a90804bc98d7cbed8deda1f079eae28b2 | |
parent | 390d08429db06038fd4fd77beaf960a4d450c8d9 (diff) | |
download | chrome-ec-7523f099fc2efef7b7303ea80a6c369e3b39f8e4.tar.gz |
ec: Add tests for printing fixed point numbers
The coverage reports are flaky, and sometimes cover the overflow errors
and sometimes not. Add tests for those overflow conditions in both
legacy and zephyr, and add tests for the related fixed point formatting
as well.
BRANCH=None
BUG=b:242543903
FIXED=b:242543903
TEST=ran tests
Signed-off-by: Jeremy Bettis <jbettis@google.com>
Change-Id: I2229e1d1b956d207aabad4025921fef9fa8a541d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3833911
Reviewed-by: Simon Glass <sjg@chromium.org>
Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
Auto-Submit: Jeremy Bettis <jbettis@chromium.org>
Commit-Queue: Jeremy Bettis <jbettis@chromium.org>
Tested-by: Jeremy Bettis <jbettis@chromium.org>
Commit-Queue: Simon Glass <sjg@chromium.org>
-rw-r--r-- | test/printf.c | 8 | ||||
-rw-r--r-- | zephyr/test/math/src/math_util.c | 29 |
2 files changed, 37 insertions, 0 deletions
diff --git a/test/printf.c b/test/printf.c index 0d96472122..080beed0da 100644 --- a/test/printf.c +++ b/test/printf.c @@ -147,6 +147,14 @@ test_static int test_vsnprintf_int(void) T(expect_success(" +123", "%+5d", 123)); T(expect_success("00123", "%05d", 123)); T(expect_success("00123", "%005d", 123)); + /* Fixed point. */ + T(expect_success("0.00123", "%.5d", 123)); + T(expect_success("12.3", "%2.1d", 123)); + /* Precision or width larger than buffer should fail. */ + T(expect(EC_ERROR_OVERFLOW, " 1", false, 4, "%5d", 123)); + T(expect(EC_ERROR_OVERFLOW, " ", false, 4, "%10d", 123)); + T(expect(EC_ERROR_OVERFLOW, "123", false, 4, "%-10d", 123)); + T(expect(EC_ERROR_OVERFLOW, "0.0", false, 4, "%.10d", 123)); if (use_builtin_stdlib) { /* * TODO(b/239233116): These are incorrect and should be fixed. diff --git a/zephyr/test/math/src/math_util.c b/zephyr/test/math/src/math_util.c index 9b3ce6711e..74fbe021dc 100644 --- a/zephyr/test/math/src/math_util.c +++ b/zephyr/test/math/src/math_util.c @@ -5,8 +5,10 @@ #include <zephyr/ztest.h> +#include "common.h" #include "math.h" #include "math_util.h" +#include "builtin/stdio.h" ZTEST_USER(math, arc_cos__x_below_range) { @@ -39,3 +41,30 @@ ZTEST_USER(math, fp_sqrtf) zassert_within(fp_sqrtf(FLOAT_TO_FP(15)), FLOAT_TO_FP(3.872983), FLOAT_TO_FP(0.001), NULL); } + +ZTEST_USER(math, print_ints) +{ + char buffer[10]; + + /* Fixed point. */ + zassert_true(crec_snprintf(buffer, sizeof(buffer), "%.5d", 123) > 0, + NULL); + zassert_equal(0, strcmp(buffer, "0.00123"), "got '%s'", buffer); + zassert_true(crec_snprintf(buffer, sizeof(buffer), "%2.1d", 123) > 0, + NULL); + zassert_equal(0, strcmp(buffer, "12.3"), "got '%s'", buffer); + + /* Precision or width larger than buffer should fail. */ + zassert_equal(-EC_ERROR_OVERFLOW, crec_snprintf(buffer, 4, "%5d", 123), + NULL); + zassert_equal(0, strcmp(buffer, " 1"), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, crec_snprintf(buffer, 4, "%10d", 123), + NULL); + zassert_equal(0, strcmp(buffer, " "), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "%-10d", 123), NULL); + zassert_equal(0, strcmp(buffer, "123"), "got '%s'", buffer); + zassert_equal(-EC_ERROR_OVERFLOW, + crec_snprintf(buffer, 4, "%.10d", 123), NULL); + zassert_equal(0, strcmp(buffer, "0.0"), "got '%s'", buffer); +} |