Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active February 5, 2023 08:02
Show Gist options
  • Save drmalex07/13595d195c18adad4aa62420c7213a11 to your computer and use it in GitHub Desktop.
Save drmalex07/13595d195c18adad4aa62420c7213a11 to your computer and use it in GitHub Desktop.
An example unit test with Boost.Test. #boost #c++ #unit-test
#include <boost/test/included/unit_test.hpp>
#include <boost/test/parameterized_test.hpp>
using namespace boost::unit_test;
// A function-based test
void test1f(int i)
{
BOOST_CHECK(i < 4);
}
// A method-based test
struct tester {
int n;
tester(int n): n(n) {}
void test1(int i)
{
BOOST_CHECK(i < n);
}
};
tester t1(4);
// Initialize suite
void init_unit_test_suite(int argc, char* argv[])
{
int params[] = { 1, 2, 3, 4, 5 };
callback1<double> test1m = bind(&tester::test1, &t1, _1);
framework::master_test_suite()
.add(BOOST_PARAM_TEST_CASE(&test1f, params, params + 5));
framework::master_test_suite()
.add(BOOST_PARAM_TEST_CASE(test1m, params, params + 5));
return;
}
#include <boost/test/included/unit_test.hpp>
#include <boost/test/parameterized_test.hpp>
using namespace boost::unit_test;
struct parameters
{
int u,v;
parameters(int u, int v): u(u), v(v) {}
};
// A function-based test
void test1f(const parameters& p)
{
BOOST_CHECK(p.u < 4);
}
// A method-based test
struct tester
{
int n;
tester(int n): n(n) {}
void test1(const parameters& p)
{
std::cerr << "p.u" << p.u << " p.v=" << p.v << std::endl;
BOOST_CHECK(p.u < n);
}
};
tester t1(4);
// Parameter sets
std::array<parameters, 4> params = {
parameters(1, 5),
parameters(2, 7),
parameters(3, 6),
parameters(5, 9),
};
// Initialize suite
void init_unit_test_suite(int argc, char* argv[])
{
callback1<const parameters&> test1m = bind(&tester::test1, &t1, _1);
framework::master_test_suite()
.add(BOOST_PARAM_TEST_CASE(&test1f, params.cbegin(), params.cend()));
framework::master_test_suite()
.add(BOOST_PARAM_TEST_CASE(test1m, params.cbegin(), params.cend()));
return;
}
#define BOOST_TEST_MODULE Test1
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test1a)
{
int i = 1;
BOOST_CHECK(i > 4);
BOOST_CHECK(i > i * i);
}
BOOST_AUTO_TEST_CASE(test1b)
{
int i = 5;
BOOST_CHECK(i > 4);
BOOST_CHECK(i > i * i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment