/[resiprocate]/main/resip/stack/test/testHash.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testHash.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10302 - (show annotations) (download)
Sat Jul 20 16:17:01 2013 UTC (6 years, 3 months ago) by dpocock
File MIME type: text/plain
File size: 7835 byte(s)
resip/stack: Android cross-compile: fix compile issues / fcntl.h
1 #include <iostream>
2 #ifndef WIN32
3 #include <unistd.h>
4 #endif
5
6 #include <limits.h>
7 #include <signal.h>
8 #if defined(__ANDROID__)
9 #include <fcntl.h>
10 #else
11 #include <sys/fcntl.h>
12 #endif
13 #include "rutil/Logger.hxx"
14 #include "resip/stack/HeaderTypes.hxx"
15 #include "resip/stack/ParameterTypeEnums.hxx"
16 #include "resip/stack/ParameterTypes.hxx"
17 #include "resip/stack/MethodTypes.hxx"
18
19 using namespace resip;
20 using namespace std;
21
22 #define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
23
24 volatile bool signalled = false;
25
26 void tick(int sig)
27 {
28 signalled = true;
29 }
30
31
32 static
33 struct {
34 char * keyword;
35 Headers::Type type;
36 int len;
37 } headerInfo[(int)(Headers::MAX_HEADERS)];
38
39
40 unsigned int InitHeaderInfo()
41 {
42 int i = static_cast<int>(Headers::UNKNOWN)+1;
43 int max = static_cast<int>(Headers::MAX_HEADERS);
44
45 for( ;
46 i < max;
47 ++i)
48 {
49 Headers::Type t = static_cast<Headers::Type>(i);
50
51 char* p = strdup(resip::Headers::getHeaderName(i).c_str());
52
53 headerInfo[i].len = Headers::getHeaderName(i).size();
54 headerInfo[i].keyword = p;
55 headerInfo[i].type = t;
56
57 DebugLog(<< headerInfo[i].keyword << " ["
58 << headerInfo[i].type <<"]");
59 }
60
61 return i;
62
63 }
64
65 bool checkMethods()
66 {
67 int i = static_cast<int>(resip::UNKNOWN)+1;
68 int max = static_cast<int>(resip::MAX_METHODS);
69 bool failure = false;
70 for( ; i < max ; ++i)
71 {
72 MethodTypes t = static_cast<MethodTypes>(i);
73 const Data& d(resip::getMethodName(t));
74 MethodTypes nt = resip::getMethodType(d.data(),d.size());
75 bool ok = nt == t;
76 // DebugLog(<<resip::MethodNames[t]<<" : " << (ok ? "OK":"FAIL"));
77 DebugLog(<< (ok ? " " : "***" ) << ' '
78 << t << ' ' << resip::getMethodName(t)
79 << '(' << d << ')'
80 << " -> "
81 << nt << ' ' << resip::getMethodName(nt));
82 // if (!ok) ErrLog(<<resip::getMethodName(t) << " : HASH FAILURE");
83 failure |= !ok;
84 }
85 return !failure;
86 }
87
88 bool
89 checkParameters()
90 {
91 int i = static_cast<int>(ParameterTypes::UNKNOWN)+1;
92 int max = static_cast<int>(ParameterTypes::MAX_PARAMETER);
93 bool failure = false;
94
95 for( ; i < max ; ++i)
96 {
97 ParameterTypes::Type t = static_cast<ParameterTypes::Type>(i);
98 Data& d = ParameterTypes::ParameterNames[t];
99 bool ok = ParameterTypes::getType(d.data(),d.size()) == t;
100 DebugLog(<<ParameterTypes::ParameterNames[t]<<" : " << (ok?"OK":"FAIL"));
101 failure |= !ok;
102 }
103 return !failure;
104 }
105
106 unsigned short randomUShort()
107 {
108 static int fd = 0;
109 static bool init = false;
110 if (!init)
111 {
112 fd = open("/dev/urandom",O_RDONLY);
113 if (fd < 0)
114 {
115 cerr << "randomShort(): unable to open /dev/urandom -- degraded mode"
116 << endl;
117 }
118 init = true;
119 }
120 if (init && fd >= 0)
121 {
122 unsigned short r;
123 int n = 0;
124 if ((n=read(fd,&r,sizeof(r))) == sizeof(r))
125 {
126 return r;
127 }
128 }
129 // degraded mode
130 return (unsigned short)((USHRT_MAX + 1.0) * rand() / (RAND_MAX+1.0));
131 }
132
133
134 int
135 main()
136 {
137 bool failure = false;
138
139 register unsigned int i = 0;
140
141 // Load up the main table
142 unsigned short nKeywords = InitHeaderInfo();
143
144
145 // Verify that the hash function works.
146 InfoLog(<<"Checking that hash function works for all known headers");
147 for(i=0;i<nKeywords;i++)
148 {
149 Headers::Type t = Headers::getType(headerInfo[i].keyword,
150 headerInfo[i].len);
151 bool ok = headerInfo[i].type == t;
152
153 InfoLog(<< headerInfo[i].keyword << " " << (ok?"OK":"FAIL"));
154 if (!ok)
155 {
156 ErrLog(<<headerInfo[i].keyword << "["
157 <<headerInfo[i].type << "] hashed to "
158 << t << " ["
159 << Headers::getHeaderName(i) << "]");
160 }
161
162 }
163
164 InfoLog(<<"Checking methods.");
165
166 bool p = checkMethods();
167
168 InfoLog(<<" methods: " << (p?"OK":"FAIL"));
169
170 p = checkParameters();
171
172 InfoLog(<<" parameters: " << (p?"OK":"FAIL"));
173
174
175 #if defined (TIME_HASH_TEST)
176 int randomList[100*1024];
177
178 // Make a large random list so we don't take a hit with
179 // random() calcs during the hash.
180 unsigned int nRandom = sizeof(randomList)/sizeof(*randomList);
181
182 InfoLog(<< "Pre-loading random list of " << nRandom << " entries");
183
184 for(i = 0; i < nRandom ; i++)
185 {
186 short r = randomUShort()%nKeywords;
187 randomList[i] = r;
188 }
189
190 i=0;
191 InfoLog(<<"Starting timing loop");
192
193 register int totalTime=30;
194 register int interval=5;
195 register int timer=totalTime;
196 register int elapsed=0;
197
198 signal(SIGALRM, tick);
199 register unsigned long long counter = 0;
200
201 assert(totalTime/interval*interval == totalTime);
202
203 while (timer>0)
204 {
205 alarm(interval);
206 while (!signalled)
207 {
208
209 volatile register Headers::Type hdr =
210 Headers::getType(headerInfo[randomList[i]].keyword,
211 headerInfo[randomList[i]].len);
212
213 counter++;
214 i++;
215 if (i >= nRandom) i = 0;
216 }
217 timer-=interval;
218 elapsed+=interval;
219 InfoLog(<< timer <<" sec rem: "
220 << counter << " hashes "
221 << counter/elapsed << " h/sec");
222 signalled=false;
223
224 }
225 #endif
226 if (failure) CritLog(<<"Problems in hashes. See above");
227 return failure?1:0;
228
229 }
230
231
232 /* ====================================================================
233 * The Vovida Software License, Version 1.0
234 *
235 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
236 *
237 * Redistribution and use in source and binary forms, with or without
238 * modification, are permitted provided that the following conditions
239 * are met:
240 *
241 * 1. Redistributions of source code must retain the above copyright
242 * notice, this list of conditions and the following disclaimer.
243 *
244 * 2. Redistributions in binary form must reproduce the above copyright
245 * notice, this list of conditions and the following disclaimer in
246 * the documentation and/or other materials provided with the
247 * distribution.
248 *
249 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
250 * and "Vovida Open Communication Application Library (VOCAL)" must
251 * not be used to endorse or promote products derived from this
252 * software without prior written permission. For written
253 * permission, please contact vocal@vovida.org.
254 *
255 * 4. Products derived from this software may not be called "VOCAL", nor
256 * may "VOCAL" appear in their name, without prior written
257 * permission of Vovida Networks, Inc.
258 *
259 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
260 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
261 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
262 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
263 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
264 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
265 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
266 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
267 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
268 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
270 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
271 * DAMAGE.
272 *
273 * ====================================================================
274 *
275 * This software consists of voluntary contributions made by Vovida
276 * Networks, Inc. and many individuals on behalf of Vovida Networks,
277 * Inc. For more information on Vovida Networks, Inc., please see
278 * <http://www.vovida.org/>.
279 *
280 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27