Debian Codegraph @

examples/AllTests/CircularBufferTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "MockPrinter.h"
30 #include "CircularBuffer.h"
31
32 TEST_GROUP(CircularBuffer)
33 { CircularBuffer* buffer;
34
35 void setup()
36 {
37 buffer = new CircularBuffer();
38 }
39 void teardown()
40 {
41 delete buffer;
42 }
43
44 void fillTheQueue(int seed, int howMany)
45 {
46 for (int i = 0; i < howMany; i++)
47 buffer->Put(seed + i);
48 }
49 void removeFromQueue(int howMany)
50 {
51 for (int i = 0; i < howMany; i++)
52 buffer->Get();
53 }
54 };
55
56 TEST(CircularBuffer, EmptyAfterCreation)
57 {
58 CHECK(buffer->IsEmpty());
59 }
60
61 TEST(CircularBuffer, NotEmpty)
62 {
63 buffer->Put(10046);
64 CHECK(!buffer->IsEmpty());
65 }
66
67 TEST(CircularBuffer, NotEmptyThenEmpty)
68 {
69 buffer->Put(4567);
70 CHECK(!buffer->IsEmpty());
71 buffer->Get();
72 CHECK(buffer->IsEmpty());
73 }
74
75 TEST(CircularBuffer, GetPutOneValue)
76 {
77 buffer->Put(4567);
78 LONGS_EQUAL(4567, buffer->Get());
79 }
80
81 TEST(CircularBuffer, GetPutAFew)
82 {
83 buffer->Put(1);
84 buffer->Put(2);
85 buffer->Put(3);
86 LONGS_EQUAL(1, buffer->Get());
87 LONGS_EQUAL(2, buffer->Get());
88 LONGS_EQUAL(3, buffer->Get());
89 }
90
91 TEST(CircularBuffer, Capacity)
92 {
93 CircularBuffer b(2);
94 LONGS_EQUAL(2, b.Capacity());
95 }
96
97 TEST(CircularBuffer, IsFull)
98 {
99 fillTheQueue(0, buffer->Capacity());
100 CHECK(buffer->IsFull());
101 }
102
103 TEST(CircularBuffer, EmptyToFullToEmpty)
104 {
105 fillTheQueue(100, buffer->Capacity());
106
107 CHECK(buffer->IsFull());
108
109 removeFromQueue(buffer->Capacity());
110
111 CHECK(buffer->IsEmpty());
112 }
113
114 TEST(CircularBuffer, WrapAround)
115 {
116 fillTheQueue(100, buffer->Capacity());
117
118 CHECK(buffer->IsFull());
119 LONGS_EQUAL(100, buffer->Get());
120 CHECK(!buffer->IsFull());
121 buffer->Put(1000);
122 CHECK(buffer->IsFull());
123
124 removeFromQueue(buffer->Capacity() - 1);
125
126 LONGS_EQUAL(1000, buffer->Get());
127 CHECK(buffer->IsEmpty());
128 }
129
130 TEST(CircularBuffer, PutToFull)
131 {
132 int capacity = buffer->Capacity();
133 fillTheQueue(900, capacity);
134 buffer->Put(9999);
135
136 for (int i = 0; i < buffer->Capacity() - 1; i++)
137 LONGS_EQUAL(i+900+1, buffer->Get());
138
139 LONGS_EQUAL(9999, buffer->Get());
140 CHECK(buffer->IsEmpty());
141 }
142
143 //Sometime people ask what tests the tests.
144 //Do you know the answer
145
146
147 TEST(CircularBuffer, GetFromEmpty)
148 {
149 LONGS_EQUAL(-1, buffer->Get());
150 CHECK(buffer->IsEmpty());
151 }
152
153 /*
154 * the next tests demonstrate using a mock object for
155 * capturing output
156 *
157 */
158
159 TEST(CircularBuffer, PrintEmpty)
160 {
161 MockPrinter mock;
162 Printer* p = &mock;
163
164 buffer->Print(p);
165 STRCMP_EQUAL("Circular buffer content:\n<>\n",
166 mock.getOutput().c_str());
167 }
168
169 TEST(CircularBuffer, PrintAfterOnePut)
170 {
171 MockPrinter mock;
172
173 buffer->Put(1);
174 buffer->Print(&mock);
175 STRCMP_EQUAL("Circular buffer content:\n<1>\n",
176 mock.getOutput().c_str());
177 }
178
179 TEST(CircularBuffer, PrintNotYetWrappedOrFull)
180 {
181 MockPrinter mock;
182
183 buffer->Put(1);
184 buffer->Put(2);
185 buffer->Put(3);
186 buffer->Print(&mock);
187 STRCMP_EQUAL("Circular buffer content:\n<1, 2, 3>\n",
188 mock.getOutput().c_str());
189 }
190
191 TEST(CircularBuffer, PrintNotYetWrappedAndIsFull)
192 {
193 MockPrinter mock;
194
195 fillTheQueue(200, buffer->Capacity());
196
197 buffer->Print(&mock);
198 const char* expected = "Circular buffer content:\n"
199 "<200, 201, 202, 203, 204>\n";
200
201 STRCMP_EQUAL(expected, mock.getOutput().c_str());
202 }
203
204 TEST(CircularBuffer, PrintWrappedAndIsFullOldestToNewest)
205 {
206 MockPrinter mock;
207
208 fillTheQueue(200, buffer->Capacity());
209 buffer->Get();
210 buffer->Put(999);
211
212 buffer->Print(&mock);
213 const char* expected = "Circular buffer content:\n"
214 "<201, 202, 203, 204, 999>\n";
215
216 STRCMP_EQUAL(expected, mock.getOutput().c_str());
217 }
218
219 TEST(CircularBuffer, PrintWrappedAndFullOverwriteOldest)
220 {
221 MockPrinter mock;
222
223 fillTheQueue(200, buffer->Capacity());
224 buffer->Put(9999);
225
226 buffer->Print(&mock);
227 const char* expected = "Circular buffer content:\n"
228 "<201, 202, 203, 204, 9999>\n";
229
230 STRCMP_EQUAL(expected, mock.getOutput().c_str());
231 }
232
233 TEST(CircularBuffer, PrintBoundary)
234 {
235 MockPrinter mock;
236
237 fillTheQueue(200, buffer->Capacity());
238 removeFromQueue(buffer->Capacity() - 2);
239 buffer->Put(888);
240 fillTheQueue(300, buffer->Capacity() - 1);
241
242 buffer->Print(&mock);
243 const char* expected = "Circular buffer content:\n"
244 "<888, 300, 301, 302, 303>\n";
245
246 STRCMP_EQUAL(expected, mock.getOutput().c_str());
247 }
248
249 TEST(CircularBuffer, FillEmptyThenPrint)
250 {
251 MockPrinter mock;
252
253 fillTheQueue(200, buffer->Capacity());
254 removeFromQueue(buffer->Capacity());
255 buffer->Print(&mock);
256 const char* expected = "Circular buffer content:\n"
257 "<>\n";
258
259 STRCMP_EQUAL(expected, mock.getOutput().c_str());
260 }