linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: SeongJae Park <sjpark@amazon.com>
To: <brendanhiggins@google.com>
Cc: <linux-kselftest@vger.kernel.org>, <kunit-dev@googlegroups.com>,
	<linux-kernel@vger.kernel.org>,
	SeongJae Park <sj38.park@gmail.com>
Subject: What is the best way to compare an unsigned and a constant?
Date: Fri, 27 Dec 2019 13:39:38 +0100	[thread overview]
Message-ID: <20191227123938.5271-1-sjpark@amazon.com> (raw)

Hello,


I have a function returning 'unsigned long', and would like to write a kunit
test for the function, as below.

    unsigned long foo(void)
    {
    	return 42;
    }

    static void foo_test(struct kunit *test)
    {
        KUNIT_EXPECT_EQ(test, 42, foo());
    }

However, this kunit gives me below warning for the above code:

    /.../linux/include/linux/kernel.h:842:29: warning: comparison of distinct pointer types lacks a cast
       (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
                                 ^
    /.../linux/include/kunit/test.h:493:9: note: in expansion of macro ‘__typecheck’
      ((void)__typecheck(__left, __right));           \
             ^~~~~~~~~~~
    /.../linux/include/kunit/test.h:517:2: note: in expansion of macro ‘KUNIT_BASE_BINARY_ASSERTION’
      KUNIT_BASE_BINARY_ASSERTION(test,           \
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    /.../linux/include/kunit/test.h:606:2: note: in expansion of macro ‘KUNIT_BASE_EQ_MSG_ASSERTION’
      KUNIT_BASE_EQ_MSG_ASSERTION(test,           \
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    /.../linux/include/kunit/test.h:616:2: note: in expansion of macro ‘KUNIT_BINARY_EQ_MSG_ASSERTION’
      KUNIT_BINARY_EQ_MSG_ASSERTION(test,           \
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /.../linux/include/kunit/test.h:979:2: note: in expansion of macro ‘KUNIT_BINARY_EQ_ASSERTION’
      KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
      ^~~~~~~~~~~~~~~~~~~~~~~~~
    /.../linux/mm/foo-test.h:565:2: note: in expansion of macro ‘KUNIT_EXPECT_EQ’
      KUNIT_EXPECT_EQ(test, 42, foo());
      ^~~~~~~~~~~~~~~

I could remove the warning by explicitly type casting the constant as below:

        KUNIT_EXPECT_EQ(test, (unsigned long)42, foo());

However, now 'checkpatch.pl' complains about the type casting as below.

    WARNING: Unnecessary typecast of c90 int constant
    #565: FILE: mm/foo-test.h:565:
    +       KUNIT_EXPECT_EQ(test, (unsigned long)42, foo());

Of course, there could be several work-arounds for these warnings, such as
using 'EXPECT_TRUE(test, 42 == foo())' or casting the function's return value.
Nonetheless, I'm not sure what is the right way.  Could you please let me know
what is the recommended way for this case?


Thanks,
SeongJae Park

             reply	other threads:[~2019-12-27 12:40 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-27 12:39 SeongJae Park [this message]
2019-12-27 12:52 ` What is the best way to compare an unsigned and a constant? Bernd Petrovitsch
2019-12-27 13:08   ` SeongJae Park
2020-01-07 11:52   ` SeongJae Park
2020-01-07 13:35     ` Brendan Higgins
2020-01-07 13:49       ` SeongJae Park
2020-01-08 14:12         ` Brendan Higgins
2020-01-08 14:26           ` SeongJae Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191227123938.5271-1-sjpark@amazon.com \
    --to=sjpark@amazon.com \
    --cc=brendanhiggins@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=sj38.park@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).