blob: 465e12d31216c8de66961e083ab0569812b4de71 [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>
27#include <inttypes.h>
David Reissfaebedd2007-09-17 23:20:38 +000028#include <cstddef>
David Reissfaebedd2007-09-17 23:20:38 +000029#include <fstream>
30#include <iostream>
Jake Farrell5d02b802014-01-07 21:42:01 -050031#include <thrift/cxxfunctional.h>
David Reiss3cc9dab2010-10-06 17:10:21 +000032
33#include <boost/random.hpp>
David Reiss9a961e72010-10-06 17:10:23 +000034#include <boost/shared_array.hpp>
35#include <boost/test/unit_test.hpp>
David Reiss3cc9dab2010-10-06 17:10:21 +000036
Roger Meier49ff8b12012-04-13 09:12:31 +000037#include <thrift/transport/TBufferTransports.h>
38#include <thrift/transport/TZlibTransport.h>
David Reissfaebedd2007-09-17 23:20:38 +000039
David Reiss9a961e72010-10-06 17:10:23 +000040using namespace std;
David Reiss9a961e72010-10-06 17:10:23 +000041using namespace apache::thrift::transport;
42
David Reissf2abcf92010-10-06 17:10:24 +000043boost::mt19937 rng;
David Reissfaebedd2007-09-17 23:20:38 +000044
David Reissf2abcf92010-10-06 17:10:24 +000045/*
46 * Utility code
47 */
David Reissfaebedd2007-09-17 23:20:38 +000048
David Reissf2abcf92010-10-06 17:10:24 +000049class SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010050public:
David Reissf2abcf92010-10-06 17:10:24 +000051 virtual ~SizeGenerator() {}
52 virtual unsigned int getSize() = 0;
David Reissfaebedd2007-09-17 23:20:38 +000053};
54
David Reissf2abcf92010-10-06 17:10:24 +000055class ConstantSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056public:
David Reissf2abcf92010-10-06 17:10:24 +000057 ConstantSizeGenerator(unsigned int value) : value_(value) {}
Konrad Grochowski16a23a62014-11-13 15:33:38 +010058 virtual unsigned int getSize() { return value_; }
David Reissf2abcf92010-10-06 17:10:24 +000059
Konrad Grochowski16a23a62014-11-13 15:33:38 +010060private:
David Reissf2abcf92010-10-06 17:10:24 +000061 unsigned int value_;
62};
63
64class LogNormalSizeGenerator : public SizeGenerator {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010065public:
66 LogNormalSizeGenerator(double mean, double std_dev)
67 : gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
David Reissf2abcf92010-10-06 17:10:24 +000068
69 virtual unsigned int getSize() {
70 // Loop until we get a size of 1 or more
71 while (true) {
72 unsigned int value = static_cast<unsigned int>(gen_());
73 if (value >= 1) {
74 return value;
75 }
76 }
77 }
78
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079private:
80 boost::variate_generator<boost::mt19937, boost::lognormal_distribution<double> > gen_;
David Reissf2abcf92010-10-06 17:10:24 +000081};
David Reiss3cc9dab2010-10-06 17:10:21 +000082
Jim King7848d882015-04-06 21:38:06 -040083boost::shared_array<uint8_t> gen_uniform_buffer(uint32_t buf_len, uint8_t c) {
David Reiss3cc9dab2010-10-06 17:10:21 +000084 uint8_t* buf = new uint8_t[buf_len];
85 memset(buf, c, buf_len);
Jim King7848d882015-04-06 21:38:06 -040086 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +000087}
88
Jim King7848d882015-04-06 21:38:06 -040089boost::shared_array<uint8_t> gen_compressible_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +000090 uint8_t* buf = new uint8_t[buf_len];
91
92 // Generate small runs of alternately increasing and decreasing bytes
93 boost::uniform_smallint<uint32_t> run_length_distribution(1, 64);
94 boost::uniform_smallint<uint8_t> byte_distribution(0, UINT8_MAX);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010095 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
96 byte_generator(rng, byte_distribution);
97 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint32_t> >
98 run_len_generator(rng, run_length_distribution);
David Reiss3cc9dab2010-10-06 17:10:21 +000099
100 uint32_t idx = 0;
101 int8_t step = 1;
102 while (idx < buf_len) {
103 uint32_t run_length = run_len_generator();
104 if (idx + run_length > buf_len) {
105 run_length = buf_len - idx;
106 }
107
108 uint8_t byte = byte_generator();
109 for (uint32_t n = 0; n < run_length; ++n) {
110 buf[idx] = byte;
111 ++idx;
112 byte += step;
113 }
114
115 step *= -1;
116 }
117
Jim King7848d882015-04-06 21:38:06 -0400118 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000119}
120
Jim King7848d882015-04-06 21:38:06 -0400121boost::shared_array<uint8_t> gen_random_buffer(uint32_t buf_len) {
David Reiss3cc9dab2010-10-06 17:10:21 +0000122 uint8_t* buf = new uint8_t[buf_len];
123
124 boost::uniform_smallint<uint8_t> distribution(0, UINT8_MAX);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100125 boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
126 generator(rng, distribution);
David Reiss3cc9dab2010-10-06 17:10:21 +0000127
128 for (uint32_t n = 0; n < buf_len; ++n) {
129 buf[n] = generator();
130 }
131
Jim King7848d882015-04-06 21:38:06 -0400132 return boost::shared_array<uint8_t>(buf);
David Reiss3cc9dab2010-10-06 17:10:21 +0000133}
David Reissfaebedd2007-09-17 23:20:38 +0000134
David Reissf2abcf92010-10-06 17:10:24 +0000135/*
136 * Test functions
137 */
138
Jim King7848d882015-04-06 21:38:06 -0400139void test_write_then_read(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
Roger Meier611f90c2011-12-11 22:08:51 +0000140 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
141 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400142 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000143 zlib_trans->finish();
David Reissfaebedd2007-09-17 23:20:38 +0000144
David Reiss9a961e72010-10-06 17:10:23 +0000145 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000146 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000147 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400148 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000149 zlib_trans->verifyChecksum();
150}
David Reissfaebedd2007-09-17 23:20:38 +0000151
Jim King7848d882015-04-06 21:38:06 -0400152void test_separate_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000153 // This one is tricky. I separate the last byte of the stream out
154 // into a separate crbuf_. The last byte is part of the checksum,
155 // so the entire read goes fine, but when I go to verify the checksum
156 // it isn't there. The original implementation complained that
157 // the stream was not complete. I'm about to go fix that.
158 // It worked. Awesome.
Roger Meier611f90c2011-12-11 22:08:51 +0000159 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
160 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400161 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000162 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000163 string tmp_buf;
164 membuf->appendBufferToString(tmp_buf);
David Reissa0e11592010-10-06 17:10:27 +0000165 zlib_trans.reset(new TZlibTransport(membuf,
David Reiss9a961e72010-10-06 17:10:23 +0000166 TZlibTransport::DEFAULT_URBUF_SIZE,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100167 static_cast<uint32_t>(tmp_buf.length() - 1)));
David Reissfaebedd2007-09-17 23:20:38 +0000168
David Reiss9a961e72010-10-06 17:10:23 +0000169 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000170 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000171 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400172 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000173 zlib_trans->verifyChecksum();
174}
David Reissfaebedd2007-09-17 23:20:38 +0000175
Jim King7848d882015-04-06 21:38:06 -0400176void test_incomplete_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000177 // Make sure we still get that "not complete" error if
178 // it really isn't complete.
Roger Meier611f90c2011-12-11 22:08:51 +0000179 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
180 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400181 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000182 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000183 string tmp_buf;
184 membuf->appendBufferToString(tmp_buf);
185 tmp_buf.erase(tmp_buf.length() - 1);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100186 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500187 static_cast<uint32_t>(tmp_buf.length()));
David Reiss9a961e72010-10-06 17:10:23 +0000188
189 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
David Reiss0a2d81e2010-10-06 17:10:40 +0000190 uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000191 BOOST_REQUIRE_EQUAL(got, buf_len);
Jim King7848d882015-04-06 21:38:06 -0400192 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000193 try {
194 zlib_trans->verifyChecksum();
195 BOOST_ERROR("verifyChecksum() did not report an error");
196 } catch (TTransportException& ex) {
197 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::CORRUPTED_DATA);
198 }
199}
200
Jim King7848d882015-04-06 21:38:06 -0400201void test_read_write_mix(const boost::shared_array<uint8_t> buf,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100202 uint32_t buf_len,
Roger Meier611f90c2011-12-11 22:08:51 +0000203 const boost::shared_ptr<SizeGenerator>& write_gen,
204 const boost::shared_ptr<SizeGenerator>& read_gen) {
David Reiss9a961e72010-10-06 17:10:23 +0000205 // Try it with a mix of read/write sizes.
Roger Meier611f90c2011-12-11 22:08:51 +0000206 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
207 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
David Reiss9a961e72010-10-06 17:10:23 +0000208 unsigned int tot;
209
David Reiss9a961e72010-10-06 17:10:23 +0000210 tot = 0;
211 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000212 uint32_t write_len = write_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000213 if (tot + write_len > buf_len) {
214 write_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000215 }
Jim King7848d882015-04-06 21:38:06 -0400216 zlib_trans->write(buf.get() + tot, write_len);
David Reiss9a961e72010-10-06 17:10:23 +0000217 tot += write_len;
David Reiss9a961e72010-10-06 17:10:23 +0000218 }
David Reissfaebedd2007-09-17 23:20:38 +0000219
David Reisse94fa332010-10-06 17:10:26 +0000220 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000221
David Reiss9a961e72010-10-06 17:10:23 +0000222 tot = 0;
223 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
224 while (tot < buf_len) {
David Reissf2abcf92010-10-06 17:10:24 +0000225 uint32_t read_len = read_gen->getSize();
David Reiss9a961e72010-10-06 17:10:23 +0000226 uint32_t expected_read_len = read_len;
227 if (tot + read_len > buf_len) {
228 expected_read_len = buf_len - tot;
David Reissfaebedd2007-09-17 23:20:38 +0000229 }
David Reiss9a961e72010-10-06 17:10:23 +0000230 uint32_t got = zlib_trans->read(mirror.get() + tot, read_len);
David Reiss0a2d81e2010-10-06 17:10:40 +0000231 BOOST_REQUIRE_LE(got, expected_read_len);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100232 BOOST_REQUIRE_NE(got, (uint32_t)0);
David Reiss9a961e72010-10-06 17:10:23 +0000233 tot += got;
David Reiss9a961e72010-10-06 17:10:23 +0000234 }
David Reissfaebedd2007-09-17 23:20:38 +0000235
Jim King7848d882015-04-06 21:38:06 -0400236 BOOST_CHECK_EQUAL(memcmp(mirror.get(), buf.get(), buf_len), 0);
David Reiss9a961e72010-10-06 17:10:23 +0000237 zlib_trans->verifyChecksum();
238}
David Reissfaebedd2007-09-17 23:20:38 +0000239
Jim King7848d882015-04-06 21:38:06 -0400240void test_invalid_checksum(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reiss9a961e72010-10-06 17:10:23 +0000241 // Verify checksum checking.
Roger Meier611f90c2011-12-11 22:08:51 +0000242 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
243 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400244 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000245 zlib_trans->finish();
David Reiss9a961e72010-10-06 17:10:23 +0000246 string tmp_buf;
247 membuf->appendBufferToString(tmp_buf);
248 // Modify a byte at the end of the buffer (part of the checksum).
249 // On rare occasions, modifying a byte in the middle of the buffer
250 // isn't caught by the checksum.
251 //
252 // (This happens especially often for the uniform buffer. The
253 // re-inflated data is correct, however. I suspect in this case that
254 // we're more likely to modify bytes that are part of zlib metadata
255 // instead of the actual compressed data.)
256 //
257 // I've also seen some failure scenarios where a checksum failure isn't
258 // reported, but zlib keeps trying to decode past the end of the data.
259 // (When this occurs, verifyChecksum() throws an exception indicating
260 // that the end of the data hasn't been reached.) I haven't seen this
261 // error when only modifying checksum bytes.
Jake Farrell5d02b802014-01-07 21:42:01 -0500262 int index = static_cast<int>(tmp_buf.size() - 1);
David Reiss9a961e72010-10-06 17:10:23 +0000263 tmp_buf[index]++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100264 membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
Jake Farrell5d02b802014-01-07 21:42:01 -0500265 static_cast<uint32_t>(tmp_buf.length()));
David Reissfaebedd2007-09-17 23:20:38 +0000266
David Reiss9a961e72010-10-06 17:10:23 +0000267 boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
268 try {
David Reiss0a2d81e2010-10-06 17:10:40 +0000269 zlib_trans->readAll(mirror.get(), buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000270 zlib_trans->verifyChecksum();
271 BOOST_ERROR("verifyChecksum() did not report an error");
272 } catch (TZlibTransportException& ex) {
273 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::INTERNAL_ERROR);
274 }
275}
David Reissfaebedd2007-09-17 23:20:38 +0000276
Jim King7848d882015-04-06 21:38:06 -0400277void test_write_after_flush(const boost::shared_array<uint8_t> buf, uint32_t buf_len) {
David Reisse94fa332010-10-06 17:10:26 +0000278 // write some data
Roger Meier611f90c2011-12-11 22:08:51 +0000279 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
280 boost::shared_ptr<TZlibTransport> zlib_trans(new TZlibTransport(membuf));
Jim King7848d882015-04-06 21:38:06 -0400281 zlib_trans->write(buf.get(), buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000282
283 // call finish()
284 zlib_trans->finish();
285
286 // make sure write() throws an error
287 try {
288 uint8_t write_buf[] = "a";
289 zlib_trans->write(write_buf, 1);
290 BOOST_ERROR("write() after finish() did not raise an exception");
291 } catch (TTransportException& ex) {
292 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
293 }
294
295 // make sure flush() throws an error
296 try {
297 zlib_trans->flush();
298 BOOST_ERROR("flush() after finish() did not raise an exception");
299 } catch (TTransportException& ex) {
300 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
301 }
302
303 // make sure finish() throws an error
304 try {
305 zlib_trans->finish();
306 BOOST_ERROR("finish() after finish() did not raise an exception");
307 } catch (TTransportException& ex) {
308 BOOST_CHECK_EQUAL(ex.getType(), TTransportException::BAD_ARGS);
309 }
310}
311
312void test_no_write() {
313 // Verify that no data is written to the underlying transport if we
314 // never write data to the TZlibTransport.
Roger Meier611f90c2011-12-11 22:08:51 +0000315 boost::shared_ptr<TMemoryBuffer> membuf(new TMemoryBuffer());
David Reisse94fa332010-10-06 17:10:26 +0000316 {
317 // Create a TZlibTransport object, and immediately destroy it
318 // when it goes out of scope.
David Reissa0e11592010-10-06 17:10:27 +0000319 TZlibTransport w_zlib_trans(membuf);
David Reisse94fa332010-10-06 17:10:26 +0000320 }
321
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100322 BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t)0);
David Reisse94fa332010-10-06 17:10:26 +0000323}
324
David Reissf2abcf92010-10-06 17:10:24 +0000325/*
326 * Initialization
327 */
328
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100329#define ADD_TEST_CASE(suite, name, function, ...) \
330 do { \
331 ::std::ostringstream name_ss; \
332 name_ss << name << "-" << BOOST_STRINGIZE(function); \
333 ::boost::unit_test::test_case* tc \
334 = ::boost::unit_test::make_test_case(::apache::thrift::stdcxx::bind(function, \
335 ##__VA_ARGS__), \
336 name_ss.str()); \
337 (suite)->add(tc); \
David Reiss9a961e72010-10-06 17:10:23 +0000338 } while (0)
David Reissfaebedd2007-09-17 23:20:38 +0000339
Jake Farrell5d02b802014-01-07 21:42:01 -0500340void add_tests(boost::unit_test::test_suite* suite,
Jim King7848d882015-04-06 21:38:06 -0400341 const boost::shared_array<uint8_t>& buf,
David Reiss9a961e72010-10-06 17:10:23 +0000342 uint32_t buf_len,
343 const char* name) {
344 ADD_TEST_CASE(suite, name, test_write_then_read, buf, buf_len);
345 ADD_TEST_CASE(suite, name, test_separate_checksum, buf, buf_len);
346 ADD_TEST_CASE(suite, name, test_incomplete_checksum, buf, buf_len);
David Reiss9a961e72010-10-06 17:10:23 +0000347 ADD_TEST_CASE(suite, name, test_invalid_checksum, buf, buf_len);
David Reisse94fa332010-10-06 17:10:26 +0000348 ADD_TEST_CASE(suite, name, test_write_after_flush, buf, buf_len);
David Reissf2abcf92010-10-06 17:10:24 +0000349
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100350 boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1 << 15));
Roger Meier611f90c2011-12-11 22:08:51 +0000351 boost::shared_ptr<SizeGenerator> size_lognormal(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100352 ADD_TEST_CASE(suite, name << "-constant", test_read_write_mix, buf, buf_len, size_32k, size_32k);
353 ADD_TEST_CASE(suite,
354 name << "-lognormal-write",
355 test_read_write_mix,
356 buf,
357 buf_len,
358 size_lognormal,
359 size_32k);
360 ADD_TEST_CASE(suite,
361 name << "-lognormal-read",
362 test_read_write_mix,
363 buf,
364 buf_len,
365 size_32k,
366 size_lognormal);
367 ADD_TEST_CASE(suite,
368 name << "-lognormal-both",
369 test_read_write_mix,
370 buf,
371 buf_len,
372 size_lognormal,
373 size_lognormal);
David Reissf2abcf92010-10-06 17:10:24 +0000374
375 // Test with a random size distribution,
376 // but use the exact same distribution for reading as for writing.
377 //
378 // Because the SizeGenerator makes a copy of the random number generator,
379 // both SizeGenerators should return the exact same set of values, since they
380 // both start with random number generators in the same state.
Roger Meier611f90c2011-12-11 22:08:51 +0000381 boost::shared_ptr<SizeGenerator> write_size_gen(new LogNormalSizeGenerator(20, 30));
382 boost::shared_ptr<SizeGenerator> read_size_gen(new LogNormalSizeGenerator(20, 30));
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100383 ADD_TEST_CASE(suite,
384 name << "-lognormal-same-distribution",
385 test_read_write_mix,
386 buf,
387 buf_len,
388 write_size_gen,
389 read_size_gen);
David Reiss9a961e72010-10-06 17:10:23 +0000390}
391
392void print_usage(FILE* f, const char* argv0) {
393 fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
394 fprintf(f, "Options:\n");
395 fprintf(f, " --seed=<N>, -s <N>\n");
396 fprintf(f, " --help\n");
397}
398
Jake Farrell5d02b802014-01-07 21:42:01 -0500399boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowskib3f5ffc2014-11-06 19:32:59 +0100400 THRIFT_UNUSED_VARIABLE(argc);
401 THRIFT_UNUSED_VARIABLE(argv);
Jake Farrell5d02b802014-01-07 21:42:01 -0500402 uint32_t seed = static_cast<uint32_t>(time(NULL));
David Reiss9a961e72010-10-06 17:10:23 +0000403 printf("seed: %" PRIu32 "\n", seed);
404 rng.seed(seed);
David Reiss9a961e72010-10-06 17:10:23 +0000405
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100406 boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
David Reiss109693c2010-10-06 17:10:42 +0000407 suite->p_name.value = "ZlibTest";
David Reiss9a961e72010-10-06 17:10:23 +0000408
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100409 uint32_t buf_len = 1024 * 32;
David Reiss9a961e72010-10-06 17:10:23 +0000410 add_tests(suite, gen_uniform_buffer(buf_len, 'a'), buf_len, "uniform");
411 add_tests(suite, gen_compressible_buffer(buf_len), buf_len, "compressible");
412 add_tests(suite, gen_random_buffer(buf_len), buf_len, "random");
413
David Reisse94fa332010-10-06 17:10:26 +0000414 suite->add(BOOST_TEST_CASE(test_no_write));
415
David Reiss109693c2010-10-06 17:10:42 +0000416 return NULL;
David Reissfaebedd2007-09-17 23:20:38 +0000417}