source: tags/0.5/test_pool.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 15.8 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <string>
4#include "gdbm_pool.h"
5#include "flatfile_pool.h"
6#include "files_pool.h"
7#include "mysql_pool.h"
8#include <vector>
9#include <stdlib.h>
10#include "wutils.h"
11#include "clocks.h"
12
13#include "stopos_key.h"
14int testkeysetget()
15{
16  typedef long long int longtype;
17  stopos_key k;
18  longtype n = k.get<longtype>();
19  if (n != 0)
20  {
21    std::cerr << "Keytesterror new key get: "<< n <<std::endl;
22    return 1;
23  }
24
25  n = 1234567890123456;
26  k.set(n);
27  longtype l = k.get<longtype>();
28  if (l != n)
29  {
30    std::cerr << "Keytesterror expect: " << n<< ":" << " got:" << l << std::endl;
31    return 1;
32  }
33  return 0;
34}
35 
36
37int testpool(bool dumpdb,bool openclose,int which)
38{
39#define DUMPDB if (dumpdb) {std::cerr << __LINE__ <<"dump\n"; p->dump_db();}
40#define OPENCLOSE \
41  if (openclose) \
42  {\
43     p->close_db();\
44     rc = p->open_db();\
45     if(rc)\
46     {\
47        std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db:" << rc << std::endl;\
48        return rc;\
49     }\
50   }
51
52  stopos_pool *p;
53  std::string dbname;
54  switch (which)
55  {
56    case 1: dbname = "/tmp/testdb_gdbm";
57            p = new gdbm_pool();
58            break;
59    case 2: dbname = "/tmp/testdb_flat";
60            p = new flatfile_pool();
61            break;
62    case 3: dbname = "/tmp/testdb_dir";
63            p = new files_pool();
64            break;
65    case 4: dbname = "testdb";
66            p = new mysql_pool();
67            break;
68    default: std::cerr << __LINE__ << "INVALID WHICH" << std::endl;
69             return 6;
70  }
71
72  std::vector <std::string> lines;
73  std::vector <std::string> keys;
74
75  std::string linesfile = "/tmp/linesfile";
76  std::string command;
77  command="man bash 2>/dev/null | head -n 100  > "+linesfile;
78  int rc;
79  rc = system(command.c_str());
80  if (rc != 0)
81    std::cerr << "Iets niet helemaal goed met man ... \n";
82  std::ifstream infile;
83  infile.open(linesfile.c_str());
84
85  int ntest = 0;
86  while (infile)
87  {
88    ntest++;
89    std::string line;
90    getline(infile,line);
91    lines.push_back(NumberToString(ntest)+":"+line);
92  }
93
94  p->set_db_name(dbname);
95  rc = p->create_db();
96  if (rc !=0)
97  {
98    std::cerr << __FILE__<<":"<<__LINE__<<": cannot create db:" << rc << std::endl;
99    return rc;
100  }
101  if (which ==0)
102  {
103    std::cerr << "Ja NU!\n";
104    sleep (5);
105    std::cerr << "continuing \n";
106  }
107  rc = p->open_db();
108  if (rc !=0)
109  {
110    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
111    return 7;
112  }
113
114  for (int i=0; i<ntest; i++)
115  {
116    keys.resize(0);
117    int rc;
118    std::string key;
119    OPENCLOSE
120    rc = p->add_line(lines[i],key);
121    if (rc!=0) 
122    {
123      p->close_db();
124      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << ":" << rc << std::endl;
125      return rc;
126    }
127    keys.push_back(key);
128  }
129  p->close_db();
130  rc = p->open_db();
131  if (rc !=0)
132  {
133    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
134    return rc;
135  }
136  DUMPDB
137  p->remove_line("4");
138  DUMPDB
139  p->remove_line("2");
140  DUMPDB
141  p->remove_line("3");
142  DUMPDB
143  p->remove_line("1");
144  DUMPDB
145  std::string line;
146  unsigned int m;
147  std::string getkey;
148  p->get_line(line,m,getkey);
149
150  p->create_db();
151  rc = p->open_db();
152  if (rc !=0)
153  {
154    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
155    return rc;
156  }
157
158  keys.resize(0);
159  for (int i=0; i<ntest; i++)
160  {
161    int rc;
162    std::string key;
163    OPENCLOSE
164    rc = p->add_line(lines[i],key);
165    if (rc!=0) 
166    {
167      p->close_db();
168      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":add_line '"<<lines[i]<<"' number: " << i << std::endl;
169      return rc;
170    }
171    keys.push_back(key);
172  }
173  DUMPDB
174
175  // try to read all lines:
176  p->close_db();
177  delete p;
178  switch (which)
179  {
180    case 1: p = new gdbm_pool();
181            break;
182    case 2: p = new flatfile_pool();
183            break;
184    case 3: p = new files_pool();
185            break;
186    case 4: p = new mysql_pool();
187            break;
188    default: std::cerr << __LINE__ << "INVALID WHICH" << std::endl;
189             return 6;
190  }
191  p->set_db_name(dbname);
192  rc = p->open_db();
193  if (rc !=0)
194  {
195    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db:"<<rc<<"\n";
196    return rc;
197  }
198
199  for (int i=0; i<ntest; i++)
200  {
201    std::string line,getkey;
202    int rc;
203    unsigned int m;
204    OPENCLOSE
205    rc = p->get_line(line,m,getkey);
206    DUMPDB
207
208    if (rc!=0) 
209    {
210      p->close_db();
211      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
212      return rc;
213    }
214    if (m != 0)
215    {
216      p->close_db();
217      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
218      return 9;
219    }
220    if (line != lines[i])
221    {
222      p->close_db();
223      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
224      return 9;
225    }
226  }
227
228  DUMPDB
229  // test if no further lines can be read
230  for (int i=0; i<10; i++)
231  {
232    std::string line,getkey;
233    int rc;
234    unsigned int m;
235    OPENCLOSE
236    rc = p->get_line(line,m,getkey);
237    if (rc == 0)
238    {
239      p->close_db();
240      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc <<std::endl;
241      return 9;
242    }
243  }
244  p->close_db();
245  rc = p->open_db();
246  if (rc !=0)
247  {
248    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
249    return rc;
250  }
251
252  DUMPDB
253  for (int i=0; i<ntest; i++)
254  {
255    int rc;
256    OPENCLOSE
257    rc = p->remove_line(keys[i]);
258    DUMPDB
259    if (rc != 0)
260    {
261      p->close_db();
262      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
263      return 9;
264    }
265  }
266  p->close_db();
267  rc = p->open_db();
268  if (rc !=0)
269  {
270    std::cerr << __FILE__<<":"<<__LINE__<<": cannot open db\n";
271    return rc;
272  }
273  for (int i=0; i<ntest; i++)
274  {
275    int rc;
276    OPENCLOSE
277    rc = p->remove_line(keys[i]);
278    if (rc == 0)
279    {
280      p->close_db();
281      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":can remove removed key " << "'"<< keys[i]<<"'" <<std::endl;
282      return 9;
283    }
284  }
285  p->close_db();
286  p->open_db();
287  p->set_kvp(0);
288
289  // typical use: fill database with lines,
290  // read and remove
291
292  {
293    for (int i=0; i<ntest; i++)
294    {
295      int rc;
296      std::string addkey;
297      OPENCLOSE
298      rc = p->add_line(lines[i],addkey);
299      if (rc!=0) 
300      {
301        p->close_db();
302        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << ":"<<rc<<std::endl;
303        return rc;
304      }
305    }
306    for (int i=0; i<ntest; i++)
307    {
308      std::string line,getkey;
309      int rc;
310      unsigned int m;
311      OPENCLOSE
312      rc = p->get_line(line,m,getkey);
313      DUMPDB
314      if (rc!=0) 
315      {
316        p->close_db();
317        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
318        return rc;
319      }
320      if (m != 0)
321      {
322        p->close_db();
323        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
324        return 9;
325      }
326      if (line != lines[i])
327      {
328        p->close_db();
329        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
330        return 9;
331      }
332      OPENCLOSE
333      p->remove_line(getkey);
334      if (rc !=0)
335      {
336        p->close_db();
337        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
338        return 9;
339      }
340    }
341    DUMPDB
342    // test if no further lines can be read
343    for (int i=0; i<10; i++)
344    {
345      std::string line,getkey;
346      int rc;
347      unsigned int m;
348      OPENCLOSE
349      rc = p->get_line(line,m,getkey);
350      if (rc == 0)
351      {
352        p->close_db();
353        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc <<std::endl;
354        return 9;
355      }
356    }
357  }
358  p->close_db();
359  p->open_db();
360  p->set_kvp(1);
361  keys.resize(0);
362  // kvp test
363  for (int i=0; i<ntest; i++)
364  {
365    int rc;
366    std::string key;
367    OPENCLOSE
368    rc = p->add_line(lines[i],key);
369    if (rc!=0) 
370    {
371      p->close_db();
372      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << std::endl;
373      return rc;
374    }
375    keys.push_back(key);
376  }
377  for (int i=0; i<ntest*3; i++)
378  {
379    std::string line,getkey;
380    int rc;
381    unsigned int m;
382    OPENCLOSE
383    rc = p->get_line(line,m,getkey);
384    if (rc!=0) 
385    {
386      p->close_db();
387      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
388      return rc;
389    }
390    if (m != (unsigned int)i/ntest)
391    {
392      p->close_db();
393      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: "<<i/ntest<<" got:" << m <<std::endl;
394      return 9;
395    }
396    if (line != lines[i%ntest])
397    {
398      p->close_db();
399      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
400      return 9;
401    }
402  }
403
404  p->close_db();
405  p->create_db();
406  p->open_db();
407
408  for (int i=0; i<10; i++)
409  {
410    std::string line,getkey;
411    int rc;
412    unsigned int m;
413    OPENCLOSE
414    rc = p->get_line(line,m,getkey);
415    if (rc == 0)
416    {
417      p->close_db();
418      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << rc << " line: '" << line << "'" <<std::endl;
419      return 9;
420    }
421  }
422
423  {
424    // test remove lines
425    p->set_kvp(0);
426    keys.resize(0);
427    for (int i=0; i<ntest; i++)
428    {
429      int rc;
430      std::string key;
431      OPENCLOSE
432      rc = p->add_line(lines[i],key);
433      if (rc!=0) 
434      {
435        p->close_db();
436        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":put_line '"<<lines[i]<<"' number: " << i << std::endl;
437        return rc;
438      }
439      keys.push_back(key);
440    }
441    //remove half of lines
442    DUMPDB
443    for (int i=0; i<ntest; i+=2)
444    {
445      OPENCLOSE
446      int rc = p->remove_line(keys[i]);
447      if (rc !=0)
448      {
449        p->close_db();
450        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":cannot remove key " << "'"<< keys[i]<<"'" <<std::endl;
451        return 9;
452      }
453    }
454    // try to get other half
455    DUMPDB
456    for (int i=1; i<ntest; i+=2)
457    {
458      int rc;
459      unsigned int m;
460      std::string line,getkey;
461      OPENCLOSE
462      rc = p->get_line(line,m,getkey);
463      if (rc!=0) 
464      {
465        p->close_db();
466        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
467        return rc;
468      }
469      if (m != 0)
470      {
471        p->close_db();
472        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: 0 got:" << m <<std::endl;
473        return 9;
474      }
475      if (line != lines[i])
476      {
477        p->close_db();
478        std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[i]<<"' got:'" << line <<"'"<<std::endl;
479        return 9;
480      }
481    }
482    // there should be no lines left, check:
483    int nrem = 0;
484    unsigned int m; 
485    std::string line,getkey;
486    while (p->get_line(line,m,getkey)==0)
487      nrem++;
488
489    if (nrem != 0)
490    {
491      p->close_db();
492      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: not 0 got:" << nrem << " line: '" << line << "'" <<std::endl;
493      return 9;
494    }
495  }
496  p->close_db();
497  p->create_db();
498  p->open_db();
499  keys.resize(0);
500
501  {
502    // test the following
503    // after an add_line, the next line to be retrieved should be
504    // that line, unless there were already other lines not
505    // retrieved yet
506    std::string line,key,getkey;
507    int rc;
508    for (int i=0; i<ntest/2; i++)
509    {
510      OPENCLOSE
511      p->add_line(lines[i],key);
512      keys.push_back(key);
513    }
514    unsigned int m;
515    int l=2;
516    for (int i=0; i<l; i++)
517    {
518      OPENCLOSE
519      p->get_line(line,m,getkey);
520    }
521    DUMPDB
522    // the next line to be retrieved should be lines[l]
523    // first, we add a new line
524      OPENCLOSE
525    p->add_line(lines[ntest/2],key);
526    DUMPDB
527    keys.push_back(key);
528    //check now which line we get:
529      OPENCLOSE
530    p->get_line(line,m,getkey);
531    if (line != lines[l])
532    {
533      p->close_db();
534      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[l]<<"' got:'" << line <<"'"<<std::endl;
535      return 9;
536    }
537    // now retrieve all lines and add a new one. Can we retrieve that:
538    while (p->get_line(line,m,getkey) == 0);
539    DUMPDB
540      OPENCLOSE
541    p->add_line(lines[0],key);
542    DUMPDB
543      OPENCLOSE
544    rc = p->get_line(line,m,getkey);
545    if (rc != 0)
546    {
547      p->close_db();
548      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
549      return rc;
550    }
551    if (line != lines[0])
552    {
553      p->close_db();
554      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
555      return 9;
556    }
557  }
558
559  p->create_db();
560  p->open_db();
561  {
562    std::string key,line,getkey;
563    unsigned int m;
564    int rc;
565    // some delete and insert tests
566    // put only one line in the database, and remove it
567    OPENCLOSE
568    rc = p->add_line(lines[0],key);
569    if (rc != 0)
570    {
571      p->close_db();
572      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":add_line\n";
573      return rc;
574    }
575
576    OPENCLOSE
577    rc = p->remove_line(key);
578    if (rc != 0)
579    {
580      p->close_db();
581      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
582      return rc;
583    }
584    OPENCLOSE
585    rc = p->get_line(line,m,getkey);
586    if (rc == 0)
587    {
588      p->close_db();
589      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line" <<
590        "expected !0, got:"<<rc <<"\n";
591      return rc;
592    }
593    // add two lines, remove last one, we expect that get_line
594    // gets the first
595    OPENCLOSE
596    p->add_line(lines[0],key);
597    OPENCLOSE
598    p->add_line(lines[1],key);
599    OPENCLOSE
600    rc = p->remove_line(key);
601    if (rc != 0)
602    {
603      p->close_db();
604      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
605      return rc;
606    }
607    OPENCLOSE
608    rc = p->get_line(line,m,getkey);
609    if (rc != 0)
610    {
611      p->close_db();
612      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
613      return rc;
614    }
615    if (line != lines[0])
616    {
617      p->close_db();
618      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
619      return 9;
620    }
621    // add two lines, remove first one, we expect that get_line
622    // gets the last
623    p->close_db();
624    p->create_db();
625    p->open_db();
626    std::string key0;
627    OPENCLOSE
628    p->add_line(lines[0],key0);
629    OPENCLOSE
630    p->add_line(lines[1],key);
631    rc = p->remove_line(key0);
632    if (rc != 0)
633    {
634      p->close_db();
635      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":remove_line\n";
636      return rc;
637    }
638    OPENCLOSE
639    rc = p->get_line(line,m,getkey);
640    if (rc != 0)
641    {
642      p->close_db();
643      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line\n";
644      return rc;
645    }
646    if (line != lines[1])
647    {
648      p->close_db();
649      std::cerr << "Error in "<<__FILE__<<":"<<__LINE__<<":get_line expected: '"<<lines[0]<<"' got:'" << line <<"'"<<std::endl;
650      return 9;
651    }
652  }
653
654  p->close_db();
655  return 0;
656}
657int main()
658{
659  double t0;
660  bool dumpen = 0;
661  bool openclose = 1;
662  std::cerr << "running keytest .... ";
663  if (testkeysetget()==0)
664    std::cerr << "ok\n";
665  else
666  {
667    std::cerr << "NOT OK\n";
668  }
669
670  std::cerr << "running gdbm ....... ";
671  t0=wallclock();
672  if (testpool(dumpen,openclose,1) == 0)
673    std::cerr << "ok " << wallclock()-t0 << std::endl;
674  else
675  {
676    std::cerr << "NOT OK\n";
677  }
678  std::cerr << "running flatfile ... ";
679  t0=wallclock();
680  if (testpool(dumpen,openclose,2) == 0)
681    std::cerr << "ok " << wallclock()-t0 << std::endl;
682  else
683  {
684    std::cerr << "NOT OK\n";
685  }
686  std::cerr << "running files ...... ";
687  t0=wallclock();
688  if (testpool(dumpen,openclose,3) == 0)
689    std::cerr << "ok " << wallclock()-t0 << std::endl;
690  else
691  {
692    std::cerr << "NOT OK\n";
693  }
694  std::cerr << "running mysql ...... ";
695  t0=wallclock();
696  if (testpool(dumpen,openclose,4) == 0)
697    std::cerr << "ok " << wallclock() -t0 << std::endl;
698  else
699  {
700    std::cerr << "NOT OK\n";
701  }
702  std::cerr << "all is well that ends well" << std::endl;
703  return 0;
704}
705
Note: See TracBrowser for help on using the repository browser.