On Tue, 07 Jul 2026 15:04:28 +0100
Rodrigo Alencar via B4 Relay <[email protected]> 
wrote:

> From: Rodrigo Alencar <[email protected]>
> 
> Add a KUnit test suite covering __iio_chan_prefix_emit(), the helper
> that builds IIO sysfs attribute name prefixes from an iio_chan_spec.
> The suite groups cases by the enum iio_shared_by mode it exercises:
> 
>   - IIO_SHARED_BY_ALL: produces an empty prefix.
>   - IIO_SHARED_BY_DIR: emits direction only ("in" / "out").
>   - IIO_SHARED_BY_TYPE: emits "<dir>_<type>" and the differential
>     "<dir>_<type>-<type>" variant.
>   - IIO_SEPARATE: covers the full matrix of indexed, differential,
>     modified, output and extend_name combinations, plus the two
>     documented error paths (differential without indexed, differential
>     with modifier).
> 
> A final case exercises the seq_buf overflow path by passing an
> undersized buffer and expects -EOVERFLOW.
> 
> Because __iio_chan_prefix_emit() is static, the test translation unit
> is pulled into industrialio-core.c.

Isn't there some magic route cases like this that makes it non static
only when self tests are enabled? 
Claude tells me to look at include/kunit/visibility.h


Very nice.  A couple of really small additions requested inline.
I might well have missed where you exercised the corners requested though!
+ I'll need an Ack from Lars for that maintainers entry. I'll guess that
Lars won't give one as not very active at the moment in this area.

Jonathan

> 
> Also, an entry is created under MAINTAINERS dedicated to tests for IIO
> core helpers.
> 
> Signed-off-by: Rodrigo Alencar <[email protected]>
> ---
>  MAINTAINERS                                |   8 +
>  drivers/iio/industrialio-core.c            |   4 +
>  drivers/iio/test/Kconfig                   |  14 ++
>  drivers/iio/test/iio-test-channel-prefix.c | 246 
> +++++++++++++++++++++++++++++
>  4 files changed, 272 insertions(+)
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 2b1ec46c5919..57ffc0dcfdb6 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -12634,6 +12634,14 @@ F:   include/dt-bindings/iio/
>  F:   include/linux/iio/
>  F:   tools/iio/
>  
> +IIO CORE KUNIT TESTS
> +M:   Lars-Peter Clausen <[email protected]>

I'd need an Ack from Lars for this entry.   If we don't get one are you
fine looking after this without Lars listed?  

> +M:   Rodrigo Alencar <[email protected]>
> +L:   [email protected]
> +S:   Maintained
> +F:   drivers/iio/test/iio-test-channel-prefix.c
> +F:   drivers/iio/test/iio-test-format.c
> +
>  IIO UNIT CONVERTER
>  M:   Peter Rosin <[email protected]>
>  L:   [email protected]
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index ecc69adf61de..78a3c27d17a1 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -2232,6 +2232,10 @@ EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
>  subsys_initcall(iio_init);
>  module_exit(iio_exit);
>  
> +#if IS_ENABLED(CONFIG_IIO_CHANNEL_PREFIX_KUNIT_TEST)
> +#include "test/iio-test-channel-prefix.c"
> +#endif

> +
>  MODULE_AUTHOR("Jonathan Cameron <[email protected]>");
>  MODULE_DESCRIPTION("Industrial I/O core");
>  MODULE_LICENSE("GPL");

> diff --git a/drivers/iio/test/iio-test-channel-prefix.c 
> b/drivers/iio/test/iio-test-channel-prefix.c
> new file mode 100644
> index 000000000000..e6f2739331f2
> --- /dev/null
> +++ b/drivers/iio/test/iio-test-channel-prefix.c

> +
> +static void iio_test_prefix_shared_by_all(struct kunit *test)
> +{
> +     const struct iio_chan_spec chan = {
> +             .type = IIO_VOLTAGE,

I'd throw some stuff that won't be used in here.  Maybe even have
two different ones where we ignore things and then end up with the
same answer?  Same for the other ones where fields are ignored.

Obviously it is a bit of a dead chicken test (wave it around
and if still dead, all good :) so only really illustrates that
typically there is more stuff there that gets ignored.


> +     };
> +     char *buf = iio_test_prefix_alloc(test);
> +     ssize_t ret;
> +
> +     ret = __iio_chan_prefix_emit(NULL, &chan, IIO_SHARED_BY_ALL,
> +                                  buf, PREFIX_BUF_SIZE);
> +     EXPECT_PREFIX(test, buf, ret, "");
> +}

> +static void iio_test_prefix_separate_modified(struct kunit *test)
> +{
> +     const struct iio_chan_spec chan = {
> +             .type = IIO_ACCEL,
> +             .modified = 1,
> +             .channel2 = IIO_MOD_X,
> +     };
> +     char *buf = iio_test_prefix_alloc(test);
> +     ssize_t ret;
> +
> +     ret = __iio_chan_prefix_emit(NULL, &chan, IIO_SEPARATE,
> +                                  buf, PREFIX_BUF_SIZE);
> +     EXPECT_PREFIX(test, buf, ret, "in_accel_x");

I think it is worth one indexed + modified test

> +}
> +
> +static void iio_test_prefix_separate_extend_name(struct kunit *test)
> +{
> +     const struct iio_chan_spec chan = {
> +             .type = IIO_VOLTAGE,
> +             .indexed = 1,
> +             .channel = 2,
> +             .extend_name = "supply",
> +     };
> +     char *buf = iio_test_prefix_alloc(test);
> +     ssize_t ret;
> +
> +     ret = __iio_chan_prefix_emit(NULL, &chan, IIO_SEPARATE,
> +                                  buf, PREFIX_BUF_SIZE);
> +     EXPECT_PREFIX(test, buf, ret, "in_voltage2_supply");
> +}
> +
> +static void iio_test_prefix_output_separate(struct kunit *test)
> +{
> +     const struct iio_chan_spec chan = {
> +             .type = IIO_VOLTAGE,
> +             .output = 1,
> +             .indexed = 1,
> +             .channel = 0,
> +     };
> +     char *buf = iio_test_prefix_alloc(test);
> +     ssize_t ret;
> +
> +     ret = __iio_chan_prefix_emit(NULL, &chan, IIO_SEPARATE,
> +                                  buf, PREFIX_BUF_SIZE);
> +     EXPECT_PREFIX(test, buf, ret, "out_voltage0");
> +}

Reply via email to