1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053 |
- #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 testChildStdInClose() {
-
- std::string errorDescription;
-
- // Test with no child process.
- try {
- CodeDweller::Child child;
-
- child.closeStdIn();
-
- NO_EXCEPTION_TERM("closeStdIn() with no child process");
- return false;
- } catch (std::exception &e) {
- }
-
- // Test with no waiting.
- try {
- CodeDweller::Child child(childName);
-
- child.closeStdIn();
-
- child.close();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("closeStdIn() with no waiting");
- return false;
- }
-
- // Run child that waits for stdin to close.
- std::vector<std::string> cmd;
-
- cmd.push_back(childName);
- cmd.push_back("checkStdinEOF");
-
- try {
-
- CodeDweller::Child child(cmd);
-
- child.closeStdIn();
-
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
-
- int32_t result = child.result();
-
- if (15 != result) {
- std::cout << "closeStdIn() failure; returned " << result
- << " instead of 15." << std::endl;
- return false;
- }
-
- child.close();
-
- if (child.errorOccurred(errorDescription)) {
- std::ostringstream temp;
-
- temp << " Failure in: testChildStdInClose: " << errorDescription;
- RETURN_FALSE(temp.str());
- }
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("closeStdIn() or close()");
- return false;
- }
-
- // Test after the child exits.
- cmd.clear();
- 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.closeStdIn();
-
- } catch (std::exception &e) {
- EXCEPTION_TERM("closeStdIn() after child exits");
- return false;
- }
-
- // Test exception thrown for out-of-order calling.
- try {
- CodeDweller::Child child;
-
- child.closeStdIn();
-
- NO_EXCEPTION_TERM("closeStdIn() 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(testChildStdInClose);
- 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;
- }
|