18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/sort.h>
38c2ecf20Sopenharmony_ci#include <linux/slab.h>
48c2ecf20Sopenharmony_ci#include <linux/module.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/* a simple boot-time regression test */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define TEST_LEN 1000
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic int __init cmpint(const void *a, const void *b)
118c2ecf20Sopenharmony_ci{
128c2ecf20Sopenharmony_ci	return *(int *)a - *(int *)b;
138c2ecf20Sopenharmony_ci}
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic int __init test_sort_init(void)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	int *a, i, r = 1, err = -ENOMEM;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	a = kmalloc_array(TEST_LEN, sizeof(*a), GFP_KERNEL);
208c2ecf20Sopenharmony_ci	if (!a)
218c2ecf20Sopenharmony_ci		return err;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	for (i = 0; i < TEST_LEN; i++) {
248c2ecf20Sopenharmony_ci		r = (r * 725861) % 6599;
258c2ecf20Sopenharmony_ci		a[i] = r;
268c2ecf20Sopenharmony_ci	}
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	err = -EINVAL;
318c2ecf20Sopenharmony_ci	for (i = 0; i < TEST_LEN-1; i++)
328c2ecf20Sopenharmony_ci		if (a[i] > a[i+1]) {
338c2ecf20Sopenharmony_ci			pr_err("test has failed\n");
348c2ecf20Sopenharmony_ci			goto exit;
358c2ecf20Sopenharmony_ci		}
368c2ecf20Sopenharmony_ci	err = 0;
378c2ecf20Sopenharmony_ci	pr_info("test passed\n");
388c2ecf20Sopenharmony_ciexit:
398c2ecf20Sopenharmony_ci	kfree(a);
408c2ecf20Sopenharmony_ci	return err;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void __exit test_sort_exit(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cimodule_init(test_sort_init);
488c2ecf20Sopenharmony_cimodule_exit(test_sort_exit);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
51