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