1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors.
4*9507f98cSAndroid Build Coastguard Worker
5*9507f98cSAndroid Build Coastguard Worker #include "util/testutil.h"
6*9507f98cSAndroid Build Coastguard Worker
7*9507f98cSAndroid Build Coastguard Worker #include <string>
8*9507f98cSAndroid Build Coastguard Worker
9*9507f98cSAndroid Build Coastguard Worker #include "util/random.h"
10*9507f98cSAndroid Build Coastguard Worker
11*9507f98cSAndroid Build Coastguard Worker namespace leveldb {
12*9507f98cSAndroid Build Coastguard Worker namespace test {
13*9507f98cSAndroid Build Coastguard Worker
RandomString(Random * rnd,int len,std::string * dst)14*9507f98cSAndroid Build Coastguard Worker Slice RandomString(Random* rnd, int len, std::string* dst) {
15*9507f98cSAndroid Build Coastguard Worker dst->resize(len);
16*9507f98cSAndroid Build Coastguard Worker for (int i = 0; i < len; i++) {
17*9507f98cSAndroid Build Coastguard Worker (*dst)[i] = static_cast<char>(' ' + rnd->Uniform(95)); // ' ' .. '~'
18*9507f98cSAndroid Build Coastguard Worker }
19*9507f98cSAndroid Build Coastguard Worker return Slice(*dst);
20*9507f98cSAndroid Build Coastguard Worker }
21*9507f98cSAndroid Build Coastguard Worker
RandomKey(Random * rnd,int len)22*9507f98cSAndroid Build Coastguard Worker std::string RandomKey(Random* rnd, int len) {
23*9507f98cSAndroid Build Coastguard Worker // Make sure to generate a wide variety of characters so we
24*9507f98cSAndroid Build Coastguard Worker // test the boundary conditions for short-key optimizations.
25*9507f98cSAndroid Build Coastguard Worker static const char kTestChars[] = {'\0', '\1', 'a', 'b', 'c',
26*9507f98cSAndroid Build Coastguard Worker 'd', 'e', '\xfd', '\xfe', '\xff'};
27*9507f98cSAndroid Build Coastguard Worker std::string result;
28*9507f98cSAndroid Build Coastguard Worker for (int i = 0; i < len; i++) {
29*9507f98cSAndroid Build Coastguard Worker result += kTestChars[rnd->Uniform(sizeof(kTestChars))];
30*9507f98cSAndroid Build Coastguard Worker }
31*9507f98cSAndroid Build Coastguard Worker return result;
32*9507f98cSAndroid Build Coastguard Worker }
33*9507f98cSAndroid Build Coastguard Worker
CompressibleString(Random * rnd,double compressed_fraction,size_t len,std::string * dst)34*9507f98cSAndroid Build Coastguard Worker Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
35*9507f98cSAndroid Build Coastguard Worker std::string* dst) {
36*9507f98cSAndroid Build Coastguard Worker int raw = static_cast<int>(len * compressed_fraction);
37*9507f98cSAndroid Build Coastguard Worker if (raw < 1) raw = 1;
38*9507f98cSAndroid Build Coastguard Worker std::string raw_data;
39*9507f98cSAndroid Build Coastguard Worker RandomString(rnd, raw, &raw_data);
40*9507f98cSAndroid Build Coastguard Worker
41*9507f98cSAndroid Build Coastguard Worker // Duplicate the random data until we have filled "len" bytes
42*9507f98cSAndroid Build Coastguard Worker dst->clear();
43*9507f98cSAndroid Build Coastguard Worker while (dst->size() < len) {
44*9507f98cSAndroid Build Coastguard Worker dst->append(raw_data);
45*9507f98cSAndroid Build Coastguard Worker }
46*9507f98cSAndroid Build Coastguard Worker dst->resize(len);
47*9507f98cSAndroid Build Coastguard Worker return Slice(*dst);
48*9507f98cSAndroid Build Coastguard Worker }
49*9507f98cSAndroid Build Coastguard Worker
50*9507f98cSAndroid Build Coastguard Worker } // namespace test
51*9507f98cSAndroid Build Coastguard Worker } // namespace leveldb
52