stream_map_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <grpc/support/log.h>
22 
24 
25 #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
26 
27 /* test creation & destruction */
28 static void test_no_op(void) {
30 
31  LOG_TEST("test_no_op");
32 
35 }
36 
37 /* test lookup on an empty map */
38 static void test_empty_find(void) {
40 
41  LOG_TEST("test_empty_find");
42 
44  GPR_ASSERT(nullptr == grpc_chttp2_stream_map_find(&map, 39128));
46 }
47 
48 /* test add & lookup */
51  uint32_t i;
52  size_t got;
53 
54  LOG_TEST("test_basic_add_find");
55  gpr_log(GPR_INFO, "n = %d", n);
56 
59  for (i = 1; i <= n; i++) {
60  grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
61  }
64  GPR_ASSERT(nullptr == grpc_chttp2_stream_map_find(&map, n + 1));
65  for (i = 1; i <= n; i++) {
66  got = reinterpret_cast<uintptr_t>(grpc_chttp2_stream_map_find(&map, i));
67  GPR_ASSERT(i == got);
68  }
70 }
71 
72 /* verify that for_each gets the right values during test_delete_evens_XXX */
73 static void verify_for_each(void* user_data, uint32_t stream_id, void* ptr) {
74  uint32_t* for_each_check = static_cast<uint32_t*>(user_data);
75  GPR_ASSERT(ptr);
76  GPR_ASSERT(*for_each_check == stream_id);
77  *for_each_check += 2;
78 }
79 
81  uint32_t for_each_check = 1;
82  uint32_t i;
83  size_t got;
84 
86  GPR_ASSERT(nullptr == grpc_chttp2_stream_map_find(map, n + 1));
87  for (i = 1; i <= n; i++) {
88  if (i & 1) {
89  got = reinterpret_cast<uintptr_t>(grpc_chttp2_stream_map_find(map, i));
90  GPR_ASSERT(i == got);
91  } else {
93  }
94  }
95 
97  if (n & 1) {
98  GPR_ASSERT(for_each_check == n + 2);
99  } else {
100  GPR_ASSERT(for_each_check == n + 1);
101  }
102 }
103 
104 /* add a bunch of keys, delete the even ones, and make sure the map is
105  consistent */
108  uint32_t i;
109 
110  LOG_TEST("test_delete_evens_sweep");
111  gpr_log(GPR_INFO, "n = %d", n);
112 
114  for (i = 1; i <= n; i++) {
115  grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
116  }
117  for (i = 1; i <= n; i++) {
118  if ((i & 1) == 0) {
120  }
121  }
124 }
125 
126 /* add a bunch of keys, delete the even ones immediately, and make sure the map
127  is consistent */
130  uint32_t i;
131 
132  LOG_TEST("test_delete_evens_incremental");
133  gpr_log(GPR_INFO, "n = %d", n);
134 
136  for (i = 1; i <= n; i++) {
137  grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
138  if ((i & 1) == 0) {
140  }
141  }
144 }
145 
146 /* add a bunch of keys, delete old ones after some time, ensure the
147  backing array does not grow */
150  uint32_t i;
151  uint32_t del;
152 
153  LOG_TEST("test_periodic_compaction");
154  gpr_log(GPR_INFO, "n = %d", n);
155 
157  GPR_ASSERT(map.capacity == 16);
158  for (i = 1; i <= n; i++) {
159  grpc_chttp2_stream_map_add(&map, i, reinterpret_cast<void*>(i));
160  if (i > 8) {
161  del = i - 8;
162  GPR_ASSERT((void*)(uintptr_t)del ==
164  }
165  }
166  GPR_ASSERT(map.capacity == 16);
168 }
169 
170 int main(int argc, char** argv) {
171  uint32_t n = 1;
172  uint32_t prev = 1;
173  uint32_t tmp;
174 
175  grpc::testing::TestEnvironment env(&argc, argv);
176 
177  test_no_op();
178  test_empty_find();
179 
180  while (n < 100000) {
185 
186  tmp = n;
187  n += prev;
188  prev = tmp;
189  }
190 
191  return 0;
192 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
generate.env
env
Definition: generate.py:37
LOG_TEST
#define LOG_TEST(x)
Definition: stream_map_test.cc:25
test_delete_evens_sweep
static void test_delete_evens_sweep(uint32_t n)
Definition: stream_map_test.cc:106
main
int main(int argc, char **argv)
Definition: stream_map_test.cc:170
verify_for_each
static void verify_for_each(void *user_data, uint32_t stream_id, void *ptr)
Definition: stream_map_test.cc:73
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
test_no_op
static void test_no_op(void)
Definition: stream_map_test.cc:28
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_chttp2_stream_map_add
void grpc_chttp2_stream_map_add(grpc_chttp2_stream_map *map, uint32_t key, void *value)
Definition: stream_map.cc:59
test_empty_find
static void test_empty_find(void)
Definition: stream_map_test.cc:38
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_chttp2_stream_map_find
void * grpc_chttp2_stream_map_find(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:145
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_chttp2_stream_map_for_each
void grpc_chttp2_stream_map_for_each(grpc_chttp2_stream_map *map, void(*f)(void *user_data, uint32_t key, void *value), void *user_data)
Definition: stream_map.cc:166
check_delete_evens
static void check_delete_evens(grpc_chttp2_stream_map *map, uint32_t n)
Definition: stream_map_test.cc:80
grpc_chttp2_stream_map_delete
void * grpc_chttp2_stream_map_delete(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:129
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
stream_map.h
grpc_chttp2_stream_map_size
size_t grpc_chttp2_stream_map_size(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:150
grpc_chttp2_stream_map
Definition: stream_map.h:33
test_config.h
grpc_chttp2_stream_map_destroy
void grpc_chttp2_stream_map_destroy(grpc_chttp2_stream_map *map)
Definition: stream_map.cc:40
grpc_chttp2_stream_map_init
void grpc_chttp2_stream_map_init(grpc_chttp2_stream_map *map, size_t initial_capacity)
Definition: stream_map.cc:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
test_basic_add_find
static void test_basic_add_find(uint32_t n)
Definition: stream_map_test.cc:49
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
test_delete_evens_incremental
static void test_delete_evens_incremental(uint32_t n)
Definition: stream_map_test.cc:128
test_periodic_compaction
static void test_periodic_compaction(uint32_t n)
Definition: stream_map_test.cc:148
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:20