選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

testChild.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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.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. // Sleep to let the child exit.
  303. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  304. if (!child.isDone()) {
  305. std::cout << "first isDone() failure in testReader." << std::endl;
  306. return false;
  307. }
  308. if (!child.isDone()) {
  309. std::cout << "second isDone() failure in testReader." << std::endl;
  310. return false;
  311. }
  312. } catch (std::exception &e) {
  313. EXCEPTION_TERM("reader()");
  314. return false;
  315. }
  316. return true;
  317. }
  318. bool
  319. testBinaryRead() {
  320. try {
  321. std::vector<std::string> cmd;
  322. cmd.push_back(childName);
  323. size_t bufSize = 164;
  324. CodeDweller::Child child(cmd, bufSize);
  325. child.run();
  326. // Write.
  327. std::string childInput("abc");
  328. child.writer << childInput;
  329. child.writer.flush();
  330. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  331. // Read one character.
  332. char ch;
  333. child.reader.read(&ch, 1);
  334. if (ch != 'A') {
  335. RETURN_FALSE(" reader.read() returned incorrect value");
  336. }
  337. // Read.
  338. char buf[bufSize * 2];
  339. child.reader.read(buf, 2);
  340. buf[2] = '\0';
  341. std::string input(buf);
  342. if (input != "BC") {
  343. RETURN_FALSE(" reader.read() returned incorrect value");
  344. }
  345. // Fill input buffer.
  346. std::string output("abcdefghijklmnopprstuvwxyz");
  347. std::string expectedInput(output);
  348. for (int i = 0; i < output.size(); i++) {
  349. expectedInput[i] = std::toupper(output[i]);
  350. }
  351. child.writer << output;
  352. child.writer.flush();
  353. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  354. child.reader.read(&ch, 1);
  355. int index = 0;
  356. if (ch != expectedInput[index++]) {
  357. RETURN_FALSE(" reader.read() returned incorrect value");
  358. }
  359. size_t nBytesRead = expectedInput.size() - 1;
  360. child.reader.read(buf, nBytesRead);
  361. buf[nBytesRead] = '\0';
  362. if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
  363. RETURN_FALSE(" reader.read() failure");
  364. }
  365. // Send exit message.
  366. child.writer << 'q';
  367. child.writer.flush();
  368. if (!child.writer) {
  369. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  370. }
  371. // Verify exit.
  372. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  373. if (!child.isDone()) {
  374. std::cout << " Failure in testNonblockingReader: "
  375. << "Child program did not exit." << std::endl;
  376. return false;
  377. }
  378. } catch (std::exception &e) {
  379. EXCEPTION_TERM("Binary read test");
  380. return false;
  381. }
  382. return true;
  383. }
  384. bool
  385. testNonBlockingRead() {
  386. try {
  387. std::vector<std::string> cmd;
  388. cmd.push_back(childName);
  389. size_t bufSize = 16;
  390. CodeDweller::Child child(cmd, bufSize);
  391. child.run();
  392. // Check for available input with no input.
  393. if (child.numBytesAvailable() != 0) {
  394. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  395. }
  396. // Check for available input with input.
  397. std::string childInput("abc");
  398. child.writer << childInput;
  399. child.writer.flush();
  400. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  401. // Check that input is available.
  402. if (child.numBytesAvailable() != 1) {
  403. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  404. }
  405. // Read one character.
  406. char ch;
  407. child.reader.read(&ch, 1);
  408. if (ch != 'A') {
  409. RETURN_FALSE(" reader.read() returned incorrect value");
  410. }
  411. // Check that input is available.
  412. if (child.numBytesAvailable() != 2) {
  413. RETURN_FALSE(" numBytesAvailable() did not return expected 2");
  414. }
  415. // Read.
  416. char buf[bufSize * 2];
  417. child.reader.read(buf, 2);
  418. buf[2] = '\0';
  419. std::string input(buf);
  420. if (input != "BC") {
  421. RETURN_FALSE(" reader.read() returned incorrect value");
  422. }
  423. // Check that no input is available.
  424. if (child.numBytesAvailable() != 0) {
  425. RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
  426. "after reading");
  427. }
  428. // Fill input buffer.
  429. std::string output("abcdefghijklmnopprstuvwxyz");
  430. std::string expectedInput(output);
  431. for (int i = 0; i < output.size(); i++) {
  432. expectedInput[i] = std::toupper(output[i]);
  433. }
  434. child.writer << output;
  435. child.writer.flush();
  436. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  437. if (child.numBytesAvailable() != 1) {
  438. RETURN_FALSE(" numBytesAvailable() did not return expected 1");
  439. }
  440. child.reader.read(&ch, 1);
  441. int index = 0;
  442. if (ch != expectedInput[index++]) {
  443. RETURN_FALSE(" reader.read() returned incorrect value");
  444. }
  445. size_t nBytesAvailable;
  446. nBytesAvailable = child.numBytesAvailable();
  447. if (nBytesAvailable != bufSize) {
  448. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  449. }
  450. std::fill_n(buf, sizeof(buf), 0);
  451. child.reader.read(buf, nBytesAvailable);
  452. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  453. RETURN_FALSE(" reader.read() failure");
  454. }
  455. index += nBytesAvailable;
  456. nBytesAvailable = child.numBytesAvailable();
  457. if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
  458. RETURN_FALSE(" numBytesAvailable() did not return expected value");
  459. }
  460. std::fill_n(buf, sizeof(buf), 0);
  461. child.reader.read(buf, nBytesAvailable);
  462. if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
  463. RETURN_FALSE(" reader.read() failure");
  464. }
  465. index += nBytesAvailable;
  466. if (expectedInput.size() != index) {
  467. RETURN_FALSE(" not all data was read by reader.read()");
  468. }
  469. if (child.numBytesAvailable() != 0) {
  470. RETURN_FALSE(" numBytesAvailable() did not return expected 0");
  471. }
  472. // Send exit message.
  473. child.writer << 'q';
  474. child.writer.flush();
  475. if (!child.writer) {
  476. RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
  477. }
  478. // Verify exit.
  479. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  480. if (!child.isDone()) {
  481. std::cout << " Failure in testNonblockingReader: "
  482. << "Child program did not exit." << std::endl;
  483. return false;
  484. }
  485. } catch (std::exception &e) {
  486. EXCEPTION_TERM("Non-blocking reader test");
  487. return false;
  488. }
  489. return true;
  490. }
  491. ////////////////////////////////////////////////////////////////////////////////
  492. // End of tests ////////////////////////////////////////////////////////////////
  493. ////////////////////////////////////////////////////////////////////////////////
  494. int main()
  495. {
  496. std::cout << "CodeDweller::Child unit tests" << std::endl << std::endl;
  497. CodeDweller::Child child(childName);
  498. RUN_TEST(testIsDone);
  499. RUN_TEST(testResult);
  500. RUN_TEST(testTerminate);
  501. RUN_TEST(testReader);
  502. RUN_TEST(testReaderWriter);
  503. RUN_TEST(testBinaryRead);
  504. RUN_TEST(testNonBlockingRead);
  505. SUMMARY;
  506. return 0;
  507. }