41 #include "rnr/rnrconfig.h" 74 "Unit test librnr_appkit LogBook class.",
77 "The %P command unit tests the librnr_appkit LogBook operation.",
104 Argv0 = basename(argv[0]);
110 static string HdrSep(
"-------------------------------------------------------");
112 static void printTestHdr(
const string strSynopsis)
114 cout << endl << HdrSep << endl;
115 cout << strSynopsis << endl;
116 cout << HdrSep << endl;
119 static void captainsLogMuddsWomen(
LogBook &log)
121 log.
setName(
"Captain's Log, USS Enterprise - Mudd's Women");
123 log <<
bookmark(
"Captain's Log, Stardate 1328.8.")
124 <<
"The USS Enterprise in pursuit of an unidentified vessel." 127 log <<
bookmark(
"Captain's Log, Stardate 1329.1.")
129 "We've taken aboard from unregistered transport vessel, its captain,\n" 130 "and three unusual females. These women have a mysterious magnetic effect on\n" 131 "the male members of my crew. Including myself. Explanation unknown at present." 134 log <<
bookmark(
"Captain's Log, Stardate 1329.2.")
136 "On board the USS Enterprise, a ship's hearing has been convened against the\n" 137 "transport vessel's captain. I'm becoming concerned about the almost hypnotic\n" 138 "effect produced by the women." 141 log <<
bookmark(
"Captain's Log, Stardate 1330.1.")
143 "Position, 14 hours out of Rigel XII. We're on auxiliary impulse engines.\n" 144 "Fuel low, barely sufficient to achieve orbit over the planet. Lithium\n" 145 "replacements are now imperative. The effect of Mudd's women on my crew\n" 146 "continues to grow, still totally unexplained. Harry Mudd is confined to his\n" 147 "quarters under guard." 150 log <<
bookmark(
"Captain's Log, Supplemental 1.")
152 "Transporting down to surface of planet Rigel XII to acquire replacement\n" 153 "lithium crystals. Expect further difficulty from miners." 156 log <<
bookmark(
"Captain's Log, Supplemental 2.")
158 "I've transported aboard the Enterprise to implement search with infrared\n" 159 "scanners and sensing system. Magnetic storms on the planet's surface are\n" 160 "cutting down speed and efficiency of our equipment. Search now in progress\n" 161 "for three hours, 18 minutes." 164 log <<
bookmark(
"Captain's Log, Supplemental 3.")
166 "Have expended all but 43 minutes of power. Ship's condition: critical.\n" 167 "Search now in progress, 7 hours, 31 minutes. Magnetic storms are easing." 171 static void captainsLogManTrap(
LogBook &log)
173 log.
setName(
"Captain's Log, USS Enterprise - The Man Trap");
175 log <<
bookmark(
"Captain's Log, Stardate 1513.1.")
177 "Our position, orbiting planet M-113. On board the Enterprise, Mr. Spock,\n" 178 "temporarily in command. On the planet, the ruins of an ancient and long dead\n" 179 "civilization. Ship's surgeon McCoy and myself are now beaming down to the\n" 180 "planet's surface. Our mission, routine medical examination of archaeologist\n" 181 "Robert Crater, and his wife, Nancy. Routine, but for the fact that Nancy\n" 182 "Crater is that one woman in Dr. McCoy's past." 185 log <<
bookmark(
"Captain's Log, additional entry 1.")
187 "Since our mission was routine, we had beamed down to the planet without\n" 188 "suspicion. We were totally unaware that each member of the landing party was\n" 189 "seeing a different woman. A different Nancy Crater." 192 log <<
bookmark(
"Captain's Log, Stardate 1513.4.")
194 "In orbit around planet M-113. One crewman, member of the landing party, dead\n" 195 "by violence. Cause, unknown. We are certain the cause of death was not poison." 198 log <<
bookmark(
"Captain's Log, Stardate 1513.8.")
200 "I am now certain that the violent death of my crewman was caused by some\n" 204 log <<
bookmark(
"Captain's Log, additional 2.")
206 "Armed and able-bodied crewmen are not attacked and slaughtered this easily.\n" 207 "Apparently the killer can immobilize them as it approaches, perhaps with\n" 208 "some hypnotic or paralyzing power. The answer lies with Professor Crater." 211 log <<
bookmark(
"Captain's Log, continuing.")
213 "The Enterprise has been invaded by a creature capable of assuming any form,\n" 214 "and with the capacity to paralyze and draw the life from any one of us." 218 static void printMarks(
LogBook &log,
int whence)
222 cout <<
"+getBookMarks, whence = " << whence << endl;
225 for(
size_t i = 0; i < n; ++i)
227 cout << i <<
": \"" << list[i].m_strMark
228 <<
"\", " << list[i].m_index << endl;
232 static void testLogAttrs(
LogBook &log,
bool bIsTest =
true)
236 printTestHdr(
"Test LogBook Attributes");
239 cout <<
"Name: " << log.
getName() << endl;
240 cout <<
"max_size: " << log.
max_size() << endl;
241 cout <<
"size: " << log.
size() << endl;
243 cout <<
"flags: " <<
"0x" << hex << log.
getFlags() << dec << endl;
246 static void testLogEdits(
LogBook &log)
250 printTestHdr(
"Test LogBook Edits");
252 cout << endl <<
"++Current State" << endl;
256 cout <<
"+log.getflags 0x" << hex << log.
getFlags() << dec << endl;
260 cout <<
"+log attrs" << endl;
261 testLogAttrs(log,
false);
263 cout <<
"+cout << log" << endl;
269 cout << endl <<
"++Test " << tcnt++ << endl;
270 cout <<
"+create logMudd with defaults" << endl;
274 logMudd.
setFlags(LogBook::FlagOMark);
276 cout <<
"+insert entries" << endl;
277 captainsLogMuddsWomen(logMudd);
279 cout <<
"+logMudd attrs" << endl;
280 testLogAttrs(logMudd,
false);
282 cout <<
"+cout << logMudd" << endl;
288 cout << endl <<
"++Test " << tcnt++ << endl;
289 cout <<
"+create logTrap with 24 max entries" << endl;
291 LogBook logTrap(
"Temp Name", 24);
293 logTrap.
setFlags(LogBook::FlagOMark);
295 cout <<
"+insert entries" << endl;
296 captainsLogManTrap(logTrap);
298 cout <<
"+logTrap attrs" << endl;
299 testLogAttrs(logTrap,
false);
301 cout <<
"+cout << logTrap" << endl;
307 cout << endl <<
"++Test " << tcnt++ << endl;
308 cout <<
"+logTrap = logMudd" << endl;
311 cout <<
"+logTrap attrs" << endl;
312 testLogAttrs(logTrap,
false);
314 cout <<
"+cout << logTrap" << endl;
320 cout << endl <<
"++Test " << tcnt++ << endl;
321 cout <<
"+logTrap << logMudd" << endl;
324 cout <<
"+logTrap attrs" << endl;
325 testLogAttrs(logTrap,
false);
327 cout <<
"+cout << logTrap" << endl;
333 cout << endl <<
"++Test " << tcnt++ << endl;
334 cout <<
"+log.clear" << endl;
337 cout <<
"+log attrs" << endl;
338 testLogAttrs(log,
false);
340 cout <<
"+cout << log" << endl;
346 cout << endl <<
"++Test " << tcnt++ << endl;
347 cout <<
"+re-creating logTrap entries" << endl;
349 captainsLogManTrap(logTrap);
351 cout <<
"+log << logMudd << logTrap" << endl;
352 log << logMudd << logTrap;
354 cout <<
"+log attrs" << endl;
355 testLogAttrs(log,
false);
357 cout <<
"+cout << log" << endl;
363 cout << endl <<
"++Test " << tcnt++ << endl;
365 int whence = LogBook::OLDEST;
367 cout <<
"+log.eraseEntries(n=" << n <<
", whence=" << whence <<
")" << endl;
368 log.eraseEntries(n, whence);
370 cout <<
"+cout << log" << endl;
373 cout << endl <<
"++Test " << tcnt++ << endl;
374 whence = LogBook::NEWEST;
376 cout <<
"+log.eraseEntries(n=" << n <<
", whence=" << whence <<
")" << endl;
377 log.eraseEntries(n, whence);
379 cout <<
"+cout << log" << endl;
382 cout <<
"+log attrs" << endl;
383 testLogAttrs(log,
false);
388 cout << endl <<
"++Test " << tcnt++ << endl;
392 whence = LogBook::OLDEST;
393 n = log.getBookMarks(list, whence);
395 mark = list[1].m_strMark;
397 cout <<
"+log.eraseEntries(mark=\"" << mark <<
"\", whence=" 398 << whence <<
")" << endl;
399 log.eraseToMark(mark, whence);
401 cout <<
"+cout << log" << endl;
404 cout << endl <<
"++Test " << tcnt++ << endl;
405 whence = LogBook::NEWEST;
406 mark = list[list.size()-1].m_strMark;
408 cout <<
"+log.eraseEntries(mark=\"" << mark <<
"\", whence=" 409 << whence <<
")" << endl;
410 log.eraseToMark(mark, whence);
412 cout <<
"+cout << log" << endl;
415 cout <<
"+log attrs" << endl;
416 testLogAttrs(log,
false);
419 static void testLogOutput(
LogBook &log)
423 printTestHdr(
"Test LogBook Output");
427 cout <<
"+getflags 0x" << hex << log.
getFlags() << dec << endl;
434 cout << endl <<
"++Test " << tcnt++ << endl;
435 cout <<
"+operator<<(os, log), forward" << endl;
441 cout << endl <<
"++Test " << tcnt++ << endl;
442 log.
orFlags(LogBook::FlagORev);
443 cout <<
"+getflags 0x" << hex << log.getFlags() << dec << endl;
445 cout <<
"+operator<<(os, log), reverse" << endl;
449 int whence = LogBook::OLDEST;
451 printMarks(log, whence);
452 size_t n = log.getBookMarks(list, whence);
454 unsigned flagsFwd = LogBook::FlagONum | LogBook::FlagOMark;
455 unsigned flagsRev = flagsFwd | LogBook::FlagORev;
457 for(
size_t i = 0; i < n; ++i)
459 for(
int endpt = LogBook::OLDEST; endpt <= LogBook::NEWEST; ++endpt)
461 cout << endl <<
"++Test " << tcnt++ << endl;
462 cout <<
"+printToMark(mark=\"" << list[i].m_strMark <<
"\", endpt=" 463 << endpt <<
"), forward" 465 log.setFlags(flagsFwd);
466 log.printToMark(cout, list[i].m_strMark, endpt);
468 cout << endl <<
"++Test " << tcnt++ << endl;
469 cout <<
"+printToMark(mark=\"" << list[i].m_strMark <<
"\", endpt=" 470 << endpt <<
"), reverse" 472 log.setFlags(flagsRev);
473 log.printToMark(cout, list[i].m_strMark, endpt);
478 static void testLogFundamentals(
LogBook &log)
483 const bool vBool =
true;
487 unsigned short vUShort = 9;
491 unsigned long vULong = 153;
492 long long vLongLong = 0xffffffff;
493 unsigned long long vULongLong = 0x7fffffff;
494 float vFloat = -4.1414;
495 double vDouble = -9e-12;
496 long double vLongDouble = -9e-12;
498 void * vVoid = (
void *)vS;
499 strcpy(buf,
"hello");
500 const char * vConstS =
"good bye";
501 const string vConstStr(
"constant pain");
502 string vStr(
"pleasure");
506 int ecnt = 0, mcnt = 0;
510 printTestHdr(
"Test LogBook Fundamentals");
513 string mark0(
"Test Fundamentals (top)");
523 cout << endl <<
"++Test " << tcnt++ << endl;
527 log <<
bookmark(mark0) << li << what <<
": " 528 << 1 <<
" \"lonely number\" " << 0x13 <<
" endl" << endl
529 <<
"Second line of entry." <<
eoe;
531 cout <<
"+inserted bookmark " << mcnt++ <<
": \"" << mark0 <<
"\"" 532 <<
" and entry " << ecnt++ <<
": \"" << li << what <<
"...\"" << endl;
537 cout << endl <<
"++Test " << tcnt++ << endl;
541 log << li << what <<
": " << vBool <<
eoe;
543 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 549 cout << endl <<
"++Test " << tcnt++ << endl;
551 what =
"hex & dec int";
553 mark1 = li +
"only the lonely";
556 cout <<
"+inserted bookmark " << mcnt++ <<
": \"" << mark1 <<
"\"" << endl;
558 log << li << what <<
": " << hex << vInt << dec <<
" " << vInt <<
eoe;
560 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 564 printMarks(log, LogBook::OLDEST);
565 printMarks(log, LogBook::NEWEST);
570 cout << endl <<
"++Test " << tcnt++ << endl;
571 cout <<
"+textAt(mark), where mark is good, bad, good " << endl;
572 cout <<
"0: " << log.
textAt(mark0) << endl;
573 cout <<
"1: " << log.
textAt(
"bad mark") << endl;
574 cout <<
"2: " << log.
textAt(mark1) << endl;
579 cout << endl <<
"++Test " << tcnt++ << endl;
581 what =
"char & byte";
583 log << li << what <<
": " << vChar <<
" " << vByte <<
eoe;
585 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 591 cout << endl <<
"++Test " << tcnt++ << endl;
593 what =
"short & ushort";
595 log << li << what <<
": " << vShort <<
" " << vUShort <<
eoe;
597 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 603 cout << endl <<
"++Test " << tcnt++ << endl;
608 log <<
bookmark << li << what <<
": " << vInt <<
" " << vUInt <<
eoe;
610 cout <<
"+inserted bookmark " << mcnt++ <<
": \"" << mark <<
"\"" 611 <<
" and entry " << ecnt++ <<
": \"" << li << what <<
"...\"" << endl;
614 printMarks(log, LogBook::OLDEST);
617 log.
orFlags(LogBook::FlagOMark);
618 cout <<
"+getflags 0x" << hex << log.
getFlags() << dec << endl;
621 cout <<
"+operator<<(os, log)" << endl;
627 cout << endl <<
"++Test " << tcnt++ << endl;
629 what =
"long & ulong";
631 log << li << what <<
": " << vLong <<
" " << vULong <<
eoe;
633 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 639 cout << endl <<
"++Test " << tcnt++ << endl;
641 what =
"longlong & ulonglong";
642 mark =
"anon@longlong";
644 log <<
bookmark << li << what <<
": " 645 << vLongLong <<
"(0x" << hex << vLongLong << dec <<
") " 646 << vULongLong <<
"(0x" << hex << vULongLong << dec <<
") " 649 cout <<
"+inserted bookmark " << mcnt++ <<
": \"" << mark <<
"\"" 650 <<
" and entry " << ecnt++ <<
": \"" << li << what <<
"...\"" << endl;
655 cout << endl <<
"++Test " << tcnt++ << endl;
659 log << li << what <<
": " << vFloat <<
eoe;
661 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 665 printMarks(log, LogBook::OLDEST);
668 cout <<
"+operator<<(os, log)" << endl;
674 cout << endl <<
"++Test " << tcnt++ << endl;
677 mark2 = li +
"DoubleFun";
681 cout <<
"+inserted pending bookmark " << mcnt++
682 <<
": \"" << mark2 <<
"\"" << endl;
685 cout <<
"+operator<<(os, log)" << endl;
688 log << li << what <<
": " << vDouble <<
eoe;
690 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 694 cout <<
"+operator<<(os, log)" << endl;
700 cout << endl <<
"++Test " << tcnt++ << endl;
704 log << li << what <<
": " << vLongDouble <<
eoe;
706 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 710 cout <<
"+operator<<(os, log)" << endl;
716 cout << endl <<
"++Test " << tcnt++ << endl;
718 what =
"char* & const char*";
720 log << li << what <<
": " << vS <<
" " << vConstS <<
eoe;
722 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 728 cout << endl <<
"++Test " << tcnt++ << endl;
732 log << li << what <<
": " << vVoid <<
eoe;
734 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 740 cout << endl <<
"++Test " << tcnt++ << endl;
742 what =
"string & const string";
744 log << li << what <<
": " << vStr <<
" " << vConstStr <<
eoe;
746 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 752 cout << endl <<
"++Test " << tcnt++ << endl;
754 what =
"End of Fundamentals";
756 log << li << what <<
eoe;
758 cout <<
"+inserted entry " << ecnt++ <<
": \"" << li << what <<
"...\"" 762 cout <<
"+operator<<(os, log)" << endl;
774 int main(
int argc,
char* argv[])
776 string strName(
"Star Trek Log");
784 testLogFundamentals(log);
unsigned setFlags(const unsigned uFlags)
Set the current formatting flags.
static char * Argv0
the command
static void mainInit(int argc, char *argv[])
Main initialization.
void setName(const std::string &strName)
Set the name of the log book.
size_t getBookMarks(BookMarkList &list, int whence) const
Get a sorted list of bookmark labels.
size_t numOfTotalEver() const
Return the total number of entries ever added during log book's life.
unsigned orFlags(const unsigned uFlags)
Or new flags into the current formatting flags.
size_t size() const
Return the number of logged entries in the log book.
const std::string & getName() const
Get the name of the log book.
#define APP_EC_OK
success exit code
static OptsInfo_T OptsInfo[]
Command line options information.
LogBook & eoe(LogBook &log)
LogBook end-of-entry stream manipulator.
static const PkgInfo_T PkgInfo
unsigned getFlags() const
Get the current formatting flags.
const size_t max_size() const
Return the maximum number entries the log book can hold.
LogBook & bookmark(LogBook &log)
LogBook bookmark stream manipulator.
Package version information.
std::vector< BookMark > BookMarkList
bookmark list type
int main(int argc, char *argv[])
Main.
const std::string & textAt(const std::string &strMark) const
Get the entry text at the bookmark.
static OptsPgmInfo_T PgmInfo
Program information.