Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1666209lqb; Sun, 26 May 2024 11:10:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX562Ieb00L0z0M8IkVyI/ed+5CHChHBpplbtZsXjoTAt2TObeo12tngbBkO/DCxCXeHw+ez6GCwlu/hIQ17ty9QnEjAkrhrXpSXO5kkQ== X-Google-Smtp-Source: AGHT+IHavWoSdOtEp14HVqsxpkOPxshkwPkX9Pv6UCFIRCrFMjg2H0AuQygq+LOa8P8RfAF4wTF4 X-Received: by 2002:a17:907:cb14:b0:a5a:8673:cccd with SMTP id a640c23a62f3a-a626250e1camr606455066b.30.1716747040065; Sun, 26 May 2024 11:10:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716747040; cv=pass; d=google.com; s=arc-20160816; b=sYpJ3k5Rj5tAhDIT6XClUEQ1FJE8lZKZKeG0BZGQy742UatLWNGQu8am4UgTbiS/Ik U/OkOS7UU+Fzf6ZO5y0CPhQ05TPW/OfabZwgb/gXQznv51LgnjEwVJ1CfdpNp92+bRcA BMrH2sabpYYtRicBs7JqM20gEzUJZq48BkSx6WApmRz1RxwBzzF6vq9nKGbRbqZUMTmN VEnBXrjjB4SOEZfqPhtm/AMvZtdTN4OJ4BgxsOFaX1dvUVBuj0J2gFw4direw2Hurzt1 FCgS8hd0OQGixxcA6pkpkMMG/2QUtbc0pYtEQkOdJG4frxUYFaKlSQSolOPZ3ABvkMKn 81mA== 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=3pl6+I07pBV/dmn7xrtIXV33Igmd+N4vU2efVjVAmzw=; fh=q8ciodeUhhSUl1vkGMisO0DFQQjOd0K/8tvREZ69uYc=; b=XkZj3lnnjkHJrflaQEOnVHvjX0NC/G+mQrK28A7Ya2HyNdXFeCxngh6BvEGkNy7+BF QLk2cQXYDf5fzMwICxnHsx64zYHjZcomg9pKvfRd3/wj7jidjbPiwzQ0eAqlMVr2mUcL qFcYGYiqCTasrpUs5LLoV87j5zxfi2p4Dlt2U43iVFRGrWXdwlzyGmBaTS9Me30zMEKi 0bcV9bfjUyNhSmpiJFiJvpT20WrcgJ6tKKC4u+0n0tjLch2hH4FCTmfI8KB5IpSxZBjP qkW9evpj64yQ1BUFuf59zCrQMhd6+nPaS0PznL/kv3knRd0/DDCmDHhqo2rKYdEkyQ5A 9QCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=cny73jka; 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-189818-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189818-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-a626c9185f4si299197666b.240.2024.05.26.11.10.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 11:10:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189818-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=cny73jka; 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-189818-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189818-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 CAECD1F211A8 for ; Sun, 26 May 2024 18:10:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D81AD12BE9F; Sun, 26 May 2024 18:10:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="cny73jka" Received: from lelv0142.ext.ti.com (lelv0142.ext.ti.com [198.47.23.249]) (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 6143912B143; Sun, 26 May 2024 18:10:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.249 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716747029; cv=none; b=Zpvv375DuoBCYIdejuyteWj9oyzBSr7/O49Upfkj/dXinOcE53+deDbRVJKGrI10bVAqcBBUvASxiR+69cSh5EWyUTcSdFF/64uVyYSdbHaiRs9adWzD9632ilgr2RwXNCimF+FreGrM4qiQnO24Jyy9ggRM8/LbOBxVjz+KpHo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716747029; c=relaxed/simple; bh=8IF6KhFcrhUQtnYV+GbhDwSqKu2tzjqU6AYTllXcLO4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=n2aL0hjPRAjbdnZerrPNHNtuk3Lo1DPcUlcmUzHDh3hQ7n/9xfIDthLNUXTzWilQcYAzEiNFIblOzHc8EcgsSICD+7tajGton1i8ZzxtHfV0P1G8/oG2GECDCQi/C6OE5stu9YBv/cPPUEhz9rUuUsE9BIBV/tucDarmV3p7vfs= 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=cny73jka; arc=none smtp.client-ip=198.47.23.249 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 lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 44QIA7x3104402; Sun, 26 May 2024 13:10:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1716747007; bh=3pl6+I07pBV/dmn7xrtIXV33Igmd+N4vU2efVjVAmzw=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=cny73jkaBLegUBqzHJBzWZaBNQ+mrT/MHNaBYn+HMOsTzSOW3fB2rqMpD/k0LilmP FCpbSkghOYR2vuxnT9r/GV2NwHk8HgM2SmzzUbFszlbDkgcTCimTtsYDhxEX4dOAxT yIjk7jnLC/6YUWd12GbjrFaCXHilqVUbS9/1PnlM= Received: from DFLE103.ent.ti.com (dfle103.ent.ti.com [10.64.6.24]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 44QIA7wm044542 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Sun, 26 May 2024 13:10:07 -0500 Received: from DFLE109.ent.ti.com (10.64.6.30) by DFLE103.ent.ti.com (10.64.6.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Sun, 26 May 2024 13:10:07 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DFLE109.ent.ti.com (10.64.6.30) 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; Sun, 26 May 2024 13:10:07 -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 44QIA60a099464; Sun, 26 May 2024 13:10:06 -0500 From: Devarsh Thakkar To: , , , , , , , , , , , , , , , CC: , , , , , , , , , , , , , Subject: [PATCH v9 08/10] lib: math_kunit: Add tests for new macros related to rounding to nearest value Date: Sun, 26 May 2024 23:40:06 +0530 Message-ID: <20240526181006.1127512-1-devarsht@ti.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20240526175655.1093707-1-devarsht@ti.com> References: <20240526175655.1093707-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->V9 (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 16263e30a292..60b66aa41121 100644 --- a/lib/math/math_kunit.c +++ b/lib/math/math_kunit.c @@ -67,6 +67,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) { @@ -92,6 +112,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); @@ -269,8 +301,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