1 | // |
2 | // StreamCopierTest.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 "StreamCopierTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/StreamCopier.h" |
15 | #include <sstream> |
16 | |
17 | |
18 | using Poco::StreamCopier; |
19 | |
20 | |
21 | StreamCopierTest::StreamCopierTest(const std::string& rName): CppUnit::TestCase(rName) |
22 | { |
23 | } |
24 | |
25 | |
26 | StreamCopierTest::~StreamCopierTest() |
27 | { |
28 | } |
29 | |
30 | |
31 | void StreamCopierTest::testBufferedCopy() |
32 | { |
33 | { |
34 | std::string src; |
35 | for (int i = 0; i < 255; ++i) src += char(i); |
36 | std::istringstream istr(src); |
37 | std::ostringstream ostr; |
38 | std::streamsize n = StreamCopier::copyStream(istr, ostr); |
39 | assertTrue (ostr.str() == src); |
40 | assertTrue (n == src.size()); |
41 | } |
42 | { |
43 | std::string src; |
44 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
45 | std::istringstream istr(src); |
46 | std::ostringstream ostr; |
47 | std::streamsize n = StreamCopier::copyStream(istr, ostr, 100); |
48 | assertTrue (ostr.str() == src); |
49 | assertTrue (n == src.size()); |
50 | } |
51 | { |
52 | std::string src; |
53 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
54 | std::istringstream istr(src); |
55 | std::ostringstream ostr; |
56 | std::streamsize n = StreamCopier::copyStream(istr, ostr, 128); |
57 | assertTrue (ostr.str() == src); |
58 | assertTrue (n == src.size()); |
59 | } |
60 | { |
61 | std::string src; |
62 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
63 | std::istringstream istr(src); |
64 | std::ostringstream ostr; |
65 | std::streamsize n = StreamCopier::copyStream(istr, ostr, 512); |
66 | assertTrue (ostr.str() == src); |
67 | assertTrue (n == src.size()); |
68 | } |
69 | } |
70 | |
71 | |
72 | void StreamCopierTest::testUnbufferedCopy() |
73 | { |
74 | std::string src; |
75 | for (int i = 0; i < 255; ++i) src += char(i); |
76 | std::istringstream istr(src); |
77 | std::ostringstream ostr; |
78 | std::streamsize n = StreamCopier::copyStreamUnbuffered(istr, ostr); |
79 | assertTrue (ostr.str() == src); |
80 | assertTrue (n == src.size()); |
81 | } |
82 | |
83 | |
84 | void StreamCopierTest::testCopyToString() |
85 | { |
86 | std::string src; |
87 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
88 | std::istringstream istr(src); |
89 | std::string dest; |
90 | std::streamsize n = StreamCopier::copyToString(istr, dest, 100); |
91 | assertTrue (src == dest); |
92 | assertTrue (n == src.size()); |
93 | } |
94 | |
95 | |
96 | #if defined(POCO_HAVE_INT64) |
97 | void StreamCopierTest::testBufferedCopy64() |
98 | { |
99 | { |
100 | std::string src; |
101 | for (int i = 0; i < 255; ++i) src += char(i); |
102 | std::istringstream istr(src); |
103 | std::ostringstream ostr; |
104 | Poco::UInt64 n = StreamCopier::copyStream64(istr, ostr); |
105 | assertTrue (ostr.str() == src); |
106 | assertTrue (n == src.size()); |
107 | } |
108 | { |
109 | std::string src; |
110 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
111 | std::istringstream istr(src); |
112 | std::ostringstream ostr; |
113 | Poco::UInt64 n = StreamCopier::copyStream64(istr, ostr, 100); |
114 | assertTrue (ostr.str() == src); |
115 | assertTrue (n == src.size()); |
116 | } |
117 | { |
118 | std::string src; |
119 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
120 | std::istringstream istr(src); |
121 | std::ostringstream ostr; |
122 | Poco::UInt64 n = StreamCopier::copyStream64(istr, ostr, 128); |
123 | assertTrue (ostr.str() == src); |
124 | assertTrue (n == src.size()); |
125 | } |
126 | { |
127 | std::string src; |
128 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
129 | std::istringstream istr(src); |
130 | std::ostringstream ostr; |
131 | Poco::UInt64 n = StreamCopier::copyStream64(istr, ostr, 512); |
132 | assertTrue (ostr.str() == src); |
133 | assertTrue (n == src.size()); |
134 | } |
135 | } |
136 | |
137 | |
138 | void StreamCopierTest::testUnbufferedCopy64() |
139 | { |
140 | std::string src; |
141 | for (int i = 0; i < 255; ++i) src += char(i); |
142 | std::istringstream istr(src); |
143 | std::ostringstream ostr; |
144 | Poco::UInt64 n = StreamCopier::copyStreamUnbuffered64(istr, ostr); |
145 | assertTrue (ostr.str() == src); |
146 | assertTrue (n == src.size()); |
147 | } |
148 | |
149 | |
150 | void StreamCopierTest::testCopyToString64() |
151 | { |
152 | std::string src; |
153 | for (int i = 0; i < 512; ++i) src += char(i % 256); |
154 | std::istringstream istr(src); |
155 | std::string dest; |
156 | Poco::UInt64 n = StreamCopier::copyToString64(istr, dest, 100); |
157 | assertTrue (src == dest); |
158 | assertTrue (n == src.size()); |
159 | } |
160 | #endif |
161 | |
162 | |
163 | void StreamCopierTest::setUp() |
164 | { |
165 | } |
166 | |
167 | |
168 | void StreamCopierTest::tearDown() |
169 | { |
170 | } |
171 | |
172 | |
173 | CppUnit::Test* StreamCopierTest::suite() |
174 | { |
175 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StreamCopierTest" ); |
176 | |
177 | CppUnit_addTest(pSuite, StreamCopierTest, testBufferedCopy); |
178 | CppUnit_addTest(pSuite, StreamCopierTest, testUnbufferedCopy); |
179 | CppUnit_addTest(pSuite, StreamCopierTest, testCopyToString); |
180 | |
181 | #if defined(POCO_HAVE_INT64) |
182 | CppUnit_addTest(pSuite, StreamCopierTest, testBufferedCopy64); |
183 | CppUnit_addTest(pSuite, StreamCopierTest, testUnbufferedCopy64); |
184 | CppUnit_addTest(pSuite, StreamCopierTest, testCopyToString64); |
185 | #endif |
186 | |
187 | return pSuite; |
188 | } |
189 | |