test_sort.c 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/sort.h>
  3. #include <linux/slab.h>
  4. #include <linux/module.h>
  5. /* a simple boot-time regression test */
  6. #define TEST_LEN 1000
  7. static int __init cmpint(const void *a, const void *b)
  8. {
  9. return *(int *)a - *(int *)b;
  10. }
  11. static int __init test_sort_init(void)
  12. {
  13. int *a, i, r = 1, err = -ENOMEM;
  14. a = kmalloc_array(TEST_LEN, sizeof(*a), GFP_KERNEL);
  15. if (!a)
  16. return err;
  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. err = -EINVAL;
  23. for (i = 0; i < TEST_LEN-1; i++)
  24. if (a[i] > a[i+1]) {
  25. pr_err("test has failed\n");
  26. goto exit;
  27. }
  28. err = 0;
  29. pr_info("test passed\n");
  30. exit:
  31. kfree(a);
  32. return err;
  33. }
  34. static void __exit test_sort_exit(void)
  35. {
  36. }
  37. module_init(test_sort_init);
  38. module_exit(test_sort_exit);
  39. MODULE_LICENSE("GPL");