]> git.tdb.fi Git - libs/core.git/blob - tests/pipe.cpp
Add a refcount function to RefPtr
[libs/core.git] / tests / pipe.cpp
1 #include <msp/core/thread.h>
2 #include <msp/io/pipe.h>
3 #include <msp/io/poll.h>
4 #include <msp/time/timedelta.h>
5 #include <msp/test/test.h>
6
7 using namespace Msp;
8
9 class PipeTests: public Test::RegisteredTest<PipeTests>
10 {
11 public:
12         PipeTests();
13
14         static const char *get_name() { return "Pipe"; }
15
16 private:
17         void readwrite();
18         void poll();
19 };
20
21
22 class PipeWriter: public Thread
23 {
24 private:
25         IO::Pipe &pipe;
26
27 public:
28         PipeWriter(IO::Pipe &);
29
30 private:
31         virtual void main();
32 };
33
34
35 PipeTests::PipeTests()
36 {
37         add(&PipeTests::readwrite, "Read/write");
38         add(&PipeTests::poll, "Poll");
39 }
40
41 void PipeTests::readwrite()
42 {
43         IO::Pipe pipe;
44         PipeWriter writer(pipe);
45
46         for(unsigned i=0; i<256; ++i)
47         {
48                 unsigned char c = pipe.get();
49                 if(c!=i)
50                         fail("Invalid data");
51         }
52
53         writer.join();
54 }
55
56 void PipeTests::poll()
57 {
58         IO::Pipe pipe;
59
60         pipe.put(1);
61         IO::PollEvent ev = IO::poll(pipe, IO::P_INPUT);
62         EXPECT_EQUAL(ev, IO::P_INPUT);
63
64         pipe.get();
65
66         ev = IO::poll(pipe, IO::P_INPUT, 100*Time::msec);
67         EXPECT_EQUAL(ev, IO::P_NONE);
68 }
69
70
71 PipeWriter::PipeWriter(IO::Pipe &p):
72         pipe(p)
73 {
74         launch();
75 }
76
77 void PipeWriter::main()
78 {
79         for(unsigned i=0; i<256; ++i)
80                 pipe.put(i);
81 }