testharness.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "test_util/testharness.h"
  10. #include <regex>
  11. #include <string>
  12. #include <thread>
  13. namespace ROCKSDB_NAMESPACE::test {
  14. #ifdef OS_WIN
  15. #include <windows.h>
  16. std::string GetPidStr() { return std::to_string(GetCurrentProcessId()); }
  17. #else
  18. std::string GetPidStr() { return std::to_string(getpid()); }
  19. #endif
  20. ::testing::AssertionResult AssertStatus(const char* s_expr, const Status& s) {
  21. if (s.ok()) {
  22. return ::testing::AssertionSuccess();
  23. } else {
  24. return ::testing::AssertionFailure() << s_expr << std::endl << s.ToString();
  25. }
  26. }
  27. std::string TmpDir(Env* env) {
  28. std::string dir;
  29. Status s = env->GetTestDirectory(&dir);
  30. EXPECT_OK(s);
  31. return dir;
  32. }
  33. std::string PerThreadDBPath(std::string dir, std::string name) {
  34. size_t tid = std::hash<std::thread::id>()(std::this_thread::get_id());
  35. return dir + "/" + name + "_" + GetPidStr() + "_" + std::to_string(tid);
  36. }
  37. std::string PerThreadDBPath(std::string name) {
  38. return PerThreadDBPath(test::TmpDir(), name);
  39. }
  40. std::string PerThreadDBPath(Env* env, std::string name) {
  41. return PerThreadDBPath(test::TmpDir(env), name);
  42. }
  43. int RandomSeed() {
  44. const char* env = getenv("TEST_RANDOM_SEED");
  45. int result = (env != nullptr ? atoi(env) : 301);
  46. if (result <= 0) {
  47. result = 301;
  48. }
  49. return result;
  50. }
  51. TestRegex::TestRegex(const std::string& pattern)
  52. : impl_(std::make_shared<Impl>(pattern)), pattern_(pattern) {}
  53. TestRegex::TestRegex(const char* pattern)
  54. : impl_(std::make_shared<Impl>(pattern)), pattern_(pattern) {}
  55. const std::string& TestRegex::GetPattern() const { return pattern_; }
  56. class TestRegex::Impl : public std::regex {
  57. public:
  58. using std::regex::basic_regex;
  59. };
  60. bool TestRegex::Matches(const std::string& str) const {
  61. if (impl_) {
  62. return std::regex_match(str, *impl_);
  63. } else {
  64. // Should not call Matches on unset Regex
  65. assert(false);
  66. return false;
  67. }
  68. }
  69. ::testing::AssertionResult AssertMatchesRegex(const char* str_expr,
  70. const char* pattern_expr,
  71. const std::string& str,
  72. const TestRegex& pattern) {
  73. if (pattern.Matches(str)) {
  74. return ::testing::AssertionSuccess();
  75. } else if (TestRegex("\".*\"").Matches(pattern_expr)) {
  76. // constant regex string
  77. return ::testing::AssertionFailure()
  78. << str << " (" << str_expr << ")" << std::endl
  79. << "does not match regex " << pattern.GetPattern();
  80. } else {
  81. // runtime regex string
  82. return ::testing::AssertionFailure()
  83. << str << " (" << str_expr << ")" << std::endl
  84. << "does not match regex" << std::endl
  85. << pattern.GetPattern() << " (" << pattern_expr << ")";
  86. }
  87. }
  88. } // namespace ROCKSDB_NAMESPACE::test