Files
Centos-kernel-stream-9/lib/test_sort.c
T
Nico Pache acf08c2aa4 lib/test: convert test_sort.c to use KUnit
commit 36f33b562936295a0fb365ea0b1b2afcd8974242
Author: Daniel Latypov <dlatypov@google.com>
Date:   Tue Sep 7 19:58:48 2021 -0700

    lib/test: convert test_sort.c to use KUnit

    This follows up commit ebd09577be ("lib/test: convert
    lib/test_list_sort.c to use KUnit").

    Converting this test to KUnit makes the test a bit shorter, standardizes
    how it reports pass/fail, and adds an easier way to run the test [1].

    Like ebd09577be, this leaves the file and Kconfig option name the same,
    but slightly changes their dependencies (needs CONFIG_KUNIT).

    [1] Can be run via
    $ ./tools/testing/kunit/kunit.py run --kunitconfig /dev/stdin <<EOF
    CONFIG_KUNIT=y
    CONFIG_TEST_SORT=y
    EOF

    [11:30:27] Starting KUnit Kernel ...
    [11:30:30] ============================================================
    [11:30:30] ======== [PASSED] lib_sort ========
    [11:30:30] [PASSED] test_sort
    [11:30:30] ============================================================
    [11:30:30] Testing complete. 1 tests run. 0 failed. 0 crashed. 0 skipped.
    [11:30:30] Elapsed time: 37.032s total, 0.001s configuring, 34.090s building, 0.000s running

    Note: this is the time it took after a `make mrproper`.

    With an incremental rebuild, this looks more like:
    [11:38:58] Elapsed time: 6.444s total, 0.001s configuring, 3.416s building, 0.000s running

    Since the test has no dependencies, it can also be run (with some other
    tests) with just:
    $ ./tools/testing/kunit/kunit.py run

    Link: https://lkml.kernel.org/r/20210715232441.1380885-1-dlatypov@google.com
    Signed-off-by: Daniel Latypov <dlatypov@google.com>
    Cc: Pravin Shedge <pravin.shedge4linux@gmail.com>
    Cc: Brendan Higgins <brendanhiggins@google.com>
    Cc: David Gow <davidgow@google.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2168378
Signed-off-by: Nico Pache <npache@redhat.com>
2023-04-17 11:20:40 -06:00

49 lines
907 B
C

// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/sort.h>
#include <linux/slab.h>
#include <linux/module.h>
/* a simple boot-time regression test */
#define TEST_LEN 1000
static int cmpint(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
static void test_sort(struct kunit *test)
{
int *a, i, r = 1;
a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
for (i = 0; i < TEST_LEN; i++) {
r = (r * 725861) % 6599;
a[i] = r;
}
sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
for (i = 0; i < TEST_LEN-1; i++)
KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
}
static struct kunit_case sort_test_cases[] = {
KUNIT_CASE(test_sort),
{}
};
static struct kunit_suite sort_test_suite = {
.name = "lib_sort",
.test_cases = sort_test_cases,
};
kunit_test_suites(&sort_test_suite);
MODULE_LICENSE("GPL");