blob: cf628ed295ee8e7bd16fa9ec0ec78b5d4b01ac5e [file] [log] [blame]
David Reissfaebedd2007-09-17 23:20:38 +00001/*
David Reissea2cba82009-03-30 21:35:00 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Reiss3cc9dab2010-10-06 17:10:21 +000020#define __STDC_FORMAT_MACROS
21
David Reiss9a961e72010-10-06 17:10:23 +000022#ifndef _GNU_SOURCE
23#define _GNU_SOURCE // needed for getopt_long
24#endif
25
David Reiss3cc9dab2010-10-06 17:10:21 +000026#include <stdint.h>
Jim King9de9b1f2015-04-30 16:03:34 -040027#ifdef HAVE_INTTYPES_H
David Reiss3cc9dab2010-10-06 17:10:21 +000028#include <inttypes.h>
Jim King9de9b1f2015-04-30 16:03:34 -040029#endif
David Reissfaebedd2007-09-17 23:20:38 +000030#include <cstddef>
David Reissfaebedd2007-09-17 23:20:38 +000031#include <fstream>
32#include <iostream>
Jake Farrell5d02b802014-01-07 21:42:01 -050033#include <thrift/cxxfunctional.h>
David Reiss3cc9dab2010-10-06 17:10:21 +000034
35#include <boost/random.hpp>
David Reiss9a961e72010-10-06 17:10:23 +000036#include <boost/shared_array.hpp>
37#include <boost/test/unit_test.hpp>
David Reiss3cc9dab2010-10-06 17:10:21 +000038
Roger Meier49ff8b12012-04-13 09:12:31 +000039#include <thrift/transport/TBufferTransports.h>
40#include <thrift/transport/TZlibTransport.h>
David Reissfaebedd2007-09-17 23:20:38 +000041
David Reiss9a961e72010-10-06 17:10:23 +000042using namespace std;
David Reiss9a961e72010-10-06 17:10:23 +000043using namespace apache::thrift::transport;
44
David Reissf2abcf92010-10-06 17:10:24 +000045boost::mt19937 rng;
David Reissfaebedd2007-09-17 23:20:38 +000046
David Reissf2abcf92010-10-06 17:10:24 +000047/*
48 * Utility code
49 */
David Reissfaebedd2007-09-17 23:20:38 +000050
David Reissf2abcf92010-10-06 17:10:24 +000051class SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052public:
David Reissf2abcf92010-10-06 17:10:24 +000053 virtual ~SizeGenerator() {}
54 virtual unsigned int getSize() = 0;
David Reissfaebedd2007-09-17 23:20:38 +000055};
56
David Reissf2abcf92010-10-06 17:10:24 +000057class ConstantSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010058public:
David Reissf2abcf92010-10-06 17:10:24 +000059 ConstantSizeGenerator(unsigned int value) : value_(value) {}
Konrad Grochowski16a23a62014-11-13 15:33:38 +010060 virtual unsigned int getSize() { return value_; }
David Reissf2abcf92010-10-06 17:10:24 +000061
Konrad Grochowski16a23a62014-11-13 15:33:38 +010062private:
David Reissf2abcf92010-10-06 17:10:24 +000063 unsigned int value_;
64};
65
66class LogNormalSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010067public:
68 LogNormalSizeGenerator(double mean, double std_dev)
69 : gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
David Reissf2abcf92010-10-06 17:10:24 +000070
71 virtual unsigned int getSize() {
72 // Loop until we get a size of 1 or more
73 while (true) {
74 unsigned int value = static_cast<unsigned int>(gen_());
75 if (value >= 1) {
76 return value;
77 }
78 }
79 }
80
Konrad Grochowski16a23a62014-11-13 15:33:38 +010081private:
82 boost::variate_generator<boost::mt19937, boost::lognormal_distribution<double> > gen_;
David Reissf2abcf92010-10-06 17:10:24 +000083};
David Reiss3cc9dab2010-10-06 17:10:21 +000084
Jim King7848d882015-04-06 21:38:06 -040085boost::shared_array<uint8_t> gen_uniform_buffer(uint32_t buf_len, uint8_t c) {
David Reiss3cc9dab2010-10-06 17:10:21 +000086 uint8_t* buf = new uint8_t[buf_len];
87 memset(buf, c, buf_len);
Jim King7848d882015-04-06 21:38:06 -040088 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +000089}
90
Jim King7848d882015-04-06 21:38:06 -040091boost::shared_array<uint8_t> gen_compressible_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +000092 uint8_t* buf = new uint8_t[buf_len];
93
94 // Generate small runs of alternately increasing and decreasing bytes
95 boost::uniform_smallint<uint32_t> run_length_distribution(1, 64);
96 boost::uniform_smallint<uint8_t> byte_distribution(0, UINT8_MAX);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
98 byte_generator(rng, byte_distribution);
99 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint32_t> >
100 run_len_generator(rng, run_length_distribution);
David Reiss3cc9dab2010-10-06 17:10:21 +0000101
102 uint32_t idx = 0;
103 int8_t step = 1;
104 while (idx < buf_len) {
105 uint32_t run_length = run_len_generator();
106 if (idx + run_length > buf_len) {
107 run_length = buf_len - idx;
108 }
109
110 uint8_t byte = byte_generator();
111 for (uint32_t n = 0; n < run_length; ++n) {
112 buf[idx] = byte;
113 ++idx;
114 byte += step;
115 }
116
117 step *= -1;
118 }
119
Jim King7848d882015-04-06 21:38:06 -0400120 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000121}
122
Jim King7848d882015-04-06 21:38:06 -0400123boost::shared_array<uint8_t> gen_random_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +0000124 uint8_t* buf = new uint8_t[buf_len];
125
126 boost::uniform_smallint<uint8_t> distribution(0, UINT8_MAX);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
128 generator(rng, distribution);
David Reiss3cc9dab2010-10-06 17:10:21 +0000129
130 for (uint32_t n = 0; n < buf_len; ++n) {
131 buf[n] = generator();
132 }
133
Jim King7848d882015-04-06 21:38:06 -0400134 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000135}
David Reissfaebedd2007-09-17 23:20:38 +0000136
David Reissf2abcf92010-10-06 17:10:24 +0000137/*
138 * Test functions
139 */
140
Jim King7848d882015-04-06 21:38:06 -0400141void test_write_then_read(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
Roger Meier611f90c2011-12-11 22:08:51 +0000142 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
143 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400144 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000145 zlib_trans->finish();
David Reissfaebedd2007-09-17 23:20:38 +0000146
David Reiss9a961e72010-10-06 17:10:23 +0000147 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000148 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000149 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400150 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000151 zlib_trans->verifyChecksum();
152}
David Reissfaebedd2007-09-17 23:20:38 +0000153
Jim King7848d882015-04-06 21:38:06 -0400154void test_separate_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000155 // This one is tricky. I separate the last byte of the stream out
156 // into a separate crbuf_. The last byte is part of the checksum,
157 // so the entire read goes fine, but when I go to verify the checksum
158 // it isn't there. The original implementation complained that
159 // the stream was not complete. I'm about to go fix that.
160 // It worked. Awesome.
Roger Meier611f90c2011-12-11 22:08:51 +0000161 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
162 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400163 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000164 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000165 string tmp_buf;
166 membuf->appendBufferToString(tmp_buf);
David Reissa0e11592010-10-06 17:10:27 +0000167 zlib_trans.reset(new TZlibTransport(membuf,
David Reiss9a961e72010-10-06 17:10:23 +0000168 TZlibTransport::DEFAULT_URBUF_SIZE,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100169 static_cast<uint32_t>(tmp_buf.length() - 1)));
David Reissfaebedd2007-09-17 23:20:38 +0000170
David Reiss9a961e72010-10-06 17:10:23 +0000171 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000172 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000173 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400174 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000175 zlib_trans->verifyChecksum();
176}
David Reissfaebedd2007-09-17 23:20:38 +0000177
Jim King7848d882015-04-06 21:38:06 -0400178void test_incomplete_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000179 // Make sure we still get that "not complete" error if
180 // it really isn't complete.
Roger Meier611f90c2011-12-11 22:08:51 +0000181 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
182 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400183 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000184 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000185 string tmp_buf;
186 membuf->appendBufferToString(tmp_buf);
187 tmp_buf.erase(tmp_buf.length() - 1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100188 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500189 static_cast<uint32_t>(tmp_buf.length()));
David Reiss9a961e72010-10-06 17:10:23 +0000190
191 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000192 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000193 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400194 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000195 try {
196 zlib_trans->verifyChecksum();
197 BOOST_ERROR("verifyChecksum() did not report an error");
198 } catch (TTransportException& ex) {
199 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::CORRUPTED_DATA);
200 }
201}
202
Jim King7848d882015-04-06 21:38:06 -0400203void test_read_write_mix(const boost::shared_array<uint8_t> buf,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100204 uint32_t buf_len,
Roger Meier611f90c2011-12-11 22:08:51 +0000205 const boost::shared_ptr<SizeGenerator>& write_gen,
206 const boost::shared_ptr<SizeGenerator>& read_gen) {
David Reiss9a961e72010-10-06 17:10:23 +0000207 // Try it with a mix of read/write sizes.
Roger Meier611f90c2011-12-11 22:08:51 +0000208 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
209 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
David Reiss9a961e72010-10-06 17:10:23 +0000210 unsigned int tot;
211
David Reiss9a961e72010-10-06 17:10:23 +0000212 tot = 0;
213 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000214 uint32_t write_len = write_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000215 if (tot + write_len > buf_len) {
216 write_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000217 }
Jim King7848d882015-04-06 21:38:06 -0400218 zlib_trans->write(buf.get() + tot, write_len);
David Reiss9a961e72010-10-06 17:10:23 +0000219 tot += write_len;
David Reiss9a961e72010-10-06 17:10:23 +0000220 }
David Reissfaebedd2007-09-17 23:20:38 +0000221
David Reisse94fa332010-10-06 17:10:26 +0000222 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000223
David Reiss9a961e72010-10-06 17:10:23 +0000224 tot = 0;
225 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
226 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000227 uint32_t read_len = read_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000228 uint32_t expected_read_len = read_len;
229 if (tot + read_len > buf_len) {
230 expected_read_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000231 }
David Reiss9a961e72010-10-06 17:10:23 +0000232 uint32_t got = zlib_trans->read(mirror.get() + tot, read_len);
David Reiss0a2d81e2010-10-06 17:10:40 +0000233 BOOST_REQUIRE_LE(got, expected_read_len);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100234 BOOST_REQUIRE_NE(got, (uint32_t)0);
David Reiss9a961e72010-10-06 17:10:23 +0000235 tot += got;
David Reiss9a961e72010-10-06 17:10:23 +0000236 }
David Reissfaebedd2007-09-17 23:20:38 +0000237
Jim King7848d882015-04-06 21:38:06 -0400238 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000239 zlib_trans->verifyChecksum();
240}
David Reissfaebedd2007-09-17 23:20:38 +0000241
Jim King7848d882015-04-06 21:38:06 -0400242void test_invalid_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000243 // Verify checksum checking.
Roger Meier611f90c2011-12-11 22:08:51 +0000244 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
245 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400246 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000247 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000248 string tmp_buf;
249 membuf->appendBufferToString(tmp_buf);
250 // Modify a byte at the end of the buffer (part of the checksum).
251 // On rare occasions, modifying a byte in the middle of the buffer
252 // isn't caught by the checksum.
253 //
254 // (This happens especially often for the uniform buffer. The
255 // re-inflated data is correct, however. I suspect in this case that
256 // we're more likely to modify bytes that are part of zlib metadata
257 // instead of the actual compressed data.)
258 //
259 // I've also seen some failure scenarios where a checksum failure isn't
260 // reported, but zlib keeps trying to decode past the end of the data.
261 // (When this occurs, verifyChecksum() throws an exception indicating
262 // that the end of the data hasn't been reached.) I haven't seen this
263 // error when only modifying checksum bytes.
Jake Farrell5d02b802014-01-07 21:42:01 -0500264 int index = static_cast<int>(tmp_buf.size() - 1);
David Reiss9a961e72010-10-06 17:10:23 +0000265 tmp_buf[index]++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100266 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500267 static_cast<uint32_t>(tmp_buf.length()));
David Reissfaebedd2007-09-17 23:20:38 +0000268
David Reiss9a961e72010-10-06 17:10:23 +0000269 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
270 try {
David Reiss0a2d81e2010-10-06 17:10:40 +0000271 zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000272 zlib_trans->verifyChecksum();
273 BOOST_ERROR("verifyChecksum() did not report an error");
274 } catch (TZlibTransportException& ex) {
275 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::INTERNAL_ERROR);
276 }
277}
David Reissfaebedd2007-09-17 23:20:38 +0000278
Jim King7848d882015-04-06 21:38:06 -0400279void test_write_after_flush(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reisse94fa332010-10-06 17:10:26 +0000280 // write some data
Roger Meier611f90c2011-12-11 22:08:51 +0000281 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
282 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400283 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000284
285 // call finish()
286 zlib_trans->finish();
287
288 // make sure write() throws an error
289 try {
290 uint8_t write_buf[] = "a";
291 zlib_trans->write(write_buf, 1);
292 BOOST_ERROR("write() after finish() did not raise an exception");
293 } catch (TTransportException& ex) {
294 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
295 }
296
297 // make sure flush() throws an error
298 try {
299 zlib_trans->flush();
300 BOOST_ERROR("flush() after finish() did not raise an exception");
301 } catch (TTransportException& ex) {
302 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
303 }
304
305 // make sure finish() throws an error
306 try {
307 zlib_trans->finish();
308 BOOST_ERROR("finish() after finish() did not raise an exception");
309 } catch (TTransportException& ex) {
310 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
311 }
312}
313
314void test_no_write() {
315 // Verify that no data is written to the underlying transport if we
316 // never write data to the TZlibTransport.
Roger Meier611f90c2011-12-11 22:08:51 +0000317 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
David Reisse94fa332010-10-06 17:10:26 +0000318 {
319 // Create a TZlibTransport object, and immediately destroy it
320 // when it goes out of scope.
David Reissa0e11592010-10-06 17:10:27 +0000321 TZlibTransport w_zlib_trans(membuf);
David Reisse94fa332010-10-06 17:10:26 +0000322 }
323
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100324 BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t)0);
David Reisse94fa332010-10-06 17:10:26 +0000325}
326
David Reissf2abcf92010-10-06 17:10:24 +0000327/*
328 * Initialization
329 */
330
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100331#define ADD_TEST_CASE(suite, name, function, ...) \
332 do { \
333 ::std::ostringstream name_ss; \
334 name_ss << name << "-" << BOOST_STRINGIZE(function); \
335 ::boost::unit_test::test_case* tc \
336 = ::boost::unit_test::make_test_case(::apache::thrift::stdcxx::bind(function, \
337 ##__VA_ARGS__), \
338 name_ss.str()); \
339 (suite)->add(tc); \
David Reiss9a961e72010-10-06 17:10:23 +0000340 } while (0)
David Reissfaebedd2007-09-17 23:20:38 +0000341
Jake Farrell5d02b802014-01-07 21:42:01 -0500342void add_tests(boost::unit_test::test_suite* suite,
Jim King7848d882015-04-06 21:38:06 -0400343 const boost::shared_array<uint8_t>& buf,
David Reiss9a961e72010-10-06 17:10:23 +0000344 uint32_t buf_len,
345 const char* name) {
346 ADD_TEST_CASE(suite, name, test_write_then_read, buf, buf_len);
347 ADD_TEST_CASE(suite, name, test_separate_checksum, buf, buf_len);
348 ADD_TEST_CASE(suite, name, test_incomplete_checksum, buf, buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000349 ADD_TEST_CASE(suite, name, test_invalid_checksum, buf, buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000350 ADD_TEST_CASE(suite, name, test_write_after_flush, buf, buf_len);
David Reissf2abcf92010-10-06 17:10:24 +0000351
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1 << 15));
Roger Meier611f90c2011-12-11 22:08:51 +0000353 boost::shared_ptr<SizeGenerator> size_lognormal(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100354 ADD_TEST_CASE(suite, name << "-constant", test_read_write_mix, buf, buf_len, size_32k, size_32k);
355 ADD_TEST_CASE(suite,
356 name << "-lognormal-write",
357 test_read_write_mix,
358 buf,
359 buf_len,
360 size_lognormal,
361 size_32k);
362 ADD_TEST_CASE(suite,
363 name << "-lognormal-read",
364 test_read_write_mix,
365 buf,
366 buf_len,
367 size_32k,
368 size_lognormal);
369 ADD_TEST_CASE(suite,
370 name << "-lognormal-both",
371 test_read_write_mix,
372 buf,
373 buf_len,
374 size_lognormal,
375 size_lognormal);
David Reissf2abcf92010-10-06 17:10:24 +0000376
377 // Test with a random size distribution,
378 // but use the exact same distribution for reading as for writing.
379 //
380 // Because the SizeGenerator makes a copy of the random number generator,
381 // both SizeGenerators should return the exact same set of values, since they
382 // both start with random number generators in the same state.
Roger Meier611f90c2011-12-11 22:08:51 +0000383 boost::shared_ptr<SizeGenerator> write_size_gen(new LogNormalSizeGenerator(20, 30));
384 boost::shared_ptr<SizeGenerator> read_size_gen(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100385 ADD_TEST_CASE(suite,
386 name << "-lognormal-same-distribution",
387 test_read_write_mix,
388 buf,
389 buf_len,
390 write_size_gen,
391 read_size_gen);
David Reiss9a961e72010-10-06 17:10:23 +0000392}
393
394void print_usage(FILE* f, const char* argv0) {
395 fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
396 fprintf(f, "Options:\n");
397 fprintf(f, " --seed=<N>, -s <N>\n");
398 fprintf(f, " --help\n");
399}
400
Jake Farrell5d02b802014-01-07 21:42:01 -0500401boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowskib3f5ffc2014-11-06 19:32:59 +0100402 THRIFT_UNUSED_VARIABLE(argc);
403 THRIFT_UNUSED_VARIABLE(argv);
Jake Farrell5d02b802014-01-07 21:42:01 -0500404 uint32_t seed = static_cast<uint32_t>(time(NULL));
Jim King9de9b1f2015-04-30 16:03:34 -0400405#ifdef HAVE_INTTYPES_H
David Reiss9a961e72010-10-06 17:10:23 +0000406 printf("seed: %" PRIu32 "\n", seed);
Jim King9de9b1f2015-04-30 16:03:34 -0400407#endif
David Reiss9a961e72010-10-06 17:10:23 +0000408 rng.seed(seed);
David Reiss9a961e72010-10-06 17:10:23 +0000409
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100410 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
David Reiss109693c2010-10-06 17:10:42 +0000411 suite->p_name.value = "ZlibTest";
David Reiss9a961e72010-10-06 17:10:23 +0000412
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100413 uint32_t buf_len = 1024 * 32;
David Reiss9a961e72010-10-06 17:10:23 +0000414 add_tests(suite, gen_uniform_buffer(buf_len, 'a'), buf_len, "uniform");
415 add_tests(suite, gen_compressible_buffer(buf_len), buf_len, "compressible");
416 add_tests(suite, gen_random_buffer(buf_len), buf_len, "random");
417
David Reisse94fa332010-10-06 17:10:26 +0000418 suite->add(BOOST_TEST_CASE(test_no_write));
419
David Reiss109693c2010-10-06 17:10:42 +0000420 return NULL;
David Reissfaebedd2007-09-17 23:20:38 +0000421}