test_sort.c 752 B

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