小组成员:姚凯文(kevinyao0901),姜嘉琪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.7 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. int 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. };
  40. }
  41. }
  42. #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_