MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
testDataBuffer.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <ndb_global.h>
21 #include <NdbTick.h>
22 #include <DataBuffer.hpp>
23 
24 #undef test
25 
26 struct Buffer {
27  Buffer(Uint32 size){ m_sz = size; buffer = new Uint32[m_sz]; m_len = 0;}
28  ~Buffer(){ delete [] buffer;}
29 
30  Uint32 m_sz;
31  Uint32 m_len;
32  Uint32 * buffer;
33 };
34 
35 inline
36 void
37 require(bool b){
38  if(!b)
39  abort();
40 }
41 
42 template<Uint32 sz>
43 void
46  db.first(it);
47  for(Uint32 i = 0; i<buf.m_len; i++){
48  if(buf.buffer[i] != * it.data){
49  db.print(stdout);
50  abort();
51  }
52  db.next(it);
53  }
54 
55  for(Uint32 i = 0; i<buf.m_len; i++){
56  if(!db.position(it, i))
57  abort();
58  if(buf.buffer[i] != * it.data){
59  db.print(stdout);
60  abort();
61  }
62  }
63 }
64 
65 template<Uint32 sz>
66 void
67 test(Uint32 loops, Uint32 iter){
68 
69  ndbout_c("DataBuffer<%d> loops=%d iter=%d", sz, loops, iter);
70 
71  while(loops-- > 0){
72  Uint32 size = sz*((10 + (rand() % (10 * sz)) + sz - 1)/sz);
73 
74  typename DataBuffer<sz>::DataBufferPool thePool;
76  DataBuffer<sz> db(thePool);
77 
78  thePool.setSize((size + sz - 1) / sz);
79  Buffer buf(size);
80 
81  bool testOverRun = true;
82 
83  for(Uint32 i = 0; i<iter; i++){
84  Uint32 c = (rand() % (testOverRun ? 7 : 4));
85  Uint32 free = (size - db.getSize());
86  Uint32 alloc = 0;
87  if(free == 0){
88  c = (testOverRun ? c : 0);
89  if(c >= 1 && c <= 3)
90  c += 3;
91  }
92 
93  if(free <= 1)
94  alloc = 1;
95  else
96  alloc = 1 + (rand() % (free - 1));
97 
98  //ndbout_c("iter=%d case=%d free=%d alloc=%d", i, c, free, alloc);
99  switch(c){
100  case 0: // Release
101  db.first(it);
102  for(; !it.curr.isNull(); db.next(it))
103  * it.data = 0;
104 
105  db.release();
106  buf.m_len = 0;
107  break;
108  case 1:{ // Append (success)
109  for(Uint32 i = 0; i<alloc; i++)
110  buf.buffer[buf.m_len + i] = buf.m_len + i;//rand();
111 
112  require(db.append(&buf.buffer[buf.m_len], alloc));
113  buf.m_len += alloc;
114  break;
115  }
116  case 2: { // Seize(1) (success)
117  for(Uint32 i = 0; i<alloc; i++){
118  buf.buffer[buf.m_len + i] = buf.m_len + i;//rand();
119  require(db.seize(1));
120  require(db.position(it, db.getSize()-1));
121  * it.data = buf.buffer[buf.m_len + i];
122  }
123  buf.m_len += alloc;
124  break;
125  }
126  case 3: { // Seize(n) (success)
127  for(Uint32 i = 0; i<alloc; i++){
128  buf.buffer[buf.m_len + i] = buf.m_len + i;//rand();
129  }
130  Uint32 pos = db.getSize();
131  require(db.seize(alloc));
132  require(db.position(it, pos));
133  for(Uint32 i = 0; i<alloc; i++){
134  * it.data = buf.buffer[buf.m_len + i];
135  db.next(it);
136  }
137  buf.m_len += alloc;
138  break;
139  }
140  case 4: { // Append fail
141  require(!db.append(buf.buffer, alloc + free));
142  require(db.getSize() == 0);
143  buf.m_len = 0;
144  break;
145  }
146  case 5: { // Seize(1) - fail
147  for(Uint32 i = 0; i<free; i++){
148  require(db.seize(1));
149  }
150 
151  require(!db.seize(1));
152  require(db.getSize() == 0);
153  buf.m_len = 0;
154  break;
155  }
156  case 6: { // Seize(n) - fail
157  require(!db.seize(alloc + free));
158  require(db.getSize() == 0);
159  buf.m_len = 0;
160  break;
161  }
162  }
163  compare(db, buf);
164  }
165  }
166 }
167 
168 int
169 main(void){
170 
171  srand(NdbTick_CurrentMillisecond());
172 
173 
174  test<1>(1000, 1000);
175  test<11>(1000, 1000);
176  test<15>(1000, 1000);
177  test<16>(1000, 1000);
178  test<17>(1000, 1000);
179 #if 0
180 #endif
181  return 0;
182 }
183 
184 void
185 ErrorReporter::handleAssert(const char * msg, const char * file, int line)
186 {
187  ndbout << "ErrorReporter::handleAssert activated - "
188  << " line= " << line << endl;
189  abort();
190 }