Change Section to debug for libmimetic0-dbg.
[debian/mimetic.git] / test / t.circular_buffer.h
1 #ifndef _T_CIRCULAR_BUFFER_H_
2 #define _T_CIRCULAR_BUFFER_H_
3 #include <string>
4 #include "cutee.h"
5 #include <mimetic/circular_buffer.h>
6
7
8 namespace mimetic
9 {
10
11 class TEST_CLASS( test_circular_buffer)
12 {
13     struct test_item
14     {
15         int buf_sz;
16         char* push_back_items;
17         char* result;
18     };
19     static const test_item tb[];
20     static const unsigned int test_count;
21     test_item ti;
22     const char* putstr;
23     const char* result;
24 public:
25     test_circular_buffer()
26     : ti(tb[0])
27     {
28     }
29     void TEST_FUNCTION( testOverflow )
30     {
31         circular_buffer<char> cb(3);
32         for(unsigned i = 0; i < 1000; i++)
33             cb.push_back('a');
34     }
35     void TEST_FUNCTION( testCount )
36     {
37         unsigned int maxsize = 64;
38         circular_buffer<char> cb(maxsize);
39         for(unsigned int i = 0; i < 1000; i++)
40         {
41             if(cb.count() < maxsize)
42             {
43                 TEST_ASSERT_EQUALS_P( i, cb.count());
44             } else {
45                 TEST_ASSERT_EQUALS_P( maxsize, cb.count());
46             }
47             cb.push_back('a');
48         }
49     }
50     void TEST_FUNCTION( testCmpCh )
51     {
52         for(unsigned int i = 0 ; i < test_count; ++i)
53         {
54             ti = tb[i];
55             putstr = ti.push_back_items;
56             result = ti.result;
57             circular_buffer<char> cb(ti.buf_sz);
58             char c;
59             while(0 != (c = *putstr++))
60                 cb.push_back(c);
61             while(0 != (c = *result++))
62             {
63                 TEST_ASSERT_EQUALS_P( cb.front(), c );
64                 cb.pop_front();
65             }
66         }
67     }
68
69     void TEST_FUNCTION( testInOut )
70     {
71         for(unsigned int i =0 ; i < test_count; ++i)
72         {
73             ti = tb[i];
74             putstr = ti.push_back_items;
75             circular_buffer<char> cb(ti.buf_sz);
76             char c;
77             while(0 != (c = *putstr++))
78             {
79                 cb.push_back(c);
80                 TEST_ASSERT_EQUALS_P( cb.front(), c );
81                 cb.pop_front();
82             }
83         }
84
85     }
86     void TEST_FUNCTION( testFill )
87     {
88         for(unsigned int i =0 ; i < test_count; ++i)
89         {
90             ti = tb[i];
91             putstr = ti.push_back_items;
92             circular_buffer<char> cb(ti.buf_sz);
93             char c;
94             const char * cmp = putstr;
95             while(0 != (c = *putstr++))
96             {
97                 cb.push_back(c);
98                 if(cb.count() < cb.max_size()) continue;
99                 c = *cmp++;
100                 TEST_ASSERT_EQUALS_P( cb.front(), c );
101                 cb.pop_front();
102             }
103         }
104     }
105     void TEST_FUNCTION( testIdx )
106     {
107         for(unsigned int i =0 ; i < test_count; i++)
108         {
109             ti = tb[i];
110             putstr = ti.push_back_items;
111             result = ti.result;
112             circular_buffer<char> cb(ti.buf_sz);
113             char c;
114             while(0 != (c = *putstr++))
115                 cb.push_back(c);
116             for(unsigned int t = 0 ; t < cb.count(); ++t)
117             {
118                 TEST_ASSERT_EQUALS_P( cb[t], result[t]);
119             }
120         }
121     }
122     void TEST_FUNCTION( testEq )
123     {
124         for(unsigned int i =0 ; i < test_count; i++)
125         {
126             ti = tb[i];
127             putstr = ti.push_back_items;
128             result = ti.result;
129             circular_buffer<char> cb(ti.buf_sz);
130             char c;
131             while(0 != (c = *putstr++))
132                 cb.push_back(c);
133             // test == and != operators
134             TEST_ASSERT( cb == result );
135             TEST_ASSERT( false == (cb != result) );
136         }
137
138     }
139     void TEST_FUNCTION( testCompare )
140     {
141         for(unsigned int i =0 ; i < test_count; i++)
142         {
143             ti = tb[i];
144             putstr = ti.push_back_items;
145             //result = ti.result;
146             std::string res = ti.result;
147             circular_buffer<char> cb(ti.buf_sz);
148             char c;
149             while(0 != (c = *putstr++))
150                 cb.push_back(c);
151             for(unsigned int c = 0; c < res.length(); c++)
152             {
153                 std::string subs = res.substr(c);
154                 TEST_ASSERT( cb.compare(c,subs.length(),subs) );
155             }
156             for(unsigned int c = 0; c < res.length(); c++)
157             {
158                 std::string subs = res.substr(c, res.length()-c);
159                 TEST_ASSERT( cb.compare(c,subs.length(),subs) );
160             }
161             for(unsigned int c = res.length(); c != 0;  c--)
162             {
163                 std::string subs = res.substr(0,c);
164                 TEST_ASSERT( cb.compare(0,subs.length(),subs) );
165             }
166             for(unsigned int c = res.length(); c != 0;  c--)
167             {
168                 TEST_ASSERT( cb.compare(0,c,res) );
169             }
170         }
171
172     }
173     void TEST_FUNCTION( testToStr )
174     {
175         for(unsigned int i =0 ; i < test_count; ++i)
176         {
177             ti = tb[i];
178             putstr = ti.push_back_items;
179             result = ti.result;
180             circular_buffer<char> cb(ti.buf_sz);
181             char c;
182             while(0 != (c = *putstr++))
183                 cb.push_back(c);
184             TEST_ASSERT_EQUALS_P( cb.str(), result );
185         }
186     }
187
188 };
189
190 }
191
192 #endif
193