Skip to content

Instantly share code, notes, and snippets.

@yaraki
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaraki/ef6b03190cc2ffe6ffbf to your computer and use it in GitHub Desktop.
Save yaraki/ef6b03190cc2ffe6ffbf to your computer and use it in GitHub Desktop.
Minimal testing
// clang++ -o minitest minitest.cpp -std=c++1y -stdlib=libc++
#include <iostream>
#define TEST_PREDICATE(expr, oper) \
do { \
auto expr__ = (expr); \
if (!(expr__ oper)) { \
std::cout << __FILE__ << ":" << __LINE__ << " " \
<< "FAILED " << #expr << " " << #oper \
<< " (actual: " << expr__ << ")" << std::endl; \
} \
} while (0)
#define TEST_TRUE(expr) \
do { \
if (!(expr)) { \
std::cout << __FILE__ << ":" << __LINE__ << " " \
<< "FAILED " #expr << std::endl; \
} \
} while (0)
#define TEST_MACRO(_1, _2, NAME, ...) NAME
#define TEST(...) TEST_MACRO(__VA_ARGS__, TEST_PREDICATE, TEST_TRUE)(__VA_ARGS__)
// Test target sample
template <typename item_t>
class rectangle
{
private:
item_t width_;
item_t height_;
public:
rectangle(item_t width, item_t height)
: width_(width), height_(height)
{
}
item_t area() const
{
return width_ + height_; // TODO: Fix bug here
}
bool is_square() const
{
return width_ == height_;
}
};
// Test sample
int main()
{
rectangle<int> r(2, 3);
TEST(r.area(), == 6);
TEST(r.area(), >= 6);
TEST(r.area(), != 5);
TEST(r.is_square()); // TODO: Fix bug here
return 0;
}
@yaraki
Copy link
Author

yaraki commented Dec 16, 2014

$ ./minitest 
minitest.cpp:58 FAILED r.area() == 6 (actual: 5)
minitest.cpp:59 FAILED r.area() >= 6 (actual: 5)
minitest.cpp:60 FAILED r.area() != 5 (actual: 5)
minitest.cpp:61 FAILED r.is_square()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment