/[resiprocate]/main/rutil/test/testFifo.cxx
ViewVC logotype

Contents of /main/rutil/test/testFifo.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3374 - (show annotations) (download)
Wed Oct 6 11:18:51 2004 UTC (15 years, 2 months ago) by dlb
Original Path: main/sip/resiprocate/test/testFifo.cxx
File size: 7547 byte(s)
clear out fifos -- for leak checking
1 #include <iostream>
2 #include "resiprocate/os/Log.hxx"
3 #include "resiprocate/os/Fifo.hxx"
4 #include "resiprocate/os/FiniteFifo.hxx"
5 #include "resiprocate/os/TimeLimitFifo.hxx"
6 #include "resiprocate/os/Data.hxx"
7 #include <unistd.h>
8
9 using namespace resip;
10 using namespace std;
11
12 class Foo
13 {
14 public:
15 Foo(const Data& val)
16 : mVal(val)
17 {}
18
19 Data mVal;
20 };
21
22 bool
23 isNear(int value, int reference, int epsilon=250)
24 {
25 int diff = ::abs(value-reference);
26 return (diff < epsilon);
27 }
28
29 int
30 main()
31 {
32 Log::initialize(Log::Cout, Log::Debug, Data::Empty);
33
34 Fifo<Foo> f;
35 FiniteFifo<Foo> ff(5);
36
37 {
38 bool c;
39 TimeLimitFifo<Foo> tlf(5, 10); // 5 seconds or 10 count limit
40
41 assert(tlf.empty());
42 assert(tlf.size() == 0);
43 assert(tlf.timeDepth() == 0);
44
45 c = tlf.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
46 assert(c);
47
48 assert(!tlf.empty());
49 assert(tlf.size() == 1);
50 cerr << tlf.timeDepth() << endl;
51 assert(tlf.timeDepth() == 0);
52
53 sleep(2);
54
55 assert(!tlf.empty());
56 assert(tlf.size() == 1);
57 assert(tlf.timeDepth() > 1);
58
59 delete tlf.getNext();
60
61 assert(tlf.empty());
62 assert(tlf.size() == 0);
63 assert(tlf.timeDepth() == 0);
64
65 c = tlf.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
66 assert(c);
67 sleep(3);
68 c = tlf.add(new Foo("second"), TimeLimitFifo<Foo>::EnforceTimeDepth);
69 assert(c);
70 sleep(3);
71 c = tlf.add(new Foo("nope"), TimeLimitFifo<Foo>::EnforceTimeDepth);
72 assert(!c);
73 c = tlf.add(new Foo("yep"), TimeLimitFifo<Foo>::IgnoreTimeDepth);
74 assert(c);
75 c = tlf.add(new Foo("internal"), TimeLimitFifo<Foo>::InternalElement);
76 assert(c);
77
78 Foo* fp = tlf.getNext();
79 assert(fp->mVal == "first");
80 delete fp;
81 c = tlf.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);
82 assert(c);
83
84 while (!tlf.empty())
85 {
86 delete tlf.getNext();
87 }
88 }
89
90 {
91 TimeLimitFifo<Foo> tlfNS(5, 0); // 5 seconds, no count limit
92 bool c;
93
94 assert(tlfNS.empty());
95 assert(tlfNS.size() == 0);
96 assert(tlfNS.timeDepth() == 0);
97
98 c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
99 assert(c);
100 sleep(3);
101 c = tlfNS.add(new Foo("second"), TimeLimitFifo<Foo>::EnforceTimeDepth);
102 assert(c);
103 sleep(3);
104 c = tlfNS.add(new Foo("nope"), TimeLimitFifo<Foo>::EnforceTimeDepth);
105 assert(!c);
106 Foo* fp = tlfNS.getNext();
107 assert(fp->mVal == "first");
108 delete fp;
109 c = tlfNS.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);
110 assert(c);
111
112 while (!tlfNS.empty())
113 {
114 delete tlfNS.getNext();
115 }
116 }
117
118 {
119 TimeLimitFifo<Foo> tlfNS(5, 0); // 5 seconds, no count limit
120 bool c;
121
122 assert(tlfNS.empty());
123 assert(tlfNS.size() == 0);
124 assert(tlfNS.timeDepth() == 0);
125
126 for (int i = 0; i < 100; ++i)
127 {
128 c = tlfNS.add(new Foo(Data("element") + Data(i)), TimeLimitFifo<Foo>::EnforceTimeDepth);
129 assert(c);
130 }
131
132 sleep(6);
133 c = tlfNS.add(new Foo("nope"), TimeLimitFifo<Foo>::EnforceTimeDepth);
134 assert(!c);
135
136 c = tlfNS.add(new Foo("yep"), TimeLimitFifo<Foo>::IgnoreTimeDepth);
137 assert(c);
138
139 assert(tlfNS.size() == 101);
140
141 while (!tlfNS.empty())
142 {
143 delete tlfNS.getNext();
144 }
145
146 c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
147 assert(c);
148
149 while (!tlfNS.empty())
150 {
151 delete tlfNS.getNext();
152 }
153 }
154
155 {
156 TimeLimitFifo<Foo> tlfNS(5, 10); // 5 seconds, limit 10 (2 reserved)
157 bool c;
158
159 assert(tlfNS.empty());
160 assert(tlfNS.size() == 0);
161 assert(tlfNS.timeDepth() == 0);
162
163 for (int i = 0; i < 8; ++i)
164 {
165 c = tlfNS.add(new Foo(Data("element") + Data(i)), TimeLimitFifo<Foo>::EnforceTimeDepth);
166 assert(c);
167 }
168
169 c = tlfNS.add(new Foo("nope"), TimeLimitFifo<Foo>::IgnoreTimeDepth);
170 assert(!c);
171
172 assert(tlfNS.size() == 8);
173
174 c = tlfNS.add(new Foo("yep"), TimeLimitFifo<Foo>::InternalElement);
175 assert(c);
176
177 c = tlfNS.add(new Foo("yepAgain"), TimeLimitFifo<Foo>::InternalElement);
178 assert(c);
179
180 c = tlfNS.add(new Foo("hard nope!"), TimeLimitFifo<Foo>::InternalElement);
181 assert(!c);
182
183 while (!tlfNS.empty())
184 {
185 delete tlfNS.getNext();
186 }
187
188 c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
189 assert(c);
190
191 while (!tlfNS.empty())
192 {
193 delete tlfNS.getNext();
194 }
195 }
196
197 {
198 TimeLimitFifo<Foo> tlfNS(0, 0); // unlimited
199
200 bool c;
201
202 assert(tlfNS.empty());
203 assert(tlfNS.size() == 0);
204 assert(tlfNS.timeDepth() == 0);
205
206 for (int i = 0; i < 100; ++i)
207 {
208 c = tlfNS.add(new Foo(Data("element") + Data(i)), TimeLimitFifo<Foo>::EnforceTimeDepth);
209 assert(c);
210 sleep(1);
211 }
212
213 while (!tlfNS.empty())
214 {
215 delete tlfNS.getNext();
216 }
217 }
218
219 cerr << "All OK" << endl;
220 return 0;
221 }
222
223 /* ====================================================================
224 * The Vovida Software License, Version 1.0
225 *
226 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
227 *
228 * Redistribution and use in source and binary forms, with or without
229 * modification, are permitted provided that the following conditions
230 * are met:
231 *
232 * 1. Redistributions of source code must retain the above copyright
233 * notice, this list of conditions and the following disclaimer.
234 *
235 * 2. Redistributions in binary form must reproduce the above copyright
236 * notice, this list of conditions and the following disclaimer in
237 * the documentation and/or other materials provided with the
238 * distribution.
239 *
240 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
241 * and "Vovida Open Communication Application Library (VOCAL)" must
242 * not be used to endorse or promote products derived from this
243 * software without prior written permission. For written
244 * permission, please contact vocal@vovida.org.
245 *
246 * 4. Products derived from this software may not be called "VOCAL", nor
247 * may "VOCAL" appear in their name, without prior written
248 * permission of Vovida Networks, Inc.
249 *
250 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
251 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
252 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
253 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
254 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
255 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
256 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
257 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
258 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
259 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
260 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
261 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
262 * DAMAGE.
263 *
264 * ====================================================================
265 *
266 * This software consists of voluntary contributions made by Vovida
267 * Networks, Inc. and many individuals on behalf of Vovida Networks,
268 * Inc. For more information on Vovida Networks, Inc., please see
269 * <http://www.vovida.org/>.
270 *
271 */

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27