12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952 |
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <sstream>
- #include <algorithm>
- #include <random>
-
- #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
-
-
- bool doReadWrite(size_t bufSize,
- size_t nominalAboveMin_ms,
- size_t delta_ms,
- size_t maxChar) {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- // Characters for testing.
- char randomLetter[] = "abcdefghijklmnop#rstuvwxyz";
-
- // Calculate how often to output a "." to indicate progress.
- int maxChunks = maxChar / bufSize;
- int nChunks = 0;
- int nDots = 50;
- int outputEveryChunk = maxChunks / nDots / 2;
-
- if (outputEveryChunk == 0) {
- outputEveryChunk = 2;
- }
-
- // Send and receive random-sized chunks of data.
- size_t nChar = 0;
-
- while (nChar < maxChar) {
-
- std::uniform_int_distribution<int> randomChunk(1, bufSize * 4);
- std::default_random_engine generator(4828943);
-
- nChunks++;
-
- if (nChunks % outputEveryChunk == 0) {
- std::cout << ".";
- std::cout.flush();
- }
-
- int chunkSize;
- std::string sentChunk, expectedChunk;
-
- // Get a random chunk size for sending.
- chunkSize = randomChunk(generator);
-
- if (chunkSize + nChar > maxChar) {
- chunkSize = maxChar - nChar;
- }
-
- // Generate output to child.
- sentChunk.clear();
-
- for (size_t i = 0; i < chunkSize; i++) {
- sentChunk.push_back(randomLetter[std::rand() % 26]);
- }
-
- // Generated expected input from child.
- expectedChunk = sentChunk;
-
- for (int i = 0; i < expectedChunk.size(); i++) {
- expectedChunk[i] = std::toupper(expectedChunk[i]);
- }
-
- // Send the data.
- while (!sentChunk.empty()) {
-
- if (child.writeAndShrink(sentChunk) == 0) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
-
- // Read and compare while sending data.
- std::string chunkFromChild;
- size_t chunkFromChildSize;
-
- (void) child.read(chunkFromChild);
-
- chunkFromChildSize = chunkFromChild.size();
-
- if (!chunkFromChild.empty()) {
-
- if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
- expectedChunk.erase(0, chunkFromChildSize);
- } else {
- std::cout << "Expected: \""
- << expectedChunk.substr(0, chunkFromChildSize)
- << "\", received \"" << chunkFromChild << "\"."
- << std::endl;
- std::cout << "Remaining chunk: \"" << expectedChunk << "\""
- << std::endl;
- child.write("q");
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- RETURN_FALSE(" comparison failure");
- }
-
- }
-
- }
-
- // Read and compare after all data was sent.
- while (!expectedChunk.empty()) {
-
- std::string chunkFromChild;
- size_t chunkFromChildSize;
-
- (void) child.read(chunkFromChild);
-
- chunkFromChildSize = chunkFromChild.size();
-
- if (0 == chunkFromChildSize) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- } else {
-
- if (chunkFromChild == expectedChunk.substr(0, chunkFromChildSize)) {
- expectedChunk.erase(0, chunkFromChildSize);
- } else {
- std::cout << "Expected: \""
- << expectedChunk.substr(0, chunkFromChildSize)
- << "\", received \"" << chunkFromChild << "\"."
- << std::endl;
- std::cout << "Remaining chunk: \"" << expectedChunk << "\""
- << std::endl;
- child.write("q");
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- RETURN_FALSE(" comparison failure");
- }
-
- }
-
- }
-
- nChar += chunkSize;
-
- }
-
- // 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 testChildNonblockingReadWrite2: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildNonBlockingReadWrite2: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // 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("testChildStreamIsRunning");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildStreamResult() {
-
- try {
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("quit"); // Child sleeps for 50 ms and then exits.
-
- 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);
-
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- 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 test.
- 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);
- readLine += temp;
-
- 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 testChildIsFinishedWriting() {
-
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
-
- size_t bufSize = 16;
- std::uint16_t
- nominalAboveMin_ms = 100,
- delta_ms = 0;
-
- try {
-
- CodeDweller::Child child(cmd,
- bufSize,
- nominalAboveMin_ms,
- delta_ms);
-
- if (!child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- if (!child.write("0123456789")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Busy wait until the data is written.
- while (!child.isFinishedWriting())
- ;
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Writer thread should be pausing now.
- if (!child.write("0123456789")) {
- RETURN_FALSE(" write() failure");
- }
-
- if (child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
-
- if (child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(80));
-
- if (!child.isFinishedWriting()) {
- RETURN_FALSE(" isFinishedWriting() failure");
- }
-
- // Send exit message.
- if (!child.write("q")) {
- RETURN_FALSE(" write() failure");
- }
-
- // Verify exit.
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
-
- if (!child.isDone()) {
- std::cout << " Failure in testIsFinishedWriting: "
- << "Child program did not exit."
- << std::endl;
- return false;
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("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;
- size_t nRead = 0;
-
- for (int i = 0; i < nTries; i++) {
-
- nRead += child.read(readBuf);
- childOutput << readBuf;
-
- if (expectedChildOutput.size() == nRead) {
- break;
- }
-
- if (child.errorOccurred(readBuf)) {
- std::ostringstream temp;
-
- temp << " Failure in testReader on try " << i << ": " << 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 testChildNonBlockingReadWrite1() {
-
- 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));
-
- size_t nRead = child.read(readBuf);
-
- if (nRead != bufSize) {
- RETURN_FALSE(" read() failure");
- }
-
- if (readBuf != expectedInput.substr(0, bufSize)) {
- RETURN_FALSE(" read() failure)");
- }
-
- // Wait for reader thread to fill input buffer.
- std::this_thread::sleep_for(std::chrono::milliseconds(25));
-
- 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 testChildNonblockingReadWrite1: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildNonBlockingReadWrite1: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("Non-blocking reader test");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildNonBlockingReadWrite2() {
-
- size_t bufSize, maxChar;
- std::uint16_t
- nominalAboveMin_ms = 0,
- delta_ms = 0;
-
- bufSize = 16;
- maxChar = 1000 * 100;
- std::cout << "\n many small buffers";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- bufSize = 128 * 1024;
- maxChar = 1000 * 10;
- std::cout << "\n one big buffer";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- bufSize = 128 * 1024;
- maxChar = 1000 * 1000 * 10;
- std::cout << "\n many big buffers";
- std::cout.flush();
- if (!doReadWrite(bufSize, nominalAboveMin_ms, delta_ms, maxChar)) {
- RETURN_FALSE(" read()/write() failure");
- }
-
- return true;
-
- }
-
- bool testChildVectorReadWrite() {
-
- 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 that write fails.
- std::vector<char> badVector(bufSize + 1);
- if (child.write(badVector)) {
- RETURN_FALSE(" write() failure");
- }
-
- // Check write().
- std::vector<char> childInput = {'a', 'b', 'c'};
-
- if (!child.write(childInput)) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Read one character.
- std::vector<char> readBuf;
-
- if (child.read(readBuf, 1) != 1) {
- RETURN_FALSE(" read() failure");
- }
- if ('A' != readBuf[0]) {
- RETURN_FALSE(" read() failure");
- }
-
- // Read.
- if (child.read(readBuf, 2) != 2) {
- RETURN_FALSE(" read() failure");
- }
- if (std::vector<char>({'B', 'C'}) != 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::vector<char> output = {
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
- 'n', 'o', 'p', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
- };
- std::vector<char> expectedInput(output);
-
- for (int i = 0; i < output.size(); i++) {
- expectedInput[i] = std::toupper(output[i]);
- }
-
- if (child.write(output, bufSize) != bufSize) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- size_t nRequested = bufSize / 2;
-
- // Check partial read.
- size_t nRead = child.read(readBuf, nRequested);
-
- if (nRead != nRequested) {
- RETURN_FALSE(" read() failure");
- }
-
- if (readBuf != std::vector<char>(expectedInput.begin(),
- expectedInput.begin() + nRead)) {
- RETURN_FALSE(" read() failure)");
- }
-
- // Check remainder.
- readBuf.resize(bufSize * 2);
- nRead = child.read(readBuf);
-
- if ( (bufSize - nRequested) != nRead) {
- RETURN_FALSE(" read() failure");
- }
- if (readBuf != std::vector<char>(expectedInput.begin() + nRequested,
- expectedInput.begin() + bufSize)) {
- RETURN_FALSE(" read() failure)");
- }
-
- // Write remainder.
- output.erase(output.begin(),
- output.begin() + bufSize);
- expectedInput.erase(expectedInput.begin(),
- expectedInput.begin() + bufSize);
- size_t nExpectedBytes = output.size();
-
- if (child.writeAndShrink(output) != nExpectedBytes) {
- RETURN_FALSE(" writeAndShrink() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- if (child.read(readBuf) != nExpectedBytes) {
- RETURN_FALSE(" read() failure");
- }
-
- if (readBuf != expectedInput) {
- 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 testChildVectorReadWrite: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildVectorReadWrite: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("testChildVectorReadWrite");
- return false;
- }
-
- return true;
-
- }
-
- bool testChildReadDelimited() {
-
- 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 delimiter at beginning of buffer.
- std::vector<char> childInput = {'a', 'b', 'c', 'd', 'e'};
-
- if (!child.write(childInput)) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Check
- std::vector<char> readVec;
-
- if (!child.readDelimited(readVec, "AB")) {
- RETURN_FALSE(" readDelimited() failure");
- }
- if (!readVec.empty()) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- if (child.readDelimited(readVec, "n")) {
- RETURN_FALSE(" readDelimited() failure");
- }
- if (child.readDelimited(readVec, "nlelk;lqm;foim")) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- // Empty buffer.
- std::string temp;
-
- (void) child.read(temp);
- if ("CDE" != temp) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- // Check delimiter at middle of buffer.
- if (!child.write(childInput)) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Check
- std::string readStr;
- std::string delim = "CD";
-
- if (!child.readDelimited(readStr, delim)) {
- RETURN_FALSE(" readDelimited() failure");
- }
- if ("AB" != readStr) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- delim = "n";
- if (child.readDelimited(readStr, delim)) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- delim = "fimsoim3m4";
- if (child.readDelimited(readStr, delim)) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- readStr = "orirw";
- delim = "";
- if (!child.readDelimited(readStr, delim)) {
- RETURN_FALSE(" readDelimited() failure");
- }
- if (!readStr.empty()) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- // Empty buffer.
- (void) child.read(temp);
- if ("E" != temp) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- // Check delimiter at end of buffer.
- if (!child.write(childInput)) {
- RETURN_FALSE(" write() failure");
- }
-
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
- // Check
- std::vector<char> delimVec = {'E'};
-
- if (!child.readDelimited(readStr, delimVec)) {
- RETURN_FALSE(" readDelimited() failure");
- }
- if ("ABCD" != readStr) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- delimVec[0] = 'n';
- if (child.readDelimited(readStr, delimVec)) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- delimVec.push_back('4');
- delimVec.push_back(',');
- delimVec.push_back('m');
- delimVec.push_back('y');
- delimVec.push_back('t');
- delimVec.push_back('2');
- delimVec.push_back('e');
- delimVec.push_back('t');
- if (child.readDelimited(readStr, delimVec)) {
- RETURN_FALSE(" readDelimited() failure");
- }
-
- // Empty buffer.
- (void) child.read(temp);
- if ("" != temp) {
- RETURN_FALSE(" readDelimited() 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 testChildReadDelimited: "
- << "Child program did not exit." << std::endl;
- return false;
- }
-
- std::string errorDescription;
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildReadDelimited: "
- << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("testChildReadDelimited");
- return false;
- }
-
- return true;
-
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // End of tests ////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- int main()
- {
-
- 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);
- RUN_TEST(testChildIsFinishedWriting);
- RUN_TEST(testChildRead);
- RUN_TEST(testChildReadWrite);
- RUN_TEST(testChildNonBlockingReadWrite1);
- RUN_TEST(testChildNonBlockingReadWrite2);
- RUN_TEST(testChildVectorReadWrite);
- RUN_TEST(testChildReadDelimited);
-
- SUMMARY;
-
- return 0;
- }
|