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