1//
2// BinaryReaderWriterTest.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 "BinaryReaderWriterTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/BinaryWriter.h"
15#include "Poco/BinaryReader.h"
16#include "Poco/Buffer.h"
17#include <sstream>
18
19
20using Poco::BinaryWriter;
21using Poco::MemoryBinaryWriter;
22using Poco::BinaryReader;
23using Poco::MemoryBinaryReader;
24using Poco::Buffer;
25using Poco::Int32;
26using Poco::UInt32;
27using Poco::Int64;
28using Poco::UInt64;
29
30
31BinaryReaderWriterTest::BinaryReaderWriterTest(const std::string& rName): CppUnit::TestCase(rName)
32{
33}
34
35
36BinaryReaderWriterTest::~BinaryReaderWriterTest()
37{
38}
39
40
41void BinaryReaderWriterTest::testNative()
42{
43 std::stringstream sstream;
44 BinaryWriter writer(sstream);
45 BinaryReader reader(sstream);
46 write(writer);
47 read(reader);
48}
49
50
51void BinaryReaderWriterTest::testBigEndian()
52{
53 std::stringstream sstream;
54 BinaryWriter writer(sstream, BinaryWriter::BIG_ENDIAN_BYTE_ORDER);
55 BinaryReader reader(sstream, BinaryReader::UNSPECIFIED_BYTE_ORDER);
56 assertTrue (writer.byteOrder() == BinaryWriter::BIG_ENDIAN_BYTE_ORDER);
57 writer.writeBOM();
58 write(writer);
59 reader.readBOM();
60 assertTrue (reader.byteOrder() == BinaryReader::BIG_ENDIAN_BYTE_ORDER);
61 read(reader);
62}
63
64
65void BinaryReaderWriterTest::testLittleEndian()
66{
67 std::stringstream sstream;
68 BinaryWriter writer(sstream, BinaryWriter::LITTLE_ENDIAN_BYTE_ORDER);
69 BinaryReader reader(sstream, BinaryReader::UNSPECIFIED_BYTE_ORDER);
70 assertTrue (writer.byteOrder() == BinaryWriter::LITTLE_ENDIAN_BYTE_ORDER);
71 writer.writeBOM();
72 write(writer);
73 reader.readBOM();
74 assertTrue (reader.byteOrder() == BinaryReader::LITTLE_ENDIAN_BYTE_ORDER);
75 read(reader);
76}
77
78
79void BinaryReaderWriterTest::write(BinaryWriter& writer)
80{
81 writer << true;
82 writer << false;
83 writer << 'a';
84 writer << (short) -100;
85 writer << (unsigned short) 50000;
86 writer << -123456;
87 writer << (unsigned) 123456;
88#ifndef POCO_LONG_IS_64_BIT
89 writer << (long) -1234567890;
90 writer << (unsigned long) 1234567890;
91#endif // POCO_LONG_IS_64_BIT
92 writer << (Int64) -1234567890;
93 writer << (UInt64) 1234567890;
94
95 writer << (float) 1.5;
96 writer << (double) -1.5;
97
98 writer << "foo";
99 writer << "";
100
101 writer << std::string("bar");
102 writer << std::string();
103
104 writer.write7BitEncoded((UInt32) 100);
105 writer.write7BitEncoded((UInt32) 1000);
106 writer.write7BitEncoded((UInt32) 10000);
107 writer.write7BitEncoded((UInt32) 100000);
108 writer.write7BitEncoded((UInt32) 1000000);
109
110 writer.write7BitEncoded((UInt64) 100);
111 writer.write7BitEncoded((UInt64) 1000);
112 writer.write7BitEncoded((UInt64) 10000);
113 writer.write7BitEncoded((UInt64) 100000);
114 writer.write7BitEncoded((UInt64) 1000000);
115
116 std::vector<int> vec;
117 vec.push_back(1);
118 vec.push_back(2);
119 vec.push_back(3);
120 writer << vec;
121
122 writer.writeRaw("RAW");
123}
124
125
126void BinaryReaderWriterTest::read(BinaryReader& reader)
127{
128 bool b;
129 reader >> b;
130 assertTrue (b);
131 reader >> b;
132 assertTrue (!b);
133
134 char c;
135 reader >> c;
136 assertTrue (c == 'a');
137
138 short shortv;
139 reader >> shortv;
140 assertTrue (shortv == -100);
141
142 unsigned short ushortv;
143 reader >> ushortv;
144 assertTrue (ushortv == 50000);
145
146 int intv;
147 reader >> intv;
148 assertTrue (intv == -123456);
149
150 unsigned uintv;
151 reader >> uintv;
152 assertTrue (uintv == 123456);
153
154#ifndef POCO_LONG_IS_64_BIT
155 long longv;
156 reader >> longv;
157 assertTrue (longv == -1234567890);
158
159 unsigned long ulongv;
160 reader >> ulongv;
161 assertTrue (ulongv == 1234567890);
162#endif // POCO_LONG_IS_64_BIT
163
164 Int64 int64v;
165 reader >> int64v;
166 assertTrue (int64v == -1234567890);
167
168 UInt64 uint64v;
169 reader >> uint64v;
170 assertTrue (uint64v == 1234567890);
171
172 float floatv;
173 reader >> floatv;
174 assertTrue (floatv == 1.5);
175
176 double doublev;
177 reader >> doublev;
178 assertTrue (doublev == -1.5);
179
180 std::string str;
181 reader >> str;
182 assertTrue (str == "foo");
183 reader >> str;
184 assertTrue (str == "");
185 reader >> str;
186 assertTrue (str == "bar");
187 reader >> str;
188 assertTrue (str == "");
189
190 UInt32 uint32v;
191 reader.read7BitEncoded(uint32v);
192 assertTrue (uint32v == 100);
193 reader.read7BitEncoded(uint32v);
194 assertTrue (uint32v == 1000);
195 reader.read7BitEncoded(uint32v);
196 assertTrue (uint32v == 10000);
197 reader.read7BitEncoded(uint32v);
198 assertTrue (uint32v == 100000);
199 reader.read7BitEncoded(uint32v);
200 assertTrue (uint32v == 1000000);
201
202 reader.read7BitEncoded(uint64v);
203 assertTrue (uint64v == 100);
204 reader.read7BitEncoded(uint64v);
205 assertTrue (uint64v == 1000);
206 reader.read7BitEncoded(uint64v);
207 assertTrue (uint64v == 10000);
208 reader.read7BitEncoded(uint64v);
209 assertTrue (uint64v == 100000);
210 reader.read7BitEncoded(uint64v);
211 assertTrue (uint64v == 1000000);
212
213 std::vector<int> vec;
214 reader >> vec;
215 assertTrue (vec.size() == 3);
216 assertTrue (vec[0] == 1);
217 assertTrue (vec[1] == 2);
218 assertTrue (vec[2] == 3);
219
220 reader.readRaw(3, str);
221 assertTrue (str == "RAW");
222}
223
224
225void BinaryReaderWriterTest::testWrappers()
226{
227 bool b = false; char c = '0'; int i = 0;
228 Buffer<char> buf(2 * sizeof(bool) + sizeof(char) + 2 * sizeof(int));
229
230 MemoryBinaryWriter writer(buf);
231 writer << true;
232 writer << false;
233 writer << 'a';
234 writer << 1;
235 writer << -1;
236
237 MemoryBinaryReader reader(writer.data());
238 reader >> b; assertTrue (b);
239 reader >> b; assertTrue (!b);
240 reader >> c; assertTrue ('a' == c);
241 assertTrue (reader.available() == sizeof(i) * 2);
242 reader >> i; assertTrue (1 == i);
243 assertTrue (reader.available() == sizeof(i));
244 reader >> i; assertTrue (-1 == i);
245 assertTrue (reader.available() == 0);
246
247 reader.setExceptions(std::istream::eofbit);
248 try
249 {
250 reader >> i;
251 fail ("must throw on EOF");
252 } catch(std::exception&) { }
253}
254
255
256void BinaryReaderWriterTest::setUp()
257{
258}
259
260
261void BinaryReaderWriterTest::tearDown()
262{
263}
264
265
266CppUnit::Test* BinaryReaderWriterTest::suite()
267{
268 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("BinaryReaderWriterTest");
269
270 CppUnit_addTest(pSuite, BinaryReaderWriterTest, testNative);
271 CppUnit_addTest(pSuite, BinaryReaderWriterTest, testBigEndian);
272 CppUnit_addTest(pSuite, BinaryReaderWriterTest, testLittleEndian);
273 CppUnit_addTest(pSuite, BinaryReaderWriterTest, testWrappers);
274
275 return pSuite;
276}
277