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);
352 test->RIA(
"createia10", &ia);
354 if (!
test->IsReadOnly()) {
356 report_fail(
"failed to create 'createia10' of size 10");
361 std::vector<uint32_t> u32a;
362 u32a.push_back(0x11110000);
363 u32a.push_back(0x22220000);
364 u32a.push_back(0x33330000);
365 u32a.push_back(0x44440000);
366 test->RU32A(
"dwa", &u32a,
true);
370 std::vector<double> da;
375 test->RDA(
"da", &da,
true, 0);
379 std::vector<float> fa;
385 test->RFA(
"fa", &fa,
true, 0);
389 std::vector<bool> ba;
393 test->RBA(
"ba", &ba,
true, 0);
397 std::vector<std::string> sa;
398 sa.push_back(
"line1");
399 sa.push_back(
"line2");
400 sa.push_back(
"line3");
401 sa.push_back(
"line4");
402 test->RSA(
"sa", &sa,
true, 0, 32);
408 test->RSA(
"sa10", &sa,
true, 10, 32);
410 test->RSA(
"createsa10", NULL,
true, 10, 32);
414 printf(
"Test string sizes:\n");
418 test->WS(
"test_size",
"12345");
420 test->RS(
"test_size", &s);
421 printf(
"read test_size [%s]\n", s.c_str());
424 test->WS(
"test_size_32",
"1234567890", 32);
426 test->RS(
"test_size_32", &s);
427 printf(
"read test_size_32 [%s]\n", s.c_str());
430 test->WS(
"test_size_5",
"1234567890", 5);
432 test->RS(
"test_size_5", &s);
433 printf(
"read test_size_5 [%s]\n", s.c_str());
436 std::string s =
"1234567890";
437 test->RS(
"test_rsize", &s,
true);
438 printf(
"read test_rsize [%s]\n", s.c_str());
441 std::string s =
"123456";
442 test->RS(
"test_size_r32", &s,
true, 32);
443 printf(
"read test_size_r32 [%s]\n", s.c_str());
446 std::string s =
"1234567890";
447 test->RS(
"test_size_r8", &s,
true, 8);
448 printf(
"read test_size_r8 [%s]\n", s.c_str());
451 test->RSA(
"test_size_a8", NULL,
true, 2, 8);
452 std::string s =
"1234567890";
453 test->WSAI(
"test_size_a8", 0, s.c_str());
454 test->RSAI(
"test_size_a8", 0, &s);
455 printf(
"read test_size_a8 [%s]\n", s.c_str());
459 printf(
"Test creating and resizing arrays:\n");
463 test->RIA(
"create_ia15", NULL,
true, 15);
464 test->RBA(
"create_ba12", NULL,
true, 12);
465 test->RSA(
"create_sa5", NULL,
true, 5, 32);
468 test->RIA(
"resize_ia10", NULL,
true, 5);
469 test->RIA(
"resize_ia10", NULL,
true, 10);
470 test->RSA(
"resize_sa3", NULL,
true, 5, 32);
471 test->WSAI(
"resize_sa3", 0,
"00000000");
472 test->WSAI(
"resize_sa3", 1,
"11111111");
473 test->WSAI(
"resize_sa3", 2,
"22222222");
474 test->WSAI(
"resize_sa3", 3,
"33333333");
475 test->WSAI(
"resize_sa3", 4,
"44444444");
476 test->RSA(
"resize_sa3", NULL,
true, 3, 5);
480 printf(
"Test array index access:\n");
488 test->WIA(
"index_ia", ia);
489 for (
int i=0;
i<5;
i++) {
491 test->RIAI(
"index_ia",
i, &ivalue);
492 printf(
"index %d value %d\n",
i, ivalue);
494 for (
int i=0;
i<4;
i++) {
496 test->WIAI(
"index_ia",
i, ivalue);
498 for (
int i=0;
i<5;
i++) {
500 test->RIAI(
"index_ia",
i, &ivalue);
501 printf(
"index %d value %d\n",
i, ivalue);
506 printf(
"Test string array index access:\n");
510 std::vector<std::string> sa;
514 test->WSA(
"index_sa", sa, 32);
515 for (
int i=0;
i<5;
i++) {
516 std::string s =
"aaa";
517 test->RSAI(
"index_sa",
i, &s);
518 printf(
"index %d value [%s]\n",
i, s.c_str());
520 for (
int i=0;
i<4;
i++) {
521 std::string s =
"sa_qqq";
523 test->WSAI(
"index_sa",
i, s.c_str());
525 for (
int i=0;
i<5;
i++) {
526 std::string s =
"bbb";
527 test->RSAI(
"index_sa",
i, &s);
528 printf(
"index %d value [%s]\n",
i, s.c_str());
533 printf(
"Test string truncation:\n");
537 std::vector<std::string> sa;
538 sa.push_back(
"1234567890");
539 sa.push_back(
"aaa1");
540 sa.push_back(
"aaa2");
541 test->WSA(
"trunc_sa5", sa, 5);
542 test->WSAI(
"trunc_sa5", 1,
"1234567890");
543 for (
int i=0;
i<5;
i++) {
544 std::string s =
"bbb";
545 test->RSAI(
"trunc_sa5",
i, &s);
546 printf(
"index %d value [%s]\n",
i, s.c_str());
551 printf(
"Test against subdirectory:\n");
555 printf(
"Creating subdir\n");
556 MVOdb* subdir =
test->Chdir(
"subdir",
true);
557 printf(
"Creating subdir/i\n");
560 printf(
"RI(subdir)\n");
561 test->RI(
"subdir", &
i);
562 printf(
"WI(subdir)\n");
563 test->WI(
"subdir", 10);
568 printf(
"WIA(subdir)\n");
569 test->WIA(
"subdir", ia);
570 printf(
"RIA(subdir)\n");
571 test->RIA(
"subdir", &ia);
572 printf(
"RIA() returned: ");
575 printf(
"RIA(subdir, create=true)\n");
576 test->RIA(
"subdir", &ia,
true);
577 printf(
"RIA() returned: ");
583 printf(
"Test special cases:\n");
587 printf(
"test RI() of non existant variable:\n");
589 test->RI(
"nonexistant", &ivalue);
590 printf(
"RI() returned ivalue %d\n", ivalue);
594 std::vector<double> da;
595 printf(
"test RDA() of integer array:\n");
597 test->RDA(
"ia10", &da,
false, 0);
598 printf(
"RDA() returned: ");
604 printf(
"test RD() of integer array:\n");
607 test->RD(
"ia10", &v);
608 printf(
"RD() returned %f\n", v);
613 printf(
"test RI() of array ia10:\n");
615 test->RI(
"ia10", &ivalue);
616 printf(
"RI() returned ivalue %d\n", ivalue);
621 printf(
"test RIA() of non-array int:\n");
623 test->RIA(
"int", &ia);
624 printf(
"RIA() returned: ");
630 printf(
"test index 0 of non-array int:\n");
632 test->RIAI(
"int", 0, &ivalue);
633 printf(
"RIAI() returned ivalue %d\n", ivalue);
638 printf(
"test index of non-array int:\n");
640 test->RIAI(
"int", 10, &ivalue);
641 printf(
"RIAI() returned ivalue %d\n", ivalue);
646 printf(
"test invalid index -1 of array ia10:\n");
648 test->RIAI(
"ia10", -1, &ivalue);
649 printf(
"RIAI() returned ivalue %d\n", ivalue);
654 printf(
"test invalid index 999 of array ia10:\n");
656 test->RIAI(
"ia10", 999, &ivalue);
657 printf(
"RIAI() returned ivalue %d\n", ivalue);
661 printf(
"test string array invalid index -1:\n");
663 test->RSAI(
"sa10", -1, &svalue);
664 printf(
"RSAI() returned [%s]\n", svalue.c_str());
667 printf(
"test string array invalid index 999:\n");
669 test->RSAI(
"sa10", 999, &svalue);
670 printf(
"RSAI() returned [%s]\n", svalue.c_str());
673 printf(
"test write invalid index -1:\n");
674 test->WIAI(
"ia10", -1, 10);
677 printf(
"test string write invalid index -1:\n");
678 test->WSAI(
"sa10", -1,
"aaa");
682 printf(
"Test Chdir():\n");
686 printf(
"test Chdir(true):\n");
687 MVOdb* dir =
test->Chdir(
"test_subdir",
true);
689 if (dir == NULL)
report_fail(
"Chdir(new directory, true)");
693 printf(
"test Chdir(false):\n");
694 MVOdb* dir =
test->Chdir(
"non-existant-subdir",
false);
696 if (dir != NULL)
report_fail(
"Chdir(new directory, false)");
700 printf(
"test Chdir(\"not a dir\", true):\n");
701 MVOdb* dir =
test->Chdir(
"ia10",
true);
703 if (dir == NULL)
report_fail(
"Chdir(not a directory, true)");
707 printf(
"test Chdir(\"not a dir\", false):\n");
708 MVOdb* dir =
test->Chdir(
"ia10",
false);
710 if (dir != NULL)
report_fail(
"Chdir(not a directory, false)");
715 printf(
"Test ReadKeyLastWritten():\n");
719 const char* varname =
"ia";
722 test->ReadKeyLastWritten(varname, &last_written);
724 printf(
"\"%s\" last_written is %d\n", varname, last_written);
729 printf(
"Test ReadKey():\n");
733 const char* varname =
"ia";
739 test->ReadKey(varname, &tid, &num_values, &total_size, &item_size);
741 printf(
" : tid num size item name\n");
742 printf(
" : %3d %3d %6d %6d \"%s\"\n", tid, num_values, total_size, item_size, varname);
746 printf(
"Test ReadDir():\n");
750 std::vector<std::string> varname;
751 std::vector<int> tid;
752 std::vector<int> num_values;
753 std::vector<int> total_size;
754 std::vector<int> item_size;
756 test->ReadDir(&varname, &tid, &num_values, &total_size, &item_size);
758 printf(
" : tid num size item name\n");
759 for (
size_t i=0;
i<varname.size();
i++) {
760 printf(
"%3zu: %3d %3d %6d %6d \"%s\"\n",
i, tid[
i], num_values[
i], total_size[
i], item_size[
i], varname[
i].c_str());
769 printf(
"Number of FAILED tests: %d\n",
gCountFail);