qdf_ptr_hash_test.c 4.9 KB

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