proxygen
folly::test::detail Namespace Reference

Classes

class  CheckResult
 
struct  SavedState
 

Typedefs

typedef boost::random::mt19937 RandomT
 

Functions

SavedState disableInvalidParameters ()
 
void enableInvalidParameters (SavedState)
 
bool hasPCREPatternMatch (StringPiece pattern, StringPiece target)
 
bool hasNoPCREPatternMatch (StringPiece pattern, StringPiece target)
 
static RandomT rng (seed)
 
template<class Integral1 , class Integral2 >
Integral2 random (Integral1 low, Integral2 up)
 
template<class String >
void randomString (String *toFill, unsigned int maxSize=1000)
 
template<class String , class Integral >
void Num2String (String &str, Integral)
 
std::list< char > RandomList (unsigned int maxSize)
 
template<class T >
T randomObject ()
 
template<>
int randomObject< int > ()
 
template<>
std::string randomObject< std::string > ()
 
template<>
folly::fbstring randomObject< folly::fbstring > ()
 
template<typename Fn >
CheckResult checkThrowErrno (Fn &&fn, int errnoValue, const char *statementStr)
 
template<typename ExType , typename Fn >
CheckResult checkThrowRegex (Fn &&fn, const char *pattern, const char *statementStr, const char *excTypeStr)
 

Variables

static auto const seed = randomNumberSeed()
 

Typedef Documentation

typedef boost::random::mt19937 folly::test::detail::RandomT

Definition at line 32 of file FBVectorTestUtil.h.

Function Documentation

template<typename Fn >
CheckResult folly::test::detail::checkThrowErrno ( Fn &&  fn,
int  errnoValue,
const char *  statementStr 
)

Helper function for implementing EXPECT_THROW

Definition at line 181 of file TestUtils.h.

References folly::test::detail::CheckResult::CheckResult(), errnoValue, and folly::exceptionStr().

181  {
182  try {
183  fn();
184  } catch (const std::system_error& ex) {
185  // TODO: POSIX errno values should use std::generic_category(), but
186  // folly/Exception.h incorrectly throws them using std::system_category()
187  // at the moment.
188  // For now we also accept std::system_category so that we will also handle
189  // exceptions from folly/Exception.h correctly.
190  if (ex.code().category() != std::generic_category() &&
191  ex.code().category() != std::system_category()) {
192  return CheckResult(false)
193  << "Expected: " << statementStr << " throws an exception with errno "
194  << errnoValue << " (" << std::generic_category().message(errnoValue)
195  << ")\nActual: it throws a system_error with category "
196  << ex.code().category().name() << ": " << ex.what();
197  }
198  if (ex.code().value() != errnoValue) {
199  return CheckResult(false)
200  << "Expected: " << statementStr << " throws an exception with errno "
201  << errnoValue << " (" << std::generic_category().message(errnoValue)
202  << ")\nActual: it throws errno " << ex.code().value() << ": "
203  << ex.what();
204  }
205  return CheckResult(true);
206  } catch (const std::exception& ex) {
207  return CheckResult(false)
208  << "Expected: " << statementStr << " throws an exception with errno "
209  << errnoValue << " (" << std::generic_category().message(errnoValue)
210  << ")\nActual: it throws a different exception: " << exceptionStr(ex);
211  } catch (...) {
212  return CheckResult(false)
213  << "Expected: " << statementStr << " throws an exception with errno "
214  << errnoValue << " (" << std::generic_category().message(errnoValue)
215  << ")\nActual: it throws a non-exception type";
216  }
217  return CheckResult(false)
218  << "Expected: " << statementStr << " throws an exception with errno "
219  << errnoValue << " (" << std::generic_category().message(errnoValue)
220  << ")\nActual: it throws nothing";
221 }
fbstring exceptionStr(const std::exception &e)
int errnoValue
Definition: Subprocess.cpp:225
template<typename ExType , typename Fn >
CheckResult folly::test::detail::checkThrowRegex ( Fn &&  fn,
const char *  pattern,
const char *  statementStr,
const char *  excTypeStr 
)

Helper function for implementing EXPECT_THROW_RE

Definition at line 227 of file TestUtils.h.

References folly::test::detail::CheckResult::CheckResult(), folly::exceptionStr(), regex, and value.

231  {
232  static_assert(
234  "EXPECT_THROW_RE() exception type must derive from std::exception");
235 
236  try {
237  fn();
238  } catch (const std::exception& ex) {
239  const auto* derived = dynamic_cast<const ExType*>(&ex);
240  if (!derived) {
241  return CheckResult(false)
242  << "Expected: " << statementStr << "throws a " << excTypeStr
243  << ")\nActual: it throws a different exception type: "
244  << exceptionStr(ex);
245  }
246 
247  std::regex re(pattern);
248  if (!std::regex_search(derived->what(), re)) {
249  return CheckResult(false)
250  << "Expected: " << statementStr << " throws a " << excTypeStr
251  << " with message matching \"" << pattern
252  << "\"\nActual: message is: " << derived->what();
253  }
254  return CheckResult(true);
255  } catch (...) {
256  return CheckResult(false)
257  << "Expected: " << statementStr << " throws a " << excTypeStr
258  << ")\nActual: it throws a non-exception type";
259  }
260  return CheckResult(false) << "Expected: " << statementStr << " throws a "
261  << excTypeStr << ")\nActual: it throws nothing";
262 }
regex
Definition: CMakeCache.txt:563
fbstring exceptionStr(const std::exception &e)
static const char *const value
Definition: Conv.cpp:50
SavedState folly::test::detail::disableInvalidParameters ( )

Definition at line 146 of file TestUtil.cpp.

References enableInvalidParameters(), folly::test::detail::SavedState::previousCrtReportMode, and folly::test::detail::SavedState::previousThreadLocalHandler.

Referenced by folly::test::msvcSuppressAbortOnInvalidParams().

146  {
147 #ifdef _WIN32
148  SavedState ret;
149  ret.previousThreadLocalHandler =
150  _set_thread_local_invalid_parameter_handler([](const wchar_t*,
151  const wchar_t*,
152  const wchar_t*,
153  unsigned int,
154  uintptr_t) {});
155  ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
156  return ret;
157 #else
158  return SavedState();
159 #endif
160 }
void folly::test::detail::enableInvalidParameters ( SavedState  )

Definition at line 169 of file TestUtil.cpp.

Referenced by disableInvalidParameters(), and folly::test::msvcSuppressAbortOnInvalidParams().

169 {}
bool folly::test::detail::hasNoPCREPatternMatch ( StringPiece  pattern,
StringPiece  target 
)

Definition at line 179 of file TestUtil.cpp.

References hasPCREPatternMatch().

179  {
180  return !hasPCREPatternMatch(pattern, target);
181 }
bool hasPCREPatternMatch(StringPiece pattern, StringPiece target)
Definition: TestUtil.cpp:172
bool folly::test::detail::hasPCREPatternMatch ( StringPiece  pattern,
StringPiece  target 
)

Definition at line 172 of file TestUtil.cpp.

References folly::Range< Iter >::begin(), folly::Range< Iter >::end(), and regex.

Referenced by hasNoPCREPatternMatch().

172  {
173  return boost::regex_match(
174  target.begin(),
175  target.end(),
176  boost::regex(pattern.begin(), pattern.end()));
177 }
regex
Definition: CMakeCache.txt:563
template<class String , class Integral >
void folly::test::detail::Num2String ( String &  str,
Integral   
)

Definition at line 51 of file FBVectorTestUtil.h.

51  {
52  str.resize(10, '\0');
53  sprintf(&str[0], "%ul", 10);
54  str.resize(strlen(str.c_str()));
55 }
template<class Integral1 , class Integral2 >
Integral2 folly::test::detail::random ( Integral1  low,
Integral2  up 
)

Definition at line 36 of file FBVectorTestUtil.h.

References folly::range(), and rng().

Referenced by RandomList(), randomObject< int >(), and randomString().

36  {
37  boost::uniform_int<> range(low, up);
38  return range(rng);
39 }
auto rng
Definition: CollectTest.cpp:31
Gen range(Value begin, Value end)
Definition: Base.h:467
std::list<char> folly::test::detail::RandomList ( unsigned int  maxSize)

Definition at line 57 of file FBVectorTestUtil.h.

References i, random(), randomObject(), and folly::T.

57  {
58  std::list<char> lst(random(0u, maxSize));
59  std::list<char>::iterator i = lst.begin();
60  for (; i != lst.end(); ++i) {
61  *i = random('a', 'z');
62  }
63  return lst;
64 }
Integral2 random(Integral1 low, Integral2 up)
template<class T >
T folly::test::detail::randomObject ( )

Referenced by RandomList().

Definition at line 82 of file FBVectorTestUtil.h.

References randomString().

82  {
83  folly::fbstring result;
84  randomString(&result);
85  return result;
86 }
void randomString(String *toFill, size_t size=1000)
template<>
int folly::test::detail::randomObject< int > ( )

Definition at line 70 of file FBVectorTestUtil.h.

References random().

Referenced by TEST().

70  {
71  return random(0, 1024);
72 }
Integral2 random(Integral1 low, Integral2 up)

Definition at line 75 of file FBVectorTestUtil.h.

References randomString(), and string.

75  {
76  std::string result;
77  randomString(&result);
78  return result;
79 }
const char * string
Definition: Conv.cpp:212
void randomString(String *toFill, size_t size=1000)
template<class String >
void folly::test::detail::randomString ( String *  toFill,
unsigned int  maxSize = 1000 
)

Definition at line 42 of file FBVectorTestUtil.h.

References c, and random().

Referenced by randomObject< folly::fbstring >(), and randomObject< std::string >().

42  {
43  assert(toFill);
44  toFill->resize(random(0, maxSize));
45  for (auto& c : *toFill) {
46  c = random('a', 'z');
47  }
48 }
Integral2 random(Integral1 low, Integral2 up)
char c
static RandomT folly::test::detail::rng ( seed  )
static

Referenced by random().

Variable Documentation

auto const folly::test::detail::seed = randomNumberSeed()
static

Definition at line 31 of file FBVectorTestUtil.h.