49 lines
907 B
C
Raw Permalink Normal View History

2026-01-21 18:59:54 +08:00
// SPDX-License-Identifier: GPL-2.0-only
2026-01-29 22:25:33 +08:00
#include <kunit/test.h>
2026-01-21 18:59:54 +08:00
#include <linux/sort.h>
#include <linux/slab.h>
#include <linux/module.h>
/* a simple boot-time regression test */
#define TEST_LEN 1000
2026-01-29 22:25:33 +08:00
static int cmpint(const void *a, const void *b)
2026-01-21 18:59:54 +08:00
{
return *(int *)a - *(int *)b;
}
2026-01-29 22:25:33 +08:00
static void test_sort(struct kunit *test)
2026-01-21 18:59:54 +08:00
{
2026-01-29 22:25:33 +08:00
int *a, i, r = 1;
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
2026-01-21 18:59:54 +08:00
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++)
2026-01-29 22:25:33 +08:00
KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
2026-01-21 18:59:54 +08:00
}
2026-01-29 22:25:33 +08:00
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,
};
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
kunit_test_suites(&sort_test_suite);
2026-01-21 18:59:54 +08:00
MODULE_LICENSE("GPL");