From: Christian P. <cp...@us...> - 2004-12-31 15:02:08
|
Update of /cvsroot/pclasses/pclasses2/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13840/test Modified Files: IOTest.cpp Log Message: Added ProcessIO test. Index: IOTest.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/test/IOTest.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- IOTest.cpp 30 Dec 2004 17:18:26 -0000 1.1 +++ IOTest.cpp 31 Dec 2004 15:01:57 -0000 1.2 @@ -21,25 +21,122 @@ #include "Test.h" #include "pclasses/IO/IOStream.h" #include "pclasses/System/File.h" +#include "pclasses/System/Pipe.h" +#include "pclasses/System/Process.h" +#include "pclasses/System/ProcessIO.h" + +#include <string> namespace P { +using IO::IOStream; using System::File; +using System::Pipe; +using System::Process; +using System::ProcessIO; class FileTest: public UnitTest { public: void run() throw() { - File file("pclasses_file_test.tmp", File::Write); - P_TEST(file.write("test", 4) == 4); + char data[] = "test"; + + File file("pclasses_file_test.tmp", File::ReadWrite); + P_TEST(file.valid()); + + // write data to file + P_TEST(file.write(data, 4) == 4); P_TEST(file.size() == 4); + // seek to beginning of file P_TEST(file.seek(0, File::SeekSet) == 0); - //@fixme .. wtf ? why is errno==EBADF ? + // read data from file char tmp[4]; P_TEST(file.read(tmp, 4) == 4); + + // verify read data + for(int i = 0; i < 4; i++) + P_TEST(tmp[i] == data[i]); + + // read past the end ... + P_TEST(file.read(tmp, 1) == 0); + P_TEST(file.eof()); + + // close file + file.close(); + P_TEST(!file.valid()); + P_TEST_EXCEPTION(file.close(), LogicError); + + // this must fail cause pclasses_file_test.tmp already exists... + P_TEST_EXCEPTION( + file.open("pclasses_file_test.tmp", File::Read, File::CreateFail), + IO::IOError + ); + } +}; + +class PipeTest: public UnitTest { + public: + + void run() throw() + { + char data[] = "test"; + + Pipe::Pair pipes = Pipe::create(); + + Pipe& readPipe = pipes.first; + Pipe& writePipe = pipes.second; + + P_TEST(writePipe.valid()); + P_TEST(writePipe.write(data, 4) == 4); + + char tmp[4]; + P_TEST(readPipe.valid()); + P_TEST(!readPipe.eof()); + P_TEST(readPipe.read(tmp, 4) == 4); + + for(int i = 0; i < 4; i++) + P_TEST(tmp[i] == data[i]); + + writePipe.close(); + P_TEST(!writePipe.valid()); + P_TEST_EXCEPTION(writePipe.close(), LogicError); + + P_TEST(readPipe.read(tmp, 1) == 0); + P_TEST(readPipe.eof()); + + readPipe.close(); + P_TEST(!readPipe.valid()); + P_TEST_EXCEPTION(readPipe.close(), LogicError); + } +}; + +class ProcessIOTest: public UnitTest { + public: + + void run() throw() + { + char data[] = "test"; + + Process proc("./IOTest"); + proc.addArg("arg1"); + + P_TEST(proc.state() == Process::Stopped); + proc.start(Process::RedirectAll); + P_TEST(proc.state() == Process::Running); + + char tmp[4]; + ProcessIO& io = proc.processIO(); + + P_TEST(io.read(tmp, 4) == 4); + P_TEST(io.readErr(tmp, 4) == 4); + P_TEST(io.write(data, 4) == 4); + P_TEST(io.write("\r", 1) == 1); + + P_TEST(proc.wait() == 0); + P_TEST(proc.state() == Process::Stopped); } }; @@ -47,8 +144,26 @@ int main(int argc, char* argv[]) { + if(argc > 1) + { + std::cout << "test"; + std::cerr << "test"; + std::string tmp; + std::cin >> tmp; + if(tmp != "test") + return 1; + + return 0; + } + + P::PipeTest pt; + pt.run(); + P::FileTest ft; ft.run(); - + + P::ProcessIOTest piot; + piot.run(); + return 0; } |