Map_test.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "memory/MallocAllocator.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 = MallocAllocator_new(20000);
  29. struct Random* rand = Random_new(mainAlloc, NULL, NULL);
  30. for (int cycles = 0; cycles < CYCLES; cycles++) {
  31. struct Allocator* alloc = MallocAllocator_new(1<<18);
  32. struct Map_OfLongsByInteger* map = Map_OfLongsByInteger_new(alloc);
  33. uint32_t size;
  34. Random_bytes(rand, (uint8_t*) &size, 4);
  35. size = (size % 4096) + 101;
  36. uint32_t key = 3;
  37. uint64_t val = 4;
  38. for (uint32_t i = 0; i < size; i++) {
  39. Map_OfLongsByInteger_put(&key, &val, map);
  40. key += val >> 13 ^ size << 19;
  41. val += key >> 19 ^ i << 13;
  42. }
  43. // If a key is duplicated, the entry will br replaced.
  44. size = map->count;
  45. for (uint32_t i = size - 1; i > size - 100; i--) {
  46. int index = map->keys[i] % size;
  47. uint32_t handle = map->handles[index];
  48. if (index != Map_OfLongsByInteger_indexForHandle(handle, map)) {
  49. uint32_t num = 0;
  50. for (int i = 0; i < (int)map->count; i++) {
  51. if (num > map->handles[i]) {
  52. Assert_true(!"map out of order");
  53. }
  54. num = map->handles[i];
  55. }
  56. printf("failed to find the correct index for the handle "
  57. "handle[%u], index[%u], indexForHandle[%u]\n",
  58. handle, index, Map_OfLongsByInteger_indexForHandle(handle, map));
  59. Assert_true(false);
  60. }
  61. }
  62. Allocator_free(alloc);
  63. }
  64. Allocator_free(mainAlloc);
  65. return 0;
  66. }