From patchwork Fri Feb 11 09:41:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 542264 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5495C433F5 for ; Fri, 11 Feb 2022 09:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343896AbiBKJlq (ORCPT ); Fri, 11 Feb 2022 04:41:46 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:58804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234778AbiBKJlm (ORCPT ); Fri, 11 Feb 2022 04:41:42 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAFC910A0 for ; Fri, 11 Feb 2022 01:41:41 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id p24so21659355ejo.1 for ; Fri, 11 Feb 2022 01:41:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vsf2QgtdatJ2wExDY4gvcboSixkFghVOFaraVlgt+kM=; b=aNwI2IxJubmpP+64mkhgrKN3wO139Ak4PiJbNw49dZKYsqpNJI+pDZM2rZPtC678IL aJWoY90Bp44guIS+JWCfSc57udtqvLH4jW8X/owNqgbRjJCWwf80hg1guWj/5UJrm7JJ TFlPmtmtnjwf4dGP01QJ4wG20e/F9lWZOxm58= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vsf2QgtdatJ2wExDY4gvcboSixkFghVOFaraVlgt+kM=; b=FTekXFfzwe74PfPm3QSoEmJOx9UDYn0MzoDrjsn7Y9scqpxlQqWW1nY/236lBuxMGE ETA5YYrHx3k6hyWeOVKYI4N5bTG0zIEbFBlSc7x9PQgRcOIyMqbp3faxA9+D5hAVi3q1 OgcRxsxY1tkU/OqipfEg9PAd3D3VM4qxvHPg/yPRhv41CAxDJArCX9W55QnIkKJYg3Vi p/b6SDAGmOJheLnnANXxrB5OrFARNofJZukCJ4MIfYHMQuyMoNiZL+utd7htaSxn1Q3b dzt/vI44vDhPtqVcV84w/MpvBmIBzv8aMfE1jQ9DFvv2aNxTCwyvzCXAkONI8714UFVE J1VQ== X-Gm-Message-State: AOAM530adkTajQ9DLExD2WgVXqS16hWlt2t+o1pUwR9/dnZuwTk5F0qR 8jIoIlnh7tkfxFpftmRFJdiwXQ== X-Google-Smtp-Source: ABdhPJw+InPe3gyY5Qn/XyNmPEB0uQKhUgpF3GibZGze3g8tA8WhsUQwOKE05nMV03myAGheeUFWTA== X-Received: by 2002:a17:906:7948:: with SMTP id l8mr663890ejo.752.1644572500145; Fri, 11 Feb 2022 01:41:40 -0800 (PST) Received: from alco.corp.google.com ([2620:0:1059:10:83e3:abbd:d188:2cc5]) by smtp.gmail.com with ESMTPSA id e8sm603196ejl.68.2022.02.11.01.41.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Feb 2022 01:41:39 -0800 (PST) From: Ricardo Ribalda To: kunit-dev@googlegroups.com, kasan-dev@googlegroups.com, linux-kselftest@vger.kernel.org, Brendan Higgins , Mika Westerberg , Daniel Latypov Cc: Ricardo Ribalda Subject: [PATCH v5 1/6] kunit: Introduce _NULL and _NOT_NULL macros Date: Fri, 11 Feb 2022 10:41:28 +0100 Message-Id: <20220211094133.265066-1-ribalda@chromium.org> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Today, when we want to check if a pointer is NULL and not ERR we have two options: KUNIT_EXPECT_TRUE(test, ptr == NULL); or KUNIT_EXPECT_PTR_NE(test, ptr, (struct mystruct *)NULL); Create a new set of macros that take care of NULL checks. Reviewed-by: Brendan Higgins Reviewed-by: Daniel Latypov Signed-off-by: Ricardo Ribalda --- include/kunit/test.h | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 00b9ff7783ab..e6c18b609b47 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1218,6 +1218,48 @@ do { \ fmt, \ ##__VA_ARGS__) +/** + * KUNIT_EXPECT_NULL() - Expects that @ptr is null. + * @test: The test context object. + * @ptr: an arbitrary pointer. + * + * Sets an expectation that the value that @ptr evaluates to is null. This is + * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL). + * See KUNIT_EXPECT_TRUE() for more information. + */ +#define KUNIT_EXPECT_NULL(test, ptr) \ + KUNIT_EXPECT_NULL_MSG(test, \ + ptr, \ + NULL) + +#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \ + KUNIT_BINARY_PTR_ASSERTION(test, \ + KUNIT_EXPECTATION, \ + ptr, ==, NULL, \ + fmt, \ + ##__VA_ARGS__) + +/** + * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null. + * @test: The test context object. + * @ptr: an arbitrary pointer. + * + * Sets an expectation that the value that @ptr evaluates to is not null. This + * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL). + * See KUNIT_EXPECT_TRUE() for more information. + */ +#define KUNIT_EXPECT_NOT_NULL(test, ptr) \ + KUNIT_EXPECT_NOT_NULL_MSG(test, \ + ptr, \ + NULL) + +#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \ + KUNIT_BINARY_PTR_ASSERTION(test, \ + KUNIT_EXPECTATION, \ + ptr, !=, NULL, \ + fmt, \ + ##__VA_ARGS__) + /** * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. * @test: The test context object. @@ -1485,6 +1527,48 @@ do { \ fmt, \ ##__VA_ARGS__) +/** + * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. + * @test: The test context object. + * @ptr: an arbitrary pointer. + * + * Sets an assertion that the values that @ptr evaluates to is null. This is + * the same as KUNIT_EXPECT_NULL(), except it causes an assertion + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. + */ +#define KUNIT_ASSERT_NULL(test, ptr) \ + KUNIT_ASSERT_NULL_MSG(test, \ + ptr, \ + NULL) + +#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \ + KUNIT_BINARY_PTR_ASSERTION(test, \ + KUNIT_ASSERTION, \ + ptr, ==, NULL, \ + fmt, \ + ##__VA_ARGS__) + +/** + * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null. + * @test: The test context object. + * @ptr: an arbitrary pointer. + * + * Sets an assertion that the values that @ptr evaluates to is not null. This + * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion + * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. + */ +#define KUNIT_ASSERT_NOT_NULL(test, ptr) \ + KUNIT_ASSERT_NOT_NULL_MSG(test, \ + ptr, \ + NULL) + +#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \ + KUNIT_BINARY_PTR_ASSERTION(test, \ + KUNIT_ASSERTION, \ + ptr, !=, NULL, \ + fmt, \ + ##__VA_ARGS__) + /** * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. * @test: The test context object.