diff options
Diffstat (limited to 'zephyr/test/math/src/math_util.c')
-rw-r--r-- | zephyr/test/math/src/math_util.c | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/zephyr/test/math/src/math_util.c b/zephyr/test/math/src/math_util.c index 901c3a6cc6..d3bd2c6fb6 100644 --- a/zephyr/test/math/src/math_util.c +++ b/zephyr/test/math/src/math_util.c @@ -1,12 +1,14 @@ -/* Copyright 2022 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <ztest.h> +#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); +} |