MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test.cpp
1 /*
2  Copyright (C) 2003-2006, 2008 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 #include <ndb_global.h>
20 #include <NdbTick.h>
21 #include <TransporterDefinitions.hpp>
22 #include <SimpleProperties.hpp>
23 #include <LongSignal.hpp>
24 
25 #undef test
26 
27 struct Buffer {
28  Buffer(Uint32 size){ m_sz = size; buffer = new Uint32[m_sz]; m_len = 0;}
29  ~Buffer(){ delete [] buffer;}
30 
31  Uint32 m_sz;
32  Uint32 m_len;
33  Uint32 * buffer;
34 };
35 
36 inline
37 void
38 require(bool b){
39  if(!b)
40  abort();
41 }
42 
43 /* Calculate number of segments to release based on section size
44  * Always release one segment, even if size is zero
45  */
46 #define relSz(x) ((x == 0)? 1 : ((x + SectionSegment::DataLength - 1) / SectionSegment::DataLength))
47 
48 void
49 release(SectionSegmentPool & thePool, SegmentedSectionPtr & ptr){
50  const Uint32 sz = relSz(ptr.sz);
51  thePool.releaseList(sz,
52  ptr.i,
53  ptr.p->m_lastSegment);
54 }
55 
56 void
58 
59  {
60  bool fail = false;
61  db.reset();
62  for(Uint32 i = 0; i<buf.m_len; i++){
63  Uint32 tmp;
64  if(!db.getWord(&tmp)){
65  ndbout_c("getWord(...) failed i=%d size=%d", i, buf.m_len);
66  abort();
67  }
68 
69  if(tmp != buf.buffer[i]){
70  ndbout_c("getWord(...)=%d != buf[%d]=%d size=%d", tmp, i,
71  buf.buffer[i], buf.m_len);
72  fail = true;
73  }
74  }
75  require(!fail);
76  }
77 
78  {
79  db.reset();
80  Buffer buf2(buf.m_sz);
81  if(!db.getWords(buf2.buffer, buf.m_len))
82  abort();
83 
84  bool fail = false;
85  for(Uint32 i = 0; i<buf.m_len; i++){
86  if(buf.buffer[i] != buf2.buffer[i]){
87  ndbout_c("getWords(...) buf[%d] != buf2[%d] size=%d", i, i, buf.m_len);
88  fail = true;
89  }
90  }
91  require(!fail);
92  }
93 }
94 
95 
96 void
97 test(Uint32 sz, Uint32 loops, Uint32 iter){
98 
99  ndbout_c("SimplePropertiesSection sz=%d loops=%d iter=%d", sz, loops, iter);
100 
101  while(loops-- > 0){
102  Uint32 size = sz*((10 + (rand() % (10 * sz)) + sz - 1)/sz);
103 
104  Buffer buf(size);
105  SectionSegmentPool thePool; thePool.setSize(size);
106 
107  for(Uint32 i = 0; i<iter; i++){
108  Uint32 c = 0 + (rand() % (2));
109 
110  const Uint32 alloc = 1 + (rand() % (size - 1));
112 
113  if(0)
114  ndbout_c("size: %d loops: %d iter: %d c=%d alloc=%d",
115  size, loops, i, c, alloc);
116 
117  switch(c){
118  case 0:{
119  for(Uint32 i = 0; i<alloc; i++)
120  buf.buffer[i] = i; //rand();
121  buf.m_len = alloc;
122 
124  for(Uint32 i = 0; i<alloc; i++){
125  w.putWord(buf.buffer[i]);
126  }
127  w.getPtr(dst);
128  break;
129  }
130  case 1:{
131  for(Uint32 i = 0; i<alloc; i++)
132  buf.buffer[i] = i; //rand();
133  buf.m_len = alloc;
134 
136  Uint32 i = 0;
137  while(i < alloc){
138  Uint32 sz = rand() % (alloc - i + 1);
139  w.putWords(&buf.buffer[i], sz);
140  i += sz;
141  }
142  w.getPtr(dst);
143  break;
144  }
145  case 2:{
146  break;
147  }
148  }
149  SimplePropertiesSectionReader r(dst, thePool);
150  compare(r, buf);
151  release(thePool, dst);
152  require(thePool.getSize() == thePool.getNoOfFree());
153  }
154  }
155 }
156 
157 int
158 main(void){
159 
160  srand(NdbTick_CurrentMillisecond());
161 
162  //test( 1, 1000, 1000);
163  test(54, 1000, 1000);
164  test(59, 1000, 1000);
165  test(60, 1000, 1000);
166  test(61, 1000, 1000);
167  return 0;
168 }
169 
170 void
171 ErrorReporter::handleAssert(const char * msg, const char * file, int line)
172 {
173  ndbout << "ErrorReporter::handleAssert activated - "
174  << " line= " << line << endl;
175  abort();
176 }