1//
2// FIFOBufferStreamTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "FIFOBufferStreamTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/FIFOBuffer.h"
15#include "Poco/FIFOBufferStream.h"
16#include "Poco/Delegate.h"
17
18
19using Poco::FIFOBuffer;
20using Poco::FIFOBufferStream;
21using Poco::delegate;
22
23
24FIFOBufferStreamTest::FIFOBufferStreamTest(const std::string& rName): CppUnit::TestCase(rName)
25{
26}
27
28
29FIFOBufferStreamTest::~FIFOBufferStreamTest()
30{
31}
32
33
34void FIFOBufferStreamTest::testInput()
35{
36 const char* data = "This is a test";
37 FIFOBuffer fb1(data, 14);
38 FIFOBufferStream str1(fb1);
39 assertTrue (str1.rdbuf()->fifoBuffer().isFull());
40
41 int c = str1.get();
42 assertTrue (c == 'T');
43 c = str1.get();
44 assertTrue (c == 'h');
45
46 std::string str;
47 str1 >> str;
48 assertTrue (str == "is");
49
50 char buffer[32];
51 str1.read(buffer, sizeof(buffer));
52 assertTrue (str1.gcount() == 10);
53 buffer[str1.gcount()] = 0;
54 assertTrue (std::string(" is a test") == buffer);
55
56 const char* data2 = "123";
57 FIFOBufferStream str2(data2, 3);
58
59 c = str2.get();
60 assertTrue (c == '1');
61 assertTrue (str2.good());
62 c = str2.get();
63 assertTrue (c == '2');
64 str2.unget();
65 c = str2.get();
66 assertTrue (c == '2');
67 assertTrue (str2.good());
68 c = str2.get();
69 assertTrue (c == '3');
70 assertTrue (str2.good());
71 c = str2.get();
72 assertTrue (c == -1);
73 assertTrue (str2.eof());
74}
75
76
77void FIFOBufferStreamTest::testOutput()
78{
79 char output[64];
80 FIFOBufferStream iostr1(output, 64);
81 iostr1 << "This is a test " << 42 << std::ends << std::flush;
82 assertTrue (std::string("This is a test 42") == output);
83}
84
85
86void FIFOBufferStreamTest::testNotify()
87{
88 FIFOBuffer fb(18);
89 FIFOBufferStream iostr(fb);
90 assertTrue (iostr.rdbuf()->fifoBuffer().isEmpty());
91
92 assertTrue (0 == _readableToNot);
93 assertTrue (0 == _notToReadable);
94 assertTrue (0 == _writableToNot);
95 assertTrue (0 == _notToWritable);
96
97 iostr.readable += delegate(this, &FIFOBufferStreamTest::onReadable);
98 iostr.writable += delegate(this, &FIFOBufferStreamTest::onWritable);
99
100 iostr << "This is a test " << 42 << std::ends << std::flush;
101 assertTrue (iostr.rdbuf()->fifoBuffer().isFull());
102
103 assertTrue (0 == _readableToNot);
104 assertTrue (1 == _notToReadable);
105 assertTrue (1 == _writableToNot);
106 assertTrue (0 == _notToWritable);
107
108 char input[64];
109 iostr >> input;
110 assertTrue (std::string("This") == input);
111 assertTrue (iostr.rdbuf()->fifoBuffer().isEmpty());
112
113 assertTrue (1 == _readableToNot);
114 assertTrue (1 == _notToReadable);
115 assertTrue (1 == _writableToNot);
116 assertTrue (1 == _notToWritable);
117
118 iostr >> input;
119 assertTrue (std::string("is") == input);
120
121 assertTrue (1 == _readableToNot);
122 assertTrue (1 == _notToReadable);
123 assertTrue (1 == _writableToNot);
124 assertTrue (1 == _notToWritable);
125
126 iostr >> input;
127 assertTrue (std::string("a") == input);
128
129 assertTrue (1 == _readableToNot);
130 assertTrue (1 == _notToReadable);
131 assertTrue (1 == _writableToNot);
132 assertTrue (1 == _notToWritable);
133
134 iostr >> input;
135 assertTrue (std::string("test") == input);
136
137 assertTrue (1 == _readableToNot);
138 assertTrue (1 == _notToReadable);
139 assertTrue (1 == _writableToNot);
140 assertTrue (1 == _notToWritable);
141
142 iostr >> input;
143 assertTrue (std::string("42") == input);
144
145 assertTrue (1 == _readableToNot);
146 assertTrue (1 == _notToReadable);
147 assertTrue (1 == _writableToNot);
148 assertTrue (1 == _notToWritable);
149
150 iostr.clear();
151 assertTrue (iostr.good());
152 iostr << "This is a test " << 42 << std::ends << std::flush;
153 assertTrue (iostr.rdbuf()->fifoBuffer().isFull());
154
155 assertTrue (1 == _readableToNot);
156 assertTrue (2 == _notToReadable);
157 assertTrue (2 == _writableToNot);
158 assertTrue (1 == _notToWritable);
159
160 iostr.readable -= delegate(this, &FIFOBufferStreamTest::onReadable);
161 iostr.writable -= delegate(this, &FIFOBufferStreamTest::onWritable);
162}
163
164
165void FIFOBufferStreamTest::onReadable(bool& b)
166{
167 if (b) ++_notToReadable;
168 else ++_readableToNot;
169};
170
171
172void FIFOBufferStreamTest::onWritable(bool& b)
173{
174 if (b) ++_notToWritable;
175 else ++_writableToNot;
176}
177
178
179void FIFOBufferStreamTest::setUp()
180{
181 _readableToNot = 0;
182 _notToReadable = 0;
183 _writableToNot = 0;
184 _notToWritable = 0;
185}
186
187
188void FIFOBufferStreamTest::tearDown()
189{
190}
191
192
193CppUnit::Test* FIFOBufferStreamTest::suite()
194{
195 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FIFOBufferStreamTest");
196
197 CppUnit_addTest(pSuite, FIFOBufferStreamTest, testInput);
198 CppUnit_addTest(pSuite, FIFOBufferStreamTest, testOutput);
199 CppUnit_addTest(pSuite, FIFOBufferStreamTest, testNotify);
200
201 return pSuite;
202}
203