2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2021-09-07 20:04:07 +00:00
|
|
|
|
|
|
|
#include <regex>
|
2011-08-16 01:21:01 +00:00
|
|
|
#include <string>
|
2018-07-14 00:18:39 +00:00
|
|
|
#include <thread>
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
namespace test {
|
|
|
|
|
2021-03-30 23:50:17 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
std::string GetPidStr() { return std::to_string(GetCurrentProcessId()); }
|
|
|
|
#else
|
|
|
|
std::string GetPidStr() { return std::to_string(getpid()); }
|
|
|
|
#endif
|
|
|
|
|
2015-03-20 00:32:43 +00:00
|
|
|
::testing::AssertionResult AssertStatus(const char* s_expr, const Status& s) {
|
|
|
|
if (s.ok()) {
|
|
|
|
return ::testing::AssertionSuccess();
|
|
|
|
} else {
|
|
|
|
return ::testing::AssertionFailure() << s_expr << std::endl
|
|
|
|
<< s.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:08:10 +00:00
|
|
|
std::string TmpDir(Env* env) {
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string dir;
|
2014-10-31 22:08:10 +00:00
|
|
|
Status s = env->GetTestDirectory(&dir);
|
2020-07-29 05:58:28 +00:00
|
|
|
EXPECT_OK(s);
|
2011-03-18 22:37:00 +00:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2018-07-14 00:18:39 +00:00
|
|
|
std::string PerThreadDBPath(std::string dir, std::string name) {
|
|
|
|
size_t tid = std::hash<std::thread::id>()(std::this_thread::get_id());
|
2021-03-30 23:50:17 +00:00
|
|
|
return dir + "/" + name + "_" + GetPidStr() + "_" + std::to_string(tid);
|
2018-07-14 00:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PerThreadDBPath(std::string name) {
|
|
|
|
return PerThreadDBPath(test::TmpDir(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PerThreadDBPath(Env* env, std::string name) {
|
|
|
|
return PerThreadDBPath(test::TmpDir(env), name);
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
int RandomSeed() {
|
|
|
|
const char* env = getenv("TEST_RANDOM_SEED");
|
2013-03-01 02:04:58 +00:00
|
|
|
int result = (env != nullptr ? atoi(env) : 301);
|
2011-03-18 22:37:00 +00:00
|
|
|
if (result <= 0) {
|
|
|
|
result = 301;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-07 20:04:07 +00:00
|
|
|
TestRegex::TestRegex(const std::string& pattern)
|
|
|
|
: impl_(std::make_shared<Impl>(pattern)), pattern_(pattern) {}
|
|
|
|
TestRegex::TestRegex(const char* pattern)
|
|
|
|
: impl_(std::make_shared<Impl>(pattern)), pattern_(pattern) {}
|
|
|
|
|
|
|
|
const std::string& TestRegex::GetPattern() const { return pattern_; }
|
|
|
|
|
|
|
|
class TestRegex::Impl : public std::regex {
|
|
|
|
public:
|
|
|
|
using std::regex::basic_regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool TestRegex::Matches(const std::string& str) const {
|
|
|
|
if (impl_) {
|
|
|
|
return std::regex_match(str, *impl_);
|
|
|
|
} else {
|
|
|
|
// Should not call Matches on unset Regex
|
|
|
|
assert(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::testing::AssertionResult AssertMatchesRegex(const char* str_expr,
|
|
|
|
const char* pattern_expr,
|
|
|
|
const std::string& str,
|
|
|
|
const TestRegex& pattern) {
|
|
|
|
if (pattern.Matches(str)) {
|
|
|
|
return ::testing::AssertionSuccess();
|
|
|
|
} else if (TestRegex("\".*\"").Matches(pattern_expr)) {
|
|
|
|
// constant regex string
|
|
|
|
return ::testing::AssertionFailure()
|
|
|
|
<< str << " (" << str_expr << ")" << std::endl
|
|
|
|
<< "does not match regex " << pattern.GetPattern();
|
|
|
|
} else {
|
|
|
|
// runtime regex string
|
|
|
|
return ::testing::AssertionFailure()
|
|
|
|
<< str << " (" << str_expr << ")" << std::endl
|
|
|
|
<< "does not match regex" << std::endl
|
|
|
|
<< pattern.GetPattern() << " (" << pattern_expr << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace test
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|