From patchwork Mon Feb 7 21:11:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 12737890 X-Patchwork-Delegate: brendanhiggins@google.com 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 82031C433F5 for ; Mon, 7 Feb 2022 21:11:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237092AbiBGVLu (ORCPT ); Mon, 7 Feb 2022 16:11:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236956AbiBGVLt (ORCPT ); Mon, 7 Feb 2022 16:11:49 -0500 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DA1CC06173B for ; Mon, 7 Feb 2022 13:11:48 -0800 (PST) Received: by mail-ed1-x530.google.com with SMTP id da4so10934112edb.4 for ; Mon, 07 Feb 2022 13:11:48 -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=mO0nS1vlfvTw04UTXUIpbtNuC8tb/IOjCirvjFq+c0Q=; b=itHXjjA4IXG6B60QRNb87Fdj8MUaMRFdyv36prbUOF7IADrZ5Etxsf4w9ueC5iqdHG cLNiDLNgPMWszmw/Y6tCvmLDRjlIqj8PiVtYCOcaLoth/nKj50oBfJqjNl3bTTxCMD7n R1mkE6MuePEA2TOWeqhIHl+rosOLKVExDurVM= 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=mO0nS1vlfvTw04UTXUIpbtNuC8tb/IOjCirvjFq+c0Q=; b=TE06JKtExls6qFEigHJ882ZIUBfr9FFrsc2W9L7sbL9BoeNMWAoSli3eIpyBJ3adx0 AQuzPvkdUED4ysuFTqopgbYq0AohKcaribqLLLBdoY9jKHK1LHeG4IZM8cjCqcmxknjY dFlMhKxZ7c7Dm2lL7LeogVNaV8dfN3Gf0O5tPFCryFLlmNZm87MRGy48t24YeqEBnvhJ rPHjDOmA3RvZRUMDh8D8NZ8J0g3XP3mcUX/MJlmvwbV0HuCmGcnMXMbVJ1auhSzAtWCA Y3FObBSt/CwgQPzwHYt1ote2ppD4n0YI7EMwiMuMSXCmO3fyAihyzSC+B6okwbJ4p/Vx aMmQ== X-Gm-Message-State: AOAM533oEQU7Agn/ZjJtXBaS8EXOSpUNUaFxyDQKoSGucLWccCkzIMhq ZONJXKZSoBaWBMeoDd3eUGnffA== X-Google-Smtp-Source: ABdhPJyV0DQzkwpt8drYuzNXJSJ0c88BgjaK3nmRuVL+j89y/R84CWYhWoa3Azcxux+WQ5rO3sdRmQ== X-Received: by 2002:a05:6402:40c1:: with SMTP id z1mr1325109edb.23.1644268306955; Mon, 07 Feb 2022 13:11:46 -0800 (PST) Received: from alco.lan (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id z4sm4047239ejd.39.2022.02.07.13.11.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:11:46 -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 v3 1/6] kunit: Introduce _NULL and _NOT_NULL macros Date: Mon, 7 Feb 2022 22:11:39 +0100 Message-Id: <20220207211144.1948690-1-ribalda@chromium.org> X-Mailer: git-send-email 2.35.0.263.gb82422642f-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: Daniel Latypov Signed-off-by: Ricardo Ribalda --- include/kunit/test.h | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/include/kunit/test.h b/include/kunit/test.h index 00b9ff7783ab..340169723669 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1218,6 +1218,50 @@ 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_PTR_EQ_MSG(test, \ + ptr, \ + NULL, \ + 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_PTR_NE_MSG(test, \ + ptr, \ + NULL, \ + 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 +1529,50 @@ 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_PTR_EQ_MSG(test, \ + ptr, \ + NULL, \ + 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_PTR_NE_MSG(test, \ + ptr, \ + NULL, \ + 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.