Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp201488pxy; Fri, 7 May 2021 00:58:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqOCoO0jXjO0aogvJNZ1gkI0ThI4w2estxla5oDr8QIRPxF/+oBFoW6d5K6QOk1kyb6TM6 X-Received: by 2002:a17:90a:a589:: with SMTP id b9mr1968987pjq.158.1620374316660; Fri, 07 May 2021 00:58:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620374316; cv=none; d=google.com; s=arc-20160816; b=MGpd9Wej9ay7ad+8EyaF3tpF7Wyh29Ci8i5iNAa9bPzKic9Geu5E2arxMQZh1RTPTl MTxIUhGanuHZzVF4H1d2WVeZNtf0Nfu6TnqPnSlamdLpEoh2Ej78GGRz/Mv0wf4I/oaK w/TftAmk9fmcTn6/UgyStriJ3JKxUrOHRN80RC4kdDJQMx6LIGWXr7z7qZ1as7UeTcUh oMgskjwwFZ7jpvnHB4TkKPxmAQgT0/qjBTzwr1OBBNICDJLTN+WwNQUgxmYaRgcFL249 +Ut/kM7VMr97axrnvUmvqKxTdCOwOOx7T2YAV8uwo5xJdG4emhPg+oh63tAW81QFEOWc tQyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=9gIRV9p376yzYOkVLAM08wQ8Qd0j1vezQFGSOL7bIPg=; b=IGc9PsK7G+rbKPoRP/1L+XgEcCzmcco6WFe2SiuYt17SsgGG6P72l8yD2wZ40kuv5R sfZw75Yu3kQnuz/DKNZYTtqa1jMx/+NkrIXwXnmSDtd6ubN1m6wo3P17ZMs74eXAm7tV +EwdgnsLYDH3XnzyvdQg2PwH/0HFDC3hslD0K0MswM3o0G5yVj0iKfLQCRZBRaLKwOSf hX3eP7SXzWlVT8Jv1TbMJuk6OlquUpBkkLKE5nvRMyKhzAPafCO1CMQVrMkvPQEiMNxd KaOn7QoX50pPZ6MDwfOG/79uD0kHnvC7MlpUeA20hqPLtjD/U8HPTC6NwGmWEY2TTJXi oyyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Hjd19bs6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p22si6095401pgm.190.2021.05.07.00.58.23; Fri, 07 May 2021 00:58:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Hjd19bs6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S235315AbhEGH3X (ORCPT + 99 others); Fri, 7 May 2021 03:29:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235289AbhEGH3W (ORCPT ); Fri, 7 May 2021 03:29:22 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4680EC061761 for ; Fri, 7 May 2021 00:28:23 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id di13so9067017edb.2 for ; Fri, 07 May 2021 00:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9gIRV9p376yzYOkVLAM08wQ8Qd0j1vezQFGSOL7bIPg=; b=Hjd19bs6E8MXnI2GtquNirlemBMll7SfU+KUqj54EJK5w37nAxnhCMFhAU0QjzzNoE duT+zjhLzfKaEZDv675QrmotFHyCF6J+Tozi17yt843ctjlkPn7hU2tN+wIS16lBxcIr IkFu9uQr9Pq59OCHVRCJi4W2C/GrIypg1xnDOmzKmtAejc1/RSaedabcgFh20MMtNYGG 905XfNEhy8VrqasgkNYZEMQ2vFR/jyc8ffJ1XX+bDu3kZ+Bjrhibb+R646oTOIB3OBIS /szeb0hvpqHZpS2wO4YbdMankOLRRO6pAzzxgFuC1sArBHkx/a0VvIHjK9PmAfPzecg0 8pOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9gIRV9p376yzYOkVLAM08wQ8Qd0j1vezQFGSOL7bIPg=; b=moluo5agXNgGnTbBmTAjkaTtvzkAg9RUK2nO0Lmt270/UibDcuvNgC/wvJE/3WxrP/ uAYHJVeAYf4xGAF3zr5K2g/E39sWbuqN1dZokut6feoVPT4Bs1Mxq//yJgMzua3W7weE kCAUKIEN52OnqkG5ZGLGUCSbz1a9k4Ehu90/jMcYAARydUL7XDZGC+dC62+SuM84FbOo BXTo1k9trhjQXRnacJIAwD2YVeGMrRKl2eLH0O5DrW8KQ1K3A1BqUN0j/+WKBW42XgnU 5SEReB3wEwlixUQyjkejT71eK9PE71sCzbZg3T6znMox2egnD+DSvmQuSFOG8T3Kme0A OutQ== X-Gm-Message-State: AOAM531GbvcqMgP+bYb3HyhhClttM3P30WfMDDCicZlDUh/mSUBkNcWZ TKaaFqJ/BbDbAuGgnX8hWoe9WCdUwU9C26WKqX5Vqw== X-Received: by 2002:aa7:c382:: with SMTP id k2mr9947529edq.189.1620372501833; Fri, 07 May 2021 00:28:21 -0700 (PDT) MIME-Version: 1.0 References: <20210507050908.1008686-1-davidgow@google.com> <20210507050908.1008686-2-davidgow@google.com> In-Reply-To: <20210507050908.1008686-2-davidgow@google.com> From: Daniel Latypov Date: Fri, 7 May 2021 00:28:10 -0700 Message-ID: Subject: Re: [PATCH 2/2] kunit: Assign strings to 'const char*' in STREQ assertions To: David Gow Cc: Brendan Higgins , Shuah Khan , KUnit Development , "open list:KERNEL SELFTEST FRAMEWORK" , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 6, 2021 at 10:09 PM David Gow wrote: > > Currently, the KUNIT_EXPECT_STREQ() and related macros assign both > string arguments to variables of their own type (via typeof()). This > seems to be to prevent the macro argument from being evaluated multiple > times. > > However, yhis doesn't work if one of these is a fixed-length character nit: if you ever send a v2 of this patch, s/yhis/this > array, rather than a character pointer, as (for example) char[16] will > always allocate a new string. > > By always using 'const char*' (the type strcmp expects), we're always > just taking a pointer to the string, which works even with character > arrays. > > Signed-off-by: David Gow Reviewed-by: Daniel Latypov I'm very happy to see this patch. This makes code that looks obviously correct actually work. Somewhat tangential: there are several casts that are no longer needed after this in the docs. I think the following gets rid of all of them. Should it perhaps go in a chain with this patch? I.e. if the first one is too controversial and we want to go ahead split this patch off from it. diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst index 650f99590df5..756747417a19 100644 --- a/Documentation/dev-tools/kunit/usage.rst +++ b/Documentation/dev-tools/kunit/usage.rst @@ -465,10 +465,9 @@ fictitious example for ``sha1sum(1)`` .. code-block:: c - /* Note: the cast is to satisfy overly strict type-checking. */ #define TEST_SHA1(in, want) \ sha1sum(in, out); \ - KUNIT_EXPECT_STREQ_MSG(test, (char *)out, want, "sha1sum(%s)", in); + KUNIT_EXPECT_STREQ_MSG(test, out, want, "sha1sum(%s)", in); char out[40]; TEST_SHA1("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); @@ -507,7 +506,7 @@ In some cases, it can be helpful to write a *table-driven test* instead, e.g. }; for (i = 0; i < ARRAY_SIZE(cases); ++i) { sha1sum(cases[i].str, out); - KUNIT_EXPECT_STREQ_MSG(test, (char *)out, cases[i].sha1, + KUNIT_EXPECT_STREQ_MSG(test, out, cases[i].sha1, "sha1sum(%s)", cases[i].str); } @@ -568,7 +567,7 @@ Reusing the same ``cases`` array from above, we can write the test as a struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value); sha1sum(test_param->str, out); - KUNIT_EXPECT_STREQ_MSG(test, (char *)out, test_param->sha1, + KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1, "sha1sum(%s)", test_param->str); } > --- > include/kunit/test.h | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 4c56ffcb7403..b68c61348121 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -1128,8 +1128,8 @@ do { \ > fmt, \ > ...) \ > do { \ > - typeof(left) __left = (left); \ > - typeof(right) __right = (right); \ > + const char *__left = (left); \ > + const char *__right = (right); \ > \ > KUNIT_ASSERTION(test, \ > strcmp(__left, __right) op 0, \ > -- > 2.31.1.607.g51e8a6a459-goog >