Map_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 Map_NAME OfLongsByInteger
  19. #define Map_KEY_TYPE uint32_t
  20. #define Map_VALUE_TYPE uint64_t
  21. #define Map_ENABLE_HANDLES
  22. #include "util/Map.h"
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #define CYCLES 1
  26. int main()
  27. {
  28. struct Allocator* mainAlloc = Allocator_new(20000);
  29. struct Random* rand = NULL;
  30. Err_assert(Random_new(&rand, mainAlloc, NULL));
  31. for (int cycles = 0; cycles < CYCLES; cycles++) {
  32. struct Allocator* alloc = Allocator_new(1<<18);
  33. struct Map_OfLongsByInteger* map = Map_OfLongsByInteger_new(alloc);
  34. uint32_t size;
  35. Random_bytes(rand, (uint8_t*) &size, 4);
  36. size = (size % 4096) + 101;
  37. uint32_t key = 3;
  38. uint64_t val = 4;
  39. for (uint32_t i = 0; i < size; i++) {
  40. Map_OfLongsByInteger_put(&key, &val, map);
  41. key += val >> 13 ^ size << 19;
  42. val += key >> 19 ^ i << 13;
  43. }
  44. // If a key is duplicated, the entry will br replaced.
  45. size = map->count;
  46. for (uint32_t i = size - 1; i > size - 100; i--) {
  47. int index = map->keys[i] % size;
  48. uint32_t handle = map->handles[index];
  49. if (index != Map_OfLongsByInteger_indexForHandle(handle, map)) {
  50. uint32_t num = 0;
  51. for (int i = 0; i < (int)map->count; i++) {
  52. if (num > map->handles[i]) {
  53. Assert_true(!"map out of order");
  54. }
  55. num = map->handles[i];
  56. }
  57. printf("failed to find the correct index for the handle "
  58. "handle[%u], index[%u], indexForHandle[%u]\n",
  59. handle, index, Map_OfLongsByInteger_indexForHandle(handle, map));
  60. Assert_true(false);
  61. }
  62. }
  63. Allocator_free(alloc);
  64. }
  65. Allocator_free(mainAlloc);
  66. return 0;
  67. }