Ver código fonte

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 anos atrás
pai
commit
901128f27d
1 arquivos alterados com 80 adições e 43 exclusões
  1. 80
    43
      TestChild/testChild.cpp

+ 80
- 43
TestChild/testChild.cpp Ver arquivo

@@ -57,12 +57,11 @@ bool result;
// Tests ///////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool
testIsDone() {
bool testIsDone() {
try {
CodeDweller::ChildStream child(childName);
CodeDweller::ChildStream child;
// Test exception if called out-of-order.
try {
@@ -72,7 +71,9 @@ testIsDone() {
} catch (std::exception &e) {
}
child.run();
// Start the child.
child.open(childName);
if (child.isDone()) {
std::cout << "isDone() failure; returned true." << std::endl;
return false;
@@ -98,8 +99,50 @@ testIsDone() {
}
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 {
std::vector<std::string> cmd;
@@ -117,8 +160,6 @@ testResult() {
} catch (std::exception &e) {
}
child.run();
// Test exception if called while child is running.
try {
(void) child.result();
@@ -143,27 +184,24 @@ testResult() {
return true;
}
bool
testTerminate() {
bool testClose() {
// Test with no waiting.
try {
CodeDweller::ChildStream child(childName);
child.run();
child.terminate();
child.close();
} catch (std::exception &e) {
EXCEPTION_TERM("terminate() with no waiting");
EXCEPTION_TERM("close() with no waiting");
return false;
}
// Test with waiting.
try {
CodeDweller::ChildStream child(childName);
child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate();
child.close();
} catch (std::exception &e) {
EXCEPTION_TERM("terminate() with 100 ms waiting");
EXCEPTION_TERM("close() with 100 ms waiting");
return false;
}
@@ -175,19 +213,22 @@ testTerminate() {
try {
CodeDweller::ChildStream child(cmd);
child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate();
child.close();
} catch (std::exception &e) {
EXCEPTION_TERM("terminate() after child exits");
EXCEPTION_TERM("close() after child exits");
return false;
}
// Test exception thrown for out-of-order calling.
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;
} catch (std::exception &e) {
}
@@ -195,12 +236,11 @@ testTerminate() {
return true;
}
bool
testReaderWriter() {
bool testReaderWriter() {
try {
size_t bufSize = 15;
CodeDweller::ChildStream child(childName, bufSize);
CodeDweller::ChildStream child(bufSize);
std::ostringstream childOutput;
std::vector<std::string> expectedChildOutput;
char readChar;
@@ -234,7 +274,7 @@ testReaderWriter() {
} catch (std::exception &e) {
}
child.run();
child.open(childName);
// Write, read, put back, and reread each character.
for (std::string line : expectedChildOutput) {
@@ -312,17 +352,12 @@ testReaderWriter() {
}
bool
testReader() {
bool testReader() {
try {
std::vector<std::string> cmd;
cmd.push_back(childName);
cmd.push_back("write");
size_t bufSize = 32;
CodeDweller::ChildStream child(cmd, bufSize);
CodeDweller::ChildStream child(bufSize);
// Test exception.
try {
@@ -340,7 +375,12 @@ testReader() {
std::string expectedChildOutput("This is a test");
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 >> std::noskipws;
if (!child) {
@@ -400,8 +440,7 @@ testReader() {
}
bool
testBinaryRead() {
bool testBinaryRead() {
try {
std::vector<std::string> cmd;
@@ -409,9 +448,9 @@ testBinaryRead() {
cmd.push_back(childName);
size_t bufSize = 164;
CodeDweller::ChildStream child(cmd, bufSize);
CodeDweller::ChildStream child(bufSize);
child.run();
child.open(cmd);
// Write.
std::string childInput("abc");
@@ -495,8 +534,7 @@ testBinaryRead() {
}
bool
testNonBlockingRead() {
bool testNonBlockingRead() {
try {
std::vector<std::string> cmd;
@@ -506,8 +544,6 @@ testNonBlockingRead() {
size_t bufSize = 16;
CodeDweller::ChildStream child(cmd, bufSize);
child.run();
// Check for available input with no input.
if (child.numBytesAvailable() != 0) {
RETURN_FALSE(" numBytesAvailable() did not return expected 0");
@@ -653,8 +689,9 @@ int main()
CodeDweller::ChildStream child(childName);
RUN_TEST(testIsDone);
RUN_TEST(testIsRunning);
RUN_TEST(testResult);
RUN_TEST(testTerminate);
RUN_TEST(testClose);
RUN_TEST(testReader);
RUN_TEST(testReaderWriter);
RUN_TEST(testBinaryRead);

Carregando…
Cancelar
Salvar