Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2619890imn; Tue, 2 Aug 2022 09:35:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR5H1L4yVJpL9aNMGgzixneUCdGmbEoA6We7JSoDRFN3V5For76E6amivLJcc7jiyZYJkZgZ X-Received: by 2002:a17:906:9b8b:b0:730:8e70:f3b8 with SMTP id dd11-20020a1709069b8b00b007308e70f3b8mr7031864ejc.179.1659458146306; Tue, 02 Aug 2022 09:35:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659458146; cv=none; d=google.com; s=arc-20160816; b=u2+zxJf6c4ebp9sEkrDzOjulEnE+acBeaFNImAPc1Cz0q9SZ1v+S+G5T77QYZywVKY 8zlehT4REzePiZ/iTeOv0rFO/xGsFGor3jKCqB/Pmbf0e9xzgRnEfJUAdb3nQ8Nm3bFz alJto+bTtIzfKBMzyYh08FI/Kz/vNFh9f3lA5yD0QI8Hw/pwbltoFAecBvqDnXlZLnlt Zja/x+dbnMt7OzCtz/tNh41pkEB36IuiYdeLsp7WHuppa+FFsn+NmoTVnaZOdO0v6y7L HBVVVxgQNiWmhfRvAdDR4z+TZYJ+Nfw0FBnrOe7tIfrDGcdLWVdjsOesbxU7AZMlhg2b 18+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:mime-version:date :message-id:dkim-signature; bh=jELSPehC0sHTgMQ/zgNJ4XHGCMYu/3gkNIdRpiRtL+Y=; b=PQLF1lyp5VqF+HSy6JZwyuCrb8lkyktRLp4CG5GKanCNgKQqaDoAhLCZt6dpCE3oaE q/ev/fZPL1kMv46qh+XpJoItrVM/2oc7quUJo7XUTNWxWHrrJU2HPUoNf2kkDmjN4Wdz sApTjpwGdS0wPxWwfxfEfHULfbwlDAQdnOcA/pN429pWtlz1i0lhgXjwduqc/SzL7Oef HgguEvZz3IPwfFACvVdQWQzDIbfIt2lVI0ChfhACCyOe6VLENJwIcuw2mAfj8ofCIX0p CQdw1wOOI4IoqcAmC+yuuzGEJHRmHXoDCUJcuJaXBsELoy5OMo6GU841xINvCmZ5FY+U z9wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@riseup.net header.s=squak header.b=kgpuzymP; 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=NONE sp=NONE dis=NONE) header.from=riseup.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s24-20020a50d498000000b0043ade0b9a7fsi12405856edi.195.2022.08.02.09.35.21; Tue, 02 Aug 2022 09:35:46 -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=@riseup.net header.s=squak header.b=kgpuzymP; 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=NONE sp=NONE dis=NONE) header.from=riseup.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231712AbiHBQSR (ORCPT + 99 others); Tue, 2 Aug 2022 12:18:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231853AbiHBQSI (ORCPT ); Tue, 2 Aug 2022 12:18:08 -0400 Received: from mx1.riseup.net (mx1.riseup.net [198.252.153.129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77AC1D84; Tue, 2 Aug 2022 09:18:07 -0700 (PDT) Received: from fews1.riseup.net (fews1-pn.riseup.net [10.0.1.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "mail.riseup.net", Issuer "R3" (not verified)) by mx1.riseup.net (Postfix) with ESMTPS id 4Ly0XG5lCkzDqSk; Tue, 2 Aug 2022 16:18:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1659457087; bh=PfiR7jLdYDVoXBOzYYraiTyBY6D2IUfGLPtZuLagOSs=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=kgpuzymPwDiXRj+iy+qgI4YFFadIdUz21FqrmnQqOjebtOz4D5dEBpwo2NWlJx4eF zbr9URqrVJ/M5z8gu/NaMs3uiJVEwFFgdvqVp54h/bonsT6i4Tx44bLqF0EJBZ3bMs wPmGZWpHm4iT8/rbVno4OEOPczD66D8LrNkas5Iw= X-Riseup-User-ID: 2F6C0564F674F1D5BB00B7549818878FD5957A3C8B1332C30285BB795A04B226 Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews1.riseup.net (Postfix) with ESMTPSA id 4Ly0XB3ZDwz5vfs; Tue, 2 Aug 2022 16:18:02 +0000 (UTC) Message-ID: <951b07d6-c4cb-0b15-aa16-9347a861d985@riseup.net> Date: Tue, 2 Aug 2022 13:17:59 -0300 MIME-Version: 1.0 Subject: Re: [PATCH 1/3] kunit: Introduce KUNIT_EXPECT_ARREQ and KUNIT_EXPECT_ARRNEQ macros Content-Language: en-US To: =?UTF-8?Q?Ma=c3=adra_Canal?= Cc: melissa.srw@gmail.com, jose.exposito89@gmail.com, kuba@kernel.org, siqueirajordao@riseup.net, Isabella Basso , magalilemes00@gmail.com, airlied@linux.ie, davidgow@google.com, Brendan Higgins , davem@davemloft.net, javierm@redhat.com, tales.aparecida@gmail.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, daniel@ffwll.ch References: <20220802161206.228707-1-mairacanal@riseup.net> <20220802161206.228707-2-mairacanal@riseup.net> From: =?UTF-8?Q?Andr=c3=a9_Almeida?= In-Reply-To: <20220802161206.228707-2-mairacanal@riseup.net> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS 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 Hi Maíra, Thanks for the patch! Às 13:12 de 02/08/22, Maíra Canal escreveu: > Currently, in order to compare arrays in KUnit, the KUNIT_EXPECT_EQ or > KUNIT_EXPECT_FALSE macros are used in conjunction with the memcmp > function, such as: > KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0); > > Although this usage produces correct results for the test cases, when > the expectation fails, the error message is not very helpful, > indicating only the return of the memcmp function. > > Therefore, create a new set of macros KUNIT_EXPECT_ARREQ and > KUNIT_EXPECT_ARRNEQ that compare memory blocks until a determined size. > In case of expectation failure, those macros print the hex dump of the > memory blocks, making it easier to debug test failures for arrays. > > That said, the expectation > > KUNIT_EXPECT_EQ(test, memcmp(foo, bar, size), 0); > > would translate to the expectation > > KUNIT_EXPECT_ARREQ(test, foo, bar, size); > > Signed-off-by: Maíra Canal > --- > diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c > index d00d6d181ee8..0b537a8690e0 100644 > --- a/lib/kunit/assert.c > +++ b/lib/kunit/assert.c > @@ -204,3 +204,46 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert, > kunit_assert_print_msg(message, stream); > } > EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format); > + > +/* Adds a hexdump of a buffer to a string_stream */ > +static void kunit_assert_hexdump(struct string_stream *stream, > + const void *buf, const size_t len) > +{ > + const u8 *ptr = buf; > + int i, linelen, remaining = len; > + unsigned char linebuf[32 * 3 + 2 + 32 + 1]; > + > + for (i = 0; i < len; i += 16) { > + linelen = min(remaining, 16); > + remaining -= 16; > + > + hex_dump_to_buffer(ptr + i, linelen, 16, 1, linebuf, sizeof(linebuf), false); > + > + string_stream_add(stream, "%.8x: %s\n", i, linebuf); > + } > +} > + > +void kunit_arr_assert_format(const struct kunit_assert *assert, > + const struct va_format *message, > + struct string_stream *stream) > +{ > + struct kunit_arr_assert *arr_assert; > + > + arr_assert = container_of(assert, struct kunit_arr_assert, > + assert); > + > + string_stream_add(stream, > + KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n", > + arr_assert->text->left_text, > + arr_assert->text->operation, > + arr_assert->text->right_text); > + > + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \n", arr_assert->text->left_text); > + kunit_assert_hexdump(stream, arr_assert->left_value, arr_assert->size); I think using `:` instead of `==` gives a better output here [12:38:20] dst: [12:38:20] 00000000: 33 0a 60 12 00 a8 00 00 00 00 8e 6b 33 0a 60 12 [12:38:20] 00000010: 00 00 00 00 00 a8 8e 6b 33 0a 00 00 00 00 [12:38:20] result->expected: [12:38:20] 00000000: 31 0a 60 12 00 a8 00 00 00 00 81 6b 33 0a 60 12 [12:38:20] 00000010: 00 00 00 00 01 a8 8e 6b 33 0a 00 00 00 00 > + > + string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \n", arr_assert->text->right_text); > + kunit_assert_hexdump(stream, arr_assert->right_value, arr_assert->size); > + > + kunit_assert_print_msg(message, stream); > +} > +EXPORT_SYMBOL_GPL(kunit_arr_assert_format);