Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp86651rwb; Fri, 30 Sep 2022 17:55:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7XjkPD6ECbq++evdKdA3jbUb3uahZ9tDmFpCffeuRXhNSwJ53Y9FRsjSxJWh0KSM8/ken5 X-Received: by 2002:a05:6402:34d5:b0:451:335c:2f1e with SMTP id w21-20020a05640234d500b00451335c2f1emr9701734edc.160.1664585738418; Fri, 30 Sep 2022 17:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664585738; cv=none; d=google.com; s=arc-20160816; b=VvrUw7zkl4RakHzjMaJpRm5n/RvI4NBRqXltWWrUU6PuE+kNAukNr56hCW9d7oMshx tmCNDB3vBYttXKBQHpgmdeTguv+cfaR/6wiHEUW4mFJehoh0qfMLlQRZ1MFdBOSZ6xOf 24RYAEBl8gTf8HX2q4jdW3qJ3MWh7r2rEXYN3I5Au2g6sowCOnaYHmVZq+UMQQo72exB EJeunDYxqdsulGyW3eOSVFECBJ3AXilI17bId6f1yoHlruKxEiCPf6AbVwAhe/mmMHZx JWd1wGFCJsMl0jSKhsTm/8dyv7VdKMcpTr/yYV0/BEcLPsufqcArXP1rRHYbES5sZ9T/ sbFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=VtHbDGUyBMlmUU7N3DxfY8TUG6hQduH5eTgQ3i+7K4w=; b=RW7G+RG/TmohFwBUXhUkmCysHpELCqLmuj1fGy5148cNgSHlkwrUdfpwx/rsbO+lJZ 0GV/sHNQthL3buC5mV4Vw4BflliYfFwDCi6PUeRTNQ6VDfSBrDaYEL75WrV9VesZXBEr 8Wb6wJ1Da2omfPvy09RNjk4IxlRzgL+tSwtqF61yVXYCreXo0becZ7FIZ/OKHVPDWjCQ wHKJVd0DjnbdCvyM49egPu66XV0hYXbgTyyo4RksUYsLDLivo6bphsneAzE8M++tZyJc 8DaeRP6p7F4y5Pw8Gqx3XHNj7wh7d5C3bY/jr7c02oZdXvsYvT+MI0jTOn7YKq09klj/ g+bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dqKmZOYw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k1-20020a170906128100b00780d18701f2si2624698ejb.176.2022.09.30.17.55.13; Fri, 30 Sep 2022 17:55:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=dqKmZOYw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232650AbiJAA1o (ORCPT + 99 others); Fri, 30 Sep 2022 20:27:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232437AbiJAA1C (ORCPT ); Fri, 30 Sep 2022 20:27:02 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEC801DF35 for ; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id w14-20020a170902e88e00b00177ab7a12f6so4141577plg.16 for ; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=VtHbDGUyBMlmUU7N3DxfY8TUG6hQduH5eTgQ3i+7K4w=; b=dqKmZOYwrmLA7ihchLDdTFZu8Lku9ziAfkQaHH5XF3iYrQhGAxlAwW8OUlR76A2ivi 4tEwxYMmmnP3iB2VGC2Ngm4/tRoVTspfAPibOJUoA3rIdWx/Ii/hz5GeXBttisxIP1Gf DbGBcn8GD1rmcJd3Yyfv0usZNO6OJaRj32QQHrVJVz3YxbwBKPYhFrUyC2tmXDGICqrg xXtpqsBd5mbX5KwyPai8i5DXWZm3jhRKGck9w4j2+HS/GlVk4h+hQ+Xp2uC22kr776dO 1mLxojybJsSrLBRMmb0YJ7neMafnFfonmA0YIRfM2Fah8uOlZ7pH+hQQ1mbDtJT1Zlg6 xjbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=VtHbDGUyBMlmUU7N3DxfY8TUG6hQduH5eTgQ3i+7K4w=; b=ti2lLUBxWSiJYFnnviUCBQAJnpYfhkbmSxbmbjLdbyYVJO/585CM+S5JQPg5M2Fmhw ZuEj/fHJSVx0ySg1l3kLLEYA+KJheoum9aXgv0EzBjuKGyd4oZyMl8KAif8ETwIX2fHJ k9QIhOLt3JyShO/qULjgxOkpPEAhI0gjh2KAEP940Y3SRkOk1QPEiU+VqR4ixAhR+g4B PaMgvTMFwFpvALSIzlLpQitWI3qU0i9tKWZhZE9piGhrLVoljPPmWCGf9v+a+IqHogHa BR9OR03m1nAdJFn5TddxILiV5ZDwwSgpK+RH9zNcWmhY0dBszbBh9depoRjkRzX7i93n q8hw== X-Gm-Message-State: ACrzQf2OD+zjr/X6ft2l54d4FWjTLlS8XKWe7Gc1El6KT9jRrdZbIg6b 1rJkBypMv/JAZbYmNRq8nDUKGDsA+jV4TQ== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:902:d509:b0:179:ee34:faaf with SMTP id b9-20020a170902d50900b00179ee34faafmr11510191plg.150.1664584011307; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) Date: Fri, 30 Sep 2022 17:26:37 -0700 In-Reply-To: <20221001002638.2881842-1-dlatypov@google.com> Mime-Version: 1.0 References: <20221001002638.2881842-1-dlatypov@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001002638.2881842-4-dlatypov@google.com> Subject: [PATCH 3/4] kunit: eliminate KUNIT_INIT_*_ASSERT_STRUCT macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, miguel.ojeda.sandonis@gmail.com, Daniel Latypov Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org These macros exist because passing an initializer list to other macros is hard. The goal of these macros is to generate a line like struct $ASSERT_TYPE __assertion = $APPROPRIATE_INITIALIZER; e.g. struct kunit_unary_assertion __assertion = { .condition = "foo()", .expected_true = true }; But the challenge is you can't pass `{.condition=..., .expect_true=...}` as a macro argument, since the comma means you're actually passing two arguments, `{.condition=...` and `.expect_true=....}`. So we'd made custom macros for each different initializer-list shape. But we can work around this with the following generic macro #define KUNIT_INIT_ASSERT(initializers...) { initializers } Note: this has the downside that we have to rename some macros arguments to not conflict with the struct field names (e.g. `expected_true`). It's a bit gross, but probably worth reducing the # of macros. Signed-off-by: Daniel Latypov --- include/kunit/assert.h | 48 ------------------------------------------ include/kunit/test.h | 27 +++++++++++++----------- 2 files changed, 15 insertions(+), 60 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index ace3de8d1ee7..01b191fa17c3 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -90,19 +90,6 @@ void kunit_unary_assert_format(const struct kunit_assert *assert, const struct va_format *message, struct string_stream *stream); -/** - * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_assert. - * @cond: A string representation of the expression asserted true or false. - * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwise. - * - * Initializes a &struct kunit_unary_assert. Intended to be used in - * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. - */ -#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \ - .condition = cond, \ - .expected_true = expect_true \ -} - /** * struct kunit_ptr_not_err_assert - An expectation/assertion that a pointer is * not NULL and not a -errno. @@ -123,20 +110,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert, const struct va_format *message, struct string_stream *stream); -/** - * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a - * &struct kunit_ptr_not_err_assert. - * @txt: A string representation of the expression passed to the expectation. - * @val: The actual evaluated pointer value of the expression. - * - * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in - * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. - */ -#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \ - .text = txt, \ - .value = val \ -} - /** * struct kunit_binary_assert_text - holds strings for &struct * kunit_binary_assert and friends to try and make the structs smaller. @@ -173,27 +146,6 @@ void kunit_binary_assert_format(const struct kunit_assert *assert, const struct va_format *message, struct string_stream *stream); -/** - * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like - * kunit_binary_assert, kunit_binary_ptr_assert, etc. - * - * @text_: Pointer to a kunit_binary_assert_text. - * @left_val: The actual evaluated value of the expression in the left slot. - * @right_val: The actual evaluated value of the expression in the right slot. - * - * Initializes a binary assert like kunit_binary_assert, - * kunit_binary_ptr_assert, etc. This relies on these structs having the same - * fields but with different types for left_val/right_val. - * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ, etc. - */ -#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \ - left_val, \ - right_val) { \ - .text = text_, \ - .left_value = left_val, \ - .right_value = right_val \ -} - /** * struct kunit_binary_ptr_assert - An expectation/assertion that compares two * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)). diff --git a/include/kunit/test.h b/include/kunit/test.h index fec437c8a2b7..e49348bbc6ee 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -514,22 +514,25 @@ void kunit_do_failed_assertion(struct kunit *test, fmt, \ ##__VA_ARGS__) +/* Helper to safely pass around an initializer list to other macros. */ +#define KUNIT_INIT_ASSERT(initializers...) { initializers } + #define KUNIT_UNARY_ASSERTION(test, \ assert_type, \ - condition, \ - expected_true, \ + condition_, \ + expected_true_, \ fmt, \ ...) \ do { \ - if (likely(!!(condition) == !!expected_true)) \ + if (likely(!!(condition_) == !!expected_true_)) \ break; \ \ _KUNIT_FAILED(test, \ assert_type, \ kunit_unary_assert, \ kunit_unary_assert_format, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ - expected_true), \ + KUNIT_INIT_ASSERT(.condition = #condition_, \ + .expected_true = expected_true_), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -589,9 +592,9 @@ do { \ assert_type, \ assert_class, \ format_func, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text = &__text, \ + .left_value = __left, \ + .right_value = __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -650,9 +653,9 @@ do { \ assert_type, \ kunit_binary_str_assert, \ kunit_binary_str_assert_format, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text = &__text, \ + .left_value = __left, \ + .right_value = __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -672,7 +675,7 @@ do { \ assert_type, \ kunit_ptr_not_err_assert, \ kunit_ptr_not_err_assert_format, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ + KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \ fmt, \ ##__VA_ARGS__); \ } while (0) -- 2.38.0.rc1.362.ged0d419d3c-goog