小组成员:谢瑞阳、徐翔宇
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.6 KiB

  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. #ifndef STORAGE_LEVELDB_UTIL_TESTHARNESS_H_
  5. #define STORAGE_LEVELDB_UTIL_TESTHARNESS_H_
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sstream>
  9. #include "leveldb/env.h"
  10. #include "leveldb/slice.h"
  11. #include "util/random.h"
  12. namespace leveldb {
  13. namespace test {
  14. // Run some of the tests registered by the TEST() macro. If the
  15. // environment variable "LEVELDB_TESTS" is not set, runs all tests.
  16. // Otherwise, runs only the tests whose name contains the value of
  17. // "LEVELDB_TESTS" as a substring. E.g., suppose the tests are:
  18. // TEST(Foo, Hello) { ... }
  19. // TEST(Foo, World) { ... }
  20. // LEVELDB_TESTS=Hello will run the first test
  21. // LEVELDB_TESTS=o will run both tests
  22. // LEVELDB_TESTS=Junk will run no tests
  23. //
  24. // Returns 0 if all tests pass.
  25. // Dies or returns a non-zero value if some test fails.
  26. extern int RunAllTests();
  27. // Return the directory to use for temporary storage.
  28. extern std::string TmpDir();
  29. // Return a randomization seed for this run. Typically returns the
  30. // same number on repeated invocations of this binary, but automated
  31. // runs may be able to vary the seed.
  32. extern int RandomSeed();
  33. // An instance of Tester is allocated to hold temporary state during
  34. // the execution of an assertion.
  35. class Tester {
  36. private:
  37. bool ok_;
  38. const char* fname_;
  39. int line_;
  40. std::stringstream ss_;
  41. public:
  42. Tester(const char* f, int l)
  43. : ok_(true), fname_(f), line_(l) {
  44. }
  45. ~Tester() {
  46. if (!ok_) {
  47. fprintf(stderr, "%s:%d:%s\n", fname_, line_, ss_.str().c_str());
  48. exit(1);
  49. }
  50. }
  51. Tester& Is(bool b, const char* msg) {
  52. if (!b) {
  53. ss_ << " Assertion failure " << msg;
  54. ok_ = false;
  55. }
  56. return *this;
  57. }
  58. Tester& IsOk(const Status& s) {
  59. if (!s.ok()) {
  60. ss_ << " " << s.ToString();
  61. ok_ = false;
  62. }
  63. return *this;
  64. }
  65. #define BINARY_OP(name,op) \
  66. template <class X, class Y> \
  67. Tester& name(const X& x, const Y& y) { \
  68. if (! (x op y)) { \
  69. ss_ << " failed: " << x << (" " #op " ") << y; \
  70. ok_ = false; \
  71. } \
  72. return *this; \
  73. }
  74. BINARY_OP(IsEq, ==)
  75. BINARY_OP(IsNe, !=)
  76. BINARY_OP(IsGe, >=)
  77. BINARY_OP(IsGt, >)
  78. BINARY_OP(IsLe, <=)
  79. BINARY_OP(IsLt, <)
  80. #undef BINARY_OP
  81. // Attach the specified value to the error message if an error has occurred
  82. template <class V>
  83. Tester& operator<<(const V& value) {
  84. if (!ok_) {
  85. ss_ << " " << value;
  86. }
  87. return *this;
  88. }
  89. };
  90. #define ASSERT_TRUE(c) ::leveldb::test::Tester(__FILE__, __LINE__).Is((c), #c)
  91. #define ASSERT_OK(s) ::leveldb::test::Tester(__FILE__, __LINE__).IsOk((s))
  92. #define ASSERT_EQ(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsEq((a),(b))
  93. #define ASSERT_NE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsNe((a),(b))
  94. #define ASSERT_GE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsGe((a),(b))
  95. #define ASSERT_GT(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsGt((a),(b))
  96. #define ASSERT_LE(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsLe((a),(b))
  97. #define ASSERT_LT(a,b) ::leveldb::test::Tester(__FILE__, __LINE__).IsLt((a),(b))
  98. #define TCONCAT(a,b) TCONCAT1(a,b)
  99. #define TCONCAT1(a,b) a##b
  100. #define TEST(base,name) \
  101. class TCONCAT(_Test_,name) : public base { \
  102. public: \
  103. void _Run(); \
  104. static void _RunIt() { \
  105. TCONCAT(_Test_,name) t; \
  106. t._Run(); \
  107. } \
  108. }; \
  109. bool TCONCAT(_Test_ignored_,name) = \
  110. ::leveldb::test::RegisterTest(#base, #name, &TCONCAT(_Test_,name)::_RunIt); \
  111. void TCONCAT(_Test_,name)::_Run()
  112. // Register the specified test. Typically not used directly, but
  113. // invoked via the macro expansion of TEST.
  114. extern bool RegisterTest(const char* base, const char* name, void (*func)());
  115. } // namespace test
  116. } // namespace leveldb
  117. #endif // STORAGE_LEVELDB_UTIL_TESTHARNESS_H_