X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=tests%2Fpipe.cpp;fp=tests%2Fpipe.cpp;h=0310bdfc7f88df47abfbf4f92e9935de57ab9647;hp=0000000000000000000000000000000000000000;hb=120023d8da0aabcb803a87111608ce84c94661f8;hpb=79482ba7aea1b79c7a310c940cc0292532ef3bcb diff --git a/tests/pipe.cpp b/tests/pipe.cpp new file mode 100644 index 0000000..0310bdf --- /dev/null +++ b/tests/pipe.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +using namespace Msp; + +class PipeTests: public Test::RegisteredTest +{ +public: + PipeTests(); + + static const char *get_name() { return "Pipe"; } + +private: + void readwrite(); + void poll(); +}; + + +class PipeWriter: public Thread +{ +private: + IO::Pipe &pipe; + +public: + PipeWriter(IO::Pipe &); + +private: + virtual void main(); +}; + + +PipeTests::PipeTests() +{ + add(&PipeTests::readwrite, "Read/write"); + add(&PipeTests::poll, "Poll"); +} + +void PipeTests::readwrite() +{ + IO::Pipe pipe; + PipeWriter writer(pipe); + + for(unsigned i=0; i<256; ++i) + { + unsigned char c = pipe.get(); + if(c!=i) + fail("Invalid data"); + } + + writer.join(); +} + +void PipeTests::poll() +{ + IO::Pipe pipe; + + pipe.put(1); + IO::PollEvent ev = IO::poll(pipe, IO::P_INPUT); + EXPECT_EQUAL(ev, IO::P_INPUT); + + pipe.get(); + + ev = IO::poll(pipe, IO::P_INPUT, 100*Time::msec); + EXPECT_EQUAL(ev, IO::P_NONE); +} + + +PipeWriter::PipeWriter(IO::Pipe &p): + pipe(p) +{ + launch(); +} + +void PipeWriter::main() +{ + for(unsigned i=0; i<256; ++i) + pipe.put(i); +}