10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲

63 lines
2.0 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. #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
  5. #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
  6. #include "leveldb/env.h"
  7. #include "leveldb/slice.h"
  8. #include "util/random.h"
  9. namespace leveldb {
  10. namespace test {
  11. // Store in *dst a random string of length "len" and return a Slice that
  12. // references the generated data.
  13. extern Slice RandomString(Random* rnd, int len, std::string* dst);
  14. // Return a random key with the specified length that may contain interesting
  15. // characters (e.g. \x00, \xff, etc.).
  16. extern std::string RandomKey(Random* rnd, int len);
  17. // Store in *dst a string of length "len" that will compress to
  18. // "N*compressed_fraction" bytes and return a Slice that references
  19. // the generated data.
  20. extern Slice CompressibleString(Random* rnd, double compressed_fraction,
  21. size_t len, std::string* dst);
  22. // A wrapper that allows injection of errors.
  23. class ErrorEnv : public EnvWrapper {
  24. public:
  25. bool writable_file_error_;
  26. int num_writable_file_errors_;
  27. ErrorEnv() : EnvWrapper(Env::Default()),
  28. writable_file_error_(false),
  29. num_writable_file_errors_(0) { }
  30. virtual Status NewWritableFile(const std::string& fname,
  31. WritableFile** result) {
  32. if (writable_file_error_) {
  33. ++num_writable_file_errors_;
  34. *result = NULL;
  35. return Status::IOError(fname, "fake error");
  36. }
  37. return target()->NewWritableFile(fname, result);
  38. }
  39. virtual Status NewAppendableFile(const std::string& fname,
  40. WritableFile** result) {
  41. if (writable_file_error_) {
  42. ++num_writable_file_errors_;
  43. *result = NULL;
  44. return Status::IOError(fname, "fake error");
  45. }
  46. return target()->NewAppendableFile(fname, result);
  47. }
  48. };
  49. } // namespace test
  50. } // namespace leveldb
  51. #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_