Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

testChild.cpp 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include "CodeDweller/child.hpp"
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Configuration ///////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /// Child program name.
  12. const std::string childName("./childProgram");
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // End of configuration ////////////////////////////////////////////////////////
  15. ////////////////////////////////////////////////////////////////////////////////
  16. int nTotalTests = 0;
  17. int nPass = 0;
  18. int nFail = 0;
  19. bool result;
  20. #define NO_EXCEPTION_TERM(msg) \
  21. std::cout \
  22. << msg << " failed to throw exception at line " \
  23. << __LINE__ << "." << std::endl
  24. #define EXCEPTION_TERM(msg) \
  25. std::cout \
  26. << msg << " threw unexpected exception: " << e.what() << std::endl
  27. #define RETURN_FALSE(msg) \
  28. std::cout \
  29. << msg << " at line " << __LINE__ << std::endl; \
  30. return false;
  31. #define RUN_TEST(test) \
  32. std::cout << " " #test ": "; \
  33. std::cout.flush(); \
  34. result = test(); \
  35. std::cout << (result ? "ok" : "fail") << std::endl; \
  36. nTotalTests++; \
  37. if (result) nPass++; else nFail++;
  38. #define SUMMARY \
  39. std::cout \
  40. << "\nPass: " << nPass \
  41. << ", Fail: " << nFail \
  42. << ", Total: " << nTotalTests << std::endl
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // ChildStream Tests ///////////////////////////////////////////////////////////
  45. ////////////////////////////////////////////////////////////////////////////////
  46. bool testChildStreamIsDone() {
  47. try {
  48. CodeDweller::ChildStream child;
  49. // Test exception if called out-of-order.
  50. try {
  51. child.isDone();
  52. NO_EXCEPTION_TERM("isDone() called without run()");
  53. return false;
  54. } catch (std::exception &e) {
  55. }
  56. // Start the child.
  57. child.open(childName);
  58. if (child.isDone()) {
  59. std::cout << "isDone() failure; returned true." << std::endl;
  60. return false;
  61. }
  62. // Command the child to exit.
  63. child << 'q';
  64. child.flush();
  65. // Sleep to let the child exit.
  66. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  67. if (!child.isDone()) {
  68. std::cout << "isDone() failure; returned false." << std::endl;
  69. return false;
  70. }
  71. } catch (std::exception &e) {
  72. EXCEPTION_TERM("isDone()");
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool testChildStreamIsRunning() {
  78. try {
  79. CodeDweller::ChildStream child;
  80. if (child.isRunning()) {
  81. std::cout << "isRunning() failure; returned true before starting."
  82. << std::endl;
  83. return false;
  84. }
  85. // Start the child.
  86. child.open(childName);
  87. if (!child.isRunning()) {
  88. std::cout << "isRunning() failure; returned false." << std::endl;
  89. return false;
  90. }
  91. // Command the child to exit.
  92. child << 'q';
  93. child.flush();
  94. // Sleep to let the child exit.
  95. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  96. child.close();
  97. if (child.isRunning()) {
  98. std::cout << "isRunning() failure; returned true after stopping."
  99. << std::endl;
  100. return false;
  101. }
  102. } catch (std::exception &e) {
  103. EXCEPTION_TERM("isRunning()");
  104. return false;
  105. }
  106. return true;
  107. }
  108. bool testChildStreamResult() {
  109. try {
  110. std::vector<std::string> cmd;
  111. cmd.push_back(childName);
  112. cmd.push_back("quit");
  113. CodeDweller::ChildStream child(cmd);
  114. // Test exception if called out-of-order.
  115. try {
  116. (void) child.result();
  117. NO_EXCEPTION_TERM(" result() called without run()");
  118. return false;
  119. } catch (std::exception &e) {
  120. }
  121. // Test exception if called while child is running.
  122. try {
  123. (void) child.result();
  124. NO_EXCEPTION_TERM(" result() called before child exited");
  125. return false;
  126. } catch (std::exception &e) {
  127. }
  128. // Wait for the child to exit.
  129. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  130. int32_t result = child.result();
  131. if (25 != result) {
  132. std::cout << "result() failure; returned " << result
  133. << " instead of 25." << std::endl;
  134. return false;
  135. }
  136. } catch (std::exception &e) {
  137. EXCEPTION_TERM("result()");
  138. return false;
  139. }
  140. return true;
  141. }
  142. bool testChildStreamClose() {
  143. // Test with no waiting.
  144. try {
  145. CodeDweller::ChildStream child(childName);
  146. child.close();
  147. } catch (std::exception &e) {
  148. EXCEPTION_TERM("close() with no waiting");
  149. return false;
  150. }
  151. // Test with waiting.
  152. try {
  153. CodeDweller::ChildStream child(childName);
  154. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  155. child.close();
  156. } catch (std::exception &e) {
  157. EXCEPTION_TERM("close() with 100 ms waiting");
  158. return false;
  159. }
  160. // Test after the child exits.
  161. std::vector<std::string> cmd;
  162. cmd.push_back(childName);
  163. cmd.push_back("quit");
  164. try {
  165. CodeDweller::ChildStream child(cmd);
  166. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  167. child.close();
  168. } catch (std::exception &e) {
  169. EXCEPTION_TERM("close() after child exits");
  170. return false;
  171. }
  172. // Test exception thrown for out-of-order calling.
  173. try {
  174. CodeDweller::ChildStream child;
  175. child.close();
  176. NO_EXCEPTION_TERM("close() called without run()");
  177. return false;
  178. } catch (std::exception &e) {
  179. }
  180. return true;
  181. }
  182. bool testChildStreamReaderWriter() {
  183. try {
  184. size_t bufSize = 15;
  185. CodeDweller::ChildStream child(bufSize);
  186. std::ostringstream childOutput;
  187. std::vector<std::string> expectedChildOutput;
  188. char readChar;
  189. // Generate input.
  190. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  191. int nChar = bufSize - 1;
  192. int nLines = 2;
  193. for (int iLine = 0; iLine < nLines; iLine++) {
  194. std::string line;
  195. line.erase();
  196. for (int iChar = 0; iChar < nChar; iChar++) {
  197. line.push_back(randomLetter[std::rand() % 26]);
  198. }
  199. expectedChildOutput.push_back(line);
  200. }
  201. // Test exception.
  202. try {
  203. child.exceptions(std::ostream::failbit | std::ostream::badbit);
  204. child << bufSize;
  205. child.flush();
  206. NO_EXCEPTION_TERM(" writer called without run()");
  207. return false;
  208. } catch (std::exception &e) {
  209. }
  210. // Clear the writer stream.
  211. try {
  212. child.clear();
  213. } catch (std::exception &e) {
  214. }
  215. child.open(childName);
  216. // Write, read, put back, and reread each character.
  217. for (std::string line : expectedChildOutput) {
  218. // Write one line.
  219. const char *ptr;
  220. ptr = line.data();
  221. for (std::string::size_type i = 0; i < line.length(); i++) {
  222. child << ptr[i];
  223. if (!child) {
  224. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  225. }
  226. }
  227. child.flush();
  228. if (!child) {
  229. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  230. }
  231. // Read one line.
  232. std::string readLine;
  233. readLine.erase();
  234. for (std::string::size_type i = 0; i < line.length(); i++) {
  235. child >> readChar;
  236. if (!child) {
  237. RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
  238. }
  239. readLine.push_back(readChar);
  240. }
  241. // Convert to upper case.
  242. std::string expectedLine;
  243. expectedLine = line;
  244. for (auto &c : expectedLine) {
  245. c = toupper(c);
  246. }
  247. // Compare.
  248. if (expectedLine != readLine) {
  249. std::cout << " Failure in testReaderWriter." << std::endl;
  250. std::cout << " Expected: '" << expectedLine
  251. << "'\n Received: '" << readLine << "'" << std::endl;
  252. return false;
  253. }
  254. }
  255. // Send exit message.
  256. child << 'q';
  257. child.flush();
  258. if (!child) {
  259. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  260. }
  261. // Verify exit.
  262. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  263. if (!child.isDone()) {
  264. std::cout << " Failure in testReaderWriter: "
  265. << "Child program did not exit." << std::endl;
  266. return false;
  267. }
  268. } catch (std::exception &e) {
  269. EXCEPTION_TERM("reader()/writer()");
  270. return false;
  271. }
  272. return true;
  273. }
  274. bool testChildStreamReader() {
  275. try {
  276. size_t bufSize = 32;
  277. CodeDweller::ChildStream child(bufSize);
  278. // Test exception.
  279. try {
  280. int temp;
  281. child.exceptions(std::istream::failbit | std::istream::badbit);
  282. child >> temp;
  283. NO_EXCEPTION_TERM(" reader called without run()");
  284. return false;
  285. } catch (std::exception &e) {
  286. }
  287. child.clear();
  288. child.clear();
  289. std::ostringstream childOutput;
  290. std::string expectedChildOutput("This is a test");
  291. char readChar;
  292. std::vector<std::string> cmd;
  293. cmd.push_back(childName);
  294. cmd.push_back("write");
  295. child.open(cmd);
  296. child.exceptions(std::istream::badbit);
  297. child >> std::noskipws;
  298. if (!child) {
  299. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  300. }
  301. while (child >> readChar) {
  302. if (!child) {
  303. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  304. }
  305. child.putback(readChar);
  306. if (!child) {
  307. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  308. }
  309. child >> readChar;
  310. if (!child) {
  311. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  312. }
  313. childOutput << readChar;
  314. }
  315. if (!child.eof()) {
  316. RETURN_FALSE(" Failure in testReader: Error occured before "
  317. "EOF was reached while reading");
  318. }
  319. // Check.
  320. if (childOutput.str() != expectedChildOutput) {
  321. std::cout << " reader() failure in testReader." << std::endl;
  322. std::cout << " Expected: '" << expectedChildOutput
  323. << "'\n Received: '" << childOutput.str() << "'"
  324. << std::endl;
  325. return false;
  326. }
  327. // Sleep to let the child exit.
  328. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  329. if (!child.isDone()) {
  330. std::cout << "first isDone() failure in testReader." << std::endl;
  331. return false;
  332. }
  333. if (!child.isDone()) {
  334. std::cout << "second isDone() failure in testReader." << std::endl;
  335. return false;
  336. }
  337. } catch (std::exception &e) {
  338. EXCEPTION_TERM("reader()");
  339. return false;
  340. }
  341. return true;
  342. }
  343. bool testChildStreamBinaryRead() {
  344. try {
  345. std::vector<std::string> cmd;
  346. cmd.push_back(childName);
  347. size_t bufSize = 164;
  348. CodeDweller::ChildStream child(bufSize);
  349. child.open(cmd);
  350. // Write.
  351. std::string childInput("abc");
  352. child << childInput;
  353. child.flush();
  354. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  355. // Read one character.
  356. char ch;
  357. child.read(&ch, 1);
  358. if (ch != 'A') {
  359. RETURN_FALSE(" reader.read() returned incorrect value");
  360. }
  361. // Read.
  362. char buf[bufSize * 2];
  363. child.read(buf, 2);
  364. buf[2] = '\0';
  365. std::string input(buf);
  366. if (input != "BC") {
  367. RETURN_FALSE(" reader.read() returned incorrect value");
  368. }
  369. // Fill input buffer.
  370. std::string output("abcdefghijklmnopprstuvwxyz");
  371. std::string expectedInput(output);
  372. for (int i = 0; i < output.size(); i++) {
  373. expectedInput[i] = std::toupper(output[i]);
  374. }
  375. child << output;
  376. child.flush();
  377. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  378. child.read(&ch, 1);
  379. int index = 0;
  380. if (ch != expectedInput[index++]) {
  381. RETURN_FALSE(" reader.read() returned incorrect value");
  382. }
  383. size_t nBytesRead = expectedInput.size() - 1;
  384. child.read(buf, nBytesRead);
  385. buf[nBytesRead] = '\0';
  386. if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
  387. RETURN_FALSE(" reader.read() failure");
  388. }
  389. // Send exit message.
  390. child << 'q';
  391. child.flush();
  392. if (!child) {
  393. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  394. }
  395. // Verify exit.
  396. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  397. if (!child.isDone()) {
  398. std::cout << " Failure in testNonblockingReader: "
  399. << "Child program did not exit." << std::endl;
  400. return false;
  401. }
  402. } catch (std::exception &e) {
  403. EXCEPTION_TERM("Binary read test");
  404. return false;
  405. }
  406. return true;
  407. }
  408. bool testChildStreamNonBlockingRead() {
  409. try {
  410. std::vector<std::string> cmd;
  411. cmd.push_back(childName);
  412. size_t bufSize = 16;
  413. CodeDweller::ChildStream child(cmd, bufSize);
  414. // Check for available input with no input.
  415. if (child.numBytesAvailable() != 0) {
  416. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  417. }
  418. // Check for available input with input.
  419. std::string childInput("abc");
  420. child << childInput;
  421. child.flush();
  422. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  423. // Check that input is available.
  424. if (child.numBytesAvailable() != 1) {
  425. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  426. }
  427. // Read one character.
  428. char ch;
  429. child.read(&ch, 1);
  430. if (ch != 'A') {
  431. RETURN_FALSE(" reader.read() returned incorrect value");
  432. }
  433. // Check that input is available.
  434. if (child.numBytesAvailable() != 2) {
  435. RETURN_FALSE(" numBytesAvailable() did not return expected 2");
  436. }
  437. // Read.
  438. char buf[bufSize * 2];
  439. child.read(buf, 2);
  440. buf[2] = '\0';
  441. std::string input(buf);
  442. if (input != "BC") {
  443. RETURN_FALSE(" reader.read() returned incorrect value");
  444. }
  445. // Check that no input is available.
  446. if (child.numBytesAvailable() != 0) {
  447. RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
  448. "after reading");
  449. }
  450. // Fill input buffer.
  451. std::string output("abcdefghijklmnopprstuvwxyz");
  452. std::string expectedInput(output);
  453. for (int i = 0; i < output.size(); i++) {
  454. expectedInput[i] = std::toupper(output[i]);
  455. }
  456. child << output;
  457. child.flush();
  458. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  459. if (child.numBytesAvailable() != 1) {
  460. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  461. }
  462. child.read(&ch, 1);
  463. int index = 0;
  464. if (ch != expectedInput[index++]) {
  465. RETURN_FALSE(" reader.read() returned incorrect value");
  466. }
  467. size_t nBytesAvailable;
  468. nBytesAvailable = child.numBytesAvailable();
  469. if (nBytesAvailable != bufSize) {
  470. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  471. }
  472. std::fill_n(buf, sizeof(buf), 0);
  473. child.read(buf, nBytesAvailable);
  474. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  475. RETURN_FALSE(" reader.read() failure");
  476. }
  477. index += nBytesAvailable;
  478. nBytesAvailable = child.numBytesAvailable();
  479. if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
  480. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  481. }
  482. std::fill_n(buf, sizeof(buf), 0);
  483. child.read(buf, nBytesAvailable);
  484. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  485. RETURN_FALSE(" reader.read() failure");
  486. }
  487. index += nBytesAvailable;
  488. if (expectedInput.size() != index) {
  489. RETURN_FALSE(" not all data was read by reader.read()");
  490. }
  491. if (child.numBytesAvailable() != 0) {
  492. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  493. }
  494. // Send exit message.
  495. child << 'q';
  496. child.flush();
  497. if (!child) {
  498. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  499. }
  500. // Verify exit.
  501. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  502. if (!child.isDone()) {
  503. std::cout << " Failure in testNonblockingReader: "
  504. << "Child program did not exit." << std::endl;
  505. return false;
  506. }
  507. } catch (std::exception &e) {
  508. EXCEPTION_TERM("Non-blocking reader test");
  509. return false;
  510. }
  511. return true;
  512. }
  513. ////////////////////////////////////////////////////////////////////////////////
  514. // Child Tests ///////////////////////////////////////////////////////////
  515. ////////////////////////////////////////////////////////////////////////////////
  516. bool testChildIsDone() {
  517. try {
  518. CodeDweller::Child child;
  519. // Test exception if called out-of-order.
  520. try {
  521. child.isDone();
  522. NO_EXCEPTION_TERM("isDone() called without run()");
  523. return false;
  524. } catch (std::exception &e) {
  525. }
  526. // Start the child.
  527. child.open(childName);
  528. if (child.isDone()) {
  529. std::cout << "isDone() failure; returned true." << std::endl;
  530. return false;
  531. }
  532. // Command the child to exit.
  533. if (!child.write("q")) {
  534. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  535. }
  536. // Sleep to let the child exit.
  537. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  538. if (!child.isDone()) {
  539. std::cout << "isDone() failure; returned false." << std::endl;
  540. return false;
  541. }
  542. child.close();
  543. } catch (std::exception &e) {
  544. EXCEPTION_TERM("isDone()");
  545. return false;
  546. }
  547. return true;
  548. }
  549. bool testChildIsRunning() {
  550. try {
  551. CodeDweller::Child child;
  552. if (child.isRunning()) {
  553. std::cout << "isRunning() failure; returned true before starting."
  554. << std::endl;
  555. return false;
  556. }
  557. // Start the child.
  558. child.open(childName);
  559. if (!child.isRunning()) {
  560. std::cout << "isRunning() failure; returned false." << std::endl;
  561. return false;
  562. }
  563. // Command the child to exit.
  564. if (!child.write("q")) {
  565. RETURN_FALSE(" write() failure");
  566. }
  567. // Sleep to let the child exit.
  568. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  569. child.close();
  570. if (child.isRunning()) {
  571. std::cout << "isRunning() failure; returned true after stopping."
  572. << std::endl;
  573. return false;
  574. }
  575. } catch (std::exception &e) {
  576. EXCEPTION_TERM("isRunning()");
  577. return false;
  578. }
  579. return true;
  580. }
  581. bool testChildResult() {
  582. try {
  583. std::vector<std::string> cmd;
  584. cmd.push_back(childName);
  585. cmd.push_back("quit");
  586. CodeDweller::Child child(cmd);
  587. // Test exception if called out-of-order.
  588. try {
  589. (void) child.result();
  590. NO_EXCEPTION_TERM(" result() called without run()");
  591. return false;
  592. } catch (std::exception &e) {
  593. }
  594. // Test exception if called while child is running.
  595. try {
  596. (void) child.result();
  597. NO_EXCEPTION_TERM(" result() called before child exited");
  598. return false;
  599. } catch (std::exception &e) {
  600. }
  601. // Wait for the child to exit.
  602. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  603. int32_t result = child.result();
  604. if (25 != result) {
  605. std::cout << "result() failure; returned " << result
  606. << " instead of 25." << std::endl;
  607. return false;
  608. }
  609. } catch (std::exception &e) {
  610. EXCEPTION_TERM("result()");
  611. return false;
  612. }
  613. return true;
  614. }
  615. bool testChildClose() {
  616. // Test with no child process.
  617. try {
  618. CodeDweller::Child child;
  619. child.close();
  620. NO_EXCEPTION_TERM("close() with no child process");
  621. return false;
  622. } catch (std::exception &e) {
  623. }
  624. // Test with no waiting.
  625. try {
  626. CodeDweller::Child child(childName);
  627. child.close();
  628. } catch (std::exception &e) {
  629. EXCEPTION_TERM("close() with no waiting");
  630. return false;
  631. }
  632. // Test with waiting.
  633. try {
  634. CodeDweller::Child child(childName);
  635. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  636. child.close();
  637. } catch (std::exception &e) {
  638. EXCEPTION_TERM("close() with 100 ms waiting");
  639. return false;
  640. }
  641. // Test after the child exits.
  642. std::vector<std::string> cmd;
  643. cmd.push_back(childName);
  644. cmd.push_back("quit");
  645. try {
  646. CodeDweller::Child child(cmd);
  647. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  648. child.close();
  649. } catch (std::exception &e) {
  650. EXCEPTION_TERM("close() after child exits");
  651. return false;
  652. }
  653. // Test exception thrown for out-of-order calling.
  654. try {
  655. CodeDweller::Child child;
  656. child.close();
  657. NO_EXCEPTION_TERM("close() called without open()");
  658. return false;
  659. } catch (std::exception &e) {
  660. }
  661. return true;
  662. }
  663. bool testChildOpen() {
  664. // Test with no waiting.
  665. try {
  666. CodeDweller::Child child(childName);
  667. child.close();
  668. child.open(childName);
  669. child.close();
  670. } catch (std::exception &e) {
  671. EXCEPTION_TERM("close()/open() with no waiting");
  672. return false;
  673. }
  674. // Test with waiting.
  675. try {
  676. CodeDweller::Child child(childName);
  677. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  678. child.close();
  679. child.open(childName);
  680. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  681. child.close();
  682. } catch (std::exception &e) {
  683. EXCEPTION_TERM("close()/open() with 100 ms waiting");
  684. return false;
  685. }
  686. // Test exception thrown for out-of-order calling.
  687. try {
  688. CodeDweller::Child child;
  689. child.close();
  690. NO_EXCEPTION_TERM("close() called without open()");
  691. return false;
  692. } catch (std::exception &e) {
  693. }
  694. return true;
  695. }
  696. bool testChildReadWrite() {
  697. size_t bufSize = 15;
  698. int nChar = bufSize - 1; // Size of each packet.
  699. try {
  700. CodeDweller::Child child(bufSize);
  701. std::ostringstream childOutput;
  702. std::vector<std::string> expectedChildOutput;
  703. char readChar;
  704. // Generate input.
  705. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  706. int nChar = bufSize - 1;
  707. int nLines = 2;
  708. for (int iLine = 0; iLine < nLines; iLine++) {
  709. std::string line;
  710. line.erase();
  711. for (int iChar = 0; iChar < nChar; iChar++) {
  712. line.push_back(randomLetter[std::rand() % 26]);
  713. }
  714. expectedChildOutput.push_back(line);
  715. }
  716. // Test exception.
  717. try {
  718. child.write("Should Throw");
  719. NO_EXCEPTION_TERM(" writer called without run()");
  720. return false;
  721. } catch (std::exception &e) {
  722. }
  723. child.open(childName);
  724. // Write, read, put back, and reread each character.
  725. int nLine = 0;
  726. for (std::string &line : expectedChildOutput) {
  727. nLine++;
  728. // Try to queue.
  729. bool done;
  730. int nTries = 100;
  731. for (int i = 0; i < nTries; i++) {
  732. done = child.write(line);
  733. if (done) {
  734. break;
  735. }
  736. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  737. }
  738. if (!done) {
  739. std::ostringstream temp;
  740. temp << " Failure from write(std::string const &) on line " << nLine;
  741. RETURN_FALSE(temp.str());
  742. }
  743. // Wait for transmission.
  744. for (int i = 0; i < nTries; i++) {
  745. done = child.isFinishedWriting();
  746. if (done) {
  747. break;
  748. }
  749. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  750. }
  751. if (!done) {
  752. std::ostringstream temp;
  753. temp << " Failure from isFinishedWriting() on line " << nLine;
  754. RETURN_FALSE(temp.str());
  755. }
  756. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  757. // Read one line.
  758. std::string readLine, temp;
  759. int nCharToRead, nCharRead;
  760. readLine.clear();
  761. nCharToRead = line.size();
  762. for (int i = 0; i < nTries; i++) {
  763. nCharRead = child.read(temp, nCharToRead);
  764. nCharToRead -= nCharRead;
  765. if (nCharToRead <= 0) {
  766. break;
  767. }
  768. }
  769. if (0 != nCharToRead) {
  770. std::ostringstream temp;
  771. temp << " Failure from () on line " << nLine
  772. << ": nCharRead: " << nCharRead;
  773. RETURN_FALSE(temp.str());
  774. }
  775. // Convert to upper case.
  776. std::string expectedLine;
  777. expectedLine = line;
  778. for (auto &c : expectedLine) {
  779. c = toupper(c);
  780. }
  781. // Compare.
  782. if (expectedLine != readLine) {
  783. std::cout << " Failure in testReadWrite." << std::endl;
  784. std::cout << " Expected: '" << expectedLine
  785. << "'\n Received: '" << readLine << "'" << std::endl;
  786. return false;
  787. }
  788. }
  789. // Send exit message.
  790. if (!child.write("q")) {
  791. RETURN_FALSE(" Failure in testChildIsDone: write() failure");
  792. }
  793. // Verify exit.
  794. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  795. if (!child.isDone()) {
  796. std::cout << " Failure in testReadWrite: Child program did not exit."
  797. << std::endl;
  798. return false;
  799. }
  800. } catch (std::exception &e) {
  801. EXCEPTION_TERM("read()/write()/isFinishedWriting()");
  802. return false;
  803. }
  804. return true;
  805. }
  806. bool testChildRead() {
  807. try {
  808. size_t bufSize = 32;
  809. CodeDweller::Child child(bufSize);
  810. // Test exception.
  811. try {
  812. std::string temp;
  813. (void) child.read(temp);
  814. NO_EXCEPTION_TERM(" read() called without open()");
  815. return false;
  816. } catch (std::exception &e) {
  817. }
  818. std::ostringstream childOutput;
  819. std::string expectedChildOutput("This is a test");
  820. std::string readBuf;
  821. std::vector<std::string> cmd;
  822. cmd.push_back(childName);
  823. cmd.push_back("write");
  824. child.open(cmd);
  825. int nTries = 100;
  826. for (int i = 0; i < nTries; i++) {
  827. if (child.read(readBuf) > 0) {
  828. childOutput << readBuf;
  829. }
  830. if (child.errorOccurred(readBuf)) {
  831. std::ostringstream temp;
  832. temp << " Failure in testReader: " << readBuf;
  833. RETURN_FALSE(temp.str());
  834. }
  835. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  836. }
  837. // Check.
  838. if (childOutput.str() != expectedChildOutput) {
  839. std::cout << " read() failure in testChildRead." << std::endl;
  840. std::cout << " Expected: '" << expectedChildOutput
  841. << "'\n Received: '" << childOutput.str() << "'"
  842. << std::endl;
  843. return false;
  844. }
  845. // Sleep to let the child exit.
  846. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  847. if (!child.isDone()) {
  848. std::cout << "first isDone() failure in testReader." << std::endl;
  849. return false;
  850. }
  851. if (!child.isDone()) {
  852. std::cout << "second isDone() failure in testReader." << std::endl;
  853. return false;
  854. }
  855. child.close();
  856. } catch (std::exception &e) {
  857. EXCEPTION_TERM("reader()");
  858. return false;
  859. }
  860. return true;
  861. }
  862. bool testChildNonBlockingReadWrite() {
  863. try {
  864. std::vector<std::string> cmd;
  865. cmd.push_back(childName);
  866. size_t bufSize = 16;
  867. std::uint16_t
  868. nominalAboveMin_ms = 15,
  869. delta_ms = 0;
  870. CodeDweller::Child child(cmd,
  871. bufSize,
  872. nominalAboveMin_ms,
  873. delta_ms);
  874. // Check for available input with input.
  875. std::string childInput("abc");
  876. if (!child.write(childInput)) {
  877. RETURN_FALSE(" first write() failure");
  878. }
  879. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  880. // Read one character.
  881. std::string readBuf;
  882. if (child.read(readBuf, 1) != 1) {
  883. RETURN_FALSE(" read() failure");
  884. }
  885. if ("A" != readBuf) {
  886. RETURN_FALSE(" read() failure");
  887. }
  888. // Read.
  889. if (child.read(readBuf, 2) != 2) {
  890. RETURN_FALSE(" read() failure");
  891. }
  892. if ("BC" != readBuf) {
  893. RETURN_FALSE(" read() failure");
  894. }
  895. // Check that no input is available.
  896. if (child.read(readBuf) != 0) {
  897. RETURN_FALSE(" read() failure");
  898. }
  899. if (!readBuf.empty()) {
  900. RETURN_FALSE(" read() failure");
  901. }
  902. // Fill input buffer.
  903. std::string output("abcdefghijklmnopprstuvwxyz");
  904. std::string expectedInput(output);
  905. std::string expectedOutputAfter1(output.substr(bufSize));
  906. for (int i = 0; i < output.size(); i++) {
  907. expectedInput[i] = std::toupper(output[i]);
  908. }
  909. if (child.writeAndShrink(output) != bufSize) {
  910. RETURN_FALSE(" writeAndShrink() failure");
  911. }
  912. if (output != expectedOutputAfter1) {
  913. RETURN_FALSE(" writeAndShrink() failure");
  914. }
  915. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  916. int expectedLeftOver = output.size();
  917. if (child.writeAndShrink(output) != expectedLeftOver) {
  918. RETURN_FALSE(" writeAndShrink() failure");
  919. }
  920. if (!output.empty()) {
  921. RETURN_FALSE(" writeAndShrink() failure");
  922. }
  923. std::this_thread::sleep_for(std::chrono::milliseconds(25));
  924. if (child.read(readBuf) != bufSize) {
  925. RETURN_FALSE(" read() failure");
  926. }
  927. if (readBuf != expectedInput.substr(0, bufSize)) {
  928. RETURN_FALSE(" read() failure)");
  929. }
  930. if (child.read(readBuf) != expectedLeftOver) {
  931. RETURN_FALSE(" read() failure");
  932. }
  933. if (expectedInput.substr(bufSize) != readBuf) {
  934. RETURN_FALSE(" read() failure");
  935. }
  936. // Send exit message.
  937. if (!child.write("q")) {
  938. RETURN_FALSE(" write() failure");
  939. }
  940. // Verify exit.
  941. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  942. if (!child.isDone()) {
  943. std::cout << " Failure in testNonblockingReader: "
  944. << "Child program did not exit." << std::endl;
  945. return false;
  946. }
  947. std::string errorDescription;
  948. if (child.errorOccurred(errorDescription)) {
  949. std::ostringstream temp;
  950. temp << " Failure in: testChildNonBlockingReadWrite: "
  951. << errorDescription;
  952. RETURN_FALSE(temp.str());
  953. }
  954. child.close();
  955. } catch (std::exception &e) {
  956. EXCEPTION_TERM("Non-blocking reader test");
  957. return false;
  958. }
  959. return true;
  960. }
  961. ////////////////////////////////////////////////////////////////////////////////
  962. // End of tests ////////////////////////////////////////////////////////////////
  963. ////////////////////////////////////////////////////////////////////////////////
  964. int main()
  965. {
  966. #if 0
  967. std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
  968. RUN_TEST(testChildStreamIsDone);
  969. RUN_TEST(testChildStreamIsRunning);
  970. RUN_TEST(testChildStreamResult);
  971. RUN_TEST(testChildStreamClose);
  972. RUN_TEST(testChildStreamReader);
  973. RUN_TEST(testChildStreamReaderWriter);
  974. RUN_TEST(testChildStreamBinaryRead);
  975. RUN_TEST(testChildStreamNonBlockingRead);
  976. std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
  977. RUN_TEST(testChildIsDone);
  978. RUN_TEST(testChildIsRunning);
  979. RUN_TEST(testChildResult);
  980. RUN_TEST(testChildClose);
  981. RUN_TEST(testChildOpen);
  982. #endif
  983. RUN_TEST(testChildRead);
  984. #if 1
  985. RUN_TEST(testChildReadWrite);
  986. RUN_TEST(testChildNonBlockingReadWrite);
  987. #endif
  988. SUMMARY;
  989. return 0;
  990. }