test_list_sort.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #define pr_fmt(fmt) "list_sort_test: " fmt
  2. #include <linux/kernel.h>
  3. #include <linux/list_sort.h>
  4. #include <linux/list.h>
  5. #include <linux/module.h>
  6. #include <linux/printk.h>
  7. #include <linux/slab.h>
  8. #include <linux/random.h>
  9. /*
  10. * The pattern of set bits in the list length determines which cases
  11. * are hit in list_sort().
  12. */
  13. #define TEST_LIST_LEN (512+128+2) /* not including head */
  14. #define TEST_POISON1 0xDEADBEEF
  15. #define TEST_POISON2 0xA324354C
  16. struct debug_el {
  17. unsigned int poison1;
  18. struct list_head list;
  19. unsigned int poison2;
  20. int value;
  21. unsigned serial;
  22. };
  23. /* Array, containing pointers to all elements in the test list */
  24. static struct debug_el **elts __initdata;
  25. static int __init check(struct debug_el *ela, struct debug_el *elb)
  26. {
  27. if (ela->serial >= TEST_LIST_LEN) {
  28. pr_err("error: incorrect serial %d\n", ela->serial);
  29. return -EINVAL;
  30. }
  31. if (elb->serial >= TEST_LIST_LEN) {
  32. pr_err("error: incorrect serial %d\n", elb->serial);
  33. return -EINVAL;
  34. }
  35. if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
  36. pr_err("error: phantom element\n");
  37. return -EINVAL;
  38. }
  39. if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
  40. pr_err("error: bad poison: %#x/%#x\n",
  41. ela->poison1, ela->poison2);
  42. return -EINVAL;
  43. }
  44. if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
  45. pr_err("error: bad poison: %#x/%#x\n",
  46. elb->poison1, elb->poison2);
  47. return -EINVAL;
  48. }
  49. return 0;
  50. }
  51. static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
  52. {
  53. struct debug_el *ela, *elb;
  54. ela = container_of(a, struct debug_el, list);
  55. elb = container_of(b, struct debug_el, list);
  56. check(ela, elb);
  57. return ela->value - elb->value;
  58. }
  59. static int __init list_sort_test(void)
  60. {
  61. int i, count = 1, err = -ENOMEM;
  62. struct debug_el *el;
  63. struct list_head *cur;
  64. LIST_HEAD(head);
  65. pr_debug("start testing list_sort()\n");
  66. elts = kcalloc(TEST_LIST_LEN, sizeof(*elts), GFP_KERNEL);
  67. if (!elts)
  68. return err;
  69. for (i = 0; i < TEST_LIST_LEN; i++) {
  70. el = kmalloc(sizeof(*el), GFP_KERNEL);
  71. if (!el)
  72. goto exit;
  73. /* force some equivalencies */
  74. el->value = prandom_u32() % (TEST_LIST_LEN / 3);
  75. el->serial = i;
  76. el->poison1 = TEST_POISON1;
  77. el->poison2 = TEST_POISON2;
  78. elts[i] = el;
  79. list_add_tail(&el->list, &head);
  80. }
  81. list_sort(NULL, &head, cmp);
  82. err = -EINVAL;
  83. for (cur = head.next; cur->next != &head; cur = cur->next) {
  84. struct debug_el *el1;
  85. int cmp_result;
  86. if (cur->next->prev != cur) {
  87. pr_err("error: list is corrupted\n");
  88. goto exit;
  89. }
  90. cmp_result = cmp(NULL, cur, cur->next);
  91. if (cmp_result > 0) {
  92. pr_err("error: list is not sorted\n");
  93. goto exit;
  94. }
  95. el = container_of(cur, struct debug_el, list);
  96. el1 = container_of(cur->next, struct debug_el, list);
  97. if (cmp_result == 0 && el->serial >= el1->serial) {
  98. pr_err("error: order of equivalent elements not "
  99. "preserved\n");
  100. goto exit;
  101. }
  102. if (check(el, el1)) {
  103. pr_err("error: element check failed\n");
  104. goto exit;
  105. }
  106. count++;
  107. }
  108. if (head.prev != cur) {
  109. pr_err("error: list is corrupted\n");
  110. goto exit;
  111. }
  112. if (count != TEST_LIST_LEN) {
  113. pr_err("error: bad list length %d", count);
  114. goto exit;
  115. }
  116. err = 0;
  117. exit:
  118. for (i = 0; i < TEST_LIST_LEN; i++)
  119. kfree(elts[i]);
  120. kfree(elts);
  121. return err;
  122. }
  123. module_init(list_sort_test);
  124. MODULE_LICENSE("GPL");