1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* IOQueueTest.cpp
* Test fixture for the IOQueue class.
* Copyright (C) 2012 Simon Newton
*/
#include <cppunit/extensions/HelperMacros.h>
#include <memory>
#include <iostream>
#include <string>
#include "ola/Logging.h"
#include "ola/io/IOQueue.h"
#include "ola/io/MemoryBlockPool.h"
#include "ola/testing/TestUtils.h"
using ola::io::IOQueue;
using ola::io::IOVec;
using ola::io::MemoryBlockPool;
using std::auto_ptr;
using std::string;
class IOQueueTest: public CppUnit::TestFixture {
public:
CPPUNIT_TEST_SUITE(IOQueueTest);
CPPUNIT_TEST(testBasicWrite);
CPPUNIT_TEST(testBlockOverflow);
CPPUNIT_TEST(testPop);
CPPUNIT_TEST(testPeek);
CPPUNIT_TEST(testIOVec);
CPPUNIT_TEST(testDump);
CPPUNIT_TEST(testStringRead);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown() {}
void testBasicWrite();
void testBlockOverflow();
void testPop();
void testPeek();
void testIOVec();
void testDump();
void testStringRead();
private:
auto_ptr<IOQueue> m_buffer;
unsigned int SumLengthOfIOVec(const struct IOVec *iov, int iocnt);
};
CPPUNIT_TEST_SUITE_REGISTRATION(IOQueueTest);
void IOQueueTest::setUp() {
m_buffer.reset(new IOQueue());
}
/**
* Sum up the length of data in a IOVec
*/
unsigned int IOQueueTest::SumLengthOfIOVec(const struct IOVec *iov,
int iocnt) {
unsigned int sum = 0;
for (int i = 0; i < iocnt; iov++, i++)
sum += iov->iov_len;
return sum;
}
/*
* Check that basic appending works.
*/
void IOQueueTest::testBasicWrite() {
OLA_ASSERT_EQ(0u, m_buffer->Size());
uint8_t data1[] = {0, 1, 2, 3, 4};
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(5u, m_buffer->Size());
m_buffer->Pop(1);
OLA_ASSERT_EQ(4u, m_buffer->Size());
m_buffer->Pop(4);
OLA_ASSERT_EQ(0u, m_buffer->Size());
}
/*
* Check that overflowing blocks works
*/
void IOQueueTest::testBlockOverflow() {
// block size of 4
MemoryBlockPool pool(4);
IOQueue queue(&pool);
uint8_t data1[] = {0, 1, 2, 3, 4};
uint8_t data2[] = {5, 6, 7, 8, 9};
uint8_t data3[] = {0xa, 0xb, 0xc, 0xd, 0xe};
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(5u, queue.Size());
queue.Write(data2, sizeof(data2));
OLA_ASSERT_EQ(10u, queue.Size());
queue.Write(data3, sizeof(data3));
OLA_ASSERT_EQ(15u, queue.Size());
queue.Pop(9);
OLA_ASSERT_EQ(6u, queue.Size());
// append some more data
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(11u, queue.Size());
queue.Write(data2, sizeof(data2));
OLA_ASSERT_EQ(16u, queue.Size());
}
/**
* Test that Pop behaves
*/
void IOQueueTest::testPop() {
uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, m_buffer->Size());
OLA_ASSERT_FALSE(m_buffer->Empty());
m_buffer->Pop(9);
OLA_ASSERT_EQ(0u, m_buffer->Size());
OLA_ASSERT_TRUE(m_buffer->Empty());
// try to pop off more data
m_buffer->Pop(1);
OLA_ASSERT_EQ(0u, m_buffer->Size());
OLA_ASSERT_TRUE(m_buffer->Empty());
// add the data back, then try to pop off more than we have
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, m_buffer->Size());
OLA_ASSERT_FALSE(m_buffer->Empty());
m_buffer->Pop(10);
OLA_ASSERT_EQ(0u, m_buffer->Size());
OLA_ASSERT_TRUE(m_buffer->Empty());
// one more time
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, m_buffer->Size());
// Now try a buffer with smaller blocks
MemoryBlockPool pool(4);
IOQueue queue(&pool);
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, queue.Size());
// pop the same amount as the first block size
queue.Pop(4);
OLA_ASSERT_EQ(5u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
// now pop more than the buffer size
queue.Pop(6);
OLA_ASSERT_EQ(0u, queue.Size());
OLA_ASSERT_TRUE(queue.Empty());
// test the block boundary
uint8_t *output_data = new uint8_t[4];
m_buffer.reset(new IOQueue(&pool));
queue.Write(data1, 4);
OLA_ASSERT_EQ(4u, queue.Size());
unsigned int output_size = queue.Peek(output_data, 4);
OLA_ASSERT_DATA_EQUALS(data1, 4, output_data, output_size);
queue.Pop(4);
OLA_ASSERT_TRUE(queue.Empty());
// now add some more data
queue.Write(data1 + 4, 4);
OLA_ASSERT_EQ(4u, queue.Size());
output_size = queue.Peek(output_data, 4);
OLA_ASSERT_DATA_EQUALS(data1 + 4, 4, output_data, output_size);
queue.Pop(4);
OLA_ASSERT_TRUE(queue.Empty());
delete[] output_data;
}
/**
* Test that Peek behaves
*/
void IOQueueTest::testPeek() {
uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, m_buffer->Size());
OLA_ASSERT_FALSE(m_buffer->Empty());
const unsigned int DATA_SIZE = 20;
uint8_t *output_data = new uint8_t[DATA_SIZE];
// peek at the first four bytes
unsigned int output_size = m_buffer->Peek(output_data, 4);
OLA_ASSERT_DATA_EQUALS(data1, 4, output_data, output_size);
OLA_ASSERT_EQ(9u, m_buffer->Size());
// peek at the first 9 bytes
output_size = m_buffer->Peek(output_data, 9);
OLA_ASSERT_DATA_EQUALS(data1, 9, output_data, output_size);
OLA_ASSERT_EQ(9u, m_buffer->Size());
// peek at more bytes that exist in the buffer
output_size = m_buffer->Peek(output_data, DATA_SIZE);
OLA_ASSERT_EQ(9u, output_size);
OLA_ASSERT_DATA_EQUALS(data1, sizeof(data1), output_data, output_size);
OLA_ASSERT_EQ(9u, m_buffer->Size());
// Now try a buffer with smaller blocks
MemoryBlockPool pool(4);
IOQueue queue(&pool);
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, queue.Size());
// peek at he same amount as the first block size
output_size = queue.Peek(output_data, 4);
OLA_ASSERT_DATA_EQUALS(data1, 4, output_data, output_size);
OLA_ASSERT_EQ(9u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
// peek at data from more than one block
output_size = queue.Peek(output_data, 6);
OLA_ASSERT_DATA_EQUALS(data1, 6, output_data, output_size);
OLA_ASSERT_EQ(9u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
// peek at data on the two block boundary
output_size = queue.Peek(output_data, 8);
OLA_ASSERT_DATA_EQUALS(data1, 8, output_data, output_size);
OLA_ASSERT_EQ(9u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
// peek at all the data
output_size = queue.Peek(output_data, 9);
OLA_ASSERT_DATA_EQUALS(data1, 9, output_data, output_size);
OLA_ASSERT_EQ(9u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
// peek at more data than what exists
output_size = queue.Peek(output_data, DATA_SIZE);
OLA_ASSERT_EQ(9u, output_size);
OLA_ASSERT_DATA_EQUALS(data1, 9, output_data, output_size);
OLA_ASSERT_EQ(9u, queue.Size());
OLA_ASSERT_FALSE(queue.Empty());
delete[] output_data;
}
/**
* Test getting / setting IOVec work.
*/
void IOQueueTest::testIOVec() {
uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
m_buffer->Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, m_buffer->Size());
OLA_ASSERT_FALSE(m_buffer->Empty());
int iocnt;
const struct IOVec *vector = m_buffer->AsIOVec(&iocnt);
OLA_ASSERT_EQ(9u, SumLengthOfIOVec(vector, iocnt));
OLA_ASSERT_EQ(1, iocnt);
m_buffer->FreeIOVec(vector);
// try a smaller block size
MemoryBlockPool pool(4);
IOQueue queue(&pool);
m_buffer.reset(new IOQueue(&pool));
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, queue.Size());
vector = queue.AsIOVec(&iocnt);
OLA_ASSERT_EQ(3, iocnt);
OLA_ASSERT_EQ(9u, SumLengthOfIOVec(vector, iocnt));
queue.FreeIOVec(vector);
}
/**
* Test dumping to a ostream works
*/
void IOQueueTest::testDump() {
MemoryBlockPool pool(4);
IOQueue queue(&pool);
uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, queue.Size());
std::ostringstream str;
queue.Dump(&str);
OLA_ASSERT_EQ(
string("00 01 02 03 04 05 06 07 ........\n"
"08 .\n"),
str.str());
}
/**
* Test reading to a string works.
*/
void IOQueueTest::testStringRead() {
MemoryBlockPool pool(4);
IOQueue queue(&pool);
uint8_t data1[] = {'a', 'b', 'c', 'd', '1', '2', '3', '4', ' '};
queue.Write(data1, sizeof(data1));
OLA_ASSERT_EQ(9u, queue.Size());
string output;
OLA_ASSERT_EQ(9u, queue.Read(&output, 9u));
OLA_ASSERT_EQ(string("abcd1234 "), output);
}
|