Message ID | cover.1704881096.git.mazziesaccount@gmail.com |
---|---|
Headers |
Return-Path: <linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp692127dyi; Wed, 10 Jan 2024 02:07:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IFS6o9wK5VUSW1iBKTMqR6t2KqPxFrxTWJ9Z6LWZ7wCjJumbhDaoQE2fpQPeUGeAMXt42dU X-Received: by 2002:a05:620a:45a7:b0:783:1495:8c75 with SMTP id bp39-20020a05620a45a700b0078314958c75mr1043021qkb.30.1704881267458; Wed, 10 Jan 2024 02:07:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704881267; cv=none; d=google.com; s=arc-20160816; b=nA0fMbo3QLAHRpB0Hpw/gkE8mudSzeX3ORUUlrmxWvLVfjGHyECpax1M8kYPfJoSLv rh4jIp4NeXBnI+SRpmN5oz/T4PxRfcadKCuLC0dF5HOPs8hCmhZ4nIGzn3tSSIRRA00F Jbw10/LIQX0cNO+eUNX3FOowcOUgfINtJB9Ak4SwxVkmdyKsL5tbyk06NdB36OMzB8rs WZnB66+myElVOd+7PXql+o2h7CzRhvF0exmFPbHHzGzbqrDsMpP/I/aeElXu+Y2ns9+F 7VrGJR6Tc0mrtUX93ximLyEqC+fvwf2+baA/Q/Uj2IvqLHX+VoCOcjXMTI8LdPXMtwQh 4TqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=SR6M2098v0XZKYvDuASrOI/XGr4kL2jFyhf8PM2BbNw=; fh=EsD0e7Omra9j5GVSH6Hq+KkoUpU+pjkP3UXz1cLNAOA=; b=JlSX+nxfWdky8p8P51gnjwK5Hw7CiPM7IjnAGZv8pe9eN0pQL3b/aUPngpfX1GH48F t+ZLRS76xOrsU4ItbDtdr5WV2wXlRPOP3LuXiSidStju7RgnOJdhFx8J5mI8a++ulIuY vsISEHzPnpZtImoNC144iDseV3r0Tuo8lW380FEUFF9cLuti7pGHJv0V+xcGkmdGoOtb cIh6Nf3rz5yh+/+KqWwCkP0zK1XCz+W7iu9H0XizunNGX9TfPBYyswzkCXVO66G6YQfv kyqfT+7ogZ6MAM7zlz6ddHyKV7PClEd18KpdxutvhKsH9wwgLradZwblJBVq7WwSnH9L qnjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SwI3ofdf; spf=pass (google.com: domain of linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s5-20020a05620a0bc500b007830c3de57fsi3989524qki.239.2024.01.10.02.07.47 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 02:07:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SwI3ofdf; spf=pass (google.com: domain of linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21939-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 43F891C25FED for <ouuuleilei@gmail.com>; Wed, 10 Jan 2024 10:07:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 780243FB17; Wed, 10 Jan 2024 10:07:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SwI3ofdf" Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 437503F8D1; Wed, 10 Jan 2024 10:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-50e7abe4be4so5045576e87.2; Wed, 10 Jan 2024 02:07:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704881248; x=1705486048; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=SR6M2098v0XZKYvDuASrOI/XGr4kL2jFyhf8PM2BbNw=; b=SwI3ofdfwcJ/FDheRATOXy/Jn+zyxHrxdrevkHr9WGTaql9k9DUkbt+n+8Iq1I3XoQ F/4QTQFQKdawCQcVenL+TWTpdMj5610LlL0PW1Z1r9cQVWrPl1pwJBlV7J0quoZaIcaN Had7a4h5yLn+BcK/31ln1WZrzahLVQ0zYVMhtt/62ii97eY4P2vs1v0rKIt/XAluFjWV BnrLLh8VV5CsTw058eJ7I5BcUsGGfH4jAc6wh8eRoDxpcAlka9MT2woi+4UqUe8q9Rw8 JIxSlz+SDxcJlZCbjwc5O0MQs22JY1RtFIRVsoU1L/bt/OpKNmaCDH2QqaPw7eo7HLam GLjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704881248; x=1705486048; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SR6M2098v0XZKYvDuASrOI/XGr4kL2jFyhf8PM2BbNw=; b=BYNws01s+999Ce0sIoVbk9GsjP2pLGzV5qHaU+ChJpHwZ1rOyK/4S8o+avwimwyonS t0gfKcHyydJvitn2u/ixm3otqz4JkxWz3ZlB2Hhmm6mtgEBXwoep3EKKTvSiShT8x7iD MMp1ZzX2egvZSEO0QfZtxdXbOE6fOa9oo1CIauWgn91unI2WF7QQxK9dVmUzsEpqWch5 pIfkwRsD7che01FFTLPWQnx4CUOwgyXfvl8WK4h6J5BnJTTmNxnhFT4bzVwTf7yg3lPT lXyypFS8WpcpdFzgxwDpq5jDHAXdrEo/rq2Zt5RjyFF5RWTtFIVHTgvAwwIn9Yyxt1LV VczA== X-Gm-Message-State: AOJu0Yx9UYrDEQiTuvOWBesN5Gow/Et7O4jtXDc9k832dP2vI+efqBdy ecueY9mluJs2EMVGMeBkIysHMgiyuPA= X-Received: by 2002:a05:6512:3f16:b0:50e:51fa:1d3b with SMTP id y22-20020a0565123f1600b0050e51fa1d3bmr407331lfa.93.1704881247980; Wed, 10 Jan 2024 02:07:27 -0800 (PST) Received: from drtxq0yyyyyyyyyyyyybt-3.rev.dnainternet.fi (drtxq0yyyyyyyyyyyyybt-3.rev.dnainternet.fi. [2001:14ba:7426:df00::3]) by smtp.gmail.com with ESMTPSA id h41-20020a0565123ca900b0050e7fe37a29sm614232lfv.200.2024.01.10.02.07.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 02:07:26 -0800 (PST) Date: Wed, 10 Jan 2024 12:07:08 +0200 From: Matti Vaittinen <mazziesaccount@gmail.com> To: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>, Matti Vaittinen <mazziesaccount@gmail.com> Cc: Jonathan Cameron <jic23@kernel.org>, Lars-Peter Clausen <lars@metafoo.de>, Matti Vaittinen <mazziesaccount@gmail.com>, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org Subject: [PATCH 0/3] KUnit tests for the IIO GTS helpers Message-ID: <cover.1704881096.git.mazziesaccount@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="doWc2m8A9KjyRIVC" Content-Disposition: inline X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787697580053280126 X-GMAIL-MSGID: 1787697580053280126 |
Series |
KUnit tests for the IIO GTS helpers
|
|
Message
Matti Vaittinen
Jan. 10, 2024, 10:07 a.m. UTC
Add some KUnit tests for the IIO GTS helpers. These tests were originally part of the BU27034 ALS sensor driver series. https://lore.kernel.org/all/cover.1679915278.git.mazziesaccount@gmail.com/ Merging the tests was postponed because we lacked of a good generic way of creating tests devices for testing the devm managed interfaces. Now we have kunit_device APIs being merged (seems like they'll be part of the v6.8-rc1) so precondition for merging these tests are (being) met. The series is based on commit ab27740f7665 ("Merge tag 'linux_kselftest-next-6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest") in Linus' tree. I can rebase and resend when v6.8-rc1 is out if needed. Matti Vaittinen (3): iio: gts-helper: Fix division loop iio: test: test gain-time-scale helpers MAINTAINERS: add IIO GTS tests MAINTAINERS | 1 + drivers/iio/industrialio-gts-helper.c | 5 +- drivers/iio/test/Kconfig | 14 + drivers/iio/test/Makefile | 1 + drivers/iio/test/iio-test-gts.c | 517 ++++++++++++++++++++++++++ 5 files changed, 535 insertions(+), 3 deletions(-) create mode 100644 drivers/iio/test/iio-test-gts.c base-commit: ab27740f76654ed58dd32ac0ba0031c18a6dea3b
Comments
On 1/10/24 12:07, Matti Vaittinen wrote: > Add some KUnit tests for the IIO GTS helpers. .. > Matti Vaittinen (3): > iio: gts-helper: Fix division loop Please ignore the patch 1/3 above. It has been already merged. Unfortunately I noticed this commit being in the series only after sending the cover-letter. In order to not consumer reviewer's time, I omitted sending the 1/3 - so it's missing the series on purpose. Subsequent versions won't include this patch anymore. > iio: test: test gain-time-scale helper > MAINTAINERS: add IIO GTS tests > > MAINTAINERS | 1 + > drivers/iio/industrialio-gts-helper.c | 5 +- Also, this file is not changed by patches 2/3 and 3/3. Sorry! Yours, -- Matti
On Wed, 10 Jan 2024 12:12:55 +0200 Matti Vaittinen <mazziesaccount@gmail.com> wrote: > Some light sensors can adjust both the HW-gain and integration time. > There are cases where adjusting the integration time has similar impact > to the scale of the reported values as gain setting has. > > IIO users do typically expect to handle scale by a single writable 'scale' > entry. Driver should then adjust the gain/time accordingly. > > It however is difficult for a driver to know whether it should change > gain or integration time to meet the requested scale. Usually it is > preferred to have longer integration time which usually improves > accuracy, but there may be use-cases where long measurement times can be > an issue. Thus it can be preferable to allow also changing the > integration time - but mitigate the scale impact by also changing the gain > underneath. Eg, if integration time change doubles the measured values, > the driver can reduce the HW-gain to half. > > The theory of the computations of gain-time-scale is simple. However, > some people (undersigned) got that implemented wrong for more than once. > Hence some gain-time-scale helpers were introduced. > > Add some simple tests to verify the most hairy functions. > > Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com> > Hi Matti, All seems reasonable to me. Some trivial formatting things inline + I'm not planning to check the maths as you are the expert in all of this so I'll just trust you! Also if this fails you get to pick up the pieces :) Jonathan > CFLAGS_iio-test-format.o += $(DISABLE_STRUCTLEAK_PLUGIN) > diff --git a/drivers/iio/test/iio-test-gts.c b/drivers/iio/test/iio-test-gts.c > new file mode 100644 > index 000000000000..4d5271b0c7bc > --- /dev/null > +++ b/drivers/iio/test/iio-test-gts.c > @@ -0,0 +1,517 @@ .. > + > +static void test_iio_find_closest_gain_low(struct kunit *test) > +{ > + struct device *dev; > + bool in_range; > + int ret; > + > + const struct iio_gain_sel_pair gts_test_gains_gain_low[] = { > + GAIN_SCALE_GAIN(4, TEST_GSEL_4), > + GAIN_SCALE_GAIN(16, TEST_GSEL_16), > + GAIN_SCALE_GAIN(32, TEST_GSEL_32), > + }; > + > + dev = test_init_iio_gain_scale(test, >s); > + if (!dev) > + return; > + > + ret = iio_find_closest_gain_low(>s, 2, &in_range); > + KUNIT_EXPECT_EQ(test, 1, ret); > + KUNIT_EXPECT_EQ(test, true, in_range); > + > + ret = iio_find_closest_gain_low(>s, 1, &in_range); > + KUNIT_EXPECT_EQ(test, 1, ret); > + KUNIT_EXPECT_EQ(test, true, in_range); > + > + ret = iio_find_closest_gain_low(>s, 4095, &in_range); > + KUNIT_EXPECT_EQ(test, 2048, ret); > + KUNIT_EXPECT_EQ(test, true, in_range); > + > + ret = iio_find_closest_gain_low(>s, 4097, &in_range); > + KUNIT_EXPECT_EQ(test, 4096, ret); > + KUNIT_EXPECT_EQ(test, false, in_range); > + > + kunit_device_unregister(test, dev); > + > + dev = __test_init_iio_gain_scale(test, >s, gts_test_gains_gain_low, > + ARRAY_SIZE(gts_test_gains_gain_low), > + gts_test_itimes, ARRAY_SIZE(gts_test_itimes)); > + if (!dev) > + return; > + > + ret = iio_find_closest_gain_low(>s, 3, &in_range); > + KUNIT_EXPECT_EQ(test, -EINVAL, ret); > + KUNIT_EXPECT_EQ(test, false, in_range); > + As below. > +} > + > +static void test_iio_gts_total_gain_to_scale(struct kunit *test) > +{ > + struct device *dev; > + int ret, scale_int, scale_nano; > + > + dev = test_init_iio_gain_scale(test, >s); > + if (!dev) > + return; > + > + ret = iio_gts_total_gain_to_scale(>s, 1, &scale_int, &scale_nano); > + KUNIT_EXPECT_EQ(test, 0, ret); > + KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int); > + KUNIT_EXPECT_EQ(test, 0, scale_nano); > + > + ret = iio_gts_total_gain_to_scale(>s, 1, &scale_int, &scale_nano); > + KUNIT_EXPECT_EQ(test, 0, ret); > + KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int); > + KUNIT_EXPECT_EQ(test, 0, scale_nano); > + > + ret = iio_gts_total_gain_to_scale(>s, 4096 * 8, &scale_int, > + &scale_nano); > + KUNIT_EXPECT_EQ(test, 0, ret); > + KUNIT_EXPECT_EQ(test, 0, scale_int); > + KUNIT_EXPECT_EQ(test, TEST_SCALE_NANO_4096X8, scale_nano); > + No need for a blank line here. > +} > + > +static void test_iio_gts_chk_scales_all(struct kunit *test, struct iio_gts *gts, > + const int *vals, int len) > +{ > + static const int gains[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, > + 1024, 2048, 4096, 4096 * 2, 4096 * 4, > + 4096 * 8}; > + > + int expected[ARRAY_SIZE(gains) * 2]; > + int i, ret; > + int exp_len = ARRAY_SIZE(gains) * 2; Use this for expected[*] just above? > + > + KUNIT_EXPECT_EQ(test, exp_len, len); > + if (len != exp_len) > + return; > + > + for (i = 0; i < ARRAY_SIZE(gains); i++) { > + ret = iio_gts_total_gain_to_scale(gts, gains[i], > + &expected[2 * i], > + &expected[2 * i + 1]); > + KUNIT_EXPECT_EQ(test, 0, ret); > + if (ret) > + return; > + } > + > + for (i = 0; i < ARRAY_SIZE(expected); i++) > + KUNIT_EXPECT_EQ(test, expected[i], vals[i]); > +} > + > +static void test_iio_gts_avail_test(struct kunit *test) > +{ > + struct device *dev; > + int ret; > + int type, len; > + const int *vals; > + > + dev = test_init_iio_gain_scale(test, >s); > + if (!dev) > + return; > + > + /* test table building for times and iio_gts_avail_times() */ > + ret = iio_gts_avail_times(>s, &vals, &type, &len); > + KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret); > + if (ret) > + return; > + > + KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_MICRO, type); > + KUNIT_EXPECT_EQ(test, 8, len); > + if (len < 8) > + return; > + > + test_iio_gts_chk_times(test, vals); > + > + /* Test table building for all scales and iio_gts_all_avail_scales() */ > + ret = iio_gts_all_avail_scales(>s, &vals, &type, &len); > + KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret); > + if (ret) > + return; > + > + KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type); > + > + test_iio_gts_chk_scales_all(test, >s, vals, len); > + > + /* > + * Test table building for scales/time and > + * iio_gts_avail_scales_for_time() > + */ > + ret = iio_gts_avail_scales_for_time(>s, 200000, &vals, &type, &len); > + KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret); > + if (ret) > + return; > + > + KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type); > + test_iio_gts_chk_scales_t200(test, >s, vals, len); > +} > + > +static struct kunit_case iio_gts_test_cases[] = { > + KUNIT_CASE(test_init_iio_gts_invalid), > + KUNIT_CASE(test_iio_gts_find_gain_for_scale_using_time), > + KUNIT_CASE(test_iio_gts_find_new_gain_sel_by_old_gain_time), > + KUNIT_CASE(test_iio_find_closest_gain_low), > + KUNIT_CASE(test_iio_gts_total_gain_to_scale), > + KUNIT_CASE(test_iio_gts_avail_test), > + {} > +}; > + > +static struct kunit_suite iio_gts_test_suite = { > + .name = "iio-gain-time-scale", > + .test_cases = iio_gts_test_cases, > +}; > + > +kunit_test_suite(iio_gts_test_suite); > + > +MODULE_LICENSE("GPL"); > +MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>"); > +MODULE_DESCRIPTION("Test IIO light sensor gain-time-scale helpers"); > +MODULE_IMPORT_NS(IIO_GTS_HELPER); > + Looks like a bonus blank line at the end here. Jonathan