62int main(
int argc,
char *argv[])
67 signal(SIGILL, SIG_DFL);
68 signal(SIGBUS, SIG_DFL);
69 signal(SIGSEGV, SIG_DFL);
70 signal(SIGPIPE, SIG_DFL);
72 const char* hostname = NULL;
73 const char* exptname = NULL;
74 const char* filename = argv[1];
77 bool jsonfile =
false;
82 else if (strstr(filename,
".xml")!=0)
84 else if (strstr(filename,
".json")!=0)
86 else if (strstr(filename,
"null")!=0)
100 printf(
"Using NullOdb\n");
105 printf(
"Using MidasOdb\n");
110 r = mfe->
Connect(
"test_mvodb", hostname, exptname);
112 fprintf(stderr,
"Cannot connect to MIDAS, error %s\n", r.
error_message.c_str());
116 odb = MakeMidasOdb(mfe->
fDB, &odberror);
120 printf(
"Using XmlOdb\n");
121 odb = MakeXmlFileOdb(filename, &odberror);
125 printf(
"Using JsonOdb\n");
126 odb = MakeJsonFileOdb(filename, &odberror);
130 printf(
"Using FileDumpOdb\n");
131 TMReaderInterface* reader = TMNewReader(filename);
133 if (reader->fError) {
134 printf(
"Cannot open input file \"%s\"\n",filename);
140 TMEvent*
event = TMReadEvent(reader);
145 int eventId =
event->event_id;
148 if ((eventId & 0xFFFF) != 0x8000) {
156 odb = MakeFileDumpOdb(event->GetEventData(), event->data_size, &odberror);
164 printf(
"Failed to load ODB from input file \"%s\"\n",filename);
169 if (odberror.fError) {
170 fprintf(stderr,
"Cannot make MVOdb object, error: %s\n", odberror.fErrorString.c_str());
175 printf(
"Starting tests:\n");
180 odb->RI(
"runinfo/run number", &runno);
182 printf(
"read run number (RI): %d\n", runno);
184 odb->Delete(
"test_mvodb");
186 MVOdb*
test = odb->Chdir(
"test_mvodb",
true);
188 assert(
test != NULL);
190 test->SetPrintError(
true);
192 bool isreadonly =
test->IsReadOnly();
196 printf(
"This ODB is read-only!\n");
200 printf(
"Test create-and-read of all data types (set default values):\n");
204 test->RI(
"cycle", &cycle,
true);
205 printf(
"RI() cycle: %d\n", cycle);
208 test->RI(
"int", &ivalue,
true);
209 printf(
"RI() int: %d\n", ivalue);
212 test->RF(
"float", &fvalue,
true);
213 printf(
"RF() float: %f\n", fvalue);
216 test->RD(
"double", &dvalue,
true);
217 printf(
"RD() double: %f\n", dvalue);
219 bool bvalue_a =
false;
220 test->RB(
"bool_a", &bvalue_a,
true);
221 printf(
"bool_a: %d\n", bvalue_a);
223 bool bvalue_b =
true;
224 test->RB(
"bool_b", &bvalue_b,
true);
225 printf(
"bool_b: %d\n", bvalue_b);
227 uint16_t u16value = 0xabcd;
228 test->RU16(
"u16", &u16value,
true);
229 printf(
"RU16() u16: 0x%04x\n", u16value);
231 uint32_t u32value = 0x12345678;
232 test->RU32(
"u32", &u32value,
true);
233 printf(
"RU32() u32: 0x%08x\n", u32value);
235 uint64_t u64value = 0x1122334455667788;
236 test->RU64(
"u64", &u64value,
true);
237 printf(
"RU64() u64: 0x%016llx\n", (
unsigned long long)u64value);
239 std::string svalue =
"test string";
240 test->RS(
"string", &svalue,
true);
241 printf(
"RS() string: \"%s\"\n", svalue.c_str());
244 printf(
"Test write of all data types (overwrite default values):\n");
247 test->WI(
"cycle", cycle+1);
249 int wi = 10 + 100*cycle;
251 float wf = 11.1 + 100*cycle;
252 test->WF(
"float", wf);
253 double wd = 22.2 + 100*cycle;
254 test->WD(
"double", wd);
256 test->WB(
"bool_a", wba);
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());
269 printf(
"Test read of new values for all data types:\n");
273 test->RI(
"int", &ri);
274 printf(
"int: %d -> %d -> %d\n", ivalue, wi, ri);
277 test->RF(
"float", &rf);
278 printf(
"float: %f -> %f -> %f\n", fvalue, wf, rf);
281 test->RD(
"double", &rd);
282 printf(
"double: %f -> %f -> %f\n", dvalue, wd, rd);
285 test->RB(
"bool_a", &rba);
286 printf(
"bool_a: %d -> %d -> %d\n", bvalue_a, wba, rba);
289 test->RB(
"bool_b", &rbb);
290 printf(
"bool_b: %d -> %d -> %d\n", bvalue_b, wbb, rbb);
292 uint16_t ru16 = 0x4444;
293 test->RU16(
"u16", &ru16);
294 printf(
"u16: 0x%04x -> 0x%04x -> 0x%04x\n", u16value, wu16, ru16);
296 uint32_t ru32 = 0x55555555;
297 test->RU32(
"u32", &ru32);
298 printf(
"u32: 0x%08x -> 0x%08x -> 0x%08x\n", u32value, wu32, ru32);
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);
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());
309 printf(
"Test read arrays of all data types:\n");
313 printf(
"read int array ia:\n");
318 test->RIA(
"ia", &ia,
true);
319 printf(
"RIA() returned: ");
325 printf(
"read non-existant array ia-noexist:\n");
330 test->RIA(
"ia-noexist", &ia);
331 printf(
"RIA() returned: ");
339 test->RIA(
"ia10zero", &ia,
true, 10);
344 test->RIA(
"ia10", &ia,
true, 10);
349 test->RIA(
"createia10", NULL,
true, 10);
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);
362 std::vector<double> da;
367 test->RDA(
"da", &da,
true, 0);
371 std::vector<float> fa;
377 test->RFA(
"fa", &fa,
true, 0);
381 std::vector<bool> ba;
385 test->RBA(
"ba", &ba,
true, 0);
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);
400 test->RSA(
"sa10", &sa,
true, 10, 32);
402 test->RSA(
"createsa10", NULL,
true, 10, 32);
406 printf(
"Test string sizes:\n");
410 test->WS(
"test_size",
"12345");
412 test->RS(
"test_size", &s);
413 printf(
"read test_size [%s]\n", s.c_str());
416 test->WS(
"test_size_32",
"1234567890", 32);
418 test->RS(
"test_size_32", &s);
419 printf(
"read test_size_32 [%s]\n", s.c_str());
422 test->WS(
"test_size_5",
"1234567890", 5);
424 test->RS(
"test_size_5", &s);
425 printf(
"read test_size_5 [%s]\n", s.c_str());
428 std::string s =
"1234567890";
429 test->RS(
"test_rsize", &s,
true);
430 printf(
"read test_rsize [%s]\n", s.c_str());
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());
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());
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());
451 printf(
"Test creating and resizing arrays:\n");
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);
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);
472 printf(
"Test array index access:\n");
480 test->WIA(
"index_ia", ia);
481 for (
int i=0;
i<5;
i++) {
483 test->RIAI(
"index_ia",
i, &ivalue);
484 printf(
"index %d value %d\n",
i, ivalue);
486 for (
int i=0;
i<4;
i++) {
488 test->WIAI(
"index_ia",
i, ivalue);
490 for (
int i=0;
i<5;
i++) {
492 test->RIAI(
"index_ia",
i, &ivalue);
493 printf(
"index %d value %d\n",
i, ivalue);
498 printf(
"Test string array index access:\n");
502 std::vector<std::string> sa;
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());
512 for (
int i=0;
i<4;
i++) {
513 std::string s =
"sa_qqq";
515 test->WSAI(
"index_sa",
i, s.c_str());
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());
525 printf(
"Test string truncation:\n");
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());
543 printf(
"Test against subdirectory:\n");
547 printf(
"Creating subdir\n");
548 MVOdb* subdir =
test->Chdir(
"subdir",
true);
549 printf(
"Creating subdir/i\n");
552 printf(
"RI(subdir)\n");
553 test->RI(
"subdir", &
i);
554 printf(
"WI(subdir)\n");
555 test->WI(
"subdir", 10);
560 printf(
"WIA(subdir)\n");
561 test->WIA(
"subdir", ia);
562 printf(
"RIA(subdir)\n");
563 test->RIA(
"subdir", &ia);
564 printf(
"RIA() returned: ");
567 printf(
"RIA(subdir, create=true)\n");
568 test->RIA(
"subdir", &ia,
true);
569 printf(
"RIA() returned: ");
575 printf(
"Test special cases:\n");
579 printf(
"test RI() of non existant variable:\n");
581 test->RI(
"nonexistant", &ivalue);
582 printf(
"RI() returned ivalue %d\n", ivalue);
586 std::vector<double> da;
587 printf(
"test RDA() of integer array:\n");
589 test->RDA(
"ia10", &da,
false, 0);
590 printf(
"RDA() returned: ");
596 printf(
"test RD() of integer array:\n");
599 test->RD(
"ia10", &v);
600 printf(
"RD() returned %f\n", v);
605 printf(
"test RI() of array ia10:\n");
607 test->RI(
"ia10", &ivalue);
608 printf(
"RI() returned ivalue %d\n", ivalue);
613 printf(
"test RIA() of non-array int:\n");
615 test->RIA(
"int", &ia);
616 printf(
"RIA() returned: ");
622 printf(
"test index 0 of non-array int:\n");
624 test->RIAI(
"int", 0, &ivalue);
625 printf(
"RIAI() returned ivalue %d\n", ivalue);
630 printf(
"test index of non-array int:\n");
632 test->RIAI(
"int", 10, &ivalue);
633 printf(
"RIAI() returned ivalue %d\n", ivalue);
638 printf(
"test invalid index -1 of array ia10:\n");
640 test->RIAI(
"ia10", -1, &ivalue);
641 printf(
"RIAI() returned ivalue %d\n", ivalue);
646 printf(
"test invalid index 999 of array ia10:\n");
648 test->RIAI(
"ia10", 999, &ivalue);
649 printf(
"RIAI() returned ivalue %d\n", ivalue);
653 printf(
"test string array invalid index -1:\n");
655 test->RSAI(
"sa10", -1, &svalue);
656 printf(
"RSAI() returned [%s]\n", svalue.c_str());
659 printf(
"test string array invalid index 999:\n");
661 test->RSAI(
"sa10", 999, &svalue);
662 printf(
"RSAI() returned [%s]\n", svalue.c_str());
665 printf(
"test write invalid index -1:\n");
666 test->WIAI(
"ia10", -1, 10);
669 printf(
"test string write invalid index -1:\n");
670 test->WSAI(
"sa10", -1,
"aaa");
674 printf(
"Test Chdir():\n");
678 printf(
"test Chdir(true):\n");
679 MVOdb* dir =
test->Chdir(
"test_subdir",
true);
681 if (dir == NULL)
report_fail(
"Chdir(new directory, true)");
685 printf(
"test Chdir(false):\n");
686 MVOdb* dir =
test->Chdir(
"non-existant-subdir",
false);
688 if (dir != NULL)
report_fail(
"Chdir(new directory, false)");
692 printf(
"test Chdir(\"not a dir\", true):\n");
693 MVOdb* dir =
test->Chdir(
"ia10",
true);
695 if (dir == NULL)
report_fail(
"Chdir(not a directory, true)");
699 printf(
"test Chdir(\"not a dir\", false):\n");
700 MVOdb* dir =
test->Chdir(
"ia10",
false);
702 if (dir != NULL)
report_fail(
"Chdir(not a directory, false)");
707 printf(
"Test ReadKeyLastWritten():\n");
711 const char* varname =
"ia";
714 test->ReadKeyLastWritten(varname, &last_written);
716 printf(
"\"%s\" last_written is %d\n", varname, last_written);
721 printf(
"Test ReadKey():\n");
725 const char* varname =
"ia";
731 test->ReadKey(varname, &tid, &num_values, &total_size, &item_size);
733 printf(
" : tid num size item name\n");
734 printf(
" : %3d %3d %6d %6d \"%s\"\n", tid, num_values, total_size, item_size, varname);
738 printf(
"Test ReadDir():\n");
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;
748 test->ReadDir(&varname, &tid, &num_values, &total_size, &item_size);
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());
761 printf(
"Number of FAILED tests: %d\n",
gCountFail);