Received: by 2002:a05:7208:2202:b0:86:316c:7444 with SMTP id s2csp2380644rbb; Tue, 4 Jun 2024 03:53:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUb6FgdNbUgCu7VibfVdWC76/7Delx7hDmsNyoCrlypbSmaLNtD2w5lKwI/vV1UrW99DRDsRL0ZAt1gZjLWV65SCfs1hQQChF7ttrR9EA== X-Google-Smtp-Source: AGHT+IGhdIkMoCoHHlf/qF6Vk7oiYHSuOvJUWa5NQxL7I6/sv5YpEDtUERGFaZ/lGZtHUBGTYuS6 X-Received: by 2002:a17:906:d8e:b0:a69:52a:8d7d with SMTP id a640c23a62f3a-a69052a8dd1mr370768166b.40.1717498420007; Tue, 04 Jun 2024 03:53:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717498419; cv=pass; d=google.com; s=arc-20160816; b=OFeFE9CJ5r1DJsuXmuP9dhmfr1xvD48dsRBw59Lq8xqVSFwaPBNmGmaN98ev9nx3P7 /jqkgnlCRHK8kofzWsr4McmZESIxepc0CBKQWmmGVQ52Fpn4miXEnWXGB1kotxbXhXMk N1bfHvb0xJUJz2pS6SaQUlhs5aEr9YYMnAL8Td4jKDpWJmuDD3kSWiG4lL2nnCXl739w DuJQreKLrcyuukaY7wntHq4JxssAAbT/JgvkD8lw580AttvwlEwKvFxCbWVrwP4hfa3/ L6TJfHOp5bvBYJLALHNg3HVp/m11XrxEoiZAU91N5U+8Hd79tcQFz5iSRL8OBTDzTRD0 6XRQ== 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=VH0U6ipfvCppDUeQDKUpcWDfD7uSeboDDJhYoTRy+XI=; fh=q8ciodeUhhSUl1vkGMisO0DFQQjOd0K/8tvREZ69uYc=; b=btiJw1LHNDCyZpZfrJM+k2XjZhT1z/mrclNH7hvOx0NEbedqFcX1r4uDdqhKI0y1Th /Kse/29pqvdiYo2cjH1q/6GurXNzJ+yCmU1Ba/iBb4dNMduF06UrvjgDMLpCwXqIQQFU rMYVBgPJ9o7KUwvc891HunHV8TeHMrAynmandVXQV9ZoZsFcXyFUSrxiUw1Fs0j+q6xu 8Go0rNsktzLaQ32X2VDO5qlD4e39pWJ2e6Rob1UaKwmGRyANdbCdX5TQQdAlD3de15Mq Y3LpzZRDXDELGuXWqSg/XKvNaepjbmmUaxMk1wcCvPrstVF9mbd66MZJpuCc/l5VZZtt qE9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b="YDGB/fk1"; 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-200472-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200472-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a68a321e311si371792766b.687.2024.06.04.03.53.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 03:53:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200472-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b="YDGB/fk1"; 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-200472-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200472-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 375551F22477 for ; Tue, 4 Jun 2024 10:53:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 737FA145331; Tue, 4 Jun 2024 10:53:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="YDGB/fk1" Received: from fllv0016.ext.ti.com (fllv0016.ext.ti.com [198.47.19.142]) (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 193D038B; Tue, 4 Jun 2024 10:53:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.19.142 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717498409; cv=none; b=EA9p+qmvgB8sH2rMOMY4Oo6kQp8oqQUV/HBFB2BVEKci2Z2aq5MsrYljFTYRbDNicMaIMLK29O+jOeokmJ/sg0G0SNKK78Cb4xXOrrhfDq/kJyQ4DGF3HZwHjN1x3C5tmk7RfhfwiSFWbO+zla9aEYUco/21AgvSH4bAmoA+JlI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717498409; c=relaxed/simple; bh=EiWf4wFiIjCYYK9oYBVIGKDKC6ckMcj4EWCpgi0/ChE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Kz4G2Op4+JVB1EFzBfLBUzi2qzV3h0kVD/ym7wBYQFI4u6/RpmCaq6lGBV3LIBaqMwAlnFN+5d9Wrm74D1kz67ZeqjjUpZE68VKw5Cpv70xS9JbCgN02Nl4p9AH5vSLsvobiLzCeo7Cm6BEcYtuAWuNXV83PJuIMpaPRCk678JU= 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=YDGB/fk1; arc=none smtp.client-ip=198.47.19.142 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 lelv0266.itg.ti.com ([10.180.67.225]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 454ArDoY085968; Tue, 4 Jun 2024 05:53:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1717498393; bh=VH0U6ipfvCppDUeQDKUpcWDfD7uSeboDDJhYoTRy+XI=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=YDGB/fk1NJ0OqOSrWzHMLPMq+tqbJ77ElChnMjpPyfdQ7VDU3j49WIWkVAdMa5ZBh O+zNwUCKbyRG355xqBd7ATm+RfmI5lqNsVOJ4hCx4sohBDUO9u+GPhTHRIPfMQ13Gv vK7Z3IAud3EEilUX62juQvfST7ipM5MTGD1Qt0P8= Received: from DFLE113.ent.ti.com (dfle113.ent.ti.com [10.64.6.34]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 454ArD2g061520 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 4 Jun 2024 05:53:13 -0500 Received: from DFLE102.ent.ti.com (10.64.6.23) by DFLE113.ent.ti.com (10.64.6.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Tue, 4 Jun 2024 05:53:13 -0500 Received: from lelvsmtp6.itg.ti.com (10.180.75.249) by DFLE102.ent.ti.com (10.64.6.23) 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; Tue, 4 Jun 2024 05:53:12 -0500 Received: from localhost (ti.dhcp.ti.com [172.24.227.95] (may be forged)) by lelvsmtp6.itg.ti.com (8.15.2/8.15.2) with ESMTP id 454ArCR2122146; Tue, 4 Jun 2024 05:53:12 -0500 From: Devarsh Thakkar To: , , , , , , , , , , , , , , , CC: , , , , , , , , , , , , , Subject: [PATCH v12 11/13] lib: math_kunit: Add tests for new macros related to rounding to nearest value Date: Tue, 4 Jun 2024 16:23:11 +0530 Message-ID: <20240604105311.2256898-1-devarsht@ti.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240604104001.2235082-1-devarsht@ti.com> References: <20240604104001.2235082-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->V12 (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