bitcoinleveldb_test/
harness.rs

1crate::ix!();
2
3
4
5//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/testharness.h]
6//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/testharness.cc]
7
8/**
9  | An instance of Tester is allocated to
10  | hold temporary state during the execution
11  | of an assertion.
12  |
13  */
14pub struct Tester {
15    ok:    bool,
16    fname: *const u8,
17    line:  i32,
18    ss:    Box<dyn std::io::Write>,
19}
20
21impl Drop for Tester {
22    fn drop(&mut self) {
23        todo!();
24        /*
25            if (!ok_) {
26          fprintf(stderr, "%s:%d:%s\n", fname_, line_, ss_.str().c_str());
27          exit(1);
28        }
29        */
30    }
31}
32
33macro_rules! binary_op {
34    ($name:ident, $op:tt) => {
35        /*
36        
37          template <class X, class Y>                        
38          Tester& name(const X& x, const Y& y) {             
39            if (!(x op y)) {                                 
40              ss_ << " failed: " << x << (" " #op " ") << y; 
41              ok_ = false;                                   
42            }                                                
43            return *this;                                    
44          }
45        */
46    }
47}
48
49binary_op!{ IsEq, == }
50binary_op!{ IsNe, != }
51binary_op!{ IsGe, >= }
52binary_op!{ IsGt, > }
53binary_op!{ IsLe, <= }
54binary_op!{ IsLt, < }
55
56impl Tester {
57
58    pub fn new(
59        f: *const u8,
60        l: i32) -> Self {
61    
62        todo!();
63        /*
64        : ok(true),
65        : fname(f),
66        : line(l),
67
68        
69        */
70    }
71    
72    pub fn is(&mut self, 
73        b:   bool,
74        msg: *const u8) -> &mut Tester {
75        
76        todo!();
77        /*
78            if (!b) {
79          ss_ << " Assertion failure " << msg;
80          ok_ = false;
81        }
82        return *this;
83        */
84    }
85    
86    pub fn is_ok(&mut self, s: &Status) -> &mut Tester {
87        
88        todo!();
89        /*
90            if (!s.ok()) {
91          ss_ << " " << s.ToString();
92          ok_ = false;
93        }
94        return *this;
95        */
96    }
97}
98
99impl<V> Shl<&V> for Tester {
100    type Output = Tester;
101    
102    /**
103      | Attach the specified value to the error
104      | message if an error has occurred
105      |
106      */
107    #[inline] fn shl(self, rhs: &V) -> Self::Output {
108        todo!();
109        /*
110            if (!ok_) {
111          ss_ << " " << value;
112        }
113        return *this;
114        */
115    }
116}
117
118macro_rules! assert_true {
119    ($c:ident) => {
120        /*
121                ::leveldb::test::Tester(__FILE__, __LINE__).Is((c), #c)
122        */
123    }
124}
125
126macro_rules! assert_ok {
127    ($s:ident) => {
128        /*
129                ::leveldb::test::Tester(__FILE__, __LINE__).IsOk((s))
130        */
131    }
132}
133
134macro_rules! assert_eq {
135    ($a:ident, $b:ident) => {
136        /*
137        
138          ::leveldb::test::Tester(__FILE__, __LINE__).IsEq((a), (b))
139        */
140    }
141}
142
143macro_rules! assert_ne {
144    ($a:ident, $b:ident) => {
145        /*
146        
147          ::leveldb::test::Tester(__FILE__, __LINE__).IsNe((a), (b))
148        */
149    }
150}
151
152macro_rules! assert_ge {
153    ($a:ident, $b:ident) => {
154        /*
155        
156          ::leveldb::test::Tester(__FILE__, __LINE__).IsGe((a), (b))
157        */
158    }
159}
160
161macro_rules! assert_gt {
162    ($a:ident, $b:ident) => {
163        /*
164        
165          ::leveldb::test::Tester(__FILE__, __LINE__).IsGt((a), (b))
166        */
167    }
168}
169
170macro_rules! assert_le {
171    ($a:ident, $b:ident) => {
172        /*
173        
174          ::leveldb::test::Tester(__FILE__, __LINE__).IsLe((a), (b))
175        */
176    }
177}
178
179macro_rules! assert_lt {
180    ($a:ident, $b:ident) => {
181        /*
182        
183          ::leveldb::test::Tester(__FILE__, __LINE__).IsLt((a), (b))
184        */
185    }
186}
187
188macro_rules! tconcat {
189    ($a:ident, $b:ident) => {
190        /*
191                TCONCAT1(a, b)
192        */
193    }
194}
195
196macro_rules! tconcat1 {
197    ($a:ident, $b:ident) => {
198        /*
199                a##b
200        */
201    }
202}
203
204macro_rules! test {
205    ($base:ident, $name:ident) => {
206        /*
207        
208          class TCONCAT(_Test_, name) : public base {                         
209                                                                       
210            c_void _Run();                                                      
211            static c_void _RunIt() {                                            
212              TCONCAT(_Test_, name) t;                                        
213              t._Run();                                                       
214            }                                                                 
215          };                                                                  
216          bool TCONCAT(_Test_ignored_, name) = ::leveldb::test::RegisterTest( 
217              #base, #name, &TCONCAT(_Test_, name)::_RunIt);                  
218          c_void TCONCAT(_Test_, name)::_Run()
219        */
220    }
221}
222
223pub struct Test {
224    base: *const u8,
225    name: *const u8,
226    func: fn() -> c_void,
227}
228
229lazy_static!{
230    /*
231    std::vector<Test>* tests;
232    */
233}
234
235/**
236  | Register the specified test. Typically
237  | not used directly, but invoked via the
238  | macro expansion of TEST.
239  |
240  */
241pub fn register_test(
242        base: *const u8,
243        name: *const u8,
244        func: fn() -> c_void) -> bool {
245    
246    todo!();
247        /*
248            if (tests == nullptr) {
249        tests = new std::vector<Test>;
250      }
251      Test t;
252      t.base = base;
253      t.name = name;
254      t.func = func;
255      tests->push_back(t);
256      return true;
257        */
258}
259
260/**
261  | Run some of the tests registered by the TEST()
262  | macro.  If the environment variable
263  | "LEVELDB_TESTS" is not set, runs all tests.
264  |
265  | Otherwise, runs only the tests whose name
266  | contains the value of "LEVELDB_TESTS" as
267  | a substring.  E.g., suppose the tests are:
268  |
269  |    TEST(Foo, Hello) { ... }
270  |    TEST(Foo, World) { ... }
271  |
272  | LEVELDB_TESTS=Hello will run the first test
273  | LEVELDB_TESTS=o     will run both tests
274  | LEVELDB_TESTS=Junk  will run no tests
275  |
276  | Returns 0 if all tests pass.
277  |
278  | Dies or returns a non-zero value if some test
279  | fails.
280  */
281pub fn run_all_tests() -> i32 {
282    
283    todo!();
284        /*
285            const char* matcher = getenv("LEVELDB_TESTS");
286
287      int num = 0;
288      if (tests != nullptr) {
289        for (size_t i = 0; i < tests->size(); i++) {
290          const Test& t = (*tests)[i];
291          if (matcher != nullptr) {
292            std::string name = t.base;
293            name.push_back('.');
294            name.append(t.name);
295            if (strstr(name.c_str(), matcher) == nullptr) {
296              continue;
297            }
298          }
299          fprintf(stderr, "==== Test %s.%s\n", t.base, t.name);
300          (*t.func)();
301          ++num;
302        }
303      }
304      fprintf(stderr, "==== PASSED %d tests\n", num);
305      return 0;
306        */
307}
308
309/**
310  | Return the directory to use for temporary
311  | storage.
312  |
313  */
314pub fn tmp_dir() -> String {
315    
316    todo!();
317        /*
318            std::string dir;
319      crate::Status s = Env::Default()->GetTestDirectory(&dir);
320      ASSERT_TRUE(s.ok()) << s.ToString();
321      return dir;
322        */
323}
324
325/**
326  | Return a randomization seed for this run.
327  | Typically returns the same number on repeated
328  | invocations of this binary, but automated runs
329  | may be able to vary the seed.
330  */
331pub fn random_seed() -> i32 {
332    
333    todo!();
334        /*
335            const char* env = getenv("TEST_RANDOM_SEED");
336      int result = (env != nullptr ? atoi(env) : 301);
337      if (result <= 0) {
338        result = 301;
339      }
340      return result;
341        */
342}