小组成员: 曹可心-10223903406 朴祉燕-10224602413
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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