test-fs-fd-hash.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #if defined(_WIN32) && !defined(USING_UV_SHARED)
  22. #include "uv.h"
  23. #include "task.h"
  24. #include "../src/win/fs-fd-hash-inl.h"
  25. #define HASH_MAX 1000000000
  26. #define HASH_INC (1000 * UV__FD_HASH_SIZE + 2)
  27. #define BUCKET_MAX (UV__FD_HASH_SIZE * UV__FD_HASH_GROUP_SIZE * 10)
  28. #define BUCKET_INC UV__FD_HASH_SIZE
  29. #define FD_DIFF 9
  30. void assert_nonexistent(int fd) {
  31. struct uv__fd_info_s info = { 0 };
  32. ASSERT(!uv__fd_hash_get(fd, &info));
  33. ASSERT(!uv__fd_hash_remove(fd, &info));
  34. }
  35. void assert_existent(int fd) {
  36. struct uv__fd_info_s info = { 0 };
  37. ASSERT(uv__fd_hash_get(fd, &info));
  38. ASSERT(info.flags == fd + FD_DIFF);
  39. }
  40. void assert_insertion(int fd) {
  41. struct uv__fd_info_s info = { 0 };
  42. assert_nonexistent(fd);
  43. info.flags = fd + FD_DIFF;
  44. uv__fd_hash_add(fd, &info);
  45. assert_existent(fd);
  46. }
  47. void assert_removal(int fd) {
  48. struct uv__fd_info_s info = { 0 };
  49. assert_existent(fd);
  50. uv__fd_hash_remove(fd, &info);
  51. ASSERT(info.flags == fd + FD_DIFF);
  52. assert_nonexistent(fd);
  53. }
  54. /* Run a function for a set of values up to a very high number */
  55. #define RUN_HASH(function) \
  56. do { \
  57. for (fd = 0; fd < HASH_MAX; fd += HASH_INC) { \
  58. function(fd); \
  59. } \
  60. } while (0)
  61. /* Run a function for a set of values that will cause many collisions */
  62. #define RUN_COLLISIONS(function) \
  63. do { \
  64. for (fd = 1; fd < BUCKET_MAX; fd += BUCKET_INC) { \
  65. function(fd); \
  66. } \
  67. } while (0)
  68. TEST_IMPL(fs_fd_hash) {
  69. int fd;
  70. uv__fd_hash_init();
  71. /* Empty table */
  72. RUN_HASH(assert_nonexistent);
  73. RUN_COLLISIONS(assert_nonexistent);
  74. /* Fill up */
  75. RUN_HASH(assert_insertion);
  76. RUN_COLLISIONS(assert_insertion);
  77. /* Full */
  78. RUN_HASH(assert_existent);
  79. RUN_COLLISIONS(assert_existent);
  80. /* Update */
  81. {
  82. struct uv__fd_info_s info = { 0 };
  83. info.flags = FD_DIFF + FD_DIFF;
  84. uv__fd_hash_add(0, &info);
  85. }
  86. {
  87. struct uv__fd_info_s info = { 0 };
  88. ASSERT(uv__fd_hash_get(0, &info));
  89. ASSERT(info.flags == FD_DIFF + FD_DIFF);
  90. }
  91. {
  92. /* Leave as it was, will be again tested below */
  93. struct uv__fd_info_s info = { 0 };
  94. info.flags = FD_DIFF;
  95. uv__fd_hash_add(0, &info);
  96. }
  97. /* Remove all */
  98. RUN_HASH(assert_removal);
  99. RUN_COLLISIONS(assert_removal);
  100. /* Empty table */
  101. RUN_HASH(assert_nonexistent);
  102. RUN_COLLISIONS(assert_nonexistent);
  103. return 0;
  104. }
  105. #else
  106. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  107. #endif /* ifndef _WIN32 */