qdf_ptr_hash_test.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "qdf_ptr_hash.h"
  19. #include "qdf_ptr_hash_test.h"
  20. #include "qdf_trace.h"
  21. #define qdf_ptr_hash_bits 4 /* 16 buckets */
  22. #define qdf_ptr_hash_entry_count 10
  23. struct qdf_ptr_hash_test_item {
  24. uint32_t id;
  25. struct qdf_ptr_hash_entry entry;
  26. };
  27. static uint32_t __qdf_ptr_hash_test_empty(struct qdf_ptr_hash *ht)
  28. {
  29. struct qdf_ptr_hash_test_item *item;
  30. /* a new ptr_hash should ... */
  31. /* ... be empty */
  32. QDF_BUG(qdf_ptr_hash_empty(ht));
  33. /* ... return NULL with get()'d */
  34. QDF_BUG(!qdf_ptr_hash_get(ht, NULL, item, entry));
  35. return 0;
  36. }
  37. static uint32_t qdf_ptr_hash_test_empty(void)
  38. {
  39. qdf_ptr_hash_declare_ptr(ht, qdf_ptr_hash_bits);
  40. int errors;
  41. qdf_ptr_hash_init(ht);
  42. errors = __qdf_ptr_hash_test_empty(ht);
  43. qdf_ptr_hash_deinit(ht);
  44. return errors;
  45. }
  46. static uint32_t __qdf_ptr_hash_test_add_remove(struct qdf_ptr_hash *ht)
  47. {
  48. struct qdf_ptr_hash_test_item items[qdf_ptr_hash_entry_count];
  49. struct qdf_ptr_hash_test_item *item;
  50. int i;
  51. /* a ptr_hash with items should ... */
  52. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  53. items[i].id = i;
  54. qdf_ptr_hash_add(ht, &items[i], &items[i], entry);
  55. }
  56. /* ... not be empty */
  57. QDF_BUG(!qdf_ptr_hash_empty(ht));
  58. /* ... be able to get() all items previously add()'d */
  59. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  60. QDF_BUG(qdf_ptr_hash_get(ht, &items[i], item, entry));
  61. QDF_BUG(item->id == items[i].id);
  62. }
  63. /* ... be able to remove() all items previously add()'d */
  64. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  65. QDF_BUG(qdf_ptr_hash_remove(ht, &items[i], item, entry));
  66. QDF_BUG(item->id == items[i].id);
  67. }
  68. /* ... be empty after remove()'ing all items */
  69. QDF_BUG(qdf_ptr_hash_empty(ht));
  70. return 0;
  71. }
  72. static uint32_t qdf_ptr_hash_test_add_remove(void)
  73. {
  74. qdf_ptr_hash_declare_ptr(ht, qdf_ptr_hash_bits);
  75. int errors;
  76. qdf_ptr_hash_init(ht);
  77. errors = __qdf_ptr_hash_test_add_remove(ht);
  78. qdf_ptr_hash_deinit(ht);
  79. return errors;
  80. }
  81. static uint32_t __qdf_ptr_hash_test_for_each(struct qdf_ptr_hash *ht)
  82. {
  83. struct qdf_ptr_hash_bucket *bucket;
  84. struct qdf_ptr_hash_test_item items[qdf_ptr_hash_entry_count];
  85. struct qdf_ptr_hash_test_item *item;
  86. int i;
  87. int count;
  88. /* a ptr_hash with items should ... */
  89. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  90. items[i].id = i;
  91. qdf_ptr_hash_add(ht, i, &items[i], entry);
  92. }
  93. /* ... be able to iterate over each item */
  94. count = 0;
  95. qdf_ptr_hash_for_each(ht, bucket, item, entry) {
  96. QDF_BUG(item->id == items[item->id].id);
  97. count++;
  98. }
  99. QDF_BUG(count == qdf_ptr_hash_entry_count);
  100. /* ... be able to interate by hash value */
  101. count = 0;
  102. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  103. qdf_ptr_hash_for_each_by_hash(ht, i, item, entry) {
  104. QDF_BUG(item->id == items[item->id].id);
  105. count++;
  106. }
  107. }
  108. QDF_BUG(count >= qdf_ptr_hash_entry_count);
  109. /* ... be able to interate by key value */
  110. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  111. count = 0;
  112. qdf_ptr_hash_for_each_by_key(ht, i, item, entry) {
  113. QDF_BUG(item->id == items[i].id);
  114. count++;
  115. }
  116. QDF_BUG(count == 1);
  117. }
  118. /* ... be able to remove each item */
  119. for (i = 0; i < qdf_ptr_hash_entry_count; i++) {
  120. qdf_ptr_hash_remove(ht, i, item, entry);
  121. QDF_BUG(item);
  122. QDF_BUG(item->id == items[i].id);
  123. }
  124. /* ... be empty after all items are removed */
  125. QDF_BUG(qdf_ptr_hash_empty(ht));
  126. return 0;
  127. }
  128. static uint32_t qdf_ptr_hash_test_for_each(void)
  129. {
  130. qdf_ptr_hash_declare_ptr(ht, qdf_ptr_hash_bits);
  131. int errors;
  132. qdf_ptr_hash_init(ht);
  133. errors = __qdf_ptr_hash_test_for_each(ht);
  134. qdf_ptr_hash_deinit(ht);
  135. return errors;
  136. }
  137. static uint32_t qdf_ptr_hash_test_create_destroy(void)
  138. {
  139. struct qdf_ptr_hash *ht = qdf_ptr_hash_create(qdf_ptr_hash_bits);
  140. uint32_t errors = 0;
  141. QDF_BUG(ht);
  142. errors += __qdf_ptr_hash_test_empty(ht);
  143. errors += __qdf_ptr_hash_test_add_remove(ht);
  144. errors += __qdf_ptr_hash_test_for_each(ht);
  145. qdf_ptr_hash_destroy(ht);
  146. return errors;
  147. }
  148. uint32_t qdf_ptr_hash_unit_test(void)
  149. {
  150. uint32_t errors = 0;
  151. errors += qdf_ptr_hash_test_empty();
  152. errors += qdf_ptr_hash_test_add_remove();
  153. errors += qdf_ptr_hash_test_for_each();
  154. errors += qdf_ptr_hash_test_create_destroy();
  155. return errors;
  156. }