test_sort.c 829 B

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