blob: be02e3a098cf55699b4f2a15523cd3454f639111 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Daniel Latypov36f33b52021-09-07 19:58:48 -07002
3#include <kunit/test.h>
4
Kostenzer Felixc5adae92017-02-24 15:01:07 -08005#include <linux/sort.h>
6#include <linux/slab.h>
Geert Uytterhoevenebd03a92017-05-08 15:55:20 -07007#include <linux/module.h>
Kostenzer Felixc5adae92017-02-24 15:01:07 -08008
Geert Uytterhoevenebd03a92017-05-08 15:55:20 -07009/* a simple boot-time regression test */
Kostenzer Felixc5adae92017-02-24 15:01:07 -080010
11#define TEST_LEN 1000
12
Daniel Latypov36f33b52021-09-07 19:58:48 -070013static int cmpint(const void *a, const void *b)
Kostenzer Felixc5adae92017-02-24 15:01:07 -080014{
15 return *(int *)a - *(int *)b;
16}
17
Daniel Latypov36f33b52021-09-07 19:58:48 -070018static void test_sort(struct kunit *test)
Kostenzer Felixc5adae92017-02-24 15:01:07 -080019{
Daniel Latypov36f33b52021-09-07 19:58:48 -070020 int *a, i, r = 1;
Kostenzer Felixc5adae92017-02-24 15:01:07 -080021
Daniel Latypov36f33b52021-09-07 19:58:48 -070022 a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
23 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
Kostenzer Felixc5adae92017-02-24 15:01:07 -080024
25 for (i = 0; i < TEST_LEN; i++) {
26 r = (r * 725861) % 6599;
27 a[i] = r;
28 }
29
30 sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
31
Kostenzer Felixc5adae92017-02-24 15:01:07 -080032 for (i = 0; i < TEST_LEN-1; i++)
Daniel Latypov36f33b52021-09-07 19:58:48 -070033 KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
Kostenzer Felixc5adae92017-02-24 15:01:07 -080034}
Geert Uytterhoevenebd03a92017-05-08 15:55:20 -070035
Daniel Latypov36f33b52021-09-07 19:58:48 -070036static struct kunit_case sort_test_cases[] = {
37 KUNIT_CASE(test_sort),
38 {}
39};
Pravin Shedge92fc7cb2018-02-06 15:38:42 -080040
Daniel Latypov36f33b52021-09-07 19:58:48 -070041static struct kunit_suite sort_test_suite = {
42 .name = "lib_sort",
43 .test_cases = sort_test_cases,
44};
45
46kunit_test_suites(&sort_test_suite);
Pravin Shedge92fc7cb2018-02-06 15:38:42 -080047
Geert Uytterhoevenebd03a92017-05-08 15:55:20 -070048MODULE_LICENSE("GPL");