You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testChild.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. // Tests ///////////////////////////////////////////////////////////////////////
  45. ////////////////////////////////////////////////////////////////////////////////
  46. bool testIsDone() {
  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 testIsRunning() {
  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 testResult() {
  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 testClose() {
  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 testReaderWriter() {
  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 testReader() {
  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 testBinaryRead() {
  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 testNonBlockingRead() {
  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. // End of tests ////////////////////////////////////////////////////////////////
  515. ////////////////////////////////////////////////////////////////////////////////
  516. int main()
  517. {
  518. std::cout << "CodeDweller::ChildStream unit tests" << std::endl << std::endl;
  519. CodeDweller::ChildStream child(childName);
  520. RUN_TEST(testIsDone);
  521. RUN_TEST(testIsRunning);
  522. RUN_TEST(testResult);
  523. RUN_TEST(testClose);
  524. RUN_TEST(testReader);
  525. RUN_TEST(testReaderWriter);
  526. RUN_TEST(testBinaryRead);
  527. RUN_TEST(testNonBlockingRead);
  528. SUMMARY;
  529. return 0;
  530. }