bitcoinleveldb_test/util.rs
1crate::ix!();
2
3//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/testutil.h]
4//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/testutil.cc]
5
6/**
7 | A wrapper that allows injection of errors.
8 |
9 */
10pub struct ErrorEnv {
11 base: EnvWrapper,
12 writable_file_error: bool,
13 num_writable_file_errors: i32,
14}
15
16impl Default for ErrorEnv {
17
18 fn default() -> Self {
19 todo!();
20 /*
21
22
23 : EnvWrapper(NewMemEnv(Env::Default())),
24 writable_file_error_(false),
25 num_writable_file_errors_(0)
26 */
27 }
28}
29
30impl Drop for ErrorEnv {
31 fn drop(&mut self) {
32 todo!();
33 /*
34 delete target();
35 */
36 }
37}
38
39impl ErrorEnv {
40
41 pub fn new_writable_file(&mut self,
42 fname: &String,
43 result: *mut *mut dyn WritableFile) -> crate::Status {
44
45 todo!();
46 /*
47 if (writable_file_error_) {
48 ++num_writable_file_errors_;
49 *result = nullptr;
50 return crate::Status::IOError(fname, "fake error");
51 }
52 return target()->NewWritableFile(fname, result);
53 */
54 }
55
56 pub fn new_appendable_file(&mut self,
57 fname: &String,
58 result: *mut *mut dyn WritableFile) -> crate::Status {
59
60 todo!();
61 /*
62 if (writable_file_error_) {
63 ++num_writable_file_errors_;
64 *result = nullptr;
65 return crate::Status::IOError(fname, "fake error");
66 }
67 return target()->NewAppendableFile(fname, result);
68 */
69 }
70}
71
72/**
73 | Store in *dst a random string of length
74 | "len" and return a Slice that references
75 | the generated data.
76 |
77 */
78pub fn random_string(
79 rnd: *mut Random,
80 len: i32,
81 dst: *mut String) -> Slice {
82
83 todo!();
84 /*
85 dst->resize(len);
86 for (int i = 0; i < len; i++) {
87 (*dst)[i] = static_cast<char>(' ' + rnd->Uniform(95)); // ' ' .. '~'
88 }
89 return Slice(*dst);
90 */
91}
92
93/**
94 | Return a random key with the specified
95 | length that may contain interesting
96 | characters (e.g. \x00, \xff, etc.).
97 |
98 */
99pub fn random_key(
100 rnd: *mut Random,
101 len: i32) -> String {
102
103 todo!();
104 /*
105 // Make sure to generate a wide variety of characters so we
106 // test the boundary conditions for short-key optimizations.
107 static const char kTestChars[] = {'\0', '\1', 'a', 'b', 'c',
108 'd', 'e', '\xfd', '\xfe', '\xff'};
109 std::string result;
110 for (int i = 0; i < len; i++) {
111 result += kTestChars[rnd->Uniform(sizeof(kTestChars))];
112 }
113 return result;
114 */
115}
116
117/**
118 | Store in *dst a string of length "len" that
119 | will compress to "N*compressed_fraction" bytes
120 | and return a Slice that references the
121 | generated data.
122 */
123pub fn compressible_string(
124 rnd: *mut Random,
125 compressed_fraction: f64,
126 len: usize,
127 dst: *mut String) -> Slice {
128
129 todo!();
130 /*
131 int raw = static_cast<int>(len * compressed_fraction);
132 if (raw < 1) raw = 1;
133 std::string raw_data;
134 RandomString(rnd, raw, &raw_data);
135
136 // Duplicate the random data until we have filled "len" bytes
137 dst->clear();
138 while (dst->size() < len) {
139 dst->append(raw_data);
140 }
141 dst->resize(len);
142 return Slice(*dst);
143 */
144}