From nobody Wed Nov 27 13:04:12 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 CA09120A5E4 for ; Fri, 11 Oct 2024 07:31:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728631867; cv=none; b=FGYfOIixdvPjyAYQrcS3OUDkpX4bcEyW6IpVIg9uchj+oxfpi15MOX9Mo7xCzqvi/ewcyr6O3qFoXu5JNUiAgshk0dhGnPM1O3KYH7l1AlRiVnZfOpZZ3sexEcyksOVuMmDSLRDB/Qe6nFwiV8nBN8bEK4EeVURlZY4Xymn5jv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728631867; c=relaxed/simple; bh=4UNA/bzgNeQKZNn+GTcFYWje5KcHYL5lIgLuvQk+bVg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u2eZnDQe6qslo49Npx7Iz5tH5IUOAx2I2CV6dBLOdzb6WpDd1rMiEajlpByxS72mZj6Q1TPwfz8rETMWY+YVw2IWePNRvMB0Vvnq8ZhSLUYLNLFwEp4FXHbWaA1i3GqiIfcE8zWKoF5fgDYgvqTctYNgMTFW1LKyQBcXa8dOuZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MmdFR+wS; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MmdFR+wS" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e24a31ad88aso2115547276.1 for ; Fri, 11 Oct 2024 00:31:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728631865; x=1729236665; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=hyJ4dk3YkG68rGowxEN01juZ4Qxhn4HXMW65oX2xFoY=; b=MmdFR+wS7HQcaNGwTgXJZmgqQkWg44FK1z72l+XtzfdsLsKk2j+hPEhPICa0Z13W1X /PX9hJGm/av57iTIFO/o8emHKOC4PuXLTva5kcabz8CKKLKIR4SIztYdNGy4s4qjI0AD zWiKCJaJsZELx/T7IIQ4zW9XD/Zdc4sIRs//9sU0YGl+eY1ayB8zTSimwBS/M1/IyVUK Z2xdMcDLQEZlJSKHMoEtfrVQyYMRP5g59FaOrlRJDbmh5q/2xZWEg+0ndlm1grTwz2qb gVbNF2nHQdCp1/sso0cV1woK37JM08m1DHRDQZhpjTBsVhNPgzCg8/Y4Nssz9sonxTxd 0OqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728631865; x=1729236665; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=hyJ4dk3YkG68rGowxEN01juZ4Qxhn4HXMW65oX2xFoY=; b=Tl9CVIXd79L+auxL+SAVEj0G1hXgxAHpNseBLXCm5kAbweEwcTXQrKe52KnS3AhtBO ot7xmvBGzY21RZ6QG2y3e4NEz8A4dwv7Z4MGr38dkWHK2zqdgDUaqT1BMGIEus4ve7MP 5/GiOtocXPXsxprK+EgjTQOnleLfnaFc9nH28Hps3pGuKiXsj88djDdpRnvS22YyUho2 3tCZT3HfAe79Y9YRv1FX6xS801BcQaVBSXICAfc3fH4f4cVZ/RM0uhPNWPsBaFRv2ODr oop/wBN66v9MVlrADA6l6QSaPCU7AL+UhNTALVSBjLSbkTC8WJKgUzWO+NCjJwobH/0K D0og== X-Forwarded-Encrypted: i=1; AJvYcCVhrLYzhFW+vF7gatG8aa8wOgiI/I9bpweKpzuKeqKkMW1d5S44Q1/XbMY5gwniWg5mbjjrD2JNNodeH0w=@vger.kernel.org X-Gm-Message-State: AOJu0YyrAdGb6mPXyRaoQ4TbmETai/b4fXGt/fulv+5egWEsFIyRU7bS 5kC+qasIeMYoQEZmawNztsPe0xMaEQnNw6lcY6XUNl2Guk1bWkD9ATiW1S16SAV55p4SvNBOBv7 zIxI/Ei+i3Q== X-Google-Smtp-Source: AGHT+IEnt/DrbeHagqIrCVFokOqS9C4siCZ3tDEnNfDLr/bUT6Lkv6eaQ6re5XPXQMlsOouyNX83PB9KAt1ZSw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:b1:7045:ac11:6237]) (user=davidgow job=sendgmr) by 2002:a25:844c:0:b0:e27:3e6a:345 with SMTP id 3f1490d57ef6-e2919ff8519mr1303276.10.1728631864951; Fri, 11 Oct 2024 00:31:04 -0700 (PDT) Date: Fri, 11 Oct 2024 15:25:06 +0800 In-Reply-To: <20241011072509.3068328-2-davidgow@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241011072509.3068328-2-davidgow@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241011072509.3068328-4-davidgow@google.com> Subject: [PATCH 2/6] lib/math: Add int_log test suite From: David Gow To: Andrew Morton , Shuah Khan , Brendan Higgins , Rae Moar , Kees Cook Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Stephen Rothwell , "=?UTF-8?q?Bruno=20Sobreira=20Fran=C3=A7a?=" , linux-kernel-mentees@lists.linuxfoundation.org, David Gow Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Bruno Sobreira Fran=C3=A7a This commit introduces KUnit tests for the intlog2 and intlog10 functions, which compute logarithms in base 2 and base 10, respectively. The tests cover a range of inputs to ensure the correctness of these functions across common and edge cases. Signed-off-by: Bruno Sobreira Fran=C3=A7a Reviewed-by: David Gow [Rebased on top of mm-nonmm-unstable] Signed-off-by: David Gow --- lib/Kconfig.debug | 11 +++++ lib/math/tests/Makefile | 1 + lib/math/tests/int_log_kunit.c | 75 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 lib/math/tests/int_log_kunit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 3ed6cf950f04..6571243ac232 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3009,6 +3009,17 @@ config INT_POW_KUNIT_TEST =20 If unsure, say N =20 +config INT_LOG_KUNIT_TEST + tristate "Integer log (int_log) test" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This option enables the KUnit test suite for the int_log library= , which + provides two functions to compute the integer logarithm in base = 2 and + base 10, called respectively as intlog2 and intlog10. + + If unsure, say N + endif # RUNTIME_TESTING_MENU =20 config ARCH_USE_MEMTEST diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile index f9a0a0e6b73a..89a266241e98 100644 --- a/lib/math/tests/Makefile +++ b/lib/math/tests/Makefile @@ -2,5 +2,6 @@ =20 obj-$(CONFIG_DIV64_KUNIT_TEST) +=3D div64_kunit.o obj-$(CONFIG_INT_POW_KUNIT_TEST) +=3D int_pow_kunit.o +obj-$(CONFIG_INT_LOG_KUNIT_TEST) +=3D int_log_kunit.o obj-$(CONFIG_MULDIV64_KUNIT_TEST) +=3D mul_u64_u64_div_u64_kunit.o obj-$(CONFIG_RATIONAL_KUNIT_TEST) +=3D rational_kunit.o diff --git a/lib/math/tests/int_log_kunit.c b/lib/math/tests/int_log_kunit.c new file mode 100644 index 000000000000..d750a1df37c1 --- /dev/null +++ b/lib/math/tests/int_log_kunit.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +struct test_case_params { + u32 value; + unsigned int expected_result; + const char *name; +}; + + +/* The expected result takes into account the log error */ +static const struct test_case_params intlog2_params[] =3D { + {0, 0, "Log base 2 of 0"}, + {1, 0, "Log base 2 of 1"}, + {2, 16777216, "Log base 2 of 2"}, + {3, 26591232, "Log base 2 of 3"}, + {4, 33554432, "Log base 2 of 4"}, + {8, 50331648, "Log base 2 of 8"}, + {16, 67108864, "Log base 2 of 16"}, + {32, 83886080, "Log base 2 of 32"}, + {U32_MAX, 536870911, "Log base 2 of MAX"}, +}; + +static const struct test_case_params intlog10_params[] =3D { + {0, 0, "Log base 10 of 0"}, + {1, 0, "Log base 10 of 1"}, + {6, 13055203, "Log base 10 of 6"}, + {10, 16777225, "Log base 10 of 10"}, + {100, 33554450, "Log base 10 of 100"}, + {1000, 50331675, "Log base 10 of 1000"}, + {10000, 67108862, "Log base 10 of 10000"}, + {U32_MAX, 161614247, "Log base 10 of MAX"} +}; + +static void get_desc(const struct test_case_params *tc, char *desc) +{ + strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE); +} + + +KUNIT_ARRAY_PARAM(intlog2, intlog2_params, get_desc); + +static void intlog2_test(struct kunit *test) +{ + const struct test_case_params *tc =3D (const struct test_case_params *)te= st->param_value; + + KUNIT_EXPECT_EQ(test, tc->expected_result, intlog2(tc->value)); +} + +KUNIT_ARRAY_PARAM(intlog10, intlog10_params, get_desc); + +static void intlog10_test(struct kunit *test) +{ + const struct test_case_params *tc =3D (const struct test_case_params *)te= st->param_value; + + KUNIT_EXPECT_EQ(test, tc->expected_result, intlog10(tc->value)); +} + +static struct kunit_case math_int_log_test_cases[] =3D { + KUNIT_CASE_PARAM(intlog2_test, intlog2_gen_params), + KUNIT_CASE_PARAM(intlog10_test, intlog10_gen_params), + {} +}; + +static struct kunit_suite int_log_test_suite =3D { + .name =3D "math-int_log", + .test_cases =3D math_int_log_test_cases, +}; + +kunit_test_suites(&int_log_test_suite); + +MODULE_DESCRIPTION("math.int_log KUnit test suite"); +MODULE_LICENSE("GPL"); + --=20 2.47.0.rc1.288.g06298d1525-goog