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.
5 #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
6 #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
8 #include "leveldb/env.h"
9 #include "leveldb/slice.h"
10 #include "util/random.h"
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);
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);
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 int len, std::string* dst);
29 // A wrapper that allows injection of errors.
30 class ErrorEnv : public EnvWrapper {
32 bool writable_file_error_;
33 int num_writable_file_errors_;
35 ErrorEnv() : EnvWrapper(Env::Default()),
36 writable_file_error_(false),
37 num_writable_file_errors_(0) { }
39 virtual Status NewWritableFile(const std::string& fname,
40 WritableFile** result) {
41 if (writable_file_error_) {
42 ++num_writable_file_errors_;
44 return Status::IOError(fname, "fake error");
46 return target()->NewWritableFile(fname, result);
51 } // namespace leveldb
53 #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_