MIDAS
Loading...
Searching...
No Matches
test_mvodb.cxx
Go to the documentation of this file.
1//
2// test_mvodb.cxx --- test ODB access functions
3//
4// K.Olchanski
5//
6
7#undef NDEBUG // midas required assert() to be always enabled
8
9#include <stdio.h>
10#include <sys/time.h>
11#include <iostream>
12#include <assert.h>
13#include <signal.h>
14#include <string.h>
15#include <stdlib.h> // exit()
16
17#include "tmfe.h"
18#include "midasio.h"
19#include "mvodb.h"
20
21std::string toString(int i)
22{
23 char buf[256];
24 snprintf(buf, sizeof(buf), "%d", i);
25 return buf;
26}
27
28void print_ia(const std::vector<int> &ia)
29{
30 int size = ia.size();
31 printf("int[%d] has [", size);
32 for (int i=0; i<size; i++) {
33 if (i>0)
34 printf(", ");
35 printf("%d", ia[i]);
36 }
37 printf("]");
38}
39
40void print_da(const std::vector<double> &da)
41{
42 int size = da.size();
43 printf("int[%d] has [", size);
44 for (int i=0; i<size; i++) {
45 if (i>0)
46 printf(", ");
47 printf("%f", da[i]);
48 }
49 printf("]");
50}
51
52static int gCountFail = 0;
53
54void report_fail(const char* text)
55{
56 printf("FAIL: %s\n", text);
57 gCountFail++;
58}
59
60// Main function call
61
62int main(int argc, char *argv[])
63{
64 setbuf(stdout,NULL);
65 setbuf(stderr,NULL);
66
67 signal(SIGILL, SIG_DFL);
68 signal(SIGBUS, SIG_DFL);
69 signal(SIGSEGV, SIG_DFL);
70 signal(SIGPIPE, SIG_DFL);
71
72 const char* hostname = NULL;
73 const char* exptname = NULL;
74 const char* filename = argv[1];
75 bool online = false;
76 bool xmlfile = false;
77 bool jsonfile = false;
78 bool nullodb = false;
79
80 if (!filename)
81 online = true;
82 else if (strstr(filename, ".xml")!=0)
83 xmlfile = true;
84 else if (strstr(filename, ".json")!=0)
85 jsonfile = true;
86 else if (strstr(filename, "null")!=0)
87 nullodb = true;
88 else {
89 hostname = filename;
90 online = true;
91 }
92
93 TMFE* mfe = NULL;
94
95 MVOdb* odb = NULL;
96 MVOdbError odberror;
97
98 if (nullodb)
99 {
100 printf("Using NullOdb\n");
101 odb = MakeNullOdb();
102 }
103 else if (online)
104 {
105 printf("Using MidasOdb\n");
106 mfe = TMFE::Instance();
107
108 TMFeResult r;
109
110 r = mfe->Connect("test_mvodb", hostname, exptname);
111 if (r.error_flag) {
112 fprintf(stderr,"Cannot connect to MIDAS, error %s\n", r.error_message.c_str());
113 return -1;
114 }
115
116 odb = MakeMidasOdb(mfe->fDB, &odberror);
117 }
118 else if (xmlfile)
119 {
120 printf("Using XmlOdb\n");
121 odb = MakeXmlFileOdb(filename, &odberror);
122 }
123 else if (jsonfile)
124 {
125 printf("Using JsonOdb\n");
126 odb = MakeJsonFileOdb(filename, &odberror);
127 }
128 else
129 {
130 printf("Using FileDumpOdb\n");
131 TMReaderInterface* reader = TMNewReader(filename);
132
133 if (reader->fError) {
134 printf("Cannot open input file \"%s\"\n",filename);
135 delete reader;
136 return -1;
137 }
138
139 while (1) {
140 TMEvent* event = TMReadEvent(reader);
141
142 if (!event)
143 break;
144
145 int eventId = event->event_id;
146 //printf("Have an event of type %d\n", eventId);
147
148 if ((eventId & 0xFFFF) != 0x8000) {
149 delete event;
150 continue;
151 }
152
153 // begin run
154 //event.Print();
155
156 odb = MakeFileDumpOdb(event->GetEventData(), event->data_size, &odberror);
157 break;
158 }
159
160 reader->Close();
161 delete reader;
162
163 if (!odb) {
164 printf("Failed to load ODB from input file \"%s\"\n",filename);
165 return -1;
166 }
167 }
168
169 if (odberror.fError) {
170 fprintf(stderr, "Cannot make MVOdb object, error: %s\n", odberror.fErrorString.c_str());
171 exit(1);
172 }
173
174 printf("\n");
175 printf("Starting tests:\n");
176 printf("\n");
177
178 int runno = 1234;
179
180 odb->RI("runinfo/run number", &runno);
181
182 printf("read run number (RI): %d\n", runno);
183
184 odb->Delete("test_mvodb");
185
186 MVOdb* test = odb->Chdir("test_mvodb", true);
187
188 assert(test != NULL);
189
190 test->SetPrintError(true);
191
192 bool isreadonly = test->IsReadOnly();
193
194 if (isreadonly) {
195 printf("\n");
196 printf("This ODB is read-only!\n");
197 }
198
199 printf("\n");
200 printf("Test create-and-read of all data types (set default values):\n");
201 printf("\n");
202
203 int cycle = 1;
204 test->RI("cycle", &cycle, true);
205 printf("RI() cycle: %d\n", cycle);
206
207 int ivalue = 1;
208 test->RI("int", &ivalue, true);
209 printf("RI() int: %d\n", ivalue);
210
211 float fvalue = 2.2;
212 test->RF("float", &fvalue, true);
213 printf("RF() float: %f\n", fvalue);
214
215 double dvalue = 3.3;
216 test->RD("double", &dvalue, true);
217 printf("RD() double: %f\n", dvalue);
218
219 bool bvalue_a = false;
220 test->RB("bool_a", &bvalue_a, true);
221 printf("bool_a: %d\n", bvalue_a);
222
223 bool bvalue_b = true;
224 test->RB("bool_b", &bvalue_b, true);
225 printf("bool_b: %d\n", bvalue_b);
226
227 uint16_t u16value = 0xabcd;
228 test->RU16("u16", &u16value, true);
229 printf("RU16() u16: 0x%04x\n", u16value);
230
231 uint32_t u32value = 0x12345678;
232 test->RU32("u32", &u32value, true);
233 printf("RU32() u32: 0x%08x\n", u32value);
234
235 uint64_t u64value = 0x1122334455667788;
236 test->RU64("u64", &u64value, true);
237 printf("RU64() u64: 0x%016llx\n", (unsigned long long)u64value);
238
239 std::string svalue = "test string";
240 test->RS("string", &svalue, true);
241 printf("RS() string: \"%s\"\n", svalue.c_str());
242
243 printf("\n");
244 printf("Test write of all data types (overwrite default values):\n");
245 printf("\n");
246
247 test->WI("cycle", cycle+1);
248
249 int wi = 10 + 100*cycle;
250 test->WI("int", wi);
251 float wf = 11.1 + 100*cycle;
252 test->WF("float", wf);
253 double wd = 22.2 + 100*cycle;
254 test->WD("double", wd);
255 bool wba = true;
256 test->WB("bool_a", wba);
257 bool wbb = false;
258 test->WB("bool_b", wbb);
259 uint16_t wu16 = 0xcdef;
260 test->WU16("u16", wu16);
261 uint32_t wu32 = 0xdeadf00d;
262 test->WU32("u32", wu32);
263 uint64_t wu64 = 0xddeeaaddff0000dd;
264 test->WU64("u64", wu64);
265 std::string ws = "write test string cycle " + toString(cycle);
266 test->WS("string", ws.c_str());
267
268 printf("\n");
269 printf("Test read of new values for all data types:\n");
270 printf("\n");
271
272 int ri = 1;
273 test->RI("int", &ri);
274 printf("int: %d -> %d -> %d\n", ivalue, wi, ri);
275
276 float rf = 2.2;
277 test->RF("float", &rf);
278 printf("float: %f -> %f -> %f\n", fvalue, wf, rf);
279
280 double rd = 3.3;
281 test->RD("double", &rd);
282 printf("double: %f -> %f -> %f\n", dvalue, wd, rd);
283
284 bool rba = false;
285 test->RB("bool_a", &rba);
286 printf("bool_a: %d -> %d -> %d\n", bvalue_a, wba, rba);
287
288 bool rbb = false;
289 test->RB("bool_b", &rbb);
290 printf("bool_b: %d -> %d -> %d\n", bvalue_b, wbb, rbb);
291
292 uint16_t ru16 = 0x4444;
293 test->RU16("u16", &ru16);
294 printf("u16: 0x%04x -> 0x%04x -> 0x%04x\n", u16value, wu16, ru16);
295
296 uint32_t ru32 = 0x55555555;
297 test->RU32("u32", &ru32);
298 printf("u32: 0x%08x -> 0x%08x -> 0x%08x\n", u32value, wu32, ru32);
299
300 uint64_t ru64 = 0x1515151515151515;
301 test->RU64("u64", &ru64);
302 printf("u64: 0x%016llx -> 0x%016llx -> 0x%016llx\n", (unsigned long long)u64value, (unsigned long long)wu64, (unsigned long long)ru64);
303
304 std::string rs = "rs";
305 test->RS("string", &rs);
306 printf("string: \"%s\" -> \"%s\" -> \"%s\"\n", svalue.c_str(), ws.c_str(), rs.c_str());
307
308 printf("\n");
309 printf("Test read arrays of all data types:\n");
310 printf("\n");
311
312 {
313 printf("read int array ia:\n");
314 std::vector<int> ia;
315 ia.push_back(1);
316 ia.push_back(2);
317 ia.push_back(3);
318 test->RIA("ia", &ia, true);
319 printf("RIA() returned: ");
320 print_ia(ia);
321 printf("\n");
322 }
323
324 {
325 printf("read non-existant array ia-noexist:\n");
326 std::vector<int> ia;
327 ia.push_back(1);
328 ia.push_back(2);
329 ia.push_back(3);
330 test->RIA("ia-noexist", &ia);
331 printf("RIA() returned: ");
332 print_ia(ia);
333 printf("\n");
334 }
335
336 {
337 // create 10 element array, init to zero (ia is empty)
338 std::vector<int> ia;
339 test->RIA("ia10zero", &ia, true, 10);
340 // create 10 element array, init from ia
341 ia.clear();
342 ia.push_back(11);
343 ia.push_back(22);
344 test->RIA("ia10", &ia, true, 10);
345 }
346
347 {
348 // create 10 element array, init to zero (passed NULL instead of &ia)
349 test->RIA("createia10", NULL, true, 10);
350 }
351
352 {
353 std::vector<uint32_t> u32a;
354 u32a.push_back(0x11110000);
355 u32a.push_back(0x22220000);
356 u32a.push_back(0x33330000);
357 u32a.push_back(0x44440000);
358 test->RU32A("dwa", &u32a, true);
359 }
360
361 {
362 std::vector<double> da;
363 da.push_back(1.1);
364 da.push_back(1.2);
365 da.push_back(1.3);
366 da.push_back(1.4);
367 test->RDA("da", &da, true, 0);
368 }
369
370 {
371 std::vector<float> fa;
372 fa.push_back(2.1);
373 fa.push_back(2.2);
374 fa.push_back(2.3);
375 fa.push_back(20.3);
376 fa.push_back(21.3);
377 test->RFA("fa", &fa, true, 0);
378 }
379
380 {
381 std::vector<bool> ba;
382 ba.push_back(true);
383 ba.push_back(false);
384 ba.push_back(true);
385 test->RBA("ba", &ba, true, 0);
386 }
387
388 {
389 std::vector<std::string> sa;
390 sa.push_back("line1");
391 sa.push_back("line2");
392 sa.push_back("line3");
393 sa.push_back("line4");
394 test->RSA("sa", &sa, true, 0, 32);
395
396 // create 10 element array, init from ia
397 sa.clear();
398 sa.push_back("xx1");
399 sa.push_back("xx2");
400 test->RSA("sa10", &sa, true, 10, 32);
401 // create 10 element array, init to zero (passed NULL instead of &sa)
402 test->RSA("createsa10", NULL, true, 10, 32);
403 }
404
405 printf("\n");
406 printf("Test string sizes:\n");
407 printf("\n");
408
409 {
410 test->WS("test_size", "12345");
411 std::string s;
412 test->RS("test_size", &s);
413 printf("read test_size [%s]\n", s.c_str());
414 }
415 {
416 test->WS("test_size_32", "1234567890", 32);
417 std::string s;
418 test->RS("test_size_32", &s);
419 printf("read test_size_32 [%s]\n", s.c_str());
420 }
421 {
422 test->WS("test_size_5", "1234567890", 5);
423 std::string s;
424 test->RS("test_size_5", &s);
425 printf("read test_size_5 [%s]\n", s.c_str());
426 }
427 {
428 std::string s = "1234567890";
429 test->RS("test_rsize", &s, true);
430 printf("read test_rsize [%s]\n", s.c_str());
431 }
432 {
433 std::string s = "123456";
434 test->RS("test_size_r32", &s, true, 32);
435 printf("read test_size_r32 [%s]\n", s.c_str());
436 }
437 {
438 std::string s = "1234567890";
439 test->RS("test_size_r8", &s, true, 8);
440 printf("read test_size_r8 [%s]\n", s.c_str());
441 }
442 {
443 test->RSA("test_size_a8", NULL, true, 2, 8);
444 std::string s = "1234567890";
445 test->WSAI("test_size_a8", 0, s.c_str());
446 test->RSAI("test_size_a8", 0, &s);
447 printf("read test_size_a8 [%s]\n", s.c_str());
448 }
449
450 printf("\n");
451 printf("Test creating and resizing arrays:\n");
452 printf("\n");
453
454 {
455 test->RIA("create_ia15", NULL, true, 15);
456 test->RBA("create_ba12", NULL, true, 12);
457 test->RSA("create_sa5", NULL, true, 5, 32);
458 }
459 {
460 test->RIA("resize_ia10", NULL, true, 5);
461 test->RIA("resize_ia10", NULL, true, 10);
462 test->RSA("resize_sa3", NULL, true, 5, 32);
463 test->WSAI("resize_sa3", 0, "00000000");
464 test->WSAI("resize_sa3", 1, "11111111");
465 test->WSAI("resize_sa3", 2, "22222222");
466 test->WSAI("resize_sa3", 3, "33333333");
467 test->WSAI("resize_sa3", 4, "44444444");
468 test->RSA("resize_sa3", NULL, true, 3, 5);
469 }
470
471 printf("\n");
472 printf("Test array index access:\n");
473 printf("\n");
474
475 {
476 std::vector<int> ia;
477 ia.push_back(10);
478 ia.push_back(11);
479 ia.push_back(12);
480 test->WIA("index_ia", ia);
481 for (int i=0; i<5; i++) {
482 int ivalue = 999;
483 test->RIAI("index_ia", i, &ivalue);
484 printf("index %d value %d\n", i, ivalue);
485 }
486 for (int i=0; i<4; i++) {
487 int ivalue = 20+i;
488 test->WIAI("index_ia", i, ivalue);
489 }
490 for (int i=0; i<5; i++) {
491 int ivalue = 999;
492 test->RIAI("index_ia", i, &ivalue);
493 printf("index %d value %d\n", i, ivalue);
494 }
495 }
496
497 printf("\n");
498 printf("Test string array index access:\n");
499 printf("\n");
500
501 {
502 std::vector<std::string> sa;
503 sa.push_back("sa0");
504 sa.push_back("sa1");
505 sa.push_back("sa2");
506 test->WSA("index_sa", sa, 32);
507 for (int i=0; i<5; i++) {
508 std::string s = "aaa";
509 test->RSAI("index_sa", i, &s);
510 printf("index %d value [%s]\n", i, s.c_str());
511 }
512 for (int i=0; i<4; i++) {
513 std::string s = "sa_qqq";
514 s += toString(i);
515 test->WSAI("index_sa", i, s.c_str());
516 }
517 for (int i=0; i<5; i++) {
518 std::string s = "bbb";
519 test->RSAI("index_sa", i, &s);
520 printf("index %d value [%s]\n", i, s.c_str());
521 }
522 }
523
524 printf("\n");
525 printf("Test string truncation:\n");
526 printf("\n");
527
528 {
529 std::vector<std::string> sa;
530 sa.push_back("1234567890");
531 sa.push_back("aaa1");
532 sa.push_back("aaa2");
533 test->WSA("trunc_sa5", sa, 5);
534 test->WSAI("trunc_sa5", 1, "1234567890");
535 for (int i=0; i<5; i++) {
536 std::string s = "bbb";
537 test->RSAI("trunc_sa5", i, &s);
538 printf("index %d value [%s]\n", i, s.c_str());
539 }
540 }
541
542 printf("\n");
543 printf("Test against subdirectory:\n");
544 printf("\n");
545
546 {
547 printf("Creating subdir\n");
548 MVOdb* subdir = test->Chdir("subdir", true);
549 printf("Creating subdir/i\n");
550 subdir->WI("i", 10); // write something into subdir
551 int i = 1111;
552 printf("RI(subdir)\n");
553 test->RI("subdir", &i); // invalid read from a non-int
554 printf("WI(subdir)\n");
555 test->WI("subdir", 10); // invalid write into existing subdir
556 std::vector<int> ia;
557 ia.push_back(111);
558 ia.push_back(222);
559 ia.push_back(333);
560 printf("WIA(subdir)\n");
561 test->WIA("subdir", ia); // invalid write into existing subdir
562 printf("RIA(subdir)\n");
563 test->RIA("subdir", &ia); // invalid read from non-int-array
564 printf("RIA() returned: ");
565 print_ia(ia);
566 printf("\n");
567 printf("RIA(subdir, create=true)\n");
568 test->RIA("subdir", &ia, true); // invalid read from non-int-array
569 printf("RIA() returned: ");
570 print_ia(ia);
571 printf("\n");
572 }
573
574 printf("\n");
575 printf("Test special cases:\n");
576 printf("\n");
577
578 {
579 printf("test RI() of non existant variable:\n");
580 int ivalue = 999;
581 test->RI("nonexistant", &ivalue);
582 printf("RI() returned ivalue %d\n", ivalue);
583 printf("\n");
584
585 {
586 std::vector<double> da;
587 printf("test RDA() of integer array:\n");
588 // wrong data type: ODB is INT, we ask for DOUBLE
589 test->RDA("ia10", &da, false, 0);
590 printf("RDA() returned: ");
591 print_da(da);
592 printf("\n");
593 }
594
595 {
596 printf("test RD() of integer array:\n");
597 // wrong data type: ODB is INT, we ask for DOUBLE
598 double v = 999.9;
599 test->RD("ia10", &v);
600 printf("RD() returned %f\n", v);
601 printf("\n");
602 }
603
604 {
605 printf("test RI() of array ia10:\n");
606 int ivalue = 999;
607 test->RI("ia10", &ivalue);
608 printf("RI() returned ivalue %d\n", ivalue);
609 printf("\n");
610 }
611
612 {
613 printf("test RIA() of non-array int:\n");
614 std::vector<int> ia;
615 test->RIA("int", &ia);
616 printf("RIA() returned: ");
617 print_ia(ia);
618 printf("\n");
619 }
620
621 {
622 printf("test index 0 of non-array int:\n");
623 int ivalue = 999;
624 test->RIAI("int", 0, &ivalue);
625 printf("RIAI() returned ivalue %d\n", ivalue);
626 printf("\n");
627 }
628
629 {
630 printf("test index of non-array int:\n");
631 int ivalue = 999;
632 test->RIAI("int", 10, &ivalue);
633 printf("RIAI() returned ivalue %d\n", ivalue);
634 printf("\n");
635 }
636
637 {
638 printf("test invalid index -1 of array ia10:\n");
639 int ivalue = 999;
640 test->RIAI("ia10", -1, &ivalue);
641 printf("RIAI() returned ivalue %d\n", ivalue);
642 printf("\n");
643 }
644
645 {
646 printf("test invalid index 999 of array ia10:\n");
647 int ivalue = 999;
648 test->RIAI("ia10", 999, &ivalue);
649 printf("RIAI() returned ivalue %d\n", ivalue);
650 printf("\n");
651 }
652
653 printf("test string array invalid index -1:\n");
654 svalue = "aaa";
655 test->RSAI("sa10", -1, &svalue);
656 printf("RSAI() returned [%s]\n", svalue.c_str());
657 printf("\n");
658
659 printf("test string array invalid index 999:\n");
660 svalue = "aaa";
661 test->RSAI("sa10", 999, &svalue);
662 printf("RSAI() returned [%s]\n", svalue.c_str());
663 printf("\n");
664
665 printf("test write invalid index -1:\n");
666 test->WIAI("ia10", -1, 10);
667 printf("\n");
668
669 printf("test string write invalid index -1:\n");
670 test->WSAI("sa10", -1, "aaa");
671 printf("\n");
672
673 printf("\n");
674 printf("Test Chdir():\n");
675 printf("\n");
676
677 {
678 printf("test Chdir(true):\n");
679 MVOdb* dir = test->Chdir("test_subdir", true);
680 //printf("Chdir() returned %p\n", dir);
681 if (dir == NULL) report_fail("Chdir(new directory, true)");
682 }
683
684 {
685 printf("test Chdir(false):\n");
686 MVOdb* dir = test->Chdir("non-existant-subdir", false);
687 //printf("Chdir() returned %p\n", dir);
688 if (dir != NULL) report_fail("Chdir(new directory, false)");
689 }
690
691 {
692 printf("test Chdir(\"not a dir\", true):\n");
693 MVOdb* dir = test->Chdir("ia10", true);
694 //printf("Chdir() returned %p\n", dir);
695 if (dir == NULL) report_fail("Chdir(not a directory, true)");
696 }
697
698 {
699 printf("test Chdir(\"not a dir\", false):\n");
700 MVOdb* dir = test->Chdir("ia10", false);
701 //printf(" returned %p\n", dir);
702 if (dir != NULL) report_fail("Chdir(not a directory, false)");
703 }
704
705 if (0) {
706 printf("\n");
707 printf("Test ReadKeyLastWritten():\n");
708 printf("\n");
709
710 {
711 const char* varname = "ia";
712 int last_written;
713
714 test->ReadKeyLastWritten(varname, &last_written);
715
716 printf("\"%s\" last_written is %d\n", varname, last_written);
717 }
718 }
719
720 printf("\n");
721 printf("Test ReadKey():\n");
722 printf("\n");
723
724 {
725 const char* varname = "ia";
726 int tid;
727 int num_values;
728 int total_size;
729 int item_size;
730
731 test->ReadKey(varname, &tid, &num_values, &total_size, &item_size);
732
733 printf(" : tid num size item name\n");
734 printf(" : %3d %3d %6d %6d \"%s\"\n", tid, num_values, total_size, item_size, varname);
735 }
736
737 printf("\n");
738 printf("Test ReadDir():\n");
739 printf("\n");
740
741 {
742 std::vector<std::string> varname;
743 std::vector<int> tid;
744 std::vector<int> num_values;
745 std::vector<int> total_size;
746 std::vector<int> item_size;
747
748 test->ReadDir(&varname, &tid, &num_values, &total_size, &item_size);
749
750 printf(" : tid num size item name\n");
751 for (size_t i=0; i<varname.size(); i++) {
752 printf("%3zu: %3d %3d %6d %6d \"%s\"\n", i, tid[i], num_values[i], total_size[i], item_size[i], varname[i].c_str());
753 }
754 }
755 }
756
757 if (mfe)
758 mfe->Disconnect();
759
760 printf("\n");
761 printf("Number of FAILED tests: %d\n", gCountFail);
762 printf("\n");
763
764 return 0;
765}
766
767//end
768/* emacs
769 * Local Variables:
770 * tab-width: 8
771 * c-basic-offset: 3
772 * indent-tabs-mode: nil
773 * End:
774 */
Definition tmfe.h:381
int fDB
ODB database handle.
Definition tmfe.h:394
TMFeResult Connect(const char *progname=NULL, const char *hostname=NULL, const char *exptname=NULL)
Definition tmfe.cxx:65
static TMFE * Instance()
Definition tmfe.cxx:57
TMFeResult Disconnect()
Definition tmfe.cxx:154
bool error_flag
Definition tmfe.h:89
std::string error_message
Definition tmfe.h:91
int main()
Definition hwtest.cxx:23
INT online
Definition mana.cxx:241
INT i
Definition mdump.cxx:32
program test
Definition miniana.f:6
void print_da(const std::vector< double > &da)
static int gCountFail
void report_fail(const char *text)
void print_ia(const std::vector< int > &ia)
std::string toString(int i)