Line data Source code
1 : //
2 : // test_mvodb.cxx --- test ODB access functions
3 : //
4 : // K.Olchanski
5 : //
6 :
7 : #include <stdio.h>
8 : #include <sys/time.h>
9 : #include <iostream>
10 : #include <assert.h>
11 : #include <signal.h>
12 : #include <string.h>
13 : #include <stdlib.h> // exit()
14 :
15 : #include "tmfe.h"
16 : #include "midas.h"
17 :
18 : //#include "mvodb.h"
19 :
20 0 : std::string toString(int i)
21 : {
22 : char buf[256];
23 0 : sprintf(buf, "%d", i);
24 0 : return buf;
25 : }
26 :
27 0 : void print_ia(const std::vector<int> &ia)
28 : {
29 0 : int size = ia.size();
30 0 : printf("int[%d] has [", size);
31 0 : for (int i=0; i<size; i++) {
32 0 : if (i>0)
33 0 : printf(", ");
34 0 : printf("%d", ia[i]);
35 : }
36 0 : printf("]");
37 0 : }
38 :
39 0 : void print_da(const std::vector<double> &da)
40 : {
41 0 : int size = da.size();
42 0 : printf("int[%d] has [", size);
43 0 : for (int i=0; i<size; i++) {
44 0 : if (i>0)
45 0 : printf(", ");
46 0 : printf("%f", da[i]);
47 : }
48 0 : printf("]");
49 0 : }
50 :
51 : static int gCountFail = 0;
52 :
53 0 : bool report_fail(const char* text)
54 : {
55 0 : printf("FAIL: %s\n", text);
56 0 : gCountFail++;
57 0 : return false;
58 : }
59 :
60 0 : bool report_fail(const std::string& text)
61 : {
62 0 : printf("FAIL: %s\n", text.c_str());
63 0 : gCountFail++;
64 0 : return false;
65 : }
66 :
67 1 : bool rmdir(HNDLE hDB, HNDLE hROOT, const char* path)
68 : {
69 : HNDLE hKey;
70 :
71 1 : printf("rmdir(%s)\n", path);
72 :
73 1 : int status = db_find_link(hDB, hROOT, path, &hKey);
74 :
75 1 : if (status == DB_NO_KEY)
76 1 : return true;
77 :
78 0 : if (status != DB_SUCCESS)
79 0 : return report_fail(msprintf("test_mkdir: Cannot delete subdirectory \"%s\", db_find_link() status %d\n", path, status));
80 :
81 0 : status = db_delete_key(hDB, hKey, FALSE);
82 :
83 0 : if (status == DB_SUCCESS)
84 0 : return true;
85 :
86 0 : return report_fail(msprintf("test_mkdir: Delete subdirectory \"%s\" failed, status %d\n", path, status));
87 : }
88 :
89 9 : bool test_mkdir(HNDLE hDB, HNDLE hROOT, const char* path)
90 : {
91 9 : printf("mkdir(%s)\n", path);
92 :
93 9 : int status = db_create_key(hDB, hROOT, path, TID_KEY);
94 :
95 9 : if (status == DB_SUCCESS)
96 9 : return true;
97 :
98 0 : return report_fail(msprintf("test_mkdir: Create subdirectory \"%s\" failed, status %d\n", path, status));
99 : }
100 :
101 8 : bool test_mkdir_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
102 : {
103 8 : printf("mkdir_fail(%s)\n", path);
104 :
105 8 : int status = db_create_key(hDB, hROOT, path, TID_KEY);
106 :
107 8 : if (status == fail_status)
108 8 : return true;
109 :
110 0 : return report_fail(msprintf("test_mkdir_fail: Create subdirectory \"%s\" failed, unexpcted status %d instead of %d\n", path, status, fail_status));
111 : }
112 :
113 1 : bool test_chdir(HNDLE hDB, HNDLE hROOT, const char* path, HNDLE* phKey)
114 : {
115 1 : printf("chdir(%s)\n", path);
116 :
117 1 : int status = db_find_link(hDB, hROOT, path, phKey);
118 :
119 1 : if (status == DB_SUCCESS)
120 1 : return true;
121 :
122 0 : return report_fail(msprintf("test_mkdir: Cannot find subdirectory \"%s\", db_find_link() status %d\n", path, status));
123 : }
124 :
125 7 : bool test_delete(HNDLE hDB, HNDLE hROOT, const char* path, BOOL follow_links = FALSE)
126 : {
127 : HNDLE hKey;
128 :
129 7 : printf("delete(%s)\n", path);
130 :
131 7 : int status = db_find_link(hDB, hROOT, path, &hKey);
132 :
133 7 : if (status == DB_NO_KEY)
134 0 : return true;
135 :
136 7 : if (status != DB_SUCCESS)
137 0 : return report_fail(msprintf("test_delete: Cannot delete \"%s\", db_find_link() status %d\n", path, status));
138 :
139 7 : status = db_delete_key(hDB, hKey, follow_links);
140 :
141 7 : if (status == DB_SUCCESS)
142 7 : return true;
143 :
144 0 : return report_fail(msprintf("test_delete: Delete \"%s\" failed, status %d\n", path, status));
145 : }
146 :
147 14 : bool test_find_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
148 : {
149 : HNDLE hKey;
150 :
151 14 : printf("find_fail(%s)\n", path);
152 :
153 14 : int status = db_find_link(hDB, hROOT, path, &hKey);
154 :
155 14 : if (status == fail_status)
156 14 : return true;
157 :
158 0 : return report_fail(msprintf("test_find_fail: Unexpected db_find_link(%s) status %d instead of %d\n", path, status, fail_status));
159 : }
160 :
161 1 : bool test_find_key_fail(HNDLE hDB, HNDLE hROOT, const char* path, int fail_status)
162 : {
163 : HNDLE hKey;
164 :
165 1 : printf("find_key_fail(%s)\n", path);
166 :
167 1 : int status = db_find_key(hDB, hROOT, path, &hKey);
168 :
169 1 : if (status == fail_status)
170 1 : return true;
171 :
172 0 : return report_fail(msprintf("test_find_key_fail: Unexpected db_find_key(%s) status %d instead of %d\n", path, status, fail_status));
173 : }
174 :
175 8 : bool test_symlink(HNDLE hDB, HNDLE hROOT, const char* path, const char* link_target)
176 : {
177 8 : printf("symlink(%s) to \"%s\"\n", path, link_target);
178 :
179 8 : int status = db_create_link(hDB, hROOT, path, link_target);
180 :
181 8 : if (status == DB_SUCCESS)
182 8 : return true;
183 :
184 0 : return report_fail(msprintf("test_symlink: db_create_link(%s) status %d\n", path, status));
185 : }
186 :
187 3 : bool test_symlink_fail(HNDLE hDB, HNDLE hROOT, const char* path, const char* link_target, int fail_status)
188 : {
189 3 : printf("symlink_fail(%s) to \"%s\"\n", path, link_target);
190 :
191 3 : int status = db_create_link(hDB, hROOT, path, link_target);
192 :
193 3 : if (status == fail_status)
194 3 : return true;
195 :
196 0 : return report_fail(msprintf("test_symlink_fail: db_create_link(%s) status %d instead of %d\n", path, status, fail_status));
197 : }
198 :
199 1 : bool test_all(HNDLE hDB)
200 : {
201 1 : bool ok = true;
202 :
203 1 : ok &= rmdir(hDB, 0, "test_odb");
204 :
205 1 : if (!ok)
206 0 : return false;
207 :
208 1 : ok &= test_mkdir(hDB, 0, "test_odb");
209 :
210 1 : if (!ok)
211 0 : return false;
212 :
213 1 : HNDLE hROOT = 0;
214 :
215 1 : ok &= test_chdir(hDB, 0, "test_odb", &hROOT);
216 :
217 1 : if (!ok)
218 0 : return false;
219 :
220 1 : ok &= test_find_fail(hDB, hROOT, "does-not-exist", DB_NO_KEY);
221 1 : ok &= test_find_fail(hDB, hROOT, "/does-not-exist", DB_NO_KEY);
222 :
223 1 : ok &= test_find_fail(hDB, 0, "/System", DB_SUCCESS); // exists at top level
224 1 : ok &= test_find_fail(hDB, hROOT, "/System", DB_NO_KEY); // but not in our subdirectory
225 :
226 1 : ok &= test_mkdir(hDB, hROOT, "test_mkdir");
227 1 : ok &= test_mkdir_fail(hDB, hROOT, "/test_mkdir", DB_KEY_EXIST); // unexpected, leading slash is ignored?
228 :
229 : // test db_create_key()
230 :
231 1 : ok &= test_mkdir_fail(hDB, hROOT, "invalid_name[", DB_INVALID_NAME);
232 1 : ok &= test_mkdir_fail(hDB, hROOT, "invalid_name]", DB_INVALID_NAME);
233 1 : ok &= test_mkdir_fail(hDB, hROOT, "", DB_INVALID_NAME);
234 1 : ok &= test_mkdir_fail(hDB, hROOT, "/", DB_INVALID_NAME);
235 1 : ok &= test_mkdir_fail(hDB, hROOT, "//", DB_INVALID_NAME);
236 1 : ok &= test_mkdir_fail(hDB, hROOT, "invalid//invalid", DB_INVALID_NAME);
237 1 : ok &= test_mkdir_fail(hDB, hROOT, NULL, DB_INVALID_NAME);
238 :
239 : // test db_delete_key()
240 :
241 1 : ok &= test_mkdir(hDB, hROOT, "test_delete");
242 1 : ok &= test_delete(hDB, hROOT, "test_delete");
243 :
244 1 : ok &= test_mkdir(hDB, hROOT, "test_dir/test/test/test_mkdir"); // multi-level create and delete
245 1 : ok &= test_delete(hDB, hROOT, "test_dir");
246 :
247 : // test symlinks
248 :
249 1 : ok &= test_symlink_fail(hDB, hROOT, "test_symlink_fail", "/does-not-exist", DB_NO_KEY);
250 1 : ok &= test_symlink_fail(hDB, hROOT, "test_symlink_fail", "no-leading-slash", DB_INVALID_NAME);
251 1 : ok &= test_symlink_fail(hDB, hROOT, "test_symlink", "/test_odb/test_mkdir", DB_NO_KEY); // unexpected
252 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_symlink", "/test_odb/test_mkdir");
253 :
254 : //
255 : // test db_delete_key with follow_links set to true
256 : //
257 :
258 : // delete(false) deletes the symlink,
259 : // delete(true) deletes the link target, and the link
260 :
261 1 : ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_target");
262 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_symlink1", "/test_odb/test_symlink_target");
263 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_symlink2", "/test_odb/test_symlink_target");
264 1 : ok &= test_delete(hDB, hROOT, "test_symlink1"); // delete symlink
265 1 : ok &= test_delete(hDB, hROOT, "test_symlink2", true); // delete symlink target
266 1 : ok &= test_find_fail(hDB, hROOT, "test_symlink_target", DB_NO_KEY); // deleted
267 1 : ok &= test_find_fail(hDB, hROOT, "test_symlink1", DB_NO_KEY); // deleted
268 1 : ok &= test_find_fail(hDB, hROOT, "test_symlink2", DB_NO_KEY); // dangling link
269 :
270 : // chained links, target of last link is deleted, intermediate link remain (dangling)
271 :
272 1 : ok &= test_mkdir(hDB, 0, "/test_odb/test_link_to_link");
273 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_link_to_link1", "/test_odb/test_link_to_link");
274 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_link_to_link2", "/test_odb/test_link_to_link1");
275 1 : ok &= test_delete(hDB, hROOT, "test_link_to_link2", true); // delete symlink target
276 1 : ok &= test_find_fail(hDB, hROOT, "test_link_to_link", DB_NO_KEY); // deleted
277 1 : ok &= test_find_fail(hDB, hROOT, "test_link_to_link1", DB_SUCCESS); // unexpectedly not deleted
278 1 : ok &= test_find_fail(hDB, hROOT, "test_link_to_link2", DB_NO_KEY); // deleted
279 1 : ok &= test_find_key_fail(hDB, hROOT, "test_link_to_link1", DB_INVALID_LINK); // dangling link
280 :
281 : // subdirectory has a link to something else, it is also deleted
282 :
283 1 : ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_dir1");
284 1 : ok &= test_mkdir(hDB, 0, "/test_odb/test_symlink_dir2");
285 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_symlink_dir1/test_symlink", "/test_odb/test_symlink_dir2");
286 1 : ok &= test_delete(hDB, hROOT, "test_symlink_dir1", true); // delete symlink target
287 1 : ok &= test_find_fail(hDB, hROOT, "test_symlink_dir1", DB_NO_KEY); // deleted
288 1 : ok &= test_find_fail(hDB, hROOT, "test_symlink_dir2", DB_NO_KEY); // deleted
289 :
290 : // test delete of circular links
291 :
292 1 : ok &= test_mkdir(hDB, 0, "/test_odb/test_circular_link");
293 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_circular_link/test_link", "/test_odb/test_circular_link");
294 1 : ok &= test_symlink(hDB, 0, "/test_odb/test_circular_link1", "/test_odb/test_circular_link");
295 1 : ok &= test_delete(hDB, hROOT, "test_circular_link1", true); // delete symlink target
296 1 : ok &= test_find_fail(hDB, hROOT, "test_circular_link", DB_NO_KEY); // deleted
297 1 : ok &= test_find_fail(hDB, hROOT, "test_circular_link1", DB_NO_KEY); // deleted
298 :
299 1 : return ok;
300 : }
301 :
302 : // Main function call
303 :
304 1 : int main(int argc, char *argv[])
305 : {
306 1 : setbuf(stdout,NULL);
307 1 : setbuf(stderr,NULL);
308 :
309 1 : signal(SIGILL, SIG_DFL);
310 1 : signal(SIGBUS, SIG_DFL);
311 1 : signal(SIGSEGV, SIG_DFL);
312 1 : signal(SIGPIPE, SIG_DFL);
313 :
314 1 : const char* hostname = NULL;
315 1 : const char* exptname = NULL;
316 :
317 : //MVOdb* odb = NULL;
318 : //MVOdbError odberror;
319 :
320 1 : TMFE* mfe = TMFE::Instance();
321 :
322 1 : TMFeResult r;
323 :
324 1 : r = mfe->Connect("test_odb", hostname, exptname);
325 1 : if (r.error_flag) {
326 0 : fprintf(stderr,"Cannot connect to MIDAS, error %s\n", r.error_message.c_str());
327 0 : return -1;
328 : }
329 :
330 1 : HNDLE hDB = mfe->fDB;
331 :
332 1 : bool ok = true;
333 :
334 1 : printf("\n");
335 1 : printf("Starting tests:\n");
336 1 : printf("\n");
337 :
338 1 : ok &= test_all(hDB);
339 :
340 : #if 0
341 : int runno = 1234;
342 :
343 : odb->RI("runinfo/run number", &runno);
344 :
345 : printf("read run number (RI): %d\n", runno);
346 :
347 : MVOdb* test = odb->Chdir("test_mvodb", true);
348 :
349 : assert(test != NULL);
350 :
351 : test->SetPrintError(true);
352 :
353 : bool isreadonly = test->IsReadOnly();
354 :
355 : if (isreadonly) {
356 : printf("\n");
357 : printf("This ODB is read-only!\n");
358 : }
359 :
360 : printf("\n");
361 : printf("Test create-and-read of all data types (set default values):\n");
362 : printf("\n");
363 :
364 : int cycle = 1;
365 : test->RI("cycle", &cycle, true);
366 : printf("RI() cycle: %d\n", cycle);
367 :
368 : int ivalue = 1;
369 : test->RI("int", &ivalue, true);
370 : printf("RI() int: %d\n", ivalue);
371 :
372 : float fvalue = 2.2;
373 : test->RF("float", &fvalue, true);
374 : printf("RF() float: %f\n", fvalue);
375 :
376 : double dvalue = 3.3;
377 : test->RD("double", &dvalue, true);
378 : printf("RD() double: %f\n", dvalue);
379 :
380 : bool bvalue_a = false;
381 : test->RB("bool_a", &bvalue_a, true);
382 : printf("bool_a: %d\n", bvalue_a);
383 :
384 : bool bvalue_b = true;
385 : test->RB("bool_b", &bvalue_b, true);
386 : printf("bool_b: %d\n", bvalue_b);
387 :
388 : uint16_t u16value = 0xabcd;
389 : test->RU16("u16", &u16value, true);
390 : printf("RU16() u16: 0x%04x\n", u16value);
391 :
392 : uint32_t u32value = 0x12345678;
393 : test->RU32("u32", &u32value, true);
394 : printf("RU32() u32: 0x%08x\n", u32value);
395 :
396 : std::string svalue = "test string";
397 : test->RS("string", &svalue, true);
398 : printf("RS() string: \"%s\"\n", svalue.c_str());
399 :
400 : printf("\n");
401 : printf("Test write of all data types (overwrite default values):\n");
402 : printf("\n");
403 :
404 : test->WI("cycle", cycle+1);
405 :
406 : int wi = 10 + 100*cycle;
407 : test->WI("int", wi);
408 : float wf = 11.1 + 100*cycle;
409 : test->WF("float", wf);
410 : double wd = 22.2 + 100*cycle;
411 : test->WD("double", wd);
412 : bool wba = true;
413 : test->WB("bool_a", wba);
414 : bool wbb = false;
415 : test->WB("bool_b", wbb);
416 : uint16_t wu16 = 0xcdef;
417 : test->WU16("u16", wu16);
418 : uint32_t wu32 = 0xdeadf00d;
419 : test->WU32("u32", wu32);
420 : std::string ws = "write test string cycle " + toString(cycle);
421 : test->WS("string", ws.c_str());
422 :
423 : printf("\n");
424 : printf("Test read of new values for all data types:\n");
425 : printf("\n");
426 :
427 : int ri = 1;
428 : test->RI("int", &ri);
429 : printf("int: %d -> %d -> %d\n", ivalue, wi, ri);
430 :
431 : float rf = 2.2;
432 : test->RF("float", &rf);
433 : printf("float: %f -> %f -> %f\n", fvalue, wf, rf);
434 :
435 : double rd = 3.3;
436 : test->RD("double", &rd);
437 : printf("double: %f -> %f -> %f\n", dvalue, wd, rd);
438 :
439 : bool rba = false;
440 : test->RB("bool_a", &rba);
441 : printf("bool_a: %d -> %d -> %d\n", bvalue_a, wba, rba);
442 :
443 : bool rbb = false;
444 : test->RB("bool_b", &rbb);
445 : printf("bool_b: %d -> %d -> %d\n", bvalue_b, wbb, rbb);
446 :
447 : uint16_t ru16 = 0x4444;
448 : test->RU16("u16", &ru16);
449 : printf("u16: 0x%04x -> 0x%04x -> 0x%04x\n", u16value, wu16, ru16);
450 :
451 : uint32_t ru32 = 0x55555555;
452 : test->RU32("u32", &ru32);
453 : printf("u32: 0x%08x -> 0x%08x -> 0x%08x\n", u32value, wu32, ru32);
454 :
455 : std::string rs = "rs";
456 : test->RS("string", &rs);
457 : printf("string: \"%s\" -> \"%s\" -> \"%s\"\n", svalue.c_str(), ws.c_str(), rs.c_str());
458 :
459 : printf("\n");
460 : printf("Test read arrays of all data types:\n");
461 : printf("\n");
462 :
463 : {
464 : printf("read int array ia:\n");
465 : std::vector<int> ia;
466 : ia.push_back(1);
467 : ia.push_back(2);
468 : ia.push_back(3);
469 : test->RIA("ia", &ia, true);
470 : printf("RIA() returned: ");
471 : print_ia(ia);
472 : printf("\n");
473 : }
474 :
475 : {
476 : printf("read non-existant array ia-noexist:\n");
477 : std::vector<int> ia;
478 : ia.push_back(1);
479 : ia.push_back(2);
480 : ia.push_back(3);
481 : test->RIA("ia-noexist", &ia);
482 : printf("RIA() returned: ");
483 : print_ia(ia);
484 : printf("\n");
485 : }
486 :
487 : {
488 : // create 10 element array, init to zero (ia is empty)
489 : std::vector<int> ia;
490 : test->RIA("ia10zero", &ia, true, 10);
491 : // create 10 element array, init from ia
492 : ia.clear();
493 : ia.push_back(11);
494 : ia.push_back(22);
495 : test->RIA("ia10", &ia, true, 10);
496 : }
497 :
498 : {
499 : // create 10 element array, init to zero (passed NULL instead of &ia)
500 : test->RIA("createia10", NULL, true, 10);
501 : }
502 :
503 : {
504 : std::vector<uint32_t> u32a;
505 : u32a.push_back(0x11110000);
506 : u32a.push_back(0x22220000);
507 : u32a.push_back(0x33330000);
508 : u32a.push_back(0x44440000);
509 : test->RU32A("dwa", &u32a, true);
510 : }
511 :
512 : {
513 : std::vector<double> da;
514 : da.push_back(1.1);
515 : da.push_back(1.2);
516 : da.push_back(1.3);
517 : da.push_back(1.4);
518 : test->RDA("da", &da, true, 0);
519 : }
520 :
521 : {
522 : std::vector<float> fa;
523 : fa.push_back(2.1);
524 : fa.push_back(2.2);
525 : fa.push_back(2.3);
526 : fa.push_back(20.3);
527 : fa.push_back(21.3);
528 : test->RFA("fa", &fa, true, 0);
529 : }
530 :
531 : {
532 : std::vector<bool> ba;
533 : ba.push_back(true);
534 : ba.push_back(false);
535 : ba.push_back(true);
536 : test->RBA("ba", &ba, true, 0);
537 : }
538 :
539 : {
540 : std::vector<std::string> sa;
541 : sa.push_back("line1");
542 : sa.push_back("line2");
543 : sa.push_back("line3");
544 : sa.push_back("line4");
545 : test->RSA("sa", &sa, true, 0, 32);
546 :
547 : // create 10 element array, init from ia
548 : sa.clear();
549 : sa.push_back("xx1");
550 : sa.push_back("xx2");
551 : test->RSA("sa10", &sa, true, 10, 32);
552 : // create 10 element array, init to zero (passed NULL instead of &sa)
553 : test->RSA("createsa10", NULL, true, 10, 32);
554 : }
555 :
556 : printf("\n");
557 : printf("Test string sizes:\n");
558 : printf("\n");
559 :
560 : {
561 : test->WS("test_size", "12345");
562 : std::string s;
563 : test->RS("test_size", &s);
564 : printf("read test_size [%s]\n", s.c_str());
565 : }
566 : {
567 : test->WS("test_size_32", "1234567890", 32);
568 : std::string s;
569 : test->RS("test_size_32", &s);
570 : printf("read test_size_32 [%s]\n", s.c_str());
571 : }
572 : {
573 : test->WS("test_size_5", "1234567890", 5);
574 : std::string s;
575 : test->RS("test_size_5", &s);
576 : printf("read test_size_5 [%s]\n", s.c_str());
577 : }
578 : {
579 : std::string s = "1234567890";
580 : test->RS("test_rsize", &s, true);
581 : printf("read test_rsize [%s]\n", s.c_str());
582 : }
583 : {
584 : std::string s = "123456";
585 : test->RS("test_size_r32", &s, true, 32);
586 : printf("read test_size_r32 [%s]\n", s.c_str());
587 : }
588 : {
589 : std::string s = "1234567890";
590 : test->RS("test_size_r8", &s, true, 8);
591 : printf("read test_size_r8 [%s]\n", s.c_str());
592 : }
593 : {
594 : test->RSA("test_size_a8", NULL, true, 2, 8);
595 : std::string s = "1234567890";
596 : test->WSAI("test_size_a8", 0, s.c_str());
597 : test->RSAI("test_size_a8", 0, &s);
598 : printf("read test_size_a8 [%s]\n", s.c_str());
599 : }
600 :
601 : printf("\n");
602 : printf("Test creating and resizing arrays:\n");
603 : printf("\n");
604 :
605 : {
606 : test->RIA("create_ia15", NULL, true, 15);
607 : test->RBA("create_ba12", NULL, true, 12);
608 : test->RSA("create_sa5", NULL, true, 5, 32);
609 : }
610 : {
611 : test->RIA("resize_ia10", NULL, true, 5);
612 : test->RIA("resize_ia10", NULL, true, 10);
613 : test->RSA("resize_sa3", NULL, true, 5, 32);
614 : test->WSAI("resize_sa3", 0, "00000000");
615 : test->WSAI("resize_sa3", 1, "11111111");
616 : test->WSAI("resize_sa3", 2, "22222222");
617 : test->WSAI("resize_sa3", 3, "33333333");
618 : test->WSAI("resize_sa3", 4, "44444444");
619 : test->RSA("resize_sa3", NULL, true, 3, 5);
620 : }
621 :
622 : printf("\n");
623 : printf("Test array index access:\n");
624 : printf("\n");
625 :
626 : {
627 : std::vector<int> ia;
628 : ia.push_back(10);
629 : ia.push_back(11);
630 : ia.push_back(12);
631 : test->WIA("index_ia", ia);
632 : for (int i=0; i<5; i++) {
633 : int ivalue = 999;
634 : test->RIAI("index_ia", i, &ivalue);
635 : printf("index %d value %d\n", i, ivalue);
636 : }
637 : for (int i=0; i<4; i++) {
638 : int ivalue = 20+i;
639 : test->WIAI("index_ia", i, ivalue);
640 : }
641 : for (int i=0; i<5; i++) {
642 : int ivalue = 999;
643 : test->RIAI("index_ia", i, &ivalue);
644 : printf("index %d value %d\n", i, ivalue);
645 : }
646 : }
647 :
648 : printf("\n");
649 : printf("Test string array index access:\n");
650 : printf("\n");
651 :
652 : {
653 : std::vector<std::string> sa;
654 : sa.push_back("sa0");
655 : sa.push_back("sa1");
656 : sa.push_back("sa2");
657 : test->WSA("index_sa", sa, 32);
658 : for (int i=0; i<5; i++) {
659 : std::string s = "aaa";
660 : test->RSAI("index_sa", i, &s);
661 : printf("index %d value [%s]\n", i, s.c_str());
662 : }
663 : for (int i=0; i<4; i++) {
664 : std::string s = "sa_qqq";
665 : s += toString(i);
666 : test->WSAI("index_sa", i, s.c_str());
667 : }
668 : for (int i=0; i<5; i++) {
669 : std::string s = "bbb";
670 : test->RSAI("index_sa", i, &s);
671 : printf("index %d value [%s]\n", i, s.c_str());
672 : }
673 : }
674 :
675 : printf("\n");
676 : printf("Test string truncation:\n");
677 : printf("\n");
678 :
679 : {
680 : std::vector<std::string> sa;
681 : sa.push_back("1234567890");
682 : sa.push_back("aaa1");
683 : sa.push_back("aaa2");
684 : test->WSA("trunc_sa5", sa, 5);
685 : test->WSAI("trunc_sa5", 1, "1234567890");
686 : for (int i=0; i<5; i++) {
687 : std::string s = "bbb";
688 : test->RSAI("trunc_sa5", i, &s);
689 : printf("index %d value [%s]\n", i, s.c_str());
690 : }
691 : }
692 :
693 : printf("\n");
694 : printf("Test against subdirectory:\n");
695 : printf("\n");
696 :
697 : {
698 : printf("Creating subdir\n");
699 : MVOdb* subdir = test->Chdir("subdir", true);
700 : printf("Creating subdir/i\n");
701 : subdir->WI("i", 10); // write something into subdir
702 : int i = 1111;
703 : printf("RI(subdir)\n");
704 : test->RI("subdir", &i); // invalid read from a non-int
705 : printf("WI(subdir)\n");
706 : test->WI("subdir", 10); // invalid write into existing subdir
707 : std::vector<int> ia;
708 : ia.push_back(111);
709 : ia.push_back(222);
710 : ia.push_back(333);
711 : printf("WIA(subdir)\n");
712 : test->WIA("subdir", ia); // invalid write into existing subdir
713 : printf("RIA(subdir)\n");
714 : test->RIA("subdir", &ia); // invalid read from non-int-array
715 : printf("RIA() returned: ");
716 : print_ia(ia);
717 : printf("\n");
718 : printf("RIA(subdir, create=true)\n");
719 : test->RIA("subdir", &ia, true); // invalid read from non-int-array
720 : printf("RIA() returned: ");
721 : print_ia(ia);
722 : printf("\n");
723 : }
724 :
725 : printf("\n");
726 : printf("Test special cases:\n");
727 : printf("\n");
728 :
729 : {
730 : printf("test RI() of non existant variable:\n");
731 : int ivalue = 999;
732 : test->RI("nonexistant", &ivalue);
733 : printf("RI() returned ivalue %d\n", ivalue);
734 : printf("\n");
735 :
736 : {
737 : std::vector<double> da;
738 : printf("test RDA() of integer array:\n");
739 : // wrong data type: ODB is INT, we ask for DOUBLE
740 : test->RDA("ia10", &da, false, 0);
741 : printf("RDA() returned: ");
742 : print_da(da);
743 : printf("\n");
744 : }
745 :
746 : {
747 : printf("test RD() of integer array:\n");
748 : // wrong data type: ODB is INT, we ask for DOUBLE
749 : double v = 999.9;
750 : test->RD("ia10", &v);
751 : printf("RD() returned %f\n", v);
752 : printf("\n");
753 : }
754 :
755 : {
756 : printf("test RI() of array ia10:\n");
757 : int ivalue = 999;
758 : test->RI("ia10", &ivalue);
759 : printf("RI() returned ivalue %d\n", ivalue);
760 : printf("\n");
761 : }
762 :
763 : {
764 : printf("test RIA() of non-array int:\n");
765 : std::vector<int> ia;
766 : test->RIA("int", &ia);
767 : printf("RIA() returned: ");
768 : print_ia(ia);
769 : printf("\n");
770 : }
771 :
772 : {
773 : printf("test index 0 of non-array int:\n");
774 : int ivalue = 999;
775 : test->RIAI("int", 0, &ivalue);
776 : printf("RIAI() returned ivalue %d\n", ivalue);
777 : printf("\n");
778 : }
779 :
780 : {
781 : printf("test index of non-array int:\n");
782 : int ivalue = 999;
783 : test->RIAI("int", 10, &ivalue);
784 : printf("RIAI() returned ivalue %d\n", ivalue);
785 : printf("\n");
786 : }
787 :
788 : {
789 : printf("test invalid index -1 of array ia10:\n");
790 : int ivalue = 999;
791 : test->RIAI("ia10", -1, &ivalue);
792 : printf("RIAI() returned ivalue %d\n", ivalue);
793 : printf("\n");
794 : }
795 :
796 : {
797 : printf("test invalid index 999 of array ia10:\n");
798 : int ivalue = 999;
799 : test->RIAI("ia10", 999, &ivalue);
800 : printf("RIAI() returned ivalue %d\n", ivalue);
801 : printf("\n");
802 : }
803 :
804 : printf("test string array invalid index -1:\n");
805 : svalue = "aaa";
806 : test->RSAI("sa10", -1, &svalue);
807 : printf("RSAI() returned [%s]\n", svalue.c_str());
808 : printf("\n");
809 :
810 : printf("test string array invalid index 999:\n");
811 : svalue = "aaa";
812 : test->RSAI("sa10", 999, &svalue);
813 : printf("RSAI() returned [%s]\n", svalue.c_str());
814 : printf("\n");
815 :
816 : printf("test write invalid index -1:\n");
817 : test->WIAI("ia10", -1, 10);
818 : printf("\n");
819 :
820 : printf("test string write invalid index -1:\n");
821 : test->WSAI("sa10", -1, "aaa");
822 : printf("\n");
823 :
824 : printf("\n");
825 : printf("Test Chdir():\n");
826 : printf("\n");
827 :
828 : {
829 : printf("test Chdir(true):\n");
830 : MVOdb* dir = test->Chdir("test_subdir", true);
831 : //printf("Chdir() returned %p\n", dir);
832 : if (dir == NULL) report_fail("Chdir(new directory, true)");
833 : }
834 :
835 : {
836 : printf("test Chdir(false):\n");
837 : MVOdb* dir = test->Chdir("non-existant-subdir", false);
838 : printf("Chdir() returned %p\n", dir);
839 : if (dir != NULL) report_fail("Chdir(new directory, false)");
840 : }
841 :
842 : {
843 : printf("test Chdir(\"not a dir\", true):\n");
844 : MVOdb* dir = test->Chdir("ia10", true);
845 : //printf("Chdir() returned %p\n", dir);
846 : if (dir == NULL) report_fail("Chdir(not a directory, true)");
847 : }
848 :
849 : {
850 : printf("test Chdir(\"not a dir\", false):\n");
851 : MVOdb* dir = test->Chdir("ia10", false);
852 : printf(" returned %p\n", dir);
853 : if (dir != NULL) report_fail("Chdir(not a directory, false)");
854 : }
855 : }
856 : #endif
857 :
858 1 : if (mfe)
859 1 : mfe->Disconnect();
860 :
861 1 : if (ok) {
862 1 : printf("\n");
863 1 : printf("Tests passed OK\n");
864 1 : printf("\n");
865 : } else {
866 0 : printf("\n");
867 0 : printf("Number of FAILED tests: %d\n", gCountFail);
868 0 : printf("\n");
869 : }
870 :
871 1 : return 0;
872 1 : }
873 :
874 : //end
875 : /* emacs
876 : * Local Variables:
877 : * tab-width: 8
878 : * c-basic-offset: 3
879 : * indent-tabs-mode: nil
880 : * End:
881 : */
|