1
0

Set_test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "memory/Allocator.h"
  17. #include "util/Assert.h"
  18. #define Set_NAME OfInts
  19. #define Set_TYPE uint32_t
  20. #include "util/Set.h"
  21. static inline int compareHighOnly(const uint32_t* a, const uint32_t* b)
  22. {
  23. return (*a >> 16) - (*b >> 16);
  24. }
  25. static inline uint32_t hashCodeHighOnly(const uint32_t* a)
  26. {
  27. return *a >> 16;
  28. }
  29. #define Set_COMPARE compareHighOnly
  30. #define Set_HASHCODE hashCodeHighOnly
  31. #define Set_NAME OfIntsHighOnly
  32. #define Set_TYPE uint32_t
  33. #include "util/Set.h"
  34. #include <stdio.h>
  35. #include <stdbool.h>
  36. #define CYCLES 1
  37. #define COUNT 100
  38. static void simpleTest(struct Allocator* alloc, struct Random* rand)
  39. {
  40. struct Set_OfInts* set = Set_OfInts_new(alloc);
  41. uint32_t size;
  42. Random_bytes(rand, (uint8_t*) &size, 4);
  43. size = (size % 4096) + 101;
  44. uint32_t* buff = Allocator_malloc(alloc, size * sizeof(uint32_t));
  45. Random_bytes(rand, (uint8_t*)buff, size * sizeof(uint32_t));
  46. for (uint32_t i = 0; i < size; i++) {
  47. int size = set->size;
  48. if (Set_OfInts_add(set, &buff[i]) != size + 1) {
  49. int ok = 0;
  50. for (uint32_t j = 0; j < i; j++) {
  51. if (buff[j] == buff[i]) { ok = 1; break; }
  52. }
  53. Assert_true(ok);
  54. } else {
  55. for (uint32_t j = 0; j < i; j++) { Assert_true(buff[j] != buff[i]); }
  56. }
  57. }
  58. uint32_t* val;
  59. Set_FOREACH(OfInts, set, val) {
  60. int size = set->size;
  61. Assert_true(val == Set_OfInts_remove(set, val));
  62. Assert_true(set->size == size - 1);
  63. }
  64. Assert_true(set->size == 0);
  65. for (uint32_t i = 0; i < COUNT; i++) {
  66. buff[i] = i;
  67. Assert_true(set->size == (int)i);
  68. Assert_true(Set_OfInts_add(set, &buff[i]) == (int) i+1);
  69. }
  70. for (uint32_t i = 0; i < COUNT; i++) {
  71. uint32_t num = i;
  72. Assert_true(Set_OfInts_get(set, &num) == &buff[i]);
  73. }
  74. }
  75. static void comparitorTest(struct Allocator* alloc, struct Random* rand)
  76. {
  77. struct Set_OfIntsHighOnly* set = Set_OfIntsHighOnly_new(alloc);
  78. uint32_t buff[5] = { 0, 1, 100000, 100001, 200000 };
  79. uint32_t res[3] = { 0, 100000, 200000 };
  80. for (uint32_t i = 0; i < 5; i++) { Set_OfIntsHighOnly_add(set, &buff[i]); }
  81. Assert_true(set->size == 3);
  82. int i = 0;
  83. struct Set_OfIntsHighOnly_Iter iter;
  84. for (Set_OfIntsHighOnly_iter(set, &iter); iter.val; Set_OfIntsHighOnly_iterNext(&iter)) {
  85. Assert_true(i < 3);
  86. Assert_true(*iter.val == res[i++]);
  87. }
  88. }
  89. int main()
  90. {
  91. struct Allocator* mainAlloc = Allocator_new(20000);
  92. struct Random* rand = Random_new(mainAlloc, NULL, NULL);
  93. for (int cycles = 0; cycles < CYCLES; cycles++) {
  94. struct Allocator* alloc = Allocator_new(1<<25);
  95. simpleTest(alloc, rand);
  96. Allocator_free(alloc);
  97. alloc = Allocator_new(1<<18);
  98. comparitorTest(alloc, rand);
  99. Allocator_free(alloc);
  100. }
  101. Allocator_free(mainAlloc);
  102. return 0;
  103. }