MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
arrayListTest.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 
21 #include <ndb_global.hpp>
22 
23 #include <ArrayList.hpp>
24 #include <NdbOut.hpp>
25 #include <NdbTick.h>
26 
28  Uint32 next;
29  Uint32 prev;
30  char somedata[12];
31 
32  void print (NdbOut & out) {
33  out << "ALO: next = " << next
34  << " prev = " << prev << endl;
35  }
36 };
37 
38 extern const int x_AL_Next = offsetof(A_Listable_Object, next);
39 extern const int x_AL_Prev = offsetof(A_Listable_Object, prev);
40 
41 NdbOut &
42 operator<<(NdbOut & o, A_Listable_Object & a){
43  a.print(o);
44  return o;
45 }
46 
48 
49 #define APool ArrayPool<A_Listable_Object>
50 #define AList ArrayList<A_Listable_Object>
51 
52 APool aGPool;
53 AList aGList(aGPool);
54 
56 public:
57  static void tryList0(int listSize){
58  APool aPool;
59  AList aList(aPool);
60 
61  if(!aPool.setSize(listSize)){
62  ndbout << "Failed to do aPool.setSize(" << listSize << ")" << endl;
63  return;
64  }
65 
66  int * anArray = new int[listSize];
67 
68  for(int i = 1; i<listSize; i++){
69  int arrayElements = 0;
70 
71 
72  for(int j = 0; j<i; j++){
73  A_Listable_ObjectPtr p;
74  const int ret = aList.seize(p);
75  if(ret == RNIL){
76  ndbout << "Failed to seize!!" << endl;
77  ndbout << "Have seized " << j
78  << " out of " << listSize << endl;
79  ndbout << "Terminating..." << endl;
80  abort();
81  }
82  anArray[arrayElements] = ret;
83  arrayElements++;
84  }
85  assert(aList.noOfElements() == i);
86  assert(aPool.noOfFree() == (listSize - i));
87  assert(arrayElements == i);
88 
89  for(int j = 0; j<i; j++){
90  aList.release(anArray[j]);
91  }
92 
93  assert(aList.noOfElements() == 0);
94  assert(aPool.noOfFree() == listSize);
95  }
96  }
97 
98  static void tryList1(int listSize, int iterations){
99  APool aPool;
100  AList aList(aPool);
101 
102  if(!aPool.setSize(listSize)){
103  ndbout << "Failed to do aPool.setSize(" << listSize << ")" << endl;
104  return;
105  }
106 
107  ndbout << "Seizing/Releaseing " << iterations
108  << " times over list with " << listSize << " elements" << endl;
109 
110  int * anArray = new int[listSize];
111  int arrayElements = 0;
112 
113  int noOfSeize = 0;
114  int noFailSeize = 0;
115  int noOfRelease = 0;
116 
117  for(int i = 0; i<iterations; i++){
118  assert(arrayElements <= listSize);
119  const int r = rand() % (10 * listSize);
120  if(r < (arrayElements - 1)){
124  noOfRelease++;
125  aList.release(anArray[r]);
126  arrayElements--;
127  for(int j = r; j<arrayElements; j++)
128  anArray[j] = anArray[j+1];
129 
130  } else {
134  A_Listable_ObjectPtr p;
135  const int ret = aList.seize(p);
136  if(ret == RNIL && arrayElements != listSize){
137  ndbout << "Failed to seize!!"
138  << " iteration=" << i << endl;
139  ndbout << "Have seized " << arrayElements
140  << " out of " << listSize << endl;
141  ndbout << "Terminating..." << endl;
142  abort();
143  }
144  if(arrayElements >= listSize && ret != RNIL){
145  ndbout << "Seize did not fail when it should have"
146  << " iteration=" << i << endl;
147  ndbout << "Have seized " << arrayElements
148  << " out of " << listSize << endl;
149  ndbout << "Terminating..." << endl;
150  abort();
151  }
152  if(ret != RNIL){
153  noOfSeize++;
154  anArray[arrayElements] = ret;
155  arrayElements++;
156  } else {
157  noFailSeize++;
158  }
159  }
160  }
161  delete []anArray;
162 
163  ndbout << "Seized: " << noOfSeize
164  << " Seized with buffer full: " << noFailSeize
165  << " Release: " << noOfRelease << " --- ";
166  ndbout << "(" << noOfSeize << " + " << noFailSeize << " + " << noOfRelease
167  << " = " << (noOfSeize + noFailSeize + noOfRelease) << ")" << endl;
168  }
169 
170  static void tryList2(int size, int iter, int fail){
171  APool aPool;
172  AList aList(aPool);
173 
174  if(!aPool.setSize(size)){
175  ndbout << "Failed to do aPool.setSize(" << size << ")" << endl;
176  return;
177  }
178 
179  ndbout << "doing getPtr(i) where i > size(" << size << ") "
180  << fail << " times mixed with " << iter
181  << " ordinary seize/release" << endl;
182 
183  int * anArray = new int[size];
184  int arrayElements = 0;
185 
186  int noOfSeize = 0;
187  int noFailSeize = 0;
188  int noOfRelease = 0;
189 
190  for(int i = 0; i<iter; i++){
191  assert(arrayElements <= size);
192  const int r = rand() % (10 * size);
193 
194  if((i + 1)%(iter/fail) == 0){
195  aList.getPtr(size + r);
196  continue;
197  }
198 
199  if(r < (arrayElements - 1)){
203  noOfRelease++;
204  aList.release(anArray[r]);
205  arrayElements--;
206  for(int j = r; j<arrayElements; j++)
207  anArray[j] = anArray[j+1];
208 
209  } else {
213  A_Listable_ObjectPtr p;
214  const int ret = aList.seize(p);
215  if(ret == RNIL && arrayElements != size){
216  ndbout << "Failed to seize!!"
217  << " iteration=" << i << endl;
218  ndbout << "Have seized " << arrayElements
219  << " out of " << size << endl;
220  ndbout << "Terminating..." << endl;
221  abort();
222  }
223  if(arrayElements >= size && ret != RNIL){
224  ndbout << "Seize did not fail when it should have"
225  << " iteration=" << i << endl;
226  ndbout << "Have seized " << arrayElements
227  << " out of " << size << endl;
228  ndbout << "Terminating..." << endl;
229  abort();
230  }
231  if(ret != RNIL){
232  noOfSeize++;
233  anArray[arrayElements] = ret;
234  arrayElements++;
235  } else {
236  noFailSeize++;
237  }
238  }
239  }
240  delete []anArray;
241  }
242 
243  static void
244  tryList3(int size, int fail){
245  ndbout << "Failing " << fail << " times " << endl;
246 
247  for(int i = 0; i<fail; i++){
248  APool aPool;
249  AList aList(aPool);
250 
251  if(!aPool.setSize(size)){
252  ndbout << "Failed to do aPool.setSize(" << size << ")" << endl;
253  return;
254  }
255 
256  const int noOfElementsInBufferWhenFail = (i + 1) * (size /(fail + 1));
257 
258  int * anArray = new int[size];
259  for(int i = 0; i<size; i++)
260  anArray[i] = i;
261  int arrayElements = 0;
262 
263  int noOfSeize = 0;
264  int noFailSeize = 0;
265  int noOfRelease = 0;
266 
267  while(true){
268  assert(arrayElements <= size);
269  if(arrayElements == noOfElementsInBufferWhenFail){
270  ndbout << "++ You should get a ErrorReporter::handle... " << endl;
271  aList.release(anArray[arrayElements]);
272  ndbout << "++ Inbetween these lines" << endl << endl;
273  break;
274  }
275  const int r = rand() % (10 * size);
276  if(r < (arrayElements - 1)){
280  noOfRelease++;
281  aList.release(anArray[r]);
282  arrayElements--;
283  for(int j = r; j<arrayElements; j++)
284  anArray[j] = anArray[j+1];
285 
286  } else {
290  A_Listable_ObjectPtr p;
291  const int ret = aList.seize(p);
292  if(ret == RNIL && arrayElements != size){
293  ndbout << "Failed to seize!!" << endl;
294  ndbout << "Have seized " << arrayElements
295  << " out of " << size << endl;
296  ndbout << "Terminating..." << endl;
297  abort();
298  }
299  if(arrayElements >= size && ret != RNIL){
300  ndbout << "Seize did not fail when it should have" << endl;
301  ndbout << "Have seized " << arrayElements
302  << " out of " << size << endl;
303  ndbout << "Terminating..." << endl;
304  abort();
305  }
306  if(ret != RNIL){
307  noOfSeize++;
308  anArray[arrayElements] = ret;
309  arrayElements++;
310  } else {
311  noFailSeize++;
312  }
313  }
314  }
315  delete []anArray;
316  }
317 
318  }
319 };