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_TESTHARNESS_H_
6 #define STORAGE_LEVELDB_UTIL_TESTHARNESS_H_
11 #include "leveldb/env.h"
12 #include "leveldb/slice.h"
13 #include "util/random.h"
18 // Run some of the tests registered by the TEST() macro. If the
19 // environment variable "LEVELDB_TESTS" is not set, runs all tests.
20 // Otherwise, runs only the tests whose name contains the value of
21 // "LEVELDB_TESTS" as a substring. E.g., suppose the tests are:
22 // TEST(Foo, Hello) { ... }
23 // TEST(Foo, World) { ... }
24 // LEVELDB_TESTS=Hello will run the first test
25 // LEVELDB_TESTS=o will run both tests
26 // LEVELDB_TESTS=Junk will run no tests
28 // Returns 0 if all tests pass.
29 // Dies or returns a non-zero value if some test fails.
30 extern int RunAllTests();
32 // Return the directory to use for temporary storage.
33 extern std::string TmpDir();
35 // Return a randomization seed for this run. Typically returns the
36 // same number on repeated invocations of this binary, but automated
37 // runs may be able to vary the seed.
38 extern int RandomSeed();
40 // An instance of Tester is allocated to hold temporary state during
41 // the execution of an assertion.
47 std::stringstream ss_;
50 Tester(const char* f, int l)
51 : ok_(true), fname_(f), line_(l) {
56 fprintf(stderr, "%s:%d:%s\n", fname_, line_, ss_.str().c_str());
61 Tester& Is(bool b, const char* msg) {
63 ss_ << " Assertion failure " << msg;
69 Tester& IsOk(const Status& s) {
71 ss_ << " " << s.ToString();
77 #define BINARY_OP(name,op) \
78 template <class X, class Y> \
79 Tester& name(const X& x, const Y& y) { \
81 ss_ << " failed: " << x << (" " #op " ") << y; \
95 // Attach the specified value to the error message if an error has occurred
97 Tester& operator<<(const V& value) {
105 #define ASSERT_TRUE(c) ::leveldb::test::Tester(__FILE__, __LINE__).Is((c), #c)
106 #define ASSERT_OK(s) ::leveldb::test::Tester(__FILE__, __LINE__).IsOk((s))
107 #define ASSERT_EQ(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsEq((a),(b))
108 #define ASSERT_NE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsNe((a),(b))
109 #define ASSERT_GE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsGe((a),(b))
110 #define ASSERT_GT(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsGt((a),(b))
111 #define ASSERT_LE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsLe((a),(b))
112 #define ASSERT_LT(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsLt((a),(b))
114 #define TCONCAT(a,b) TCONCAT1(a,b)
115 #define TCONCAT1(a,b) a##b
117 #define TEST(base,name) \
118 class TCONCAT(_Test_,name) : public base { \
121 static void _RunIt() { \
122 TCONCAT(_Test_,name) t; \
126 bool TCONCAT(_Test_ignored_,name) = \
127 ::leveldb::test::RegisterTest(#base, #name, &TCONCAT(_Test_,name)::_RunIt); \
128 void TCONCAT(_Test_,name)::_Run()
130 // Register the specified test. Typically not used directly, but
131 // invoked via the macro expansion of TEST.
132 extern bool RegisterTest(const char* base, const char* name, void (*func)());
136 } // namespace leveldb
138 #endif // STORAGE_LEVELDB_UTIL_TESTHARNESS_H_