I have a quick & dirty macro which I used to quickly test my program:
#define ASSERT_EQUAL(expected, actualFunc, desc)
expected
should be the first argument, and is a literal; actualFunc
should be the second argument, and is a function call with parameters, an example call looks like:
ASSERT_EQUAL(true, test1(42), "cache-friendly");
In this macro, I will also timing the second argument(the function call), in order to make sure it works as expected, I need to make sure the parameters are passed in right order, or else it should error out., like below:
ASSERT_EQUAL(test1(42), true, "cache-friendly");
I tried:
static_assert(std::is_fundamental<decltype(expected)::value)
But it doesn't work as it won't error out even if I pass in the function call as first paramter expected
, because its return value is a fundamental type.
is there a way to static assert and error out if the order of paramters are not as expected?
FYI - the macro implementation:
static int CaseNum = 0;
#define ASSERT_BASE(expected, actualFunc, desc) \
std::chrono::steady_clock clock; \
auto start = clock.now(); \
auto actual = actualFunc; \
auto elapsedTime = clock.now() - start; \
auto timeInUS = std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime).count(); \
cout << boolalpha << "CASE " << ++CaseNum << ": " << "EXPECTED=" << expected << "; ACTUAL=" << actual << "; elapsedTime=" << timeInUS << "us" << " --- " << desc << endl; \
#define ASSERT_EQUAL(expected, actualFunc, desc) \
{ \
ASSERT_BASE(expected, actualFunc, desc) \
assert(expected == actual); \
}
#define ASSERT_NEAR(expected, actualFunc, desc) \
{ \
ASSERT_BASE(expected, actualFunc, desc) \
assert(fabs(expected - actual) < 0.0001); \
}