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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <thread>
  5. #include <sstream>
  6. #include "CodeDweller/child.hpp"
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // Configuration ///////////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////
  10. /// Child program name.
  11. const std::string childName("./childProgram");
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // End of configuration ////////////////////////////////////////////////////////
  14. ////////////////////////////////////////////////////////////////////////////////
  15. int nTotalTests = 0;
  16. int nPass = 0;
  17. int nFail = 0;
  18. bool result;
  19. #define NO_EXCEPTION_TERM(msg) \
  20. std::cout \
  21. << msg << " failed to throw exception at line " \
  22. << __LINE__ << "." << std::endl
  23. #define EXCEPTION_TERM(msg) \
  24. std::cout \
  25. << msg << " threw unexpected exception at line " \
  26. << __LINE__ << ": " << 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.writer << 'q';
  64. child.writer.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.writer << bufSize;
  180. child.writer.flush();
  181. NO_EXCEPTION_TERM(" writer called without run()");
  182. return false;
  183. } catch (std::exception &e) {
  184. }
  185. // Clear the writer stream.
  186. try {
  187. child.writer.clear();
  188. } catch (std::exception &e) {
  189. }
  190. child.run();
  191. // Write, read, put back, and reread each character.
  192. for (std::string line : expectedChildOutput) {
  193. // Write one line.
  194. const char *ptr;
  195. ptr = line.data();
  196. for (std::string::size_type i = 0; i < line.length(); i++) {
  197. child.writer << ptr[i];
  198. if (!child.writer) {
  199. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  200. }
  201. }
  202. child.writer.flush();
  203. if (!child.writer) {
  204. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  205. }
  206. // Read one line.
  207. std::string readLine;
  208. readLine.erase();
  209. for (std::string::size_type i = 0; i < line.length(); i++) {
  210. child.reader >> readChar;
  211. if (!child.reader) {
  212. RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
  213. }
  214. readLine.push_back(readChar);
  215. }
  216. // Convert to upper case.
  217. std::string expectedLine;
  218. expectedLine = line;
  219. for (auto &c : expectedLine) {
  220. c = toupper(c);
  221. }
  222. // Compare.
  223. if (expectedLine != readLine) {
  224. std::cout << " Failure in testReaderWriter." << std::endl;
  225. std::cout << " Expected: '" << expectedLine
  226. << "'\n Received: '" << readLine << "'" << std::endl;
  227. return false;
  228. }
  229. }
  230. // Send exit message.
  231. child.writer << 'q';
  232. child.writer.flush();
  233. if (!child.writer) {
  234. RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
  235. }
  236. // Verify exit.
  237. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  238. if (!child.isDone()) {
  239. std::cout << " Failure in testReaderWriter: "
  240. << "Child program did not exit." << std::endl;
  241. return false;
  242. }
  243. } catch (std::exception &e) {
  244. EXCEPTION_TERM("reader()/writer()");
  245. return false;
  246. }
  247. return true;
  248. }
  249. bool
  250. testReader() {
  251. try {
  252. std::vector<std::string> cmd;
  253. cmd.push_back(childName);
  254. cmd.push_back("write");
  255. size_t bufSize = 32;
  256. CodeDweller::Child child(cmd, bufSize);
  257. // Test exception.
  258. try {
  259. int temp;
  260. child.reader >> temp;
  261. NO_EXCEPTION_TERM(" reader called without run()");
  262. return false;
  263. } catch (std::exception &e) {
  264. }
  265. child.reader.clear();
  266. child.writer.clear();
  267. std::ostringstream childOutput;
  268. std::string expectedChildOutput("This is a test");
  269. char readChar;
  270. child.run();
  271. child.reader.exceptions(std::istream::badbit);
  272. child.reader >> std::noskipws;
  273. if (!child.reader) {
  274. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  275. }
  276. while (child.reader >> readChar) {
  277. if (!child.reader) {
  278. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  279. }
  280. child.reader.putback(readChar);
  281. if (!child.reader) {
  282. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  283. }
  284. child.reader >> readChar;
  285. if (!child.reader) {
  286. RETURN_FALSE(" Failure in testReader: reader stream is bad");
  287. }
  288. childOutput << readChar;
  289. }
  290. if (!child.reader.eof()) {
  291. RETURN_FALSE(" Failure in testReader: Error occured before "
  292. "EOF was reached while reading");
  293. }
  294. // Check.
  295. if (childOutput.str() != expectedChildOutput) {
  296. std::cout << " reader() failure in testReader." << std::endl;
  297. std::cout << " Expected: '" << expectedChildOutput
  298. << "'\n Received: '" << childOutput.str() << "'"
  299. << std::endl;
  300. return false;
  301. }
  302. if (!child.isDone()) {
  303. std::cout << "isDone() failure in testReader." << std::endl;
  304. return false;
  305. }
  306. } catch (std::exception &e) {
  307. EXCEPTION_TERM("reader()");
  308. return false;
  309. }
  310. return true;
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////
  313. // End of tests ////////////////////////////////////////////////////////////////
  314. ////////////////////////////////////////////////////////////////////////////////
  315. int main()
  316. {
  317. std::cout << "CodeDweller::Child unit tests" << std::endl << std::endl;
  318. CodeDweller::Child child(childName);
  319. RUN_TEST(testIsDone);
  320. RUN_TEST(testResult);
  321. RUN_TEST(testTerminate);
  322. RUN_TEST(testReader);
  323. RUN_TEST(testReaderWriter);
  324. SUMMARY;
  325. return 0;
  326. }