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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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
  47. testIsDone() {
  48. try {
  49. CodeDweller::Child child(childName);
  50. // Test exception if called out-of-order.
  51. try {
  52. child.isDone();
  53. NO_EXCEPTION_TERM("isDone() called without run()");
  54. return false;
  55. } catch (std::exception &e) {
  56. }
  57. child.run();
  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.childStream << 'q';
  64. child.childStream.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
  78. testResult() {
  79. try {
  80. std::vector<std::string> cmd;
  81. cmd.push_back(childName);
  82. cmd.push_back("quit");
  83. CodeDweller::Child child(cmd);
  84. // Test exception if called out-of-order.
  85. try {
  86. (void) child.result();
  87. NO_EXCEPTION_TERM(" result() called without run()");
  88. return false;
  89. } catch (std::exception &e) {
  90. }
  91. child.run();
  92. // Test exception if called while child is running.
  93. try {
  94. (void) child.result();
  95. NO_EXCEPTION_TERM(" result() called before child exited");
  96. return false;
  97. } catch (std::exception &e) {
  98. }
  99. // Wait for the child to exit.
  100. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  101. int32_t result = child.result();
  102. if (25 != result) {
  103. std::cout << "result() failure; returned " << result
  104. << " instead of 25." << std::endl;
  105. return false;
  106. }
  107. } catch (std::exception &e) {
  108. EXCEPTION_TERM("result()");
  109. return false;
  110. }
  111. return true;
  112. }
  113. bool
  114. testTerminate() {
  115. // Test with no waiting.
  116. try {
  117. CodeDweller::Child child(childName);
  118. child.run();
  119. child.terminate();
  120. } catch (std::exception &e) {
  121. EXCEPTION_TERM("terminate() with no waiting");
  122. return false;
  123. }
  124. // Test with waiting.
  125. try {
  126. CodeDweller::Child child(childName);
  127. child.run();
  128. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  129. child.terminate();
  130. } catch (std::exception &e) {
  131. EXCEPTION_TERM("terminate() with 100 ms waiting");
  132. return false;
  133. }
  134. // Test after the child exits.
  135. std::vector<std::string> cmd;
  136. cmd.push_back(childName);
  137. cmd.push_back("quit");
  138. try {
  139. CodeDweller::Child child(cmd);
  140. child.run();
  141. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  142. child.terminate();
  143. } catch (std::exception &e) {
  144. EXCEPTION_TERM("terminate() after child exits");
  145. return false;
  146. }
  147. // Test exception thrown for out-of-order calling.
  148. try {
  149. CodeDweller::Child child(cmd);
  150. child.terminate();
  151. NO_EXCEPTION_TERM("terminate() called without run()");
  152. return false;
  153. } catch (std::exception &e) {
  154. }
  155. return true;
  156. }
  157. bool
  158. testReaderWriter() {
  159. try {
  160. size_t bufSize = 15;
  161. CodeDweller::Child child(childName, bufSize);
  162. std::ostringstream childOutput;
  163. std::vector<std::string> expectedChildOutput;
  164. char readChar;
  165. // Generate input.
  166. char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
  167. int nChar = bufSize - 1;
  168. int nLines = 2;
  169. for (int iLine = 0; iLine < nLines; iLine++) {
  170. std::string line;
  171. line.erase();
  172. for (int iChar = 0; iChar < nChar; iChar++) {
  173. line.push_back(randomLetter[std::rand() % 26]);
  174. }
  175. expectedChildOutput.push_back(line);
  176. }
  177. // Test exception.
  178. try {
  179. child.childStream.exceptions(std::ostream::failbit | std::ostream::badbit);
  180. child.childStream << bufSize;
  181. child.childStream.flush();
  182. NO_EXCEPTION_TERM(" writer called without run()");
  183. return false;
  184. } catch (std::exception &e) {
  185. }
  186. // Clear the writer stream.
  187. try {
  188. child.childStream.clear();
  189. } catch (std::exception &e) {
  190. }
  191. child.run();
  192. // Write, read, put back, and reread each character.
  193. for (std::string line : expectedChildOutput) {
  194. // Write one line.
  195. const char *ptr;
  196. ptr = line.data();
  197. for (std::string::size_type i = 0; i < line.length(); i++) {
  198. child.childStream << ptr[i];
  199. if (!child.childStream) {
  200. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  201. }
  202. }
  203. child.childStream.flush();
  204. if (!child.childStream) {
  205. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  206. }
  207. // Read one line.
  208. std::string readLine;
  209. readLine.erase();
  210. for (std::string::size_type i = 0; i < line.length(); i++) {
  211. child.childStream >> readChar;
  212. if (!child.childStream) {
  213. RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
  214. }
  215. readLine.push_back(readChar);
  216. }
  217. // Convert to upper case.
  218. std::string expectedLine;
  219. expectedLine = line;
  220. for (auto &c : expectedLine) {
  221. c = toupper(c);
  222. }
  223. // Compare.
  224. if (expectedLine != readLine) {
  225. std::cout << " Failure in testReaderWriter." << std::endl;
  226. std::cout << " Expected: '" << expectedLine
  227. << "'\n Received: '" << readLine << "'" << std::endl;
  228. return false;
  229. }
  230. }
  231. // Send exit message.
  232. child.childStream << 'q';
  233. child.childStream.flush();
  234. if (!child.childStream) {
  235. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  236. }
  237. // Verify exit.
  238. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  239. if (!child.isDone()) {
  240. std::cout << " Failure in testReaderWriter: "
  241. << "Child program did not exit." << std::endl;
  242. return false;
  243. }
  244. } catch (std::exception &e) {
  245. EXCEPTION_TERM("reader()/writer()");
  246. return false;
  247. }
  248. return true;
  249. }
  250. bool
  251. testReader() {
  252. try {
  253. std::vector<std::string> cmd;
  254. cmd.push_back(childName);
  255. cmd.push_back("write");
  256. size_t bufSize = 32;
  257. CodeDweller::Child child(cmd, bufSize);
  258. // Test exception.
  259. try {
  260. int temp;
  261. child.childStream.exceptions(std::istream::failbit | std::istream::badbit);
  262. child.childStream >> temp;
  263. NO_EXCEPTION_TERM(" reader called without run()");
  264. return false;
  265. } catch (std::exception &e) {
  266. }
  267. child.childStream.clear();
  268. child.childStream.clear();
  269. std::ostringstream childOutput;
  270. std::string expectedChildOutput("This is a test");
  271. char readChar;
  272. child.run();
  273. child.childStream.exceptions(std::istream::badbit);
  274. child.childStream >> std::noskipws;
  275. if (!child.childStream) {
  276. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  277. }
  278. while (child.childStream >> readChar) {
  279. if (!child.childStream) {
  280. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  281. }
  282. child.childStream.putback(readChar);
  283. if (!child.childStream) {
  284. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  285. }
  286. child.childStream >> readChar;
  287. if (!child.childStream) {
  288. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  289. }
  290. childOutput << readChar;
  291. }
  292. if (!child.childStream.eof()) {
  293. RETURN_FALSE(" Failure in testReader: Error occured before "
  294. "EOF was reached while reading");
  295. }
  296. // Check.
  297. if (childOutput.str() != expectedChildOutput) {
  298. std::cout << " reader() failure in testReader." << std::endl;
  299. std::cout << " Expected: '" << expectedChildOutput
  300. << "'\n Received: '" << childOutput.str() << "'"
  301. << std::endl;
  302. return false;
  303. }
  304. // Sleep to let the child exit.
  305. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  306. if (!child.isDone()) {
  307. std::cout << "first isDone() failure in testReader." << std::endl;
  308. return false;
  309. }
  310. if (!child.isDone()) {
  311. std::cout << "second isDone() failure in testReader." << std::endl;
  312. return false;
  313. }
  314. } catch (std::exception &e) {
  315. EXCEPTION_TERM("reader()");
  316. return false;
  317. }
  318. return true;
  319. }
  320. bool
  321. testBinaryRead() {
  322. try {
  323. std::vector<std::string> cmd;
  324. cmd.push_back(childName);
  325. size_t bufSize = 164;
  326. CodeDweller::Child child(cmd, bufSize);
  327. child.run();
  328. // Write.
  329. std::string childInput("abc");
  330. child.childStream << childInput;
  331. child.childStream.flush();
  332. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  333. // Read one character.
  334. char ch;
  335. child.childStream.read(&ch, 1);
  336. if (ch != 'A') {
  337. RETURN_FALSE(" reader.read() returned incorrect value");
  338. }
  339. // Read.
  340. char buf[bufSize * 2];
  341. child.childStream.read(buf, 2);
  342. buf[2] = '\0';
  343. std::string input(buf);
  344. if (input != "BC") {
  345. RETURN_FALSE(" reader.read() returned incorrect value");
  346. }
  347. // Fill input buffer.
  348. std::string output("abcdefghijklmnopprstuvwxyz");
  349. std::string expectedInput(output);
  350. for (int i = 0; i < output.size(); i++) {
  351. expectedInput[i] = std::toupper(output[i]);
  352. }
  353. child.childStream << output;
  354. child.childStream.flush();
  355. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  356. child.childStream.read(&ch, 1);
  357. int index = 0;
  358. if (ch != expectedInput[index++]) {
  359. RETURN_FALSE(" reader.read() returned incorrect value");
  360. }
  361. size_t nBytesRead = expectedInput.size() - 1;
  362. child.childStream.read(buf, nBytesRead);
  363. buf[nBytesRead] = '\0';
  364. if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
  365. RETURN_FALSE(" reader.read() failure");
  366. }
  367. // Send exit message.
  368. child.childStream << 'q';
  369. child.childStream.flush();
  370. if (!child.childStream) {
  371. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  372. }
  373. // Verify exit.
  374. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  375. if (!child.isDone()) {
  376. std::cout << " Failure in testNonblockingReader: "
  377. << "Child program did not exit." << std::endl;
  378. return false;
  379. }
  380. } catch (std::exception &e) {
  381. EXCEPTION_TERM("Binary read test");
  382. return false;
  383. }
  384. return true;
  385. }
  386. bool
  387. testNonBlockingRead() {
  388. try {
  389. std::vector<std::string> cmd;
  390. cmd.push_back(childName);
  391. size_t bufSize = 16;
  392. CodeDweller::Child child(cmd, bufSize);
  393. child.run();
  394. // Check for available input with no input.
  395. if (child.numBytesAvailable() != 0) {
  396. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  397. }
  398. // Check for available input with input.
  399. std::string childInput("abc");
  400. child.childStream << childInput;
  401. child.childStream.flush();
  402. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  403. // Check that input is available.
  404. if (child.numBytesAvailable() != 1) {
  405. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  406. }
  407. // Read one character.
  408. char ch;
  409. child.childStream.read(&ch, 1);
  410. if (ch != 'A') {
  411. RETURN_FALSE(" reader.read() returned incorrect value");
  412. }
  413. // Check that input is available.
  414. if (child.numBytesAvailable() != 2) {
  415. RETURN_FALSE(" numBytesAvailable() did not return expected 2");
  416. }
  417. // Read.
  418. char buf[bufSize * 2];
  419. child.childStream.read(buf, 2);
  420. buf[2] = '\0';
  421. std::string input(buf);
  422. if (input != "BC") {
  423. RETURN_FALSE(" reader.read() returned incorrect value");
  424. }
  425. // Check that no input is available.
  426. if (child.numBytesAvailable() != 0) {
  427. RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
  428. "after reading");
  429. }
  430. // Fill input buffer.
  431. std::string output("abcdefghijklmnopprstuvwxyz");
  432. std::string expectedInput(output);
  433. for (int i = 0; i < output.size(); i++) {
  434. expectedInput[i] = std::toupper(output[i]);
  435. }
  436. child.childStream << output;
  437. child.childStream.flush();
  438. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  439. if (child.numBytesAvailable() != 1) {
  440. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  441. }
  442. child.childStream.read(&ch, 1);
  443. int index = 0;
  444. if (ch != expectedInput[index++]) {
  445. RETURN_FALSE(" reader.read() returned incorrect value");
  446. }
  447. size_t nBytesAvailable;
  448. nBytesAvailable = child.numBytesAvailable();
  449. if (nBytesAvailable != bufSize) {
  450. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  451. }
  452. std::fill_n(buf, sizeof(buf), 0);
  453. child.childStream.read(buf, nBytesAvailable);
  454. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  455. RETURN_FALSE(" reader.read() failure");
  456. }
  457. index += nBytesAvailable;
  458. nBytesAvailable = child.numBytesAvailable();
  459. if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
  460. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  461. }
  462. std::fill_n(buf, sizeof(buf), 0);
  463. child.childStream.read(buf, nBytesAvailable);
  464. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  465. RETURN_FALSE(" reader.read() failure");
  466. }
  467. index += nBytesAvailable;
  468. if (expectedInput.size() != index) {
  469. RETURN_FALSE(" not all data was read by reader.read()");
  470. }
  471. if (child.numBytesAvailable() != 0) {
  472. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  473. }
  474. // Send exit message.
  475. child.childStream << 'q';
  476. child.childStream.flush();
  477. if (!child.childStream) {
  478. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  479. }
  480. // Verify exit.
  481. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  482. if (!child.isDone()) {
  483. std::cout << " Failure in testNonblockingReader: "
  484. << "Child program did not exit." << std::endl;
  485. return false;
  486. }
  487. } catch (std::exception &e) {
  488. EXCEPTION_TERM("Non-blocking reader test");
  489. return false;
  490. }
  491. return true;
  492. }
  493. ////////////////////////////////////////////////////////////////////////////////
  494. // End of tests ////////////////////////////////////////////////////////////////
  495. ////////////////////////////////////////////////////////////////////////////////
  496. int main()
  497. {
  498. std::cout << "CodeDweller::Child unit tests" << std::endl << std::endl;
  499. CodeDweller::Child child(childName);
  500. RUN_TEST(testIsDone);
  501. RUN_TEST(testResult);
  502. RUN_TEST(testTerminate);
  503. RUN_TEST(testReader);
  504. RUN_TEST(testReaderWriter);
  505. RUN_TEST(testBinaryRead);
  506. RUN_TEST(testNonBlockingRead);
  507. SUMMARY;
  508. return 0;
  509. }