Browse Source

Tested new constructors, isRunning(), renaming terminate() to close(),

and calling close() in ~ChildStream.


git-svn-id: https://svn.microneil.com/svn/CodeDweller-Tests/trunk@41 b3372362-9eaa-4a85-aa2b-6faa1ab7c995
master
adeniz 9 years ago
parent
commit
901128f27d
1 changed files with 80 additions and 43 deletions
  1. 80
    43
      TestChild/testChild.cpp

+ 80
- 43
TestChild/testChild.cpp View File

// Tests /////////////////////////////////////////////////////////////////////// // Tests ///////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool
testIsDone() {
bool testIsDone() {
try { try {
CodeDweller::ChildStream child(childName);
CodeDweller::ChildStream child;
// Test exception if called out-of-order. // Test exception if called out-of-order.
try { try {
} catch (std::exception &e) { } catch (std::exception &e) {
} }
child.run();
// Start the child.
child.open(childName);
if (child.isDone()) { if (child.isDone()) {
std::cout << "isDone() failure; returned true." << std::endl; std::cout << "isDone() failure; returned true." << std::endl;
return false; return false;
} }
bool
testResult() {
bool testIsRunning() {
try {
CodeDweller::ChildStream child;
if (child.isRunning()) {
std::cout << "isRunning() failure; returned true before starting."
<< std::endl;
return false;
}
// Start the child.
child.open(childName);
if (!child.isRunning()) {
std::cout << "isRunning() failure; returned false." << std::endl;
return false;
}
// Command the child to exit.
child << 'q';
child.flush();
// Sleep to let the child exit.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.close();
if (child.isRunning()) {
std::cout << "isRunning() failure; returned true after stopping."
<< std::endl;
return false;
}
} catch (std::exception &e) {
EXCEPTION_TERM("isRunning()");
return false;
}
return true;
}
bool testResult() {
try { try {
std::vector<std::string> cmd; std::vector<std::string> cmd;
} catch (std::exception &e) { } catch (std::exception &e) {
} }
child.run();
// Test exception if called while child is running. // Test exception if called while child is running.
try { try {
(void) child.result(); (void) child.result();
return true; return true;
} }
bool
testTerminate() {
bool testClose() {
// Test with no waiting. // Test with no waiting.
try { try {
CodeDweller::ChildStream child(childName); CodeDweller::ChildStream child(childName);
child.run();
child.terminate();
child.close();
} catch (std::exception &e) { } catch (std::exception &e) {
EXCEPTION_TERM("terminate() with no waiting");
EXCEPTION_TERM("close() with no waiting");
return false; return false;
} }
// Test with waiting. // Test with waiting.
try { try {
CodeDweller::ChildStream child(childName); CodeDweller::ChildStream child(childName);
child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate();
child.close();
} catch (std::exception &e) { } catch (std::exception &e) {
EXCEPTION_TERM("terminate() with 100 ms waiting");
EXCEPTION_TERM("close() with 100 ms waiting");
return false; return false;
} }
try { try {
CodeDweller::ChildStream child(cmd); CodeDweller::ChildStream child(cmd);
child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate();
child.close();
} catch (std::exception &e) { } catch (std::exception &e) {
EXCEPTION_TERM("terminate() after child exits");
EXCEPTION_TERM("close() after child exits");
return false; return false;
} }
// Test exception thrown for out-of-order calling. // Test exception thrown for out-of-order calling.
try { try {
CodeDweller::ChildStream child(cmd);
child.terminate();
NO_EXCEPTION_TERM("terminate() called without run()");
CodeDweller::ChildStream child;
child.close();
NO_EXCEPTION_TERM("close() called without run()");
return false; return false;
} catch (std::exception &e) { } catch (std::exception &e) {
} }
return true; return true;
} }
bool
testReaderWriter() {
bool testReaderWriter() {
try { try {
size_t bufSize = 15; size_t bufSize = 15;
CodeDweller::ChildStream child(childName, bufSize);
CodeDweller::ChildStream child(bufSize);
std::ostringstream childOutput; std::ostringstream childOutput;
std::vector<std::string> expectedChildOutput; std::vector<std::string> expectedChildOutput;
char readChar; char readChar;
} catch (std::exception &e) { } catch (std::exception &e) {
} }
child.run();
child.open(childName);
// Write, read, put back, and reread each character. // Write, read, put back, and reread each character.
for (std::string line : expectedChildOutput) { for (std::string line : expectedChildOutput) {
} }
bool
testReader() {
bool testReader() {
try { try {
std::vector<std::string> cmd;
cmd.push_back(childName);
cmd.push_back("write");
size_t bufSize = 32; size_t bufSize = 32;
CodeDweller::ChildStream child(cmd, bufSize);
CodeDweller::ChildStream child(bufSize);
// Test exception. // Test exception.
try { try {
std::string expectedChildOutput("This is a test"); std::string expectedChildOutput("This is a test");
char readChar; char readChar;
child.run();
std::vector<std::string> cmd;
cmd.push_back(childName);
cmd.push_back("write");
child.open(cmd);
child.exceptions(std::istream::badbit); child.exceptions(std::istream::badbit);
child >> std::noskipws; child >> std::noskipws;
if (!child) { if (!child) {
} }
bool
testBinaryRead() {
bool testBinaryRead() {
try { try {
std::vector<std::string> cmd; std::vector<std::string> cmd;
cmd.push_back(childName); cmd.push_back(childName);
size_t bufSize = 164; size_t bufSize = 164;
CodeDweller::ChildStream child(cmd, bufSize);
CodeDweller::ChildStream child(bufSize);
child.run();
child.open(cmd);
// Write. // Write.
std::string childInput("abc"); std::string childInput("abc");
} }
bool
testNonBlockingRead() {
bool testNonBlockingRead() {
try { try {
std::vector<std::string> cmd; std::vector<std::string> cmd;
size_t bufSize = 16; size_t bufSize = 16;
CodeDweller::ChildStream child(cmd, bufSize); CodeDweller::ChildStream child(cmd, bufSize);
child.run();
// Check for available input with no input. // Check for available input with no input.
if (child.numBytesAvailable() != 0) { if (child.numBytesAvailable() != 0) {
RETURN_FALSE(" numBytesAvailable() did not return expected 0"); RETURN_FALSE(" numBytesAvailable() did not return expected 0");
CodeDweller::ChildStream child(childName); CodeDweller::ChildStream child(childName);
RUN_TEST(testIsDone); RUN_TEST(testIsDone);
RUN_TEST(testIsRunning);
RUN_TEST(testResult); RUN_TEST(testResult);
RUN_TEST(testTerminate);
RUN_TEST(testClose);
RUN_TEST(testReader); RUN_TEST(testReader);
RUN_TEST(testReaderWriter); RUN_TEST(testReaderWriter);
RUN_TEST(testBinaryRead); RUN_TEST(testBinaryRead);

Loading…
Cancel
Save