Add test helper macros. (#465)

- Refactor the existing SNMALLOC_ASSERT and SNMALLOC_CHECK.  These now
   use the FatalErrorBuilder to format the output if a format string is
   provided.
 - Extend the FatalErrorBuilder to print decimal integers for signed
   values.
 - Rename FatalErrorBuilder to MessageBuilder.
 - Rewrite the macros used in the jemalloc tests to use
   FatalErrorBuilder and move them into a header.
 - Refactor some of the tests to use the new macros.
This commit is contained in:
David Chisnall
2022-02-25 15:57:28 +00:00
committed by GitHub
parent 93efbb4807
commit 95bd974fb0
7 changed files with 234 additions and 147 deletions

39
src/test/helpers.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#ifdef _MSC_VER
# define __PRETTY_FUNCTION__ __FUNCSIG__
#endif
namespace snmalloc
{
/**
* The name of the function under test. This is set in the START_TEST macro
* and used for error reporting in EXPECT.
*/
const char* current_test = "";
/**
* Log that the test started.
*/
#define START_TEST(msg, ...) \
do \
{ \
current_test = __PRETTY_FUNCTION__; \
MessageBuilder<1024> mb{"Starting test: " msg "\n", ##__VA_ARGS__}; \
fputs(mb.get_message(), stderr); \
} while (0)
/**
* An assertion that fires even in debug builds. Uses the value set by
* START_TEST.
*/
#define EXPECT(x, msg, ...) \
SNMALLOC_CHECK_MSG(x, " in test {} " msg "\n", current_test, ##__VA_ARGS__)
#define INFO(msg, ...) \
do \
{ \
MessageBuilder<1024> mb{msg "\n", ##__VA_ARGS__}; \
fputs(mb.get_message(), stderr); \
} while (0)
}