Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4051716pxv; Mon, 19 Jul 2021 15:32:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxa+/iftoxCokQTeUqA6kXlB5U4XjyUhc0AyvNr09RuQ4cBU6S9xWazS89Oy0EUPxwrpZof X-Received: by 2002:a05:6402:30ba:: with SMTP id df26mr37228609edb.310.1626733943032; Mon, 19 Jul 2021 15:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626733943; cv=none; d=google.com; s=arc-20160816; b=CIVfQFfV1ai8qe2sZfgBKNuYL9hv7WLUcWBKB7Yb1AAH/1hmafUJuGFKBgNpaMndGm mmTT8B6Ydg0Nf7UAar0c3ZCFEjR6thHk9ET/U+fBms40WYyITDgc7ZqHSNJ2IDlqsKSt 1LxCmb4T0xJJ5roL5KxtJJhr16KWBM5s5eSFYlLAX6tgnX9pg4fXuVJwq3I7VITQD2Nw JBe/1VERSlfa1/JeKYEFN9XBl47pfZ9Z+aZM17t7812YuYOQs4DNITmvVmNNsss5frME AYIDkgQADszKB7z7dP6QJFzHRCMgnGDe2XJ5dyd5kZ3nwdvwGw7i2x3AFZUvBKu7PD1X PEvQ== 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=N1XYgnjzAg7DsLLuF7dQCiT5m3Bavug3Psl0UPr+yEg=; b=rRoLJHdccpHHoeHsRoJcYcUkfXV8H0gV0kyzWVz8hHNeeIT9U2wgwTeeG16HgujyOs nTwyeHk64d/KDall8RyWyRF30sStJYVnOTUWG/OmYslcJV+OCluJYFb6I3rfZwdjlBYC BPubG+e367cbwBiTcjp7QmUmJgEqWxtuIzloOUw+W39ALpPJDdxUokuE0OmC2gjIv2aB ZQ/kRjOeieq4L8RufqcbXqLTr1EJPPlLlCZkHFr4b2bBbiTI9ViESET0/9Oc5SHnXCQG k9c7qq8JaPIrIIAwfHfA11lgkvHrJayqaGdmbguMrbbjZCwOUoO8rS3xTZdPtbY3JDWA XFRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=UEg+So4F; 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 by8si6284413edb.117.2021.07.19.15.31.50; Mon, 19 Jul 2021 15:32:23 -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=UEg+So4F; 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 S1387467AbhGSVca (ORCPT + 99 others); Mon, 19 Jul 2021 17:32:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1388721AbhGSVB3 (ORCPT ); Mon, 19 Jul 2021 17:01:29 -0400 Received: from mail-io1-xd34.google.com (mail-io1-xd34.google.com [IPv6:2607:f8b0:4864:20::d34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2284C0613E3 for ; Mon, 19 Jul 2021 14:39:21 -0700 (PDT) Received: by mail-io1-xd34.google.com with SMTP id z11so21813081iow.0 for ; Mon, 19 Jul 2021 14:39:21 -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=N1XYgnjzAg7DsLLuF7dQCiT5m3Bavug3Psl0UPr+yEg=; b=UEg+So4FaqHi/jiXjQuX7UuaQIbgfx5inmQtjzkmaXb0tgxJOeziM5NOB1IV2N96C7 qX//djAlTA1lH1a5IpoEH3mclT8wTVWBorTh0Fe+rOZQa3OyS040Kud+2ukKoWAHuPK8 gozw9+qTLpGo0qqRlfKRWS7GftetXnAFNZcNwBjZLLfVplhBb/YBebj5ioLySp+PjKTs qus4Z/ogyUJU1UIuM4ePCQkRCgfvUb4RHC/tnLvLZolgGCpdBiN8IJ1RUi1ke/njL0Hb HIp+Y7OZv983qM7+A8ZEFWbZjG09ucogp5WPenxHWnqFaXpktyen3zFtmWabF1jnu9UV USkQ== 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=N1XYgnjzAg7DsLLuF7dQCiT5m3Bavug3Psl0UPr+yEg=; b=WMSwbfAB6OjRsXD1bNrDvS8RMyULGJm0QbI6SOadU5NpzvS3jwgG7I0BWe3RofhpJN iifkeWP1LfMcs8RpJUUS7YntkvUoK/WaeQcQnbaPPTT5ygT5EzgWAcMgpS8yx1FFBEy2 a0Uc3LoPTcgWFEZBOgspInC/YDVwK6Q6GSKV4B4utAQ8I2Cjuz7TZj4tTgM+A4yUA+Q/ lk3HWmm6D3MQPbfEWc2jX21zPy/BlUqjqcWIKbXklzHmvvOW3Ddxjd1gz+2d9ujJdBiA oL6KCiurIIBZbx5QxenHe+8NbctVaiUZ4jtcxP4OPfsBMkk/Nz3iBXoqPozR2XJijeNU 1k3A== X-Gm-Message-State: AOAM530NH10Uu92sAvrdsXJO57JDUtqLX/kTxlGj20hWut7xcOa5qwCS WMcU7S+JydPF3hFq5K6FJr31w1r4PLZVNPts7+3fKQ== X-Received: by 2002:a02:cab9:: with SMTP id e25mr15438249jap.25.1626730760955; Mon, 19 Jul 2021 14:39:20 -0700 (PDT) MIME-Version: 1.0 References: <20210719212302.167186-1-dlatypov@google.com> In-Reply-To: <20210719212302.167186-1-dlatypov@google.com> From: Daniel Latypov Date: Mon, 19 Jul 2021 14:39:03 -0700 Message-ID: Subject: Re: [PATCH] lib/test: convert test_min_heap.c to use KUnit To: irogers@google.com Cc: peterz@infradead.org, brendanhiggins@google.com, davidgow@google.com, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 19, 2021 at 2:23 PM Daniel Latypov wrote: > > We leave the file names and Kconfig option unchanged for now to minimize > disruption. Oh, I missed that there was an equivalent patch before https://lore.kernel.org/linux-kselftest/20201015170217.291916-1-vitor@massaru.org/ The main difference is the fact this doesn't change Kconfig options or module names and the test case organization. The other had test cases for each of the 3 ops, but added "true/false" instead of the more human readable "min/max". This change just has "test_min_heap" and "test_max_heap" test cases. > > So functionally, this just means that the test output will be slightly > changed and it'll now depend on CONFIG_KUNIT=y/m. > > It'll still run at boot time and can still be built as a loadable > module, but it can now be run a bit more easily via: > $ ./tools/testing/kunit/kunit.py run > > By default, the above command will run with CONFIG_KUNIT_ALL_TESTS=y. > Users could run this test individually via something like: > > $ ./tools/testing/kunit/kunit.py run --kunitconfig /dev/stdin < CONFIG_KUNIT=y > CONFIG_TEST_MIN_HEAP=y > EOF > > Example output: > [14:57:54] ============================================================ > [14:57:54] ======== [PASSED] lib_minmax_heap ======== > [14:57:54] [PASSED] test_min_heap > [14:57:54] [PASSED] test_max_heap > [14:57:54] ============================================================ > [14:57:54] Testing complete. 2 tests run. 0 failed. 0 crashed. 0 skipped. > [14:57:54] Elapsed time: 39.696s total, 0.001s configuring, 36.705s building, 0.000s running > > Note: this is the time it took after a `make mrproper`. > > Signed-off-by: Daniel Latypov > --- > lib/Kconfig.debug | 5 ++- > lib/test_min_heap.c | 102 ++++++++++++++++++++------------------------ > 2 files changed, 49 insertions(+), 58 deletions(-) > > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > index 831212722924..36e9529ede65 100644 > --- a/lib/Kconfig.debug > +++ b/lib/Kconfig.debug > @@ -2068,8 +2068,9 @@ config TEST_LIST_SORT > If unsure, say N. > > config TEST_MIN_HEAP > - tristate "Min heap test" > - depends on DEBUG_KERNEL || m > + tristate "Min heap test" if !KUNIT_ALL_TESTS > + depends on KUNIT > + default KUNIT_ALL_TESTS > help > Enable this to turn on min heap function tests. This test is > executed only once during system boot (so affects only boot time), > diff --git a/lib/test_min_heap.c b/lib/test_min_heap.c > index d19c8080fd4d..c0f1ef78c08e 100644 > --- a/lib/test_min_heap.c > +++ b/lib/test_min_heap.c > @@ -1,27 +1,27 @@ > // SPDX-License-Identifier: GPL-2.0-only > -#define pr_fmt(fmt) "min_heap_test: " fmt > - > /* > * Test cases for the min max heap. > */ > > +#include > + > #include > #include > #include > #include > #include > > -static __init bool less_than(const void *lhs, const void *rhs) > +static bool less_than(const void *lhs, const void *rhs) > { > return *(int *)lhs < *(int *)rhs; > } > > -static __init bool greater_than(const void *lhs, const void *rhs) > +static bool greater_than(const void *lhs, const void *rhs) > { > return *(int *)lhs > *(int *)rhs; > } > > -static __init void swap_ints(void *lhs, void *rhs) > +static void swap_ints(void *lhs, void *rhs) > { > int temp = *(int *)lhs; > > @@ -29,37 +29,27 @@ static __init void swap_ints(void *lhs, void *rhs) > *(int *)rhs = temp; > } > > -static __init int pop_verify_heap(bool min_heap, > - struct min_heap *heap, > - const struct min_heap_callbacks *funcs) > +static void pop_verify_heap(struct kunit *test, bool min_heap, > + struct min_heap *heap, > + const struct min_heap_callbacks *funcs) > { > int *values = heap->data; > - int err = 0; > int last; > > last = values[0]; > min_heap_pop(heap, funcs); > while (heap->nr > 0) { > if (min_heap) { > - if (last > values[0]) { > - pr_err("error: expected %d <= %d\n", last, > - values[0]); > - err++; > - } > + KUNIT_EXPECT_LE(test, last, values[0]); > } else { > - if (last < values[0]) { > - pr_err("error: expected %d >= %d\n", last, > - values[0]); > - err++; > - } > + KUNIT_EXPECT_GE(test, last, values[0]); > } > last = values[0]; > min_heap_pop(heap, funcs); > } > - return err; > } > > -static __init int test_heapify_all(bool min_heap) > +static void test_heapify_all(struct kunit *test, bool min_heap) > { > int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0, > -3, -1, -2, -4, 0x8000000, 0x7FFFFFF }; > @@ -73,11 +63,11 @@ static __init int test_heapify_all(bool min_heap) > .less = min_heap ? less_than : greater_than, > .swp = swap_ints, > }; > - int i, err; > + int i; > > /* Test with known set of values. */ > min_heapify_all(&heap, &funcs); > - err = pop_verify_heap(min_heap, &heap, &funcs); > + pop_verify_heap(test, min_heap, &heap, &funcs); > > > /* Test with randomly generated values. */ > @@ -86,12 +76,10 @@ static __init int test_heapify_all(bool min_heap) > values[i] = get_random_int(); > > min_heapify_all(&heap, &funcs); > - err += pop_verify_heap(min_heap, &heap, &funcs); > - > - return err; > + pop_verify_heap(test, min_heap, &heap, &funcs); > } > > -static __init int test_heap_push(bool min_heap) > +static void test_heap_push(struct kunit *test, bool min_heap) > { > const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, > -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; > @@ -106,25 +94,23 @@ static __init int test_heap_push(bool min_heap) > .less = min_heap ? less_than : greater_than, > .swp = swap_ints, > }; > - int i, temp, err; > + int i, temp; > > /* Test with known set of values copied from data. */ > for (i = 0; i < ARRAY_SIZE(data); i++) > min_heap_push(&heap, &data[i], &funcs); > > - err = pop_verify_heap(min_heap, &heap, &funcs); > + pop_verify_heap(test, min_heap, &heap, &funcs); > > /* Test with randomly generated values. */ > while (heap.nr < heap.size) { > temp = get_random_int(); > min_heap_push(&heap, &temp, &funcs); > } > - err += pop_verify_heap(min_heap, &heap, &funcs); > - > - return err; > + pop_verify_heap(test, min_heap, &heap, &funcs); > } > > -static __init int test_heap_pop_push(bool min_heap) > +static void test_heap_pop_push(struct kunit *test, bool min_heap) > { > const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0, > -3, -1, -2, -4, 0x80000000, 0x7FFFFFFF }; > @@ -139,7 +125,7 @@ static __init int test_heap_pop_push(bool min_heap) > .less = min_heap ? less_than : greater_than, > .swp = swap_ints, > }; > - int i, temp, err; > + int i, temp; > > /* Fill values with data to pop and replace. */ > temp = min_heap ? 0x80000000 : 0x7FFFFFFF; > @@ -150,7 +136,7 @@ static __init int test_heap_pop_push(bool min_heap) > for (i = 0; i < ARRAY_SIZE(data); i++) > min_heap_pop_push(&heap, &data[i], &funcs); > > - err = pop_verify_heap(min_heap, &heap, &funcs); > + pop_verify_heap(test, min_heap, &heap, &funcs); > > heap.nr = 0; > for (i = 0; i < ARRAY_SIZE(data); i++) > @@ -161,34 +147,38 @@ static __init int test_heap_pop_push(bool min_heap) > temp = get_random_int(); > min_heap_pop_push(&heap, &temp, &funcs); > } > - err += pop_verify_heap(min_heap, &heap, &funcs); > + pop_verify_heap(test, min_heap, &heap, &funcs); > +} > > - return err; > +static void test_heap(struct kunit *test, bool min_heap) > +{ > + test_heapify_all(test, min_heap); > + test_heap_push(test, min_heap); > + test_heap_pop_push(test, min_heap); > } > > -static int __init test_min_heap_init(void) > +static void test_min_heap(struct kunit *test) > { > - int err = 0; > - > - err += test_heapify_all(true); > - err += test_heapify_all(false); > - err += test_heap_push(true); > - err += test_heap_push(false); > - err += test_heap_pop_push(true); > - err += test_heap_pop_push(false); > - if (err) { > - pr_err("test failed with %d errors\n", err); > - return -EINVAL; > - } > - pr_info("test passed\n"); > - return 0; > + test_heap(test, true); > } > -module_init(test_min_heap_init); > > -static void __exit test_min_heap_exit(void) > +static void test_max_heap(struct kunit *test) > { > - /* do nothing */ > + test_heap(test, false); > } > -module_exit(test_min_heap_exit); > + > +static struct kunit_case __refdata minmax_heap_test_cases[] = { > + KUNIT_CASE(test_min_heap), > + KUNIT_CASE(test_max_heap), > + {} > +}; > + > +static struct kunit_suite minmax_heap_test_suite = { > + .name = "lib_minmax_heap", > + .test_cases = minmax_heap_test_cases, > +}; > + > +kunit_test_suites(&minmax_heap_test_suite); > + > > MODULE_LICENSE("GPL"); > > base-commit: 2734d6c1b1a089fb593ef6a23d4b70903526fe0c > -- > 2.32.0.402.g57bb445576-goog >