Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1101624lqo; Fri, 17 May 2024 10:38:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW83qTGRc3mUF1gYU/fMlfLnNmITmUgQdZYJN8P2pfrxm5Y3WNQzKBR5Q802kn7tl/t3ooURW+CLiSytQiyWVbpj2W5imqDdbuEJXl+iQ== X-Google-Smtp-Source: AGHT+IEVvbAuFzCvwuEC60Lpki9CttTwWMAh0y5meuOk9UjULF1r82Ksy7w2c0SKu+v/GB86hrH+ X-Received: by 2002:a05:620a:1129:b0:792:c10c:dc82 with SMTP id af79cd13be357-792c7574a13mr2485130385a.7.1715967489789; Fri, 17 May 2024 10:38:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715967489; cv=pass; d=google.com; s=arc-20160816; b=khz4QnFN7GULfmIRa8dKU8mMkHAnOW8aseDXExKDybiMJY9opTPn1AFf7cfnABiEGq y19edy2iS1t28KP64GXDPlMb+VkY1qnIw3LAcnlk/nmPbHc8CkP0Zwbp/ItoVqtvXhXp H7WFBYVrglWgYyEh7B4v+3+KYApkF3HHi/mr+B59YoTfNHeZOfDS9o6KPg9tNLKIM+/P 1/2QSrWfSg65bNF9xYJUfR+vaY9pB44FoxKL6Cf8LG2VyzE6merYmth+ZkDb8dqoIoEl xdjC0PbBylif4qw6uIoBb13yBUfxDWs9kopgqIS9vQGNnaQoHGwdg9YmRDMPUpiU+Ikj Gs+A== 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=QwzJlg6Go90nYaTZryV4XJOfLUk1jVtqeECp9jUGAUg=; fh=q8ciodeUhhSUl1vkGMisO0DFQQjOd0K/8tvREZ69uYc=; b=RI3K9D3/x9n+6XXbuBuJpWY99Xi3OmQAzlSOxJh6oH6vzCEIqhNAySI2Ux4d1m6Yo4 23cZQ9PR5tk/r/HXtXGLqxsxngQD3oKKVlqeqiGXdVk+3EFZ+Mq6YnEO0HF9k3n0kr3M HVH/iyd/+clZuJy4L5r+vl3zljhvMfmYaKaJacHDTok98a90adkYZu9dyyumk4IRn0P+ ESZJhfhxnOZQVJ8pnN+CDHMjiZJ/GiDDIOWHN2Y2zT0pa5uRxs+YU9CPq0wpUokLW53B x8keFXTAus9cWLrySrI9AQaov8GRPpw5aAKQleUiHCEhH/L5xIWCbOjErXHUA/qOnLHq r1lw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=QXZ9HdJa; 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-182426-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182426-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-792bf30ac4bsi2090925085a.229.2024.05.17.10.38.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 10:38:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182426-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=QXZ9HdJa; 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-182426-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182426-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 8511E1C2120C for ; Fri, 17 May 2024 17:38:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 54F2313DDDE; Fri, 17 May 2024 17:37:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="QXZ9HdJa" 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 CF4AA13DDB5; Fri, 17 May 2024 17:37:44 +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=1715967466; cv=none; b=qnKftw9zF0cFEAJ5ZTRO44jC1oUeqo+s0DtearWJaQAT6slNiZVN1T1AsOhyPyVSxc+Y+/vjr+YavOf9NWYt/F2Wed5Oe4hoVs59BTOIo+Pj7pDFjWtqdNo+Y7SMg4OwcUbOTan0ftAA6iZ5TB7Wl77bRkGQqV9BIS0eIyxMl2E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715967466; c=relaxed/simple; bh=ETgq2IUFXlU+gNPfxlvPx0NXbt5wKYxRCTj/5sRS1Bk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TjtxaId4FQN3EfwRXVKFqoAVUYpsX3b4U6kk7fY+8WnU34vx0uB//8NhxhfA16i5BwbGa3CUAWAwBp5kUKDhveAuWFLwgyvTynTrUvP7zLWAw/V8HbD1yEMAA48VZkr2mT787TiGMPquTEmdTlgYt+v9zw9APaUpqaeeDEXi5nI= 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=QXZ9HdJa; 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 lelv0265.itg.ti.com ([10.180.67.224]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 44HHbSIw115953; Fri, 17 May 2024 12:37:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1715967448; bh=QwzJlg6Go90nYaTZryV4XJOfLUk1jVtqeECp9jUGAUg=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=QXZ9HdJaYbSXdbfMwBv31ydeJghwLP96ZKlrRY99Pi16WMAMEQTDaF5F0aEEh7wch XAkTSuTtZZt7UC3DpbaWKT8g8DfAfDmgPCfz0xzfl83EEh87h1XeB2+7QsYJqOjMk5 sJ9qvusXNQ4JytVr/voLMZh6bOsps2SyPDZnR9Iw= Received: from DLEE113.ent.ti.com (dlee113.ent.ti.com [157.170.170.24]) by lelv0265.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 44HHbSC8028873 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 17 May 2024 12:37:28 -0500 Received: from DLEE112.ent.ti.com (157.170.170.23) by DLEE113.ent.ti.com (157.170.170.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Fri, 17 May 2024 12:37:28 -0500 Received: from lelvsmtp6.itg.ti.com (10.180.75.249) by DLEE112.ent.ti.com (157.170.170.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; Fri, 17 May 2024 12:37:28 -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 44HHbRRw013542; Fri, 17 May 2024 12:37:27 -0500 From: Devarsh Thakkar To: , , , , , , , , , , , , , , , CC: , , , , , , , , , , , , , Subject: [PATCH v8 08/10] lib: math_kunit: Add tests for new rounding related macros Date: Fri, 17 May 2024 23:07:26 +0530 Message-ID: <20240517173726.803899-1-devarsht@ti.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240517171532.748684-1-devarsht@ti.com> References: <20240517171532.748684-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]. [1]: https://gist.github.com/devarsht/3f9042825be3da4e133b8f4eda067876 Signed-off-by: Devarsh Thakkar --- V1->V8 (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 1b00e4195a1a..1955dcd5655d 100644 --- a/lib/math/math_kunit.c +++ b/lib/math/math_kunit.c @@ -68,6 +68,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) { @@ -93,6 +113,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); @@ -270,8 +302,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