From patchwork Fri Dec 17 13:02:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 12684573 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AC9E6C433F5 for ; Fri, 17 Dec 2021 13:04:32 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id E7ED7286A; Fri, 17 Dec 2021 14:03:40 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz E7ED7286A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1639746271; bh=p0EUVadnjEh+VEL3Df8aeJ2FHtPzcNPsATIRNxkT7xA=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Nl7PgHvLYlROjsAJKUwUoZQlSHoHNDAEABu7sbAZ58MHt6y0FY0PwRnZZzuqAh55Q NUhXrdk76gFJMShlR3znWPxpEwZICHfG8JkimOWenwC2Xq0YEnnEbhL+erPcK8IMRP LtHGuoWlC3fb9jS7EFg0sDUFc0GjP8Ze0FuPht5M= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 530A8F80163; Fri, 17 Dec 2021 14:03:14 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 21D07F8027C; Fri, 17 Dec 2021 14:03:08 +0100 (CET) Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 7607AF800B0 for ; Fri, 17 Dec 2021 14:03:00 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 7607AF800B0 Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K9oOVkOC" Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AEC34B827EC; Fri, 17 Dec 2021 13:02:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1403BC36AEA; Fri, 17 Dec 2021 13:02:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1639746178; bh=p0EUVadnjEh+VEL3Df8aeJ2FHtPzcNPsATIRNxkT7xA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K9oOVkOCIPvAczOM/dV0jwQ1pRgLzB4qvdy9gxQ3XPbRR/uF/6OhjPzO5cq2k9t8T 0pDFeSI+q4KsmGrySqUqZ1Qd4xV4MwbQ7ouorQQmpJk6EUV1dhZqeULHELEPJG/U7c 5T9pDVByrn6DIedqyjIUZb2YFvq7gXWwOOcLBZ9YMTZ1QWEUKU0G+P26ttXkvT3nyl WXQSuCqbU+z5PaUaSdDwx3wtIOwn7yf9FwovE+ltKXRMLTEgGfK6lN6ybR5cYKsYFH SYAPYctv0HxdnoT6iHGJyxY11g4e6qf40sC+P4uvaiOGbFQqvyULdGD6NLldH7mJdx T+mJ1EUDUA0+g== From: Mark Brown To: Takashi Iwai , Jaroslav Kysela , Shuah Khan Subject: [PATCH v1 1/2] kselftest: alsa: Factor out check that values meet constraints Date: Fri, 17 Dec 2021 13:02:12 +0000 Message-Id: <20211217130213.3893415-2-broonie@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211217130213.3893415-1-broonie@kernel.org> References: <20211217130213.3893415-1-broonie@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7253; h=from:subject; bh=p0EUVadnjEh+VEL3Df8aeJ2FHtPzcNPsATIRNxkT7xA=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBhvIpMaXS59ZIdcpQYdCf0oZ/8LhvZ5MVIMefs1FbN uSjEfOyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCYbyKTAAKCRAk1otyXVSH0IwJB/ 9qGRnHpW7v0s7bsizAQl0W10zZ+Qwr6sJNsFpiAuw/6nK4+nTmZSv6Q7+9p34v7B5xwbKVsEvIq4AB oEjdlAv1qj0914T5Sj8C0cn1nmuqcQTt0HV37e+hqOMnIEq5BrtKqHWEoNhVGfThwlaj+/gA6nX1J3 UqhK9iovaV3g7em8H+X6RI2QT+rQB1HSeaxpQu/6dMxZjmHIL/qzPsZRnPquiXS5SJmgAgNoUX33PA GaK6hYvyl9v5S9ZA7VIMg0Ahdrp5iv9B8qeLJjKMEi6CZpwIwNv7md0I0SQryyKIgsxrW/k23llxjT LAUz5yrU7ec1zoPKSCv1ZJX8EYIzRX X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Cc: alsa-devel@alsa-project.org, Mark Brown , linux-kselftest@vger.kernel.org X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" To simplify the code a bit and allow future reuse factor the checks that values we read are valid out of test_ctl_get_value() into a separate function which can be reused later. As part of this extend the test to check all the values for the control, not just the first one. Signed-off-by: Mark Brown --- tools/testing/selftests/alsa/mixer-test.c | 141 +++++++++++++--------- 1 file changed, 82 insertions(+), 59 deletions(-) diff --git a/tools/testing/selftests/alsa/mixer-test.c b/tools/testing/selftests/alsa/mixer-test.c index b798a76f6825..b009fc5df605 100644 --- a/tools/testing/selftests/alsa/mixer-test.c +++ b/tools/testing/selftests/alsa/mixer-test.c @@ -193,124 +193,147 @@ void find_controls(void) snd_config_delete(config); } -/* - * Check that we can read the default value and it is valid. Write - * tests use the read value to restore the default. - */ -void test_ctl_get_value(struct ctl_data *ctl) +bool ctl_value_index_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val, + int index) { - int err; long int_val; long long int64_val; - /* If the control is turned off let's be polite */ - if (snd_ctl_elem_info_is_inactive(ctl->info)) { - ksft_print_msg("%s is inactive\n", ctl->name); - ksft_test_result_skip("get_value.%d.%d\n", - ctl->card->card, ctl->elem); - return; - } - - /* Can't test reading on an unreadable control */ - if (!snd_ctl_elem_info_is_readable(ctl->info)) { - ksft_print_msg("%s is not readable\n", ctl->name); - ksft_test_result_skip("get_value.%d.%d\n", - ctl->card->card, ctl->elem); - return; - } - - err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val); - if (err < 0) { - ksft_print_msg("snd_ctl_elem_read() failed: %s\n", - snd_strerror(err)); - goto out; - } - switch (snd_ctl_elem_info_get_type(ctl->info)) { case SND_CTL_ELEM_TYPE_NONE: - ksft_print_msg("%s Invalid control type NONE\n", ctl->name); - err = -1; - break; + ksft_print_msg("%s.%d Invalid control type NONE\n", + ctl->name, index); + return false; case SND_CTL_ELEM_TYPE_BOOLEAN: - int_val = snd_ctl_elem_value_get_boolean(ctl->def_val, 0); + int_val = snd_ctl_elem_value_get_boolean(val, index); switch (int_val) { case 0: case 1: break; default: - ksft_print_msg("%s Invalid boolean value %ld\n", - ctl->name, int_val); - err = -1; - break; + ksft_print_msg("%s.%d Invalid boolean value %ld\n", + ctl->name, index, int_val); + return false; } break; case SND_CTL_ELEM_TYPE_INTEGER: - int_val = snd_ctl_elem_value_get_integer(ctl->def_val, 0); + int_val = snd_ctl_elem_value_get_integer(val, index); if (int_val < snd_ctl_elem_info_get_min(ctl->info)) { - ksft_print_msg("%s value %ld less than minimum %ld\n", - ctl->name, int_val, + ksft_print_msg("%s.%d value %ld less than minimum %ld\n", + ctl->name, index, int_val, snd_ctl_elem_info_get_min(ctl->info)); - err = -1; + return false; } if (int_val > snd_ctl_elem_info_get_max(ctl->info)) { - ksft_print_msg("%s value %ld more than maximum %ld\n", - ctl->name, int_val, + ksft_print_msg("%s.%d value %ld more than maximum %ld\n", + ctl->name, index, int_val, snd_ctl_elem_info_get_max(ctl->info)); - err = -1; + return false; } /* Only check step size if there is one and we're in bounds */ - if (err >= 0 && snd_ctl_elem_info_get_step(ctl->info) && + if (snd_ctl_elem_info_get_step(ctl->info) && (int_val - snd_ctl_elem_info_get_min(ctl->info) % snd_ctl_elem_info_get_step(ctl->info))) { - ksft_print_msg("%s value %ld invalid for step %ld minimum %ld\n", - ctl->name, int_val, + ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n", + ctl->name, index, int_val, snd_ctl_elem_info_get_step(ctl->info), snd_ctl_elem_info_get_min(ctl->info)); - err = -1; + return false; } break; case SND_CTL_ELEM_TYPE_INTEGER64: - int64_val = snd_ctl_elem_value_get_integer64(ctl->def_val, 0); + int64_val = snd_ctl_elem_value_get_integer64(val, index); if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) { - ksft_print_msg("%s value %lld less than minimum %lld\n", - ctl->name, int64_val, + ksft_print_msg("%s.%d value %lld less than minimum %lld\n", + ctl->name, index, int64_val, snd_ctl_elem_info_get_min64(ctl->info)); - err = -1; + return false; } if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) { - ksft_print_msg("%s value %lld more than maximum %lld\n", - ctl->name, int64_val, + ksft_print_msg("%s.%d value %lld more than maximum %lld\n", + ctl->name, index, int64_val, snd_ctl_elem_info_get_max(ctl->info)); - err = -1; + return false; } /* Only check step size if there is one and we're in bounds */ - if (err >= 0 && snd_ctl_elem_info_get_step64(ctl->info) && + if (snd_ctl_elem_info_get_step64(ctl->info) && (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) % snd_ctl_elem_info_get_step64(ctl->info)) { - ksft_print_msg("%s value %lld invalid for step %lld minimum %lld\n", - ctl->name, int64_val, + ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n", + ctl->name, index, int64_val, snd_ctl_elem_info_get_step64(ctl->info), snd_ctl_elem_info_get_min64(ctl->info)); - err = -1; + return false; } break; default: /* No tests for other types */ + break; + } + + return true; +} + +/* + * Check that the provided value meets the constraints for the + * provided control. + */ +bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val) +{ + int i; + bool valid = true; + + for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) + if (!ctl_value_index_valid(ctl, val, i)) + valid = false; + + return valid; +} + +/* + * Check that we can read the default value and it is valid. Write + * tests use the read value to restore the default. + */ +void test_ctl_get_value(struct ctl_data *ctl) +{ + int err; + + /* If the control is turned off let's be polite */ + if (snd_ctl_elem_info_is_inactive(ctl->info)) { + ksft_print_msg("%s is inactive\n", ctl->name); + ksft_test_result_skip("get_value.%d.%d\n", + ctl->card->card, ctl->elem); + return; + } + + /* Can't test reading on an unreadable control */ + if (!snd_ctl_elem_info_is_readable(ctl->info)) { + ksft_print_msg("%s is not readable\n", ctl->name); ksft_test_result_skip("get_value.%d.%d\n", ctl->card->card, ctl->elem); return; } + err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val); + if (err < 0) { + ksft_print_msg("snd_ctl_elem_read() failed: %s\n", + snd_strerror(err)); + goto out; + } + + if (!ctl_value_valid(ctl, ctl->def_val)) + err = -EINVAL; + out: ksft_test_result(err >= 0, "get_value.%d.%d\n", ctl->card->card, ctl->elem);