Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
testfasthash.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #include "commonincludes.hpp"
18 #include "testfasthash.h"
19 #include "fasthash.h"
20 
21 
23 {
24  HRESULT hr = S_OK;
25 
26  ChkA(TestFastHash());
27  ChkA(TestRemove());
28 
29 Cleanup:
30  return hr;
31 }
32 
33 
34 
36 {
37  HRESULT hr = S_OK;
38  Item item;
39  item.key = val;
40  int ret;
41  Item* pValue = NULL;
42 
43  ret = _hashtable.Insert(val, item);
44  ChkIf(ret < 0, E_FAIL);
45 
46  ChkIf(_hashtable.Exists(val)==false, E_FAIL);
47 
48  pValue = _hashtable.Lookup(val);
49  ChkIf(pValue == NULL, E_FAIL);
50  ChkIf(pValue->key != val, E_FAIL);
51 
52 
53 
54 Cleanup:
55  return hr;
56 }
57 
59 {
60  HRESULT hr = S_OK;
61  int ret;
62 
63  ret = _hashtable.Remove(val);
64  ChkIf(ret < 0, E_FAIL);
65 
66  ChkIf(_hashtable.Exists(val), E_FAIL);
67 
68  ChkIf(_hashtable.Lookup(val) != NULL, E_FAIL);
69 
70 Cleanup:
71  return hr;
72 
73 }
74 
75 
77 {
78  HRESULT hr = S_OK;
79 
80  for (int x = first; x <= last; x++)
81  {
82  Chk(AddOne(x));
83  }
84 
85 Cleanup:
86  return hr;
87 }
88 
90 {
91  HRESULT hr = S_OK;
92 
93  for (int x = first; x <= last; x++)
94  {
95  Chk(RemoveOne(x));
96  }
97 
98 Cleanup:
99  return hr;
100 }
101 
103 {
104  HRESULT hr = S_OK;
105 
106  for (int x = first; x <= last; x++)
107  {
108  Item* pValue = NULL;
109 
110  ChkIf(_hashtable.Exists(x)==false, E_FAIL);
111 
112  pValue = _hashtable.Lookup(x);
113  ChkIf(pValue == NULL, E_FAIL);
114  ChkIf(pValue->key != x, E_FAIL);
115  }
116 
117 Cleanup:
118  return hr;
119 }
120 
122 {
123  HRESULT hr = S_OK;
124 
125  for (int x = first; x <= last; x++)
126  {
127  ChkIf(_hashtable.Lookup(x) != NULL, E_FAIL);
128  ChkIf(_hashtable.Exists(x), E_FAIL);
129  }
130 
131 Cleanup:
132  return hr;
133 }
134 
136 {
137  HRESULT hr = S_OK;
138  const int length = last - first + 1;
139  bool* arr = new bool[length];
140  size_t size = _hashtable.Size();
141 
142  memset(arr, '\0', length);
143 
144  for (int x = 0; x < (int)size; x++)
145  {
146  Item* pItem = _hashtable.LookupValueByIndex(x);
147 
148  if (pItem == NULL)
149  {
150  continue;
151  }
152 
153  int val = pItem->key;
154 
155  if ((val >= first) && (val <= last))
156  {
157  int index = val - first;
158  ChkIfA(arr[index] != false, E_FAIL);
159  arr[index] = true;
160  }
161  }
162 
163  for (int i = 0; i < length; i++)
164  {
165  ChkIfA(arr[i] == false, E_FAIL);
166  }
167 
168 
169 Cleanup:
170  delete [] arr;
171  return hr;
172 }
173 
174 
175 
177 {
178  HRESULT hr = S_OK;
179  HRESULT hrRet = S_OK;
180 
181  _hashtable.Reset();
182 
184 
185  // now make sure that we can't insert one past the limit
186  {
187  hrRet = AddOne(c_maxsize+1);
188  ChkIfA(SUCCEEDED(hrRet), E_FAIL);
189  }
190 
191  // check that the size is what's expected
192  ChkIfA(_hashtable.Size() != c_maxsize, E_FAIL);
193 
194  // validate that all the items are in the table
196 
197  // validate items not inserted don't get returned
199 
201 
202  // test a basic remove
204 
205  // revalidate that the index is ok
208 
209  // now add another item
210  ChkA(AddOne(c_maxsize+1));
211 
212  // check that the size is what's expected
213  ChkIfA(_hashtable.Size() != c_maxsize, E_FAIL);
214 
215 
216 
217 
218 Cleanup:
219  return hr;
220 
221 }
222 
224 {
225  HRESULT hr = S_OK;
226  int tracking[c_maxsize] = {};
227  size_t expected;
229 
230  for (size_t x = 0; x < c_maxsize; x++)
231  {
232  tracking[x] = (int)x;
233  }
234 
235  // shuffle our array - this is the order in which we'll do removes
236  srand(99);
237  for (size_t x = 0; x < c_maxsize; x++)
238  {
239  int firstindex = rand() % c_maxsize;
240  int secondindex = rand() % c_maxsize;
241 
242  int val1 = tracking[firstindex];
243  int val2 = tracking[secondindex];
244  int tmp;
245 
246  tmp = val1;
247  val1 = val2;
248  val2 = tmp;
249 
250  tracking[firstindex] = val1;
251  tracking[secondindex] = val2;
252  }
253 
254 
255  _hashtable.Reset();
256  ChkIfA(_hashtable.Size() != 0, E_FAIL);
257 
258  ChkA(AddRangeToSet(0, c_maxsize-1));
259 
260  // now start removing items randomly
261  for (size_t x = 0; x < (c_maxsize/2); x++)
262  {
263  ChkA(RemoveOne(tracking[x]));
264  }
265 
266  // now validate that the first half of the list is gone and that the other half of the list is present
267  for (size_t x = 0; x < (c_maxsize/2); x++)
268  {
269  ChkA(ValidateRangeNotInSet(tracking[x], tracking[x]));
270  }
271 
272  for (size_t x = (c_maxsize/2); x < c_maxsize; x++)
273  {
274  ChkA(ValidateRangeInSet(tracking[x], tracking[x]));
275  }
276 
277 
278 
279  expected = c_maxsize - c_maxsize/2;
280  ChkIfA(_hashtable.Size() != expected, E_FAIL);
281 
282  // now add them all back
283  for (size_t x = 0; x < (c_maxsize/2); x++)
284  {
285  ChkA(AddOne(tracking[x]));
286  }
287 
288  ChkIfA(_hashtable.Size() != c_maxsize, E_FAIL);
289  ChkA(ValidateRangeInSet(0, c_maxsize-1));
290 
291  ChkA(ValidateRangeInIndex(0, c_maxsize-1));
292 
293  pItem = _hashtable.LookupByIndex(0);
294  ChkA(RemoveOne(pItem->key));
295 
296  for (size_t x = 0; x < c_maxsize; x++)
297  {
298  if (x == (size_t)(pItem->key))
299  continue;
300 
302  }
303 
304 
305 Cleanup:
306  return hr;
307 }
308 
309 
310 
#define S_OK
Definition: hresult.h:46
HRESULT ValidateRangeInIndex(int first, int last)
#define Chk(expr)
Definition: chkmacros.h:53
HRESULT AddRangeToSet(int first, int last)
FastHash< int, Item, c_maxsize, c_tablesize > _hashtable
Definition: testfasthash.h:38
#define ChkIf(expr, hrerror)
Definition: chkmacros.h:63
#define SUCCEEDED(hr)
Definition: hresult.h:28
HRESULT RemoveOne(int val)
HRESULT RemoveRangeFromSet(int first, int last)
static const size_t c_maxsize
Definition: testfasthash.h:35
int32_t HRESULT
Definition: hresult.h:22
HRESULT TestFastHash()
virtual HRESULT Run()
HRESULT TestRemove()
#define E_FAIL
Definition: hresult.h:56
#define ChkA(expr)
Definition: chkmacros.h:73
HRESULT ValidateRangeInSet(int first, int last)
HRESULT ValidateRangeNotInSet(int first, int last)
HRESULT AddOne(int val)
#define ChkIfA(expr, hrerror)
Definition: chkmacros.h:84