From nobody Mon Jun 8 06:35:49 2026 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 D53E71E7C23 for ; Sat, 6 Jun 2026 03:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780715009; cv=none; b=o3hR7sk4boeKPpFcw0CIfMtj+I+r1Hu/NrciFcQDsG/V92D7RH81lSNx+nzWevfhmLiX3mLUFifXr8hKBIXTBKKVKZUc8vwLMnfvSoPPZuWvi34lQOraHHzPP5MCCaiWKwMLdYOb41XQhUcV8HgduELPUm23lk5DHEysRy1RcDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780715009; c=relaxed/simple; bh=ky/O851hUVScoQCJyHMEdXAknbRDO2OmkuDtVhtUaWM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=QY6vJ0F5725aPw+UGlEdNC7oYEHlI05dlvEYLJ7cOck2yr+7FjQQj8h7lBYzH/AqVlGh65NyECYGT3JoQ312hibwUiygb6jjjBGWPRuvl3PYKXH4Xvs9lADFEt5ctaW/myVZY9TdMEyRlmYHjkB53FJTWc8tH+4+jWb0N985nR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CxM+T2UJ; arc=none smtp.client-ip=209.85.216.53 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CxM+T2UJ" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-36b8d414666so1609355a91.3 for ; Fri, 05 Jun 2026 20:03:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780715006; x=1781319806; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=wkeCClWpNQ+LEjlwBYSSWBLUbENqf+0YKBgoFzbsoiQ=; b=CxM+T2UJod8LtEX3infGCbradUGqW107DRWd+lDNLEl7Op+ucCGc01x79yfUNra2/k GuQlapX3fXbCCPPXCcvIzLJ9DGxhmOvhhyHoYdrXX2+gmCMBz7E3EJfEPrwl5j9pYD29 G8F0/eDzry9Q5xn1k3HR1SMRmZAfYP+975aoNZFYGwFA+KugfcLfgY2fJ77aFGgFNQHe RxEd5v00J18kRym3iabOViOQxNbkbDFKsWEXCKynsXtSm/bYsS721yxk/d0xb5lWvmJD HH1mb212f1uXkt4hxrviAc3hTU5CqpmRsXMSYKrNMmni6rg9rkX/hGoacxJDxLO4VCqq 08Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780715006; x=1781319806; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=wkeCClWpNQ+LEjlwBYSSWBLUbENqf+0YKBgoFzbsoiQ=; b=dpZcQ6jzWW9zwYehwJKeaMh2hMOFfathTpku2suks51K2KsdRE3FYZ1l34xE+hbOpF Ackn89LS1FaUsRsngtRSasRbeM7GCoxUAohj0lyxJIMSJvVLJG0vcJTG/TTAe/YZMEx3 H5dHZMReTfnrAFMCvy2yopgUEnbZKaPuHkiecEW8oeNdZ5gxqnMQNV1fq3gB/JhyyP2O 94lykTwmHHf6fm6nxZIZHWlFzCgnRDMW7SKREhmUjyaTAVA6KZbpZE7lC4ioIfkciC2+ mxvfZf5SI3cjajxoHQ6wRwpH25bd/pxzly2aBcTnsaXSfgvGSLA0nwSRhEhp+1ekePKP CUYA== X-Forwarded-Encrypted: i=1; AFNElJ97ZsoALYGuK5eFVuZr+v6a3JAW2SxMyrv/mFndEsy9YHWjSu/EPbshy9kYhnqBZlZOQI2VxUvXVDljXLk=@vger.kernel.org X-Gm-Message-State: AOJu0YzcYYq7qNHAA6TIBfrMy5vY+H6rZ2vpI6655uidyeeCo975kcBd f+ZKpIpP1imkwLrqRfoJTHj99OVXqX2cYixcxSBoA4FZYF9eNZ2m2vfh X-Gm-Gg: Acq92OFNYgLkpFP6EQYPP9/c3FAajpU37NG+wO00fvN5O4f24bUwwwGxW2FgLdJGeyI gCEf2A91PB3+dass5WvjNo0RhgohG9klkSR2LLqNGTK90z89H/2dbRyqKz/t4dDyxePdLpbxgO8 gMouuUpsZ5A/g2wXwy6h7S7zmiwLHYQRyqU7f4i2rbWMNlXjBpp2ak3DVt9fXVBUeVBDd5rtkNJ pSZ4QxBr7P58MHc1ALkK48lOaj12FhI+uXpwLkzrJCHgI690qPUqssBge9nNCJkF0gQ0LDTA2mG E0r8TCRSjBfs8vqQep/FMLzGu2/MYW5/AA+QeB/jc3QdtLFBt5LvV7tz+et7mKluC/gOj2on6Oe MpbVjhvoDBeBhbx1cuaxLI+YZQf8YPZO4KfxINB+Kxhz28Pti8kePQ9knS40FluU2GfG6rkxMDU jfUSzmfZvjRjkfRui+rGBuRbt43gyzsgGs4Ntzv4g6oMEw2rNeHkzvGrYmN93gCkSSjggTfIf5D QkJ+wzEtmQhmGYJtPKe6HrhvXbVJuRC0ZSjBecLGZ6RptmONSKiJTq8hMXDmj78lg22/vIgro8k 4IRQpa4c3YCmR+0XOyhisYb1nECugwa8Jv3XCAPPiG818LVMRnLx X-Received: by 2002:a17:90b:4fcd:b0:36d:b680:3029 with SMTP id 98e67ed59e1d1-370eeff1da0mr7178373a91.1.1780715005882; Fri, 05 Jun 2026 20:03:25 -0700 (PDT) Received: from debian13.localdomain (bb219-75-96-14.singnet.com.sg. [219.75.96.14]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3713346cd9fsm3685124a91.2.2026.06.05.20.03.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jun 2026 20:03:25 -0700 (PDT) From: Adi Nata To: Andrew Morton , Adi Nata , linux-kernel@vger.kernel.org Cc: linux-kernel-mentees@lists.linux.dev Subject: [PATCH] lib/math: add KUnit test suite for polynomial_calc() Date: Sat, 6 Jun 2026 11:03:09 +0800 Message-ID: <20260606030319.316752-1-adinata.softwareengineer@gmail.com> X-Mailer: git-send-email 2.47.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a KUnit test suite for the polynomial_calc() function, which had no in-kernel test coverage. The tests verify correct evaluation of constant, linear, quadratic, and cubic polynomials, including negative coefficients, negative input data, zero-coefficient terms. The Kconfig entry uses 'select POLYNOMIAL' rather than 'depends on POLYNOMIAL' because POLYNOMIAL is a promptless tristate that cannot be manually enabled on UML without an explicit selector. Signed-off-by: Adi Nata --- lib/Kconfig.debug | 17 ++ lib/math/tests/Makefile | 1 + lib/math/tests/polynomial_kunit.c | 270 ++++++++++++++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 lib/math/tests/polynomial_kunit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 8ff5adcfe1e0..5dc1b4579903 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3489,6 +3489,23 @@ config GCD_KUNIT_TEST =20 If unsure, say N =20 +config POLYNOMIAL_KUNIT_TEST + tristate "Polynomial calculation (polynomial_calc) test" if !KUNIT_ALL_TE= STS + depends on KUNIT + select POLYNOMIAL + default KUNIT_ALL_TESTS + help + This option enables the KUnit test suite for the polynomial_calc() + function, which evaluates integer polynomials using factor + redistribution to avoid overflow. + + The test suite verifies correctness for constant, linear, and + quadratic polynomials, negative coefficients, per-step dividers, + divider_leftover, total_divider scaling, and a real sensor + N-to-temperature conversion polynomial. + + If unsure, say N + config PRIME_NUMBERS_KUNIT_TEST tristate "Prime number generator test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/math/tests/Makefile b/lib/math/tests/Makefile index 13dc96e48408..85e1ad59f29d 100644 --- a/lib/math/tests/Makefile +++ b/lib/math/tests/Makefile @@ -4,5 +4,6 @@ obj-$(CONFIG_GCD_KUNIT_TEST) +=3D gcd_kunit.o obj-$(CONFIG_INT_LOG_KUNIT_TEST) +=3D int_log_kunit.o obj-$(CONFIG_INT_POW_KUNIT_TEST) +=3D int_pow_kunit.o obj-$(CONFIG_INT_SQRT_KUNIT_TEST) +=3D int_sqrt_kunit.o +obj-$(CONFIG_POLYNOMIAL_KUNIT_TEST) +=3D polynomial_kunit.o obj-$(CONFIG_PRIME_NUMBERS_KUNIT_TEST) +=3D prime_numbers_kunit.o obj-$(CONFIG_RATIONAL_KUNIT_TEST) +=3D rational_kunit.o diff --git a/lib/math/tests/polynomial_kunit.c b/lib/math/tests/polynomial_= kunit.c new file mode 100644 index 000000000000..ef443b57fc12 --- /dev/null +++ b/lib/math/tests/polynomial_kunit.c @@ -0,0 +1,270 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +struct polynomial_test_param { + const struct polynomial *poly; + long data; + long expected; + const char *name; +}; + +/* f(x) =3D 5 */ +static const struct polynomial poly_constant =3D { + .total_divider =3D 1, + .terms =3D { + {0, 5, 1, 1}, + } +}; + +/* f(x) =3D 2x^2 + 3x + 5 */ +static const struct polynomial poly_simple =3D { + .total_divider =3D 1, + .terms =3D { + {2, 2, 1, 1}, + {1, 3, 1, 1}, + {0, 5, 1, 1}, + } +}; + +/* f(x) =3D -5x + 100 */ +static const struct polynomial poly_negative_coef =3D { + .total_divider =3D 1, + .terms =3D { + {1, -5, 1, 1}, + {0, 100, 1, 1}, + } +}; + +/* f(x) =3D (150x + 50) / 10 */ +static const struct polynomial poly_total_divider =3D { + .total_divider =3D 10, + .terms =3D { + {1, 150, 1, 1}, + {0, 50, 1, 1}, + } +}; + +/* + * f(x) =3D x / 2 + * divider=3D2 applied once per multiply: mult_frac(coef, data, 2) =3D coe= f*data/2 + */ +static const struct polynomial poly_step_divider =3D { + .total_divider =3D 1, + .terms =3D { + {1, 1, 2, 1}, + {0, 0, 1, 1}, + } +}; + +/* + * f(x) =3D (100/500) * x^2 =3D 0.2 * x^2 + * Encoded as coef=3D100, divider=3D10, divider_leftover=3D5: + * denom =3D 10^2 * 5 =3D 500 + */ +static const struct polynomial poly_leftover =3D { + .total_divider =3D 1, + .terms =3D { + {2, 100, 10, 5}, + {0, 0, 1, 1}, + } +}; + +/* + * f(x) =3D 2x^3 (single high-degree term, no constant) + * Used to exercise the power loop alone. + */ +static const struct polynomial poly_cubic =3D { + .total_divider =3D 1, + .terms =3D { + {3, 2, 1, 1}, + {0, 0, 1, 1}, + } +}; + +/* + * f(x) =3D 4x + 1 with a zero-coefficient quadratic term. + * The deg-2 term contributes nothing regardless of input. + */ +static const struct polynomial poly_zero_coef =3D { + .total_divider =3D 1, + .terms =3D { + {2, 0, 1, 1}, + {1, 4, 1, 1}, + {0, 1, 1, 1}, + } +}; + +/* + * f(x) =3D 9 with total_divider =3D 0. + * The implementation treats 0 as 1 via `total_divider ?: 1`, so the + * result must equal the constant term unchanged. + */ +static const struct polynomial poly_zero_total_divider =3D { + .total_divider =3D 0, + .terms =3D { + {0, 9, 1, 1}, + } +}; + + +static const struct polynomial_test_param test_params[] =3D { + { + .poly =3D &poly_constant, + .data =3D 0, + .expected =3D 5, + .name =3D "Constant polynomial at x=3D0", + }, + { + .poly =3D &poly_constant, + .data =3D 42, + .expected =3D 5, + .name =3D "Constant polynomial is independent of input", + }, + { + .poly =3D &poly_simple, + .data =3D 0, + .expected =3D 5, /* zero input collapses all power terms */ + .name =3D "Zero input yields constant term only", + }, + { + .poly =3D &poly_simple, + .data =3D 10, + .expected =3D 235, /* 2*100 + 3*10 + 5 */ + .name =3D "Simple quadratic at x=3D10", + }, + { + .poly =3D &poly_negative_coef, + .data =3D 10, + .expected =3D 50, /* -5*10 + 100 */ + .name =3D "Negative coefficient at x=3D10", + }, + { + .poly =3D &poly_negative_coef, + .data =3D 20, + .expected =3D 0, /* -5*20 + 100 =3D 0 */ + .name =3D "Negative coefficient result is zero", + }, + { + .poly =3D &poly_total_divider, + .data =3D 3, + .expected =3D 50, /* (150*3 + 50) / 10 =3D 500/10 */ + .name =3D "total_divider scales the final sum", + }, + { + .poly =3D &poly_step_divider, + .data =3D 100, + .expected =3D 50, /* 1*100/2 */ + .name =3D "Per-step divider halves input", + }, + { + .poly =3D &poly_leftover, + .data =3D 30, + .expected =3D 180, /* 100*30^2 / (10^2 * 5) =3D 90000/500 */ + .name =3D "divider_leftover with quadratic term", + }, + /* Boundary: unit and negative-unit input */ + { + /* + * data=3D1: each mult_frac(tmp, 1, divider) strips one factor of + * divider from coef per degree, so coef is left-shifted right + * until intermediate precision is exhausted. + * 2*1 + 3*1 + 5 =3D 10 + */ + .poly =3D &poly_simple, + .data =3D 1, + .expected =3D 10, + .name =3D "Boundary: data=3D1 (unit input)", + }, + { + /* + * data=3D-1: even degrees produce positive contributions, + * odd degrees produce negative ones. + * 2*(-1)^2 + 3*(-1) + 5 =3D 2 - 3 + 5 =3D 4 + */ + .poly =3D &poly_simple, + .data =3D -1, + .expected =3D 4, + .name =3D "Boundary: data=3D-1 (negative unit input)", + }, + + /* Boundary: negative non-trivial input */ + { + /* + * 2*(-3)^2 + 3*(-3) + 5 =3D 18 - 9 + 5 =3D 14 + * Verifies sign handling for negative data across all degrees. + */ + .poly =3D &poly_simple, + .data =3D -3, + .expected =3D 14, + .name =3D "Boundary: negative data with quadratic", + }, + + /* Boundary: total_divider =3D 0 is treated as 1 */ + { + .poly =3D &poly_zero_total_divider, + .data =3D 42, + .expected =3D 9, + .name =3D "Boundary: total_divider=3D0 defaults to 1", + }, + + /* Boundary: zero-coefficient high-degree term */ + { + /* + * The deg-2 term has coef=3D0, so it contributes 0 regardless + * of data. Result: 0 + 4*10 + 1 =3D 41 + */ + .poly =3D &poly_zero_coef, + .data =3D 10, + .expected =3D 41, + .name =3D "Boundary: zero-coefficient term is inert", + }, + + /* Boundary: single high-degree term, no constant */ + { + /* 2 * 5^3 =3D 250; also verifies the loop terminates on deg-0 */ + .poly =3D &poly_cubic, + .data =3D 5, + .expected =3D 250, + .name =3D "Boundary: single cubic term", + }, + { + /* 2 * (-2)^3 =3D -16; odd power preserves sign of negative data */ + .poly =3D &poly_cubic, + .data =3D -2, + .expected =3D -16, + .name =3D "Boundary: single cubic term, negative data", + }, + +}; + +static void get_desc(const struct polynomial_test_param *param, char *desc) +{ + strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE); +} + +KUNIT_ARRAY_PARAM(polynomial, test_params, get_desc); + +static void polynomial_calc_test(struct kunit *test) +{ + const struct polynomial_test_param *param =3D test->param_value; + + KUNIT_EXPECT_EQ(test, polynomial_calc(param->poly, param->data), + param->expected); +} + +static struct kunit_case polynomial_test_cases[] =3D { + KUNIT_CASE_PARAM(polynomial_calc_test, polynomial_gen_params), + {} +}; + +static struct kunit_suite polynomial_test_suite =3D { + .name =3D "math-polynomial", + .test_cases =3D polynomial_test_cases, +}; + +kunit_test_suites(&polynomial_test_suite); + +MODULE_DESCRIPTION("math.polynomial_calc KUnit test suite"); +MODULE_LICENSE("GPL"); --=20 2.47.3