Map_fuzz_test.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/MallocAllocator.h"
  17. #include "benc/String.h"
  18. #include "util/Assert.h"
  19. #include "util/events/Time.h"
  20. #include "util/log/FileWriterLog.h"
  21. #define Map_NAME OfLongsByInteger
  22. #define Map_KEY_TYPE uint32_t
  23. #define Map_VALUE_TYPE uint64_t
  24. #define Map_ENABLE_HANDLES
  25. #include "util/Map.h"
  26. #include <stdio.h>
  27. #include <stdbool.h>
  28. // Increase this number to make the fuzz test run longer.
  29. #define QUICK_CYCLES 5
  30. #define SLOW_CYCLES 1000
  31. int main(int argc, char* argv[])
  32. {
  33. int cycles = QUICK_CYCLES;
  34. for (int i = 0; i < argc; i++) {
  35. if (!CString_strcmp("--fuzz", argv[i])) {
  36. cycles = SLOW_CYCLES;
  37. break;
  38. }
  39. }
  40. // This test is too slow to run with the normal battery of tests
  41. if (cycles == QUICK_CYCLES) {
  42. return 0;
  43. }
  44. struct Allocator* mainAlloc = MallocAllocator_new(1<<18);
  45. struct Random* rand = Random_new(mainAlloc, NULL, NULL);
  46. struct Log* logger = FileWriterLog_new(stdout, mainAlloc);
  47. for (int cycle = 0; cycle < cycles; cycle++) {
  48. struct Allocator* alloc = MallocAllocator_new(1<<24);
  49. struct Map_OfLongsByInteger* map = Map_OfLongsByInteger_new(alloc);
  50. uint32_t size;
  51. Random_bytes(rand, (uint8_t*) &size, 4);
  52. size = size % 4096;
  53. uint32_t* keys = Allocator_malloc(alloc, sizeof(uint32_t) * size);
  54. uint64_t* vals = Allocator_malloc(alloc, sizeof(uint64_t) * size);
  55. uint32_t key = 3;
  56. uint64_t val = 4;
  57. int64_t begin = Time_hrtime();
  58. for (uint32_t i = 0; i < size; i++) {
  59. keys[i] = key;
  60. vals[i] = val;
  61. Map_OfLongsByInteger_put(&key, &val, map);
  62. key += ((val >> 13 ^ size << 19) & 0x000fffff) + 1;
  63. //Log_debug(logger, "%u", (val >> 13 ^ size << 19) & 0x000fffff);
  64. Assert_true(key > keys[i]);
  65. val += key >> 19 ^ i << 13;
  66. }
  67. int64_t timeUsed = Time_hrtime() - begin;
  68. Log_debug(logger, "cycle %d Map put %u values used %lu ms.\n",
  69. cycle, size, (unsigned long)(timeUsed / 1000000));
  70. // check all keys there
  71. for (uint32_t i = 0; i < size; ++i) {
  72. int index = Map_OfLongsByInteger_indexForKey(&keys[i], map);
  73. Assert_true(map->values[index] == vals[i]);
  74. }
  75. Allocator_free(alloc);
  76. }
  77. Log_debug(logger, "===+++=== Completed Ok ===++++===");
  78. Allocator_free(mainAlloc);
  79. return 0;
  80. }