Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1040929lqt; Fri, 7 Jun 2024 06:35:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXqHUrZi6kOasrb7BIPz+WdJZul+f8XwixCvw+eea9G0bteOD7ezxg6QfXoTEOP7TilezVt29L+Ro385awKO5O7VKVcFrdlqKhdeYaqxw== X-Google-Smtp-Source: AGHT+IF51UK4Vo1vE6sMZhDy19KH0fAWqD8ccsH+9Vfm31OyvX7nqDBy3QUGn1oWywUsSHG2JKAQ X-Received: by 2002:a05:6870:1703:b0:254:8498:5a20 with SMTP id 586e51a60fabf-25484985d7dmr599738fac.59.1717767303294; Fri, 07 Jun 2024 06:35:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717767303; cv=pass; d=google.com; s=arc-20160816; b=k7bmX4fWEf0ur8+D//7H6TooN1VSfpVIJULkCQ/RV95N/YDBh31HLKScScdPpFDbzR eUQ3BoSDCWas/Bu78cq8C2nyPxUSDfDgo7rdNnO8ChirBwdxIVVR6AXZIrjOFZBKUnQI v/yheKNjCgtDlOwPK+AFTbnZW2dxovGLZ5Mg0uyyLJlXURxEPauHLottuG5iNpC01Wbv lAU/p33UTFzG0m5aKzJcUir91gHIZJNY0sgzeUL9zg+g8fg+JbG9fEdxS3a6K71x3O+C PwSXvj4SKpqVN/seuo8lwDLHH6HwY/8SEMffVPpXAGblLaY0n3PmqvkuEG35kMlAulsK oT+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cxjK4DYOMf+01WJ2Qh8aciagA8IuXgOO5npXQIszqls=; fh=q8ciodeUhhSUl1vkGMisO0DFQQjOd0K/8tvREZ69uYc=; b=JxlMimGWIPp7O2u9eCQMNuR4WkuevxKy+UqnDwFJVkHexTKysjL0sSeDi7Om0V0b7I FqOn2QsBvkkjXIlO5RyobXtl9GbSOtz00v2X9QLTspNHNjv7vhxmgY5cjhTzyg+7XAzz 92WUjvZn+2QePqvWp/hR3h9gEFYRjti6tNlhDCaHweDSTww8oRUmxdqSth2AyXUzaVbm Ui+4PqMOSBaGO+UkR5M0p2+EBqUx86wrNroECHTDiDIon6++MhfCA37kUbwugVNjf4oA zDeBCr337nNtST82XOszIBX+btnGUMroVlc+lbakH7naBb5ZU9VtMYoYcUSXyJIlHs6p fMlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=mdu1SN37; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-206167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-79538e0badasi85489185a.129.2024.06.07.06.35.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 06:35:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=mdu1SN37; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-206167-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206167-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.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 029531C20CA8 for ; Fri, 7 Jun 2024 13:35:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16FFF1991D4; Fri, 7 Jun 2024 13:32:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="mdu1SN37" Received: from lelv0143.ext.ti.com (lelv0143.ext.ti.com [198.47.23.248]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97E241922F9; Fri, 7 Jun 2024 13:32:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.248 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767158; cv=none; b=MOdAr4il4jlcKqf8DrnIQNCzd7h5+8z2ZblZvZLv1AVkbbC2YDv9AOL3g/9SwgAjMvv8lCvq0xCFSedfKwy88DyjMwb2vEMcU4k2aMgbw3pcRkHKW3tIsn67isbnGfPQeqcuQ0u1K25naAajfmYmuHNWB0vFEfTzE8ai4q8xkw8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717767158; c=relaxed/simple; bh=OB9nuMUaz2RJ7fDsZfmD6NlU0tEuo2P3GDvpOX39uHs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fh9mZEHtJrh5hJ/aeZy8oDTewI7C2JeMywzjtsmSLjxGdZaB4l1Fl71rq3zm5ClVgDNHgKD6b1b4QdBCR0Xus/xHbDoWkFR/fvXZJwjkW+bLEu5Gw65DJQvtThnju3IeXiUiZ88mNnGtyfLeLVMzA03RC+Tp1GlsRsNOoR1/nic= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=mdu1SN37; arc=none smtp.client-ip=198.47.23.248 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id 457DWKYH086877; Fri, 7 Jun 2024 08:32:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1717767140; bh=cxjK4DYOMf+01WJ2Qh8aciagA8IuXgOO5npXQIszqls=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=mdu1SN37oY8CfEk05w2niFM7d5+o3Hn11jSxwb9H44ylAIKMBoLCzB4RTHjVKyXxF qCMIK5RuWkgWvZFY13d41lf48NzXPBUXyo3xyP82CZ13G415F9+wrdphcH3X9Fo6uy FVYjm0VKlrOFhugL0gnyxe7QfZpRo/SHyHEpxSIU= Received: from DLEE115.ent.ti.com (dlee115.ent.ti.com [157.170.170.26]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 457DWKt4099910 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 7 Jun 2024 08:32:20 -0500 Received: from DLEE105.ent.ti.com (157.170.170.35) by DLEE115.ent.ti.com (157.170.170.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Fri, 7 Jun 2024 08:32:20 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DLEE105.ent.ti.com (157.170.170.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Fri, 7 Jun 2024 08:32:20 -0500 Received: from localhost (ti.dhcp.ti.com [172.24.227.95] (may be forged)) by lelvsmtp5.itg.ti.com (8.15.2/8.15.2) with ESMTP id 457DWJ85046487; Fri, 7 Jun 2024 08:32:20 -0500 From: Devarsh Thakkar To: , , , , , , , , , , , , , , , CC: , , , , , , , , , , , , , Subject: [PATCH v13 11/13] lib: math_kunit: Add tests for new macros related to rounding to nearest value Date: Fri, 7 Jun 2024 19:02:19 +0530 Message-ID: <20240607133219.3558319-1-devarsht@ti.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240607131900.3535250-1-devarsht@ti.com> References: <20240607131900.3535250-1-devarsht@ti.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Add tests for round_closest_up/down and roundclosest macros which round to nearest multiple of specified argument. These are tested with kunit tool as shared here [1] : Link: https://gist.github.com/devarsht/3f9042825be3da4e133b8f4eda067876 [1] Signed-off-by: Devarsh Thakkar Acked-by: Andy Shevchenko --- V1->V13 (No change, patch introduced in V8) lib/math/math_kunit.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/math/math_kunit.c b/lib/math/math_kunit.c index be27f2afb8e4..05022f010be6 100644 --- a/lib/math/math_kunit.c +++ b/lib/math/math_kunit.c @@ -70,6 +70,26 @@ static void round_down_test(struct kunit *test) KUNIT_EXPECT_EQ(test, round_down((1 << 30) - 1, 1 << 29), 1 << 29); } +static void round_closest_up_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, round_closest_up(17, 4), 16); + KUNIT_EXPECT_EQ(test, round_closest_up(15, 4), 16); + KUNIT_EXPECT_EQ(test, round_closest_up(14, 4), 16); + KUNIT_EXPECT_EQ(test, round_closest_up((1 << 30) - 1, 1 << 30), 1 << 30); + KUNIT_EXPECT_EQ(test, round_closest_up((1 << 30) + 1, 1 << 30), 1 << 30); + KUNIT_EXPECT_EQ(test, round_closest_up((1 << 30) - 1, 2), 1 << 30); +} + +static void round_closest_down_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, round_closest_down(17, 4), 16); + KUNIT_EXPECT_EQ(test, round_closest_down(15, 4), 16); + KUNIT_EXPECT_EQ(test, round_closest_down(14, 4), 12); + KUNIT_EXPECT_EQ(test, round_closest_down((1 << 30) - 1, 1 << 30), 1 << 30); + KUNIT_EXPECT_EQ(test, round_closest_down((1 << 30) + 1, 1 << 30), 1 << 30); + KUNIT_EXPECT_EQ(test, round_closest_down((1 << 30) - 1, 2), (1 << 30) - 2); +} + /* These versions can round to numbers that aren't a power of two */ static void roundup_test(struct kunit *test) { @@ -95,6 +115,18 @@ static void rounddown_test(struct kunit *test) KUNIT_EXPECT_EQ(test, rounddown(4, 3), 3); } +static void roundclosest_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, roundclosest(21, 5), 20); + KUNIT_EXPECT_EQ(test, roundclosest(19, 5), 20); + KUNIT_EXPECT_EQ(test, roundclosest(17, 5), 15); + KUNIT_EXPECT_EQ(test, roundclosest((1 << 30), 3), (1 << 30) - 1); + KUNIT_EXPECT_EQ(test, roundclosest((1 << 30) - 1, 1 << 29), 1 << 30); + + KUNIT_EXPECT_EQ(test, roundclosest(4, 3), 3); + KUNIT_EXPECT_EQ(test, roundclosest(5, 3), 6); +} + static void div_round_up_test(struct kunit *test) { KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(0, 1), 0); @@ -272,8 +304,11 @@ static struct kunit_case math_test_cases[] = { KUNIT_CASE(int_sqrt_test), KUNIT_CASE(round_up_test), KUNIT_CASE(round_down_test), + KUNIT_CASE(round_closest_up_test), + KUNIT_CASE(round_closest_down_test), KUNIT_CASE(roundup_test), KUNIT_CASE(rounddown_test), + KUNIT_CASE(roundclosest_test), KUNIT_CASE(div_round_up_test), KUNIT_CASE(div_round_closest_test), KUNIT_CASE_PARAM(gcd_test, gcd_gen_params), -- 2.39.1