From: Matti Vaittinen <mazziesaccount@gmail.com>
To: "David Lechner" <dlechner@baylibre.com>,
"Jonathan Cameron" <jic23@kernel.org>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org
Subject: Re: [PATCH] iio: test: fixed-point: new kunit test
Date: Tue, 14 Oct 2025 09:43:54 +0300 [thread overview]
Message-ID: <166d0753-413f-4f83-892c-2e799c0c9f15@gmail.com> (raw)
In-Reply-To: <20251013-iio-tests-fixed-point-new-kunit-v1-1-7b52021925e6@baylibre.com>
On 14/10/2025 00:33, David Lechner wrote:
> Add a kunit test for iio_str_to_fixpoint(). This function has an
> unintuitive API so this is helpful to see how to use it and shows the
> various edge cases.
>
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
> When reviewing [1], I noticed that iio_str_to_fixpoint() has an odd API
> which lead me to find the bug in [2]. To make sure I was understanding
> the API correctly, I wrote a KUnit test for it. So here it is.
>
> [1]: https://lore.kernel.org/linux-iio/20251009173609.992452-3-flavra@baylibre.com/
> [2]: https://lore.kernel.org/linux-iio/20251010-iio-adc-ad7280a-fix-ad7280_store_balance_timer-v1-1-e11746735192@baylibre.com/
> ---
> Discussion unrelated to the patch:
>
> I'm also a little tempted to introduce a new function that is a bit
> easier to use. Many callers of iio_str_to_fixpoint_s64() are doing
> something like int_part * 1000 + fract_part and ignoring the possibility
> of negative values which require special handling.
>
> static int iio_str_to_fixpoint_s64(const char *str, u32 decimal_places, s64 *value)
> {
> int int_part, fract_part;
> int ret;
>
> ret = iio_str_to_fixpoint(str, int_pow(10, decimal_places - 1),
> &int_part, &fract_part);
> if (ret)
> return ret;
>
> *value = (s64)int_part * int_pow(10, decimal_places) +
> (int_part < 0 ? -1 : 1) * fract_part;
>
> return 0;
> }
FWIW: I like this.
> ---
> drivers/iio/test/Kconfig | 13 ++++++++
> drivers/iio/test/Makefile | 1 +
> drivers/iio/test/iio-test-fixed-point.c | 58 +++++++++++++++++++++++++++++++++
> 3 files changed, 72 insertions(+)
>
> diff --git a/drivers/iio/test/Kconfig b/drivers/iio/test/Kconfig
> index 6e65e929791ca247df9ac993fddbb4da557d5dfa..d210067ea59199d342b15bf373e724d0aa2c55a0 100644
> --- a/drivers/iio/test/Kconfig
> +++ b/drivers/iio/test/Kconfig
> @@ -4,6 +4,19 @@
// snip
> +static void iio_test_iio_str_to_fixed_point(struct kunit *test)
> +{
> + int int_part, fract_part;
> + int ret;
> +
> + /* Positive value > 1 */
> + ret = iio_str_to_fixpoint("1.234", 100, &int_part, &fract_part);
> + KUNIT_EXPECT_EQ(test, 0, ret);
> + KUNIT_EXPECT_EQ(test, int_part * 1000 + fract_part, 1234);
Do you think doing two checks:
KUNIT_EXPECT_EQ(test, int_part, 1);
KUNIT_EXPECT_EQ(test, fract_part, 234);
instead of the:
KUNIT_EXPECT_EQ(test, int_part * 1000 + fract_part, 1234);
would work? For me seeing it all ass
ret = iio_str_to_fixpoint("1.234", 100, &int_part, &fract_part);
KUNIT_EXPECT_EQ(test, 0, ret);
KUNIT_EXPECT_EQ(test, int_part, 1);
KUNIT_EXPECT_EQ(test, fract_part, 234);
would be super clear - albeit also a line longer :)
(Same applies to the rest of the cases).
Well, it's not critical, so with or without that change:
Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Thanks!
Yours,
-- Matti
next prev parent reply other threads:[~2025-10-14 6:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-13 21:33 [PATCH] iio: test: fixed-point: new kunit test David Lechner
2025-10-14 6:43 ` Matti Vaittinen [this message]
2025-10-15 3:21 ` kernel test robot
2025-10-15 5:46 ` kernel test robot
2025-10-18 16:30 ` Jonathan Cameron
-- strict thread matches above, loose matches on Subject: below --
2025-10-18 19:24 Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=166d0753-413f-4f83-892c-2e799c0c9f15@gmail.com \
--to=mazziesaccount@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox