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.

82 line
2.6 KiB

3 週之前
  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 "gmock/gmock.h"
  7. #include "gtest/gtest.h"
  8. #include "helpers/memenv/memenv.h"
  9. #include "leveldb/env.h"
  10. #include "leveldb/slice.h"
  11. #include "util/random.h"
  12. namespace leveldb {
  13. namespace test {
  14. MATCHER(IsOK, "") { return arg.ok(); }
  15. // Macros for testing the results of functions that return leveldb::Status or
  16. // absl::StatusOr<T> (for any type T).
  17. #define EXPECT_LEVELDB_OK(expression) \
  18. EXPECT_THAT(expression, leveldb::test::IsOK())
  19. #define ASSERT_LEVELDB_OK(expression) \
  20. ASSERT_THAT(expression, leveldb::test::IsOK())
  21. // Returns the random seed used at the start of the current test run.
  22. inline int RandomSeed() {
  23. return testing::UnitTest::GetInstance()->random_seed();
  24. }
  25. // Store in *dst a random string of length "len" and return a Slice that
  26. // references the generated data.
  27. Slice RandomString(Random* rnd, int len, std::string* dst);
  28. // Return a random key with the specified length that may contain interesting
  29. // characters (e.g. \x00, \xff, etc.).
  30. std::string RandomKey(Random* rnd, int len);
  31. // Store in *dst a string of length "len" that will compress to
  32. // "N*compressed_fraction" bytes and return a Slice that references
  33. // the generated data.
  34. Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
  35. std::string* dst);
  36. // A wrapper that allows injection of errors.
  37. class ErrorEnv : public EnvWrapper {
  38. public:
  39. bool writable_file_error_;
  40. int num_writable_file_errors_;
  41. ErrorEnv()
  42. : EnvWrapper(NewMemEnv(Env::Default())),
  43. writable_file_error_(false),
  44. num_writable_file_errors_(0) {}
  45. ~ErrorEnv() override { delete target(); }
  46. Status NewWritableFile(const std::string& fname,
  47. WritableFile** result) override {
  48. if (writable_file_error_) {
  49. ++num_writable_file_errors_;
  50. *result = nullptr;
  51. return Status::IOError(fname, "fake error");
  52. }
  53. return target()->NewWritableFile(fname, result);
  54. }
  55. Status NewAppendableFile(const std::string& fname,
  56. WritableFile** result) override {
  57. if (writable_file_error_) {
  58. ++num_writable_file_errors_;
  59. *result = nullptr;
  60. return Status::IOError(fname, "fake error");
  61. }
  62. return target()->NewAppendableFile(fname, result);
  63. }
  64. };
  65. } // namespace test
  66. } // namespace leveldb
  67. #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_