xref: /aosp_15_r20/external/leveldb/util/testutil.h (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors.
4*9507f98cSAndroid Build Coastguard Worker 
5*9507f98cSAndroid Build Coastguard Worker #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
6*9507f98cSAndroid Build Coastguard Worker #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
7*9507f98cSAndroid Build Coastguard Worker 
8*9507f98cSAndroid Build Coastguard Worker #include "gmock/gmock.h"
9*9507f98cSAndroid Build Coastguard Worker #include "gtest/gtest.h"
10*9507f98cSAndroid Build Coastguard Worker #include "helpers/memenv/memenv.h"
11*9507f98cSAndroid Build Coastguard Worker #include "leveldb/env.h"
12*9507f98cSAndroid Build Coastguard Worker #include "leveldb/slice.h"
13*9507f98cSAndroid Build Coastguard Worker #include "util/random.h"
14*9507f98cSAndroid Build Coastguard Worker 
15*9507f98cSAndroid Build Coastguard Worker namespace leveldb {
16*9507f98cSAndroid Build Coastguard Worker namespace test {
17*9507f98cSAndroid Build Coastguard Worker 
18*9507f98cSAndroid Build Coastguard Worker MATCHER(IsOK, "") { return arg.ok(); }
19*9507f98cSAndroid Build Coastguard Worker 
20*9507f98cSAndroid Build Coastguard Worker // Macros for testing the results of functions that return leveldb::Status or
21*9507f98cSAndroid Build Coastguard Worker // absl::StatusOr<T> (for any type T).
22*9507f98cSAndroid Build Coastguard Worker #define EXPECT_LEVELDB_OK(expression) \
23*9507f98cSAndroid Build Coastguard Worker   EXPECT_THAT(expression, leveldb::test::IsOK())
24*9507f98cSAndroid Build Coastguard Worker #define ASSERT_LEVELDB_OK(expression) \
25*9507f98cSAndroid Build Coastguard Worker   ASSERT_THAT(expression, leveldb::test::IsOK())
26*9507f98cSAndroid Build Coastguard Worker 
27*9507f98cSAndroid Build Coastguard Worker // Returns the random seed used at the start of the current test run.
RandomSeed()28*9507f98cSAndroid Build Coastguard Worker inline int RandomSeed() {
29*9507f98cSAndroid Build Coastguard Worker   return testing::UnitTest::GetInstance()->random_seed();
30*9507f98cSAndroid Build Coastguard Worker }
31*9507f98cSAndroid Build Coastguard Worker 
32*9507f98cSAndroid Build Coastguard Worker // Store in *dst a random string of length "len" and return a Slice that
33*9507f98cSAndroid Build Coastguard Worker // references the generated data.
34*9507f98cSAndroid Build Coastguard Worker Slice RandomString(Random* rnd, int len, std::string* dst);
35*9507f98cSAndroid Build Coastguard Worker 
36*9507f98cSAndroid Build Coastguard Worker // Return a random key with the specified length that may contain interesting
37*9507f98cSAndroid Build Coastguard Worker // characters (e.g. \x00, \xff, etc.).
38*9507f98cSAndroid Build Coastguard Worker std::string RandomKey(Random* rnd, int len);
39*9507f98cSAndroid Build Coastguard Worker 
40*9507f98cSAndroid Build Coastguard Worker // Store in *dst a string of length "len" that will compress to
41*9507f98cSAndroid Build Coastguard Worker // "N*compressed_fraction" bytes and return a Slice that references
42*9507f98cSAndroid Build Coastguard Worker // the generated data.
43*9507f98cSAndroid Build Coastguard Worker Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
44*9507f98cSAndroid Build Coastguard Worker                          std::string* dst);
45*9507f98cSAndroid Build Coastguard Worker 
46*9507f98cSAndroid Build Coastguard Worker // A wrapper that allows injection of errors.
47*9507f98cSAndroid Build Coastguard Worker class ErrorEnv : public EnvWrapper {
48*9507f98cSAndroid Build Coastguard Worker  public:
49*9507f98cSAndroid Build Coastguard Worker   bool writable_file_error_;
50*9507f98cSAndroid Build Coastguard Worker   int num_writable_file_errors_;
51*9507f98cSAndroid Build Coastguard Worker 
ErrorEnv()52*9507f98cSAndroid Build Coastguard Worker   ErrorEnv()
53*9507f98cSAndroid Build Coastguard Worker       : EnvWrapper(NewMemEnv(Env::Default())),
54*9507f98cSAndroid Build Coastguard Worker         writable_file_error_(false),
55*9507f98cSAndroid Build Coastguard Worker         num_writable_file_errors_(0) {}
~ErrorEnv()56*9507f98cSAndroid Build Coastguard Worker   ~ErrorEnv() override { delete target(); }
57*9507f98cSAndroid Build Coastguard Worker 
NewWritableFile(const std::string & fname,WritableFile ** result)58*9507f98cSAndroid Build Coastguard Worker   Status NewWritableFile(const std::string& fname,
59*9507f98cSAndroid Build Coastguard Worker                          WritableFile** result) override {
60*9507f98cSAndroid Build Coastguard Worker     if (writable_file_error_) {
61*9507f98cSAndroid Build Coastguard Worker       ++num_writable_file_errors_;
62*9507f98cSAndroid Build Coastguard Worker       *result = nullptr;
63*9507f98cSAndroid Build Coastguard Worker       return Status::IOError(fname, "fake error");
64*9507f98cSAndroid Build Coastguard Worker     }
65*9507f98cSAndroid Build Coastguard Worker     return target()->NewWritableFile(fname, result);
66*9507f98cSAndroid Build Coastguard Worker   }
67*9507f98cSAndroid Build Coastguard Worker 
NewAppendableFile(const std::string & fname,WritableFile ** result)68*9507f98cSAndroid Build Coastguard Worker   Status NewAppendableFile(const std::string& fname,
69*9507f98cSAndroid Build Coastguard Worker                            WritableFile** result) override {
70*9507f98cSAndroid Build Coastguard Worker     if (writable_file_error_) {
71*9507f98cSAndroid Build Coastguard Worker       ++num_writable_file_errors_;
72*9507f98cSAndroid Build Coastguard Worker       *result = nullptr;
73*9507f98cSAndroid Build Coastguard Worker       return Status::IOError(fname, "fake error");
74*9507f98cSAndroid Build Coastguard Worker     }
75*9507f98cSAndroid Build Coastguard Worker     return target()->NewAppendableFile(fname, result);
76*9507f98cSAndroid Build Coastguard Worker   }
77*9507f98cSAndroid Build Coastguard Worker };
78*9507f98cSAndroid Build Coastguard Worker 
79*9507f98cSAndroid Build Coastguard Worker }  // namespace test
80*9507f98cSAndroid Build Coastguard Worker }  // namespace leveldb
81*9507f98cSAndroid Build Coastguard Worker 
82*9507f98cSAndroid Build Coastguard Worker #endif  // STORAGE_LEVELDB_UTIL_TESTUTIL_H_
83