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