1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324 |
- #include <cstdlib>
-
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <sstream>
- #include <algorithm>
-
- #include "CodeDweller/child.hpp"
-
- ////////////////////////////////////////////////////////////////////////////////
- // Configuration ///////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- /// Child program name.
- const std::string childName("./childProgram");
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of configuration ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int nTotalTests = 0;
- int nPass = 0;
- int nFail = 0;
-
- bool result;
-
- #define NO_EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " failed to throw exception at line " \
- << __LINE__ << "." << std::endl
-
- #define EXCEPTION_TERM(msg) \
- std::cout \
- << msg << " threw unexpected exception: " << e.what() << std::endl
-
- #define RETURN_FALSE(msg) \
- std::cout \
- << msg << " at line " << __LINE__ << std::endl; \
- return false;
-
- #define RUN_TEST(test) \
- std::cout << " " #test ": "; \
- std::cout.flush(); \
- result = test(); \
- std::cout << (result ? "ok" : "fail") << std::endl; \
- nTotalTests++; \
- if (result) nPass++; else nFail++;
-
- #define SUMMARY \
- std::cout \
- << "\nPass: " << nPass \
- << ", Fail: " << nFail \
- << ", Total: " << nTotalTests << std::endl
-
- ////////////////////////////////////////////////////////////////////////////////
- // ChildStream Tests ///////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testChildStreamIsDone() {
-
- try {
-
- CodeDweller::ChildStream child;
-
- // Test exception if called out-of-order.
- try {
- child.isDone();
- NO_EXCEPTION_TERM("isDone() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Start the child.
- child.open(childName);
-
- if (child.isDone()) {
- std::cout << "isDone() failure; returned true." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- child << 'q';
- child.flush();
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "isDone() failure; returned false." << std::endl;
- return false;
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("isDone()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamIsRunning() {
-
- try {
-
- CodeDweller::ChildStream child;
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true before starting."
- << std::endl;
- return false;
- }
-
- // Start the child.
- child.open(childName);
-
- if (!child.isRunning()) {
- std::cout << "isRunning() failure; returned false." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- child << 'q';
- child.flush();
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.close();
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true after stopping."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("isRunning()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamResult() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- CodeDweller::ChildStream child(cmd);
-
- // Test exception if called out-of-order.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test exception if called while child is running.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called before child exited");
- return false;
- } catch (std::exception &e) {
- }
-
- // Wait for the child to exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- int32_t result = child.result();
- if (25 != result) {
- std::cout << "result() failure; returned " << result
- << " instead of 25." << std::endl;
- return false;
- }
- } catch (std::exception &e) {
- EXCEPTION_TERM("result()");
- return false;
- }
- return true;
- }
-
- bool testChildStreamClose() {
-
- // Test with no waiting.
- try {
- CodeDweller::ChildStream child(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::ChildStream child(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with 100 ms waiting");
- return false;
- }
-
- // Test after the child exits.
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- try {
- CodeDweller::ChildStream child(cmd);
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() after child exits");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
-
- CodeDweller::ChildStream child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildStreamReaderWriter() {
-
- try {
- size_t bufSize = 15;
- CodeDweller::ChildStream child(bufSize);
- std::ostringstream childOutput;
- std::vector<std::string> expectedChildOutput;
- char readChar;
-
- // Generate input.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
- int nChar = bufSize - 1;
- int nLines = 2;
- for (int iLine = 0; iLine < nLines; iLine++) {
- std::string line;
- line.erase();
- for (int iChar = 0; iChar < nChar; iChar++) {
- line.push_back(randomLetter[std::rand() % 26]);
- }
- expectedChildOutput.push_back(line);
- }
-
- // Test exception.
- try {
- child.exceptions(std::ostream::failbit | std::ostream::badbit);
- child << bufSize;
- child.flush();
- NO_EXCEPTION_TERM(" writer called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Clear the writer stream.
- try {
- child.clear();
- } catch (std::exception &e) {
- }
-
- child.open(childName);
-
- // Write, read, put back, and reread each character.
- for (std::string line : expectedChildOutput) {
-
- // Write one line.
- const char *ptr;
-
- ptr = line.data();
- for (std::string::size_type i = 0; i < line.length(); i++) {
- child << ptr[i];
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
- }
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
-
- // Read one line.
- std::string readLine;
-
- readLine.erase();
- for (std::string::size_type i = 0; i < line.length(); i++) {
- child >> readChar;
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
- }
- readLine.push_back(readChar);
- }
-
- // Convert to upper case.
- std::string expectedLine;
-
- expectedLine = line;
- for (auto &c : expectedLine) {
-
- c = toupper(c);
-
- }
-
- // Compare.
- if (expectedLine != readLine) {
- std::cout << " Failure in testReaderWriter." << std::endl;
- std::cout << " Expected: '" << expectedLine
- << "'\n Received: '" << readLine << "'" << std::endl;
- return false;
-
- }
-
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testReaderWriter: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()/writer()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamReader() {
-
- try {
-
- size_t bufSize = 32;
- CodeDweller::ChildStream child(bufSize);
-
- // Test exception.
- try {
- int temp;
- child.exceptions(std::istream::failbit | std::istream::badbit);
- child >> temp;
- NO_EXCEPTION_TERM(" reader called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- child.clear();
- child.clear();
- std::ostringstream childOutput;
- std::string expectedChildOutput("This is a test");
- char readChar;
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("write");
-
- child.open(cmd);
- child.exceptions(std::istream::badbit);
- child >> std::noskipws;
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
-
- while (child >> readChar) {
-
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- child.putback(readChar);
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- child >> readChar;
- if (!child) {
- RETURN_FALSE(" Failure in testReader: reader stream is bad");
- }
- childOutput << readChar;
-
- }
-
- if (!child.eof()) {
- RETURN_FALSE(" Failure in testReader: Error occured before "
- "EOF was reached while reading");
- }
-
- // Check.
- if (childOutput.str() != expectedChildOutput) {
- std::cout << " reader() failure in testReader." << std::endl;
- std::cout << " Expected: '" << expectedChildOutput
- << "'\n Received: '" << childOutput.str() << "'"
- << std::endl;
- return false;
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "first isDone() failure in testReader." << std::endl;
- return false;
- }
-
- if (!child.isDone()) {
- std::cout << "second isDone() failure in testReader." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamBinaryRead() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 164;
- CodeDweller::ChildStream child(bufSize);
-
- child.open(cmd);
-
- // Write.
- std::string childInput("abc");
-
- child << childInput;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Read one character.
- char ch;
-
- child.read(&ch, 1);
-
- if (ch != 'A') {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Read.
- char buf[bufSize * 2];
-
- child.read(buf, 2);
- buf[2] = '\0';
-
- std::string input(buf);
-
- if (input != "BC") {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- child << output;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.read(&ch, 1);
-
- int index = 0;
-
- if (ch != expectedInput[index++]) {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- size_t nBytesRead = expectedInput.size() - 1;
-
- child.read(buf, nBytesRead);
- buf[nBytesRead] = '\0';
-
- if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testNonblockingReader: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Binary read test");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamNonBlockingRead() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- CodeDweller::ChildStream child(cmd, bufSize);
-
- // Check for available input with no input.
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0");
- }
-
- // Check for available input with input.
- std::string childInput("abc");
-
- child << childInput;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Check that input is available.
- if (child.numBytesAvailable() != 1) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 1");
- }
-
- // Read one character.
- char ch;
-
- child.read(&ch, 1);
-
- if (ch != 'A') {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Check that input is available.
- if (child.numBytesAvailable() != 2) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 2");
- }
-
- // Read.
- char buf[bufSize * 2];
-
- child.read(buf, 2);
- buf[2] = '\0';
-
- std::string input(buf);
-
- if (input != "BC") {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- // Check that no input is available.
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0 "
- "after reading");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- child << output;
- child.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (child.numBytesAvailable() != 1) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 1");
- }
-
- child.read(&ch, 1);
-
- int index = 0;
-
- if (ch != expectedInput[index++]) {
- RETURN_FALSE(" reader.read() returned incorrect value");
- }
-
- size_t nBytesAvailable;
-
- nBytesAvailable = child.numBytesAvailable();
- if (nBytesAvailable != bufSize) {
- RETURN_FALSE(" numBytesAvailable() did not return expected value");
- }
- std::fill_n(buf, sizeof(buf), 0);
-
- child.read(buf, nBytesAvailable);
-
- if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
- index += nBytesAvailable;
-
- nBytesAvailable = child.numBytesAvailable();
- if (nBytesAvailable != expectedInput.size() - 1 - bufSize) {
- RETURN_FALSE(" numBytesAvailable() did not return expected value");
- }
- std::fill_n(buf, sizeof(buf), 0);
-
- child.read(buf, nBytesAvailable);
-
- if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
- RETURN_FALSE(" reader.read() failure");
- }
- index += nBytesAvailable;
-
- if (expectedInput.size() != index) {
- RETURN_FALSE(" not all data was read by reader.read()");
- }
-
- if (child.numBytesAvailable() != 0) {
- RETURN_FALSE(" numBytesAvailable() did not return expected 0");
- }
-
- // Send exit message.
- child << 'q';
- child.flush();
- if (!child) {
- RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testNonblockingReader: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // Child Tests ///////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- bool testChildIsDone() {
-
- try {
-
- CodeDweller::Child child;
-
- // Test exception if called out-of-order.
- try {
- child.isDone();
- NO_EXCEPTION_TERM("isDone() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Start the child.
- child.open(childName);
-
- if (child.isDone()) {
- std::cout << "isDone() failure; returned true." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- if (!child.write("q")) {
- RETURN_FALSE(" Failure in testChildIsDone: write() failure");
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "isDone() failure; returned false." << std::endl;
- return false;
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("isDone()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildIsRunning() {
-
- try {
-
- CodeDweller::Child child;
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true before starting."
- << std::endl;
- return false;
- }
-
- // Start the child.
- child.open(childName);
-
- if (!child.isRunning()) {
- std::cout << "isRunning() failure; returned false." << std::endl;
- return false;
- }
-
- // Command the child to exit.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- child.close();
-
- if (child.isRunning()) {
- std::cout << "isRunning() failure; returned true after stopping."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("isRunning()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildResult() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- CodeDweller::Child child(cmd);
-
- // Test exception if called out-of-order.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test exception if called while child is running.
- try {
- (void) child.result();
- NO_EXCEPTION_TERM(" result() called before child exited");
- return false;
- } catch (std::exception &e) {
- }
-
- // Wait for the child to exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- int32_t result = child.result();
-
- if (25 != result) {
- std::cout << "result() failure; returned " << result
- << " instead of 25." << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("result()");
- return false;
- }
-
- return true;
- }
-
- bool testChildClose() {
-
- // Test with no child process.
- try {
- CodeDweller::Child child;
- child.close();
- NO_EXCEPTION_TERM("close() with no child process");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test with no waiting.
- try {
- CodeDweller::Child child(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::Child child(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() with 100 ms waiting");
- return false;
- }
-
- // Test after the child exits.
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit");
-
- try {
- CodeDweller::Child child(cmd);
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close() after child exits");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
-
- CodeDweller::Child child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildOpen() {
-
- // Test with no waiting.
- try {
- CodeDweller::Child child(childName);
- child.close();
- child.open(childName);
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close()/open() with no waiting");
- return false;
- }
-
- // Test with waiting.
- try {
- CodeDweller::Child child(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- child.open(childName);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- child.close();
- } catch (std::exception &e) {
- EXCEPTION_TERM("close()/open() with 100 ms waiting");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
-
- CodeDweller::Child child;
-
- child.close();
-
- NO_EXCEPTION_TERM("close() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- return true;
- }
-
- bool testChildReadWrite() {
-
- size_t bufSize = 15;
- int nChar = bufSize - 1; // Size of each packet.
-
-
- try {
- CodeDweller::Child child(bufSize);
- std::ostringstream childOutput;
- std::vector<std::string> expectedChildOutput;
- char readChar;
-
- // Generate input.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
- int nChar = bufSize - 1;
- int nLines = 2;
- for (int iLine = 0; iLine < nLines; iLine++) {
- std::string line;
- line.erase();
- for (int iChar = 0; iChar < nChar; iChar++) {
- line.push_back(randomLetter[std::rand() % 26]);
- }
- expectedChildOutput.push_back(line);
- }
-
- // Test exception.
- try {
- child.write("Should Throw");
- NO_EXCEPTION_TERM(" writer called without run()");
- return false;
- } catch (std::exception &e) {
- }
-
- child.open(childName);
-
- // Write, read, put back, and reread each character.
- int nLine = 0;
- for (std::string &line : expectedChildOutput) {
-
- nLine++;
-
- // Try to queue.
- bool done;
- int nTries = 100;
-
- for (int i = 0; i < nTries; i++) {
-
- done = child.write(line);
-
- if (done) {
- break;
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- if (!done) {
- std::ostringstream temp;
-
- temp << " Failure from write(std::string const &) on line " << nLine;
- RETURN_FALSE(temp.str());
- }
-
- // Wait for transmission.
- for (int i = 0; i < nTries; i++) {
-
- done = child.isFinishedWriting();
-
- if (done) {
- break;
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- if (!done) {
- std::ostringstream temp;
-
- temp << " Failure from isFinishedWriting() on line " << nLine;
- RETURN_FALSE(temp.str());
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- // Read one line.
- std::string readLine, temp;
- int nCharToRead, nCharRead;
-
- readLine.clear();
- nCharToRead = line.size();
-
- for (int i = 0; i < nTries; i++) {
-
- nCharRead = child.read(temp, nCharToRead);
-
- nCharToRead -= nCharRead;
-
- if (nCharToRead <= 0) {
- break;
- }
-
- }
-
- if (0 != nCharToRead) {
- std::ostringstream temp;
-
- temp << " Failure from () on line " << nLine
- << ": nCharRead: " << nCharRead;
- RETURN_FALSE(temp.str());
- }
-
- // Convert to upper case.
- std::string expectedLine;
-
- expectedLine = line;
- for (auto &c : expectedLine) {
-
- c = toupper(c);
-
- }
-
- // Compare.
- if (expectedLine != readLine) {
- std::cout << " Failure in testReadWrite." << std::endl;
- std::cout << " Expected: '" << expectedLine
- << "'\n Received: '" << readLine << "'" << std::endl;
- return false;
-
- }
-
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" Failure in testChildIsDone: write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testReadWrite: Child program did not exit."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("read()/write()/isFinishedWriting()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildRead() {
-
- try {
-
- size_t bufSize = 32;
- CodeDweller::Child child(bufSize);
-
- // Test exception.
- try {
- std::string temp;
- (void) child.read(temp);
- NO_EXCEPTION_TERM(" read() called without open()");
- return false;
- } catch (std::exception &e) {
- }
-
- std::ostringstream childOutput;
- std::string expectedChildOutput("This is a test");
- std::string readBuf;
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("write");
-
- child.open(cmd);
-
- int nTries = 100;
-
- for (int i = 0; i < nTries; i++) {
-
- if (child.read(readBuf) > 0) {
- childOutput << readBuf;
- }
- if (child.errorOccurred(readBuf)) {
- std::ostringstream temp;
-
- temp << " Failure in testReader: " << readBuf;
- RETURN_FALSE(temp.str());
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
-
- }
-
- // Check.
- if (childOutput.str() != expectedChildOutput) {
- std::cout << " read() failure in testChildRead." << std::endl;
- std::cout << " Expected: '" << expectedChildOutput
- << "'\n Received: '" << childOutput.str() << "'"
- << std::endl;
- return false;
- }
-
- // Sleep to let the child exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << "first isDone() failure in testReader." << std::endl;
- return false;
- }
-
- if (!child.isDone()) {
- std::cout << "second isDone() failure in testReader." << std::endl;
- return false;
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("reader()");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildNonBlockingReadWrite() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- std::uint16_t
- nominalAboveMin_ms = 15,
- delta_ms = 0;
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- // Check for available input with input.
- std::string childInput("abc");
-
- if (!child.write(childInput)) {
- RETURN_FALSE(" first write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- // Read one character.
- std::string readBuf;
-
- if (child.read(readBuf, 1) != 1) {
- RETURN_FALSE(" read() failure");
- }
- if ("A" != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Read.
- if (child.read(readBuf, 2) != 2) {
- RETURN_FALSE(" read() failure");
- }
- if ("BC" != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Check that no input is available.
- if (child.read(readBuf) != 0) {
- RETURN_FALSE(" read() failure");
- }
- if (!readBuf.empty()) {
- RETURN_FALSE(" read() failure");
- }
-
- // Fill input buffer.
- std::string output("abcdefghijklmnopprstuvwxyz");
- std::string expectedInput(output);
- std::string expectedOutputAfter1(output.substr(bufSize));
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- if (child.writeAndShrink(output) != bufSize) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
- if (output != expectedOutputAfter1) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- int expectedLeftOver = output.size();
-
- if (child.writeAndShrink(output) != expectedLeftOver) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
- if (!output.empty()) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- if (child.read(readBuf) != bufSize) {
- RETURN_FALSE(" read() failure");
- }
-
- if (readBuf != expectedInput.substr(0, bufSize)) {
- RETURN_FALSE(" read() failure)");
- }
-
- if (child.read(readBuf) != expectedLeftOver) {
- RETURN_FALSE(" read() failure");
- }
-
- if (expectedInput.substr(bufSize) != readBuf) {
- RETURN_FALSE(" read() failure");
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- if (!child.isDone()) {
- std::cout << " Failure in testNonblockingReader: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildNonBlockingReadWrite: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
- #if 0
- std::cout << "\nCodeDweller::ChildStream unit tests\n" << std::endl;
-
- RUN_TEST(testChildStreamIsDone);
- RUN_TEST(testChildStreamIsRunning);
- RUN_TEST(testChildStreamResult);
- RUN_TEST(testChildStreamClose);
- RUN_TEST(testChildStreamReader);
- RUN_TEST(testChildStreamReaderWriter);
- RUN_TEST(testChildStreamBinaryRead);
- RUN_TEST(testChildStreamNonBlockingRead);
-
- std::cout << "\nCodeDweller::Child unit tests\n" << std::endl;
-
- RUN_TEST(testChildIsDone);
- RUN_TEST(testChildIsRunning);
- RUN_TEST(testChildResult);
- RUN_TEST(testChildClose);
- RUN_TEST(testChildOpen);
- #endif
- RUN_TEST(testChildRead);
- #if 1
- RUN_TEST(testChildReadWrite);
- RUN_TEST(testChildNonBlockingReadWrite);
- #endif
- SUMMARY;
-
- return 0;
- }
|