test_sort.c 907 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <kunit/test.h>
  3. #include <linux/sort.h>
  4. #include <linux/slab.h>
  5. #include <linux/module.h>
  6. /* a simple boot-time regression test */
  7. #define TEST_LEN 1000
  8. static int cmpint(const void *a, const void *b)
  9. {
  10. return *(int *)a - *(int *)b;
  11. }
  12. static void test_sort(struct kunit *test)
  13. {
  14. int *a, i, r = 1;
  15. a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
  16. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
  17. for (i = 0; i < TEST_LEN; i++) {
  18. r = (r * 725861) % 6599;
  19. a[i] = r;
  20. }
  21. sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
  22. for (i = 0; i < TEST_LEN-1; i++)
  23. KUNIT_ASSERT_LE(test, a[i], a[i + 1]);
  24. }
  25. static struct kunit_case sort_test_cases[] = {
  26. KUNIT_CASE(test_sort),
  27. {}
  28. };
  29. static struct kunit_suite sort_test_suite = {
  30. .name = "lib_sort",
  31. .test_cases = sort_test_cases,
  32. };
  33. kunit_test_suites(&sort_test_suite);
  34. MODULE_LICENSE("GPL");