libuv-svace-build
133 строки · 4.0 Кб
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
22#if defined(_WIN32) && !defined(USING_UV_SHARED)
23
24#include "uv.h"
25#include "task.h"
26
27#include "../src/win/fs-fd-hash-inl.h"
28
29
30#define HASH_MAX 1000000000
31#define HASH_INC (1000 * UV__FD_HASH_SIZE + 2)
32#define BUCKET_MAX (UV__FD_HASH_SIZE * UV__FD_HASH_GROUP_SIZE * 10)
33#define BUCKET_INC UV__FD_HASH_SIZE
34#define FD_DIFF 9
35
36
37void assert_nonexistent(int fd) {
38struct uv__fd_info_s info = { 0 };
39ASSERT(!uv__fd_hash_get(fd, &info));
40ASSERT(!uv__fd_hash_remove(fd, &info));
41}
42
43void assert_existent(int fd) {
44struct uv__fd_info_s info = { 0 };
45ASSERT(uv__fd_hash_get(fd, &info));
46ASSERT_EQ(info.flags, fd + FD_DIFF);
47}
48
49void assert_insertion(int fd) {
50struct uv__fd_info_s info = { 0 };
51assert_nonexistent(fd);
52info.flags = fd + FD_DIFF;
53uv__fd_hash_add(fd, &info);
54assert_existent(fd);
55}
56
57void assert_removal(int fd) {
58struct uv__fd_info_s info = { 0 };
59assert_existent(fd);
60uv__fd_hash_remove(fd, &info);
61ASSERT_EQ(info.flags, fd + FD_DIFF);
62assert_nonexistent(fd);
63}
64
65
66/* Run a function for a set of values up to a very high number */
67#define RUN_HASH(function) \
68do { \
69for (fd = 0; fd < HASH_MAX; fd += HASH_INC) { \
70function(fd); \
71} \
72} while (0)
73
74/* Run a function for a set of values that will cause many collisions */
75#define RUN_COLLISIONS(function) \
76do { \
77for (fd = 1; fd < BUCKET_MAX; fd += BUCKET_INC) { \
78function(fd); \
79} \
80} while (0)
81
82
83TEST_IMPL(fs_fd_hash) {
84int fd;
85
86uv__fd_hash_init();
87
88/* Empty table */
89RUN_HASH(assert_nonexistent);
90RUN_COLLISIONS(assert_nonexistent);
91
92/* Fill up */
93RUN_HASH(assert_insertion);
94RUN_COLLISIONS(assert_insertion);
95
96/* Full */
97RUN_HASH(assert_existent);
98RUN_COLLISIONS(assert_existent);
99
100/* Update */
101{
102struct uv__fd_info_s info = { 0 };
103info.flags = FD_DIFF + FD_DIFF;
104uv__fd_hash_add(0, &info);
105}
106{
107struct uv__fd_info_s info = { 0 };
108ASSERT(uv__fd_hash_get(0, &info));
109ASSERT_EQ(info.flags, FD_DIFF + FD_DIFF);
110}
111{
112/* Leave as it was, will be again tested below */
113struct uv__fd_info_s info = { 0 };
114info.flags = FD_DIFF;
115uv__fd_hash_add(0, &info);
116}
117
118/* Remove all */
119RUN_HASH(assert_removal);
120RUN_COLLISIONS(assert_removal);
121
122/* Empty table */
123RUN_HASH(assert_nonexistent);
124RUN_COLLISIONS(assert_nonexistent);
125
126return 0;
127}
128
129#else
130
131typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
132
133#endif /* ifndef _WIN32 */
134