Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

49 rader
1.4 KiB

  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. #include "util/testutil.h"
  5. #include "util/random.h"
  6. namespace leveldb {
  7. namespace test {
  8. Slice RandomString(Random* rnd, int len, std::string* dst) {
  9. dst->resize(len);
  10. for (int i = 0; i < len; i++) {
  11. (*dst)[i] = static_cast<char>(' ' + rnd->Uniform(95)); // ' ' .. '~'
  12. }
  13. return Slice(*dst);
  14. }
  15. std::string RandomKey(Random* rnd, int len) {
  16. // Make sure to generate a wide variety of characters so we
  17. // test the boundary conditions for short-key optimizations.
  18. static const char kTestChars[] = {'\0', '\1', 'a', 'b', 'c',
  19. 'd', 'e', '\xfd', '\xfe', '\xff'};
  20. std::string result;
  21. for (int i = 0; i < len; i++) {
  22. result += kTestChars[rnd->Uniform(sizeof(kTestChars))];
  23. }
  24. return result;
  25. }
  26. Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
  27. std::string* dst) {
  28. int raw = static_cast<int>(len * compressed_fraction);
  29. if (raw < 1) raw = 1;
  30. std::string raw_data;
  31. RandomString(rnd, raw, &raw_data);
  32. // Duplicate the random data until we have filled "len" bytes
  33. dst->clear();
  34. while (dst->size() < len) {
  35. dst->append(raw_data);
  36. }
  37. dst->resize(len);
  38. return Slice(*dst);
  39. }
  40. } // namespace test
  41. } // namespace leveldb