Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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