testharness.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #ifdef OS_AIX
  11. #include "gtest/gtest.h"
  12. #else
  13. #include <gtest/gtest.h>
  14. #endif
  15. // A "skipped" test has a specific meaning in Facebook infrastructure: the
  16. // test is in good shape and should be run, but something about the
  17. // compilation or execution environment means the test cannot be run.
  18. // Specifically, there is a hole in intended testing if any
  19. // parameterization of a test (e.g. Foo/FooTest.Bar/42) is skipped for all
  20. // tested build configurations/platforms/etc.
  21. //
  22. // If GTEST_SKIP is available, use it. Otherwise, define skip as success.
  23. //
  24. // The GTEST macros do not seem to print the message, even with -verbose,
  25. // so these print to stderr. Note that these do not exit the test themselves;
  26. // calling code should 'return' or similar from the test.
  27. #ifdef GTEST_SKIP_
  28. #define ROCKSDB_GTEST_SKIP(m) \
  29. do { \
  30. fputs("SKIPPED: " m "\n", stderr); \
  31. GTEST_SKIP_(m); \
  32. } while (false) /* user ; */
  33. #else
  34. #define ROCKSDB_GTEST_SKIP(m) \
  35. do { \
  36. fputs("SKIPPED: " m "\n", stderr); \
  37. GTEST_SUCCESS_("SKIPPED: " m); \
  38. } while (false) /* user ; */
  39. #endif
  40. // We add "bypass" as an alternative to ROCKSDB_GTEST_SKIP that is allowed to
  41. // be a permanent condition, e.g. for intentionally omitting or disabling some
  42. // parameterizations for some tests. (Use _DISABLED at the end of the test
  43. // name to disable an entire test.)
  44. #define ROCKSDB_GTEST_BYPASS(m) \
  45. do { \
  46. fputs("BYPASSED: " m "\n", stderr); \
  47. GTEST_SUCCESS_("BYPASSED: " m); \
  48. } while (false) /* user ; */
  49. // Avoid "loss of precision" warnings when passing in 64-bit integers
  50. #define EXPECT_NEAR2(val1, val2, abs_error) \
  51. EXPECT_NEAR(static_cast<double>(val1), static_cast<double>(val2), \
  52. static_cast<double>(abs_error))
  53. #include <string>
  54. #include "port/stack_trace.h"
  55. #include "rocksdb/env.h"
  56. namespace ROCKSDB_NAMESPACE {
  57. namespace test {
  58. // Return the directory to use for temporary storage.
  59. std::string TmpDir(Env* env = Env::Default());
  60. // A path unique within the thread
  61. std::string PerThreadDBPath(std::string name);
  62. std::string PerThreadDBPath(Env* env, std::string name);
  63. std::string PerThreadDBPath(std::string dir, std::string name);
  64. // Return a randomization seed for this run. Typically returns the
  65. // same number on repeated invocations of this binary, but automated
  66. // runs may be able to vary the seed.
  67. int RandomSeed();
  68. ::testing::AssertionResult AssertStatus(const char* s_expr, const Status& s);
  69. #define ASSERT_OK(s) \
  70. ASSERT_PRED_FORMAT1(ROCKSDB_NAMESPACE::test::AssertStatus, s)
  71. #define ASSERT_NOK(s) ASSERT_FALSE((s).ok())
  72. #define EXPECT_OK(s) \
  73. EXPECT_PRED_FORMAT1(ROCKSDB_NAMESPACE::test::AssertStatus, s)
  74. #define EXPECT_NOK(s) EXPECT_FALSE((s).ok())
  75. // Useful for testing
  76. // * No need to deal with Status like in Regex public API
  77. // * No triggering lint reports on use of std::regex in tests
  78. // * Available in LITE (unlike public API)
  79. class TestRegex {
  80. public:
  81. // These throw on bad pattern
  82. /*implicit*/ TestRegex(const std::string& pattern);
  83. /*implicit*/ TestRegex(const char* pattern);
  84. // Checks that the whole of str is matched by this regex
  85. bool Matches(const std::string& str) const;
  86. const std::string& GetPattern() const;
  87. private:
  88. class Impl;
  89. std::shared_ptr<Impl> impl_; // shared_ptr for simple implementation
  90. std::string pattern_;
  91. };
  92. ::testing::AssertionResult AssertMatchesRegex(const char* str_expr,
  93. const char* pattern_expr,
  94. const std::string& str,
  95. const TestRegex& pattern);
  96. #define ASSERT_MATCHES_REGEX(str, pattern) \
  97. ASSERT_PRED_FORMAT2(ROCKSDB_NAMESPACE::test::AssertMatchesRegex, str, pattern)
  98. #define EXPECT_MATCHES_REGEX(str, pattern) \
  99. EXPECT_PRED_FORMAT2(ROCKSDB_NAMESPACE::test::AssertMatchesRegex, str, pattern)
  100. } // namespace test
  101. using test::TestRegex;
  102. } // namespace ROCKSDB_NAMESPACE