Browse Source

Tested inheritance of Child from std::iostream.


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

+ 64
- 64
TestChild/testChild.cpp View File

try { try {
CodeDweller::Child child(childName);
CodeDweller::ChildStream child(childName);
// Test exception if called out-of-order. // Test exception if called out-of-order.
try { try {
} }
// Command the child to exit. // Command the child to exit.
child.childStream << 'q';
child.childStream.flush();
child << 'q';
child.flush();
// Sleep to let the child exit. // Sleep to let the child exit.
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
cmd.push_back(childName); cmd.push_back(childName);
cmd.push_back("quit"); cmd.push_back("quit");
CodeDweller::Child child(cmd);
CodeDweller::ChildStream child(cmd);
// Test exception if called out-of-order. // Test exception if called out-of-order.
try { try {
// Test with no waiting. // Test with no waiting.
try { try {
CodeDweller::Child child(childName);
CodeDweller::ChildStream child(childName);
child.run(); child.run();
child.terminate(); child.terminate();
} catch (std::exception &e) { } catch (std::exception &e) {
// Test with waiting. // Test with waiting.
try { try {
CodeDweller::Child child(childName);
CodeDweller::ChildStream child(childName);
child.run(); child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate(); child.terminate();
cmd.push_back("quit"); cmd.push_back("quit");
try { try {
CodeDweller::Child child(cmd);
CodeDweller::ChildStream child(cmd);
child.run(); child.run();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.terminate(); child.terminate();
// Test exception thrown for out-of-order calling. // Test exception thrown for out-of-order calling.
try { try {
CodeDweller::Child child(cmd);
CodeDweller::ChildStream child(cmd);
child.terminate(); child.terminate();
NO_EXCEPTION_TERM("terminate() called without run()"); NO_EXCEPTION_TERM("terminate() called without run()");
return false; return false;
try { try {
size_t bufSize = 15; size_t bufSize = 15;
CodeDweller::Child child(childName, bufSize);
CodeDweller::ChildStream child(childName, bufSize);
std::ostringstream childOutput; std::ostringstream childOutput;
std::vector<std::string> expectedChildOutput; std::vector<std::string> expectedChildOutput;
char readChar; char readChar;
// Test exception. // Test exception.
try { try {
child.childStream.exceptions(std::ostream::failbit | std::ostream::badbit);
child.childStream << bufSize;
child.childStream.flush();
child.exceptions(std::ostream::failbit | std::ostream::badbit);
child << bufSize;
child.flush();
NO_EXCEPTION_TERM(" writer called without run()"); NO_EXCEPTION_TERM(" writer called without run()");
return false; return false;
} catch (std::exception &e) { } catch (std::exception &e) {
// Clear the writer stream. // Clear the writer stream.
try { try {
child.childStream.clear();
child.clear();
} catch (std::exception &e) { } catch (std::exception &e) {
} }
ptr = line.data(); ptr = line.data();
for (std::string::size_type i = 0; i < line.length(); i++) { for (std::string::size_type i = 0; i < line.length(); i++) {
child.childStream << ptr[i];
if (!child.childStream) {
child << ptr[i];
if (!child) {
RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad"); RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
} }
} }
child.childStream.flush();
if (!child.childStream) {
child.flush();
if (!child) {
RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad"); RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
} }
readLine.erase(); readLine.erase();
for (std::string::size_type i = 0; i < line.length(); i++) { for (std::string::size_type i = 0; i < line.length(); i++) {
child.childStream >> readChar;
if (!child.childStream) {
child >> readChar;
if (!child) {
RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad"); RETURN_FALSE(" Failure in testReaderWriter: reader stream is bad");
} }
readLine.push_back(readChar); readLine.push_back(readChar);
} }
// Send exit message. // Send exit message.
child.childStream << 'q';
child.childStream.flush();
if (!child.childStream) {
child << 'q';
child.flush();
if (!child) {
RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad"); RETURN_FALSE(" Failure in testReaderWriter: writer stream is bad");
} }
cmd.push_back("write"); cmd.push_back("write");
size_t bufSize = 32; size_t bufSize = 32;
CodeDweller::Child child(cmd, bufSize);
CodeDweller::ChildStream child(cmd, bufSize);
// Test exception. // Test exception.
try { try {
int temp; int temp;
child.childStream.exceptions(std::istream::failbit | std::istream::badbit);
child.childStream >> temp;
child.exceptions(std::istream::failbit | std::istream::badbit);
child >> temp;
NO_EXCEPTION_TERM(" reader called without run()"); NO_EXCEPTION_TERM(" reader called without run()");
return false; return false;
} catch (std::exception &e) { } catch (std::exception &e) {
} }
child.childStream.clear();
child.childStream.clear();
child.clear();
child.clear();
std::ostringstream childOutput; std::ostringstream childOutput;
std::string expectedChildOutput("This is a test"); std::string expectedChildOutput("This is a test");
char readChar; char readChar;
child.run(); child.run();
child.childStream.exceptions(std::istream::badbit);
child.childStream >> std::noskipws;
if (!child.childStream) {
child.exceptions(std::istream::badbit);
child >> std::noskipws;
if (!child) {
RETURN_FALSE(" Failure in testReader: reader stream is bad"); RETURN_FALSE(" Failure in testReader: reader stream is bad");
} }
while (child.childStream >> readChar) {
while (child >> readChar) {
if (!child.childStream) {
if (!child) {
RETURN_FALSE(" Failure in testReader: reader stream is bad"); RETURN_FALSE(" Failure in testReader: reader stream is bad");
} }
child.childStream.putback(readChar);
if (!child.childStream) {
child.putback(readChar);
if (!child) {
RETURN_FALSE(" Failure in testReader: reader stream is bad"); RETURN_FALSE(" Failure in testReader: reader stream is bad");
} }
child.childStream >> readChar;
if (!child.childStream) {
child >> readChar;
if (!child) {
RETURN_FALSE(" Failure in testReader: reader stream is bad"); RETURN_FALSE(" Failure in testReader: reader stream is bad");
} }
childOutput << readChar; childOutput << readChar;
} }
if (!child.childStream.eof()) {
if (!child.eof()) {
RETURN_FALSE(" Failure in testReader: Error occured before " RETURN_FALSE(" Failure in testReader: Error occured before "
"EOF was reached while reading"); "EOF was reached while reading");
} }
cmd.push_back(childName); cmd.push_back(childName);
size_t bufSize = 164; size_t bufSize = 164;
CodeDweller::Child child(cmd, bufSize);
CodeDweller::ChildStream child(cmd, bufSize);
child.run(); child.run();
// Write. // Write.
std::string childInput("abc"); std::string childInput("abc");
child.childStream << childInput;
child.childStream.flush();
child << childInput;
child.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Read one character. // Read one character.
char ch; char ch;
child.childStream.read(&ch, 1);
child.read(&ch, 1);
if (ch != 'A') { if (ch != 'A') {
RETURN_FALSE(" reader.read() returned incorrect value"); RETURN_FALSE(" reader.read() returned incorrect value");
// Read. // Read.
char buf[bufSize * 2]; char buf[bufSize * 2];
child.childStream.read(buf, 2);
child.read(buf, 2);
buf[2] = '\0'; buf[2] = '\0';
std::string input(buf); std::string input(buf);
expectedInput[i] = std::toupper(output[i]); expectedInput[i] = std::toupper(output[i]);
} }
child.childStream << output;
child.childStream.flush();
child << output;
child.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
child.childStream.read(&ch, 1);
child.read(&ch, 1);
int index = 0; int index = 0;
size_t nBytesRead = expectedInput.size() - 1; size_t nBytesRead = expectedInput.size() - 1;
child.childStream.read(buf, nBytesRead);
child.read(buf, nBytesRead);
buf[nBytesRead] = '\0'; buf[nBytesRead] = '\0';
if (expectedInput.substr(index, nBytesRead) != std::string(buf)) { if (expectedInput.substr(index, nBytesRead) != std::string(buf)) {
} }
// Send exit message. // Send exit message.
child.childStream << 'q';
child.childStream.flush();
if (!child.childStream) {
child << 'q';
child.flush();
if (!child) {
RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad"); RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
} }
cmd.push_back(childName); cmd.push_back(childName);
size_t bufSize = 16; size_t bufSize = 16;
CodeDweller::Child child(cmd, bufSize);
CodeDweller::ChildStream child(cmd, bufSize);
child.run(); child.run();
// Check for available input with input. // Check for available input with input.
std::string childInput("abc"); std::string childInput("abc");
child.childStream << childInput;
child.childStream.flush();
child << childInput;
child.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Check that input is available. // Check that input is available.
// Read one character. // Read one character.
char ch; char ch;
child.childStream.read(&ch, 1);
child.read(&ch, 1);
if (ch != 'A') { if (ch != 'A') {
RETURN_FALSE(" reader.read() returned incorrect value"); RETURN_FALSE(" reader.read() returned incorrect value");
// Read. // Read.
char buf[bufSize * 2]; char buf[bufSize * 2];
child.childStream.read(buf, 2);
child.read(buf, 2);
buf[2] = '\0'; buf[2] = '\0';
std::string input(buf); std::string input(buf);
expectedInput[i] = std::toupper(output[i]); expectedInput[i] = std::toupper(output[i]);
} }
child.childStream << output;
child.childStream.flush();
child << output;
child.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (child.numBytesAvailable() != 1) { if (child.numBytesAvailable() != 1) {
RETURN_FALSE(" numBytesAvailable() did not return expected 1"); RETURN_FALSE(" numBytesAvailable() did not return expected 1");
} }
child.childStream.read(&ch, 1);
child.read(&ch, 1);
int index = 0; int index = 0;
} }
std::fill_n(buf, sizeof(buf), 0); std::fill_n(buf, sizeof(buf), 0);
child.childStream.read(buf, nBytesAvailable);
child.read(buf, nBytesAvailable);
if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) { if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
RETURN_FALSE(" reader.read() failure"); RETURN_FALSE(" reader.read() failure");
} }
std::fill_n(buf, sizeof(buf), 0); std::fill_n(buf, sizeof(buf), 0);
child.childStream.read(buf, nBytesAvailable);
child.read(buf, nBytesAvailable);
if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) { if (expectedInput.substr(index, nBytesAvailable) != std::string(buf)) {
RETURN_FALSE(" reader.read() failure"); RETURN_FALSE(" reader.read() failure");
} }
// Send exit message. // Send exit message.
child.childStream << 'q';
child.childStream.flush();
if (!child.childStream) {
child << 'q';
child.flush();
if (!child) {
RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad"); RETURN_FALSE(" Failure in testNonblockingReader: writer stream is bad");
} }
int main() int main()
{ {
std::cout << "CodeDweller::Child unit tests" << std::endl << std::endl;
std::cout << "CodeDweller::ChildStream unit tests" << std::endl << std::endl;
CodeDweller::Child child(childName);
CodeDweller::ChildStream child(childName);
RUN_TEST(testIsDone); RUN_TEST(testIsDone);
RUN_TEST(testResult); RUN_TEST(testResult);

Loading…
Cancel
Save