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
|
#ifndef _T_CIRCULAR_BUFFER_H_
#define _T_CIRCULAR_BUFFER_H_
#include <string>
#include "cutee.h"
#include <mimetic/circular_buffer.h>
namespace mimetic
{
class TEST_CLASS( test_circular_buffer)
{
struct test_item
{
int buf_sz;
const char* push_back_items;
const char* result;
};
static const test_item tb[];
static const unsigned int test_count;
test_item ti;
const char* putstr;
const char* result;
public:
test_circular_buffer()
: ti(tb[0])
{
}
void TEST_FUNCTION( testOverflow )
{
circular_buffer<char> cb(3);
for(unsigned i = 0; i < 1000; i++)
cb.push_back('a');
}
void TEST_FUNCTION( testCount )
{
unsigned int maxsize = 64;
circular_buffer<char> cb(maxsize);
for(unsigned int i = 0; i < 1000; i++)
{
if(cb.count() < maxsize)
{
TEST_ASSERT_EQUALS_P( i, cb.count());
} else {
TEST_ASSERT_EQUALS_P( maxsize, cb.count());
}
cb.push_back('a');
}
}
void TEST_FUNCTION( testCmpCh )
{
for(unsigned int i = 0 ; i < test_count; ++i)
{
ti = tb[i];
putstr = ti.push_back_items;
result = ti.result;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
cb.push_back(c);
while(0 != (c = *result++))
{
TEST_ASSERT_EQUALS_P( cb.front(), c );
cb.pop_front();
}
}
}
void TEST_FUNCTION( testInOut )
{
for(unsigned int i =0 ; i < test_count; ++i)
{
ti = tb[i];
putstr = ti.push_back_items;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
{
cb.push_back(c);
TEST_ASSERT_EQUALS_P( cb.front(), c );
cb.pop_front();
}
}
}
void TEST_FUNCTION( testFill )
{
for(unsigned int i =0 ; i < test_count; ++i)
{
ti = tb[i];
putstr = ti.push_back_items;
circular_buffer<char> cb(ti.buf_sz);
char c;
const char * cmp = putstr;
while(0 != (c = *putstr++))
{
cb.push_back(c);
if(cb.count() < cb.max_size()) continue;
c = *cmp++;
TEST_ASSERT_EQUALS_P( cb.front(), c );
cb.pop_front();
}
}
}
void TEST_FUNCTION( testIdx )
{
for(unsigned int i =0 ; i < test_count; i++)
{
ti = tb[i];
putstr = ti.push_back_items;
result = ti.result;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
cb.push_back(c);
for(unsigned int t = 0 ; t < cb.count(); ++t)
{
TEST_ASSERT_EQUALS_P( cb[t], result[t]);
}
}
}
void TEST_FUNCTION( testEq )
{
for(unsigned int i =0 ; i < test_count; i++)
{
ti = tb[i];
putstr = ti.push_back_items;
result = ti.result;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
cb.push_back(c);
// test == and != operators
TEST_ASSERT( cb == result );
TEST_ASSERT( false == (cb != result) );
}
}
void TEST_FUNCTION( testCompare )
{
for(unsigned int i =0 ; i < test_count; i++)
{
ti = tb[i];
putstr = ti.push_back_items;
//result = ti.result;
std::string res = ti.result;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
cb.push_back(c);
for(unsigned int c = 0; c < res.length(); c++)
{
std::string subs = res.substr(c);
TEST_ASSERT( cb.compare(c,subs.length(),subs) );
}
for(unsigned int c = 0; c < res.length(); c++)
{
std::string subs = res.substr(c, res.length()-c);
TEST_ASSERT( cb.compare(c,subs.length(),subs) );
}
for(unsigned int c = res.length(); c != 0; c--)
{
std::string subs = res.substr(0,c);
TEST_ASSERT( cb.compare(0,subs.length(),subs) );
}
for(unsigned int c = res.length(); c != 0; c--)
{
TEST_ASSERT( cb.compare(0,c,res) );
}
}
}
void TEST_FUNCTION( testToStr )
{
for(unsigned int i =0 ; i < test_count; ++i)
{
ti = tb[i];
putstr = ti.push_back_items;
result = ti.result;
circular_buffer<char> cb(ti.buf_sz);
char c;
while(0 != (c = *putstr++))
cb.push_back(c);
TEST_ASSERT_EQUALS_P( cb.str(), result );
}
}
};
}
#endif
|