3
0
mirror of https://github.com/Qortal/Brooklyn.git synced 2025-02-12 02:05:54 +00:00
Brooklyn/lib/test_sort.c

49 lines
907 B
C
Raw Normal View History

2021-05-27 00:09:36 +05:00
// SPDX-License-Identifier: GPL-2.0-only
2021-10-02 21:09:28 +05:00
#include <kunit/test.h>
2021-05-27 00:09:36 +05:00
#include <linux/sort.h>
#include <linux/slab.h>
#include <linux/module.h>
/* a simple boot-time regression test */
#define TEST_LEN 1000
2021-10-02 21:09:28 +05:00
static int cmpint(const void *a, const void *b)
2021-05-27 00:09:36 +05:00
{
return *(int *)a - *(int *)b;
}
2021-10-02 21:09:28 +05:00
static void test_sort(struct kunit *test)
2021-05-27 00:09:36 +05:00
{
2021-10-02 21:09:28 +05:00
int *a, i, r = 1;
2021-05-27 00:09:36 +05:00
2021-10-02 21:09:28 +05:00
a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
2021-05-27 00:09:36 +05: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++)
2021-10-02 21:09:28 +05:00
KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
2021-05-27 00:09:36 +05:00
}
2021-10-02 21:09:28 +05: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,
};
2021-05-27 00:09:36 +05:00
2021-10-02 21:09:28 +05:00
kunit_test_suites(&sort_test_suite);
2021-05-27 00:09:36 +05:00
MODULE_LICENSE("GPL");