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

testChild.cpp 17KB

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