Boost logo

Boost Users :

From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2008-01-05 03:00:47


"Jens Seidel" <jensseidel_at_[hidden]> wrote in message
news:20080105024939.GA2444_at_imkf-pc073.imkf.tu-freiberg.de...
> Hi Tomasz,
>
> On Sat, Jan 05, 2008 at 01:24:26AM +0100, Tomasz Kalkosinski wrote:
>> My problem is easy. I want to have one main test that add Second and so
>> on. I cannot get it to work :/ What's the best practice? Is
>> BOOST_CLASS_TEST_CASE a good approach on Second? I cannot get add to work
>> :/ I definitely need an example with two file, parent and child test
>> example.
>
> I use similar test cases. Please note that my code is probably not
> perfect as I use for example manuel test registration and still need to
> adapt the code (using some autotools determined macros) to avoid the
> static/shared library fiasco but it works at least with older Boost code

I must say you are comming a bit strong here, don't you think? The fact that
you were using unsupported configuration which doesn't work anymore is IMO
minor inconvinience.

> The following code is extracted from my project and mainly uses the fact
> that one can add test suites as well as simple test functions to other
> test suites. So it's easy to create hierarchies. I think I once just
> tried it and it worked. Not sure whether it can be found in the old or
> new documentation.
>
> /******************************************************/
> // test.cpp
> #include <boost/test/unit_test.hpp>
>
> using boost::unit_test::test_suite;
>
> test_suite* Jacobi_test_suite();
> test_suite* GaussSeidel_test_suite();
>
> test_suite* init_unit_test_suite(int, char *[])
> {
> test_suite *test = BOOST_TEST_SUITE("Master test suite");
> BOOST_MESSAGE("solver tests");
> test->add(Jacobi_test_suite());
> test->add(GaussSeidel_test_suite());
>
> return test;
> }

All you need in this file is

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

> /******************************************************/
> // Jacobi_test.cpp
>
> #include "../Jacobi.h"
>
> #include <boost/test/unit_test.hpp>
> #include <boost/test/floating_point_comparison.hpp>
> #include <stdexcept>
>
> using boost::unit_test::test_suite;
>
> void JacobiTest1()
> {
> DenseMatrix A(2, 3);
> Vektor x(3, 1.0);
> Vektor y(2, 1.0);
> Jacobi jacobi;
> BOOST_CHECK_THROW(jacobi.solve(A, x, y), std::logic_error);
> }
>
> test_suite* Jacobi_test_suite()
> {
> test_suite *test = BOOST_TEST_SUITE("Jacobi iteration test suite");
> BOOST_MESSAGE("Jacobi tests");
> test->add(BOOST_TEST_CASE(&JacobiTest1));
>
> return test;
> }

This file may look like this:

 #include "../Jacobi.h"
 #include <boost/test/unit_test.hpp>
 #include <stdexcept>

BOOST_AUTO_TEST_SUITE( Jacobi_iteration_test_suite )

BOOST_AUTO_TEST_CASE( JacobiTest1 )
{
   DenseMatrix A(2, 3);
   Vektor x(3, 1.0);
   Vektor y(2, 1.0);
   Jacobi jacobi;
   BOOST_CHECK_THROW(jacobi.solve(A, x, y), std::logic_error);
}

BOOST_AUTO_TEST_SUITE_END()

> /******************************************************/
> // GaussSeidel_test.cpp
>
> #include "../GaussSeidel.h"
>
> #include <boost/test/unit_test.hpp>
> #include <boost/test/floating_point_comparison.hpp>
> #include <stdexcept>
>
> using boost::unit_test::test_suite;
>
> void GaussSeidelTest4()
> {
> DenseMatrix A(3, 3);
> Vektor x(3);
> Vektor y(3);
> GaussSeidel gs1("damped Gau?-Seidel (SOR)", 0.5, 1); // 1 iteration
> gs1.solve(A, x ,y);
> BOOST_CHECK_CLOSE(X[0], -108.0/240, 1E-10);
> BOOST_CHECK_CLOSE(X[1], -27.0/240, 1E-10);
> BOOST_CHECK_CLOSE(X[2], 38.0/240, 1E-10);
> }
>
> test_suite* GaussSeidel_test_suite()
> {
> test_suite *test = BOOST_TEST_SUITE("Gau?-Seidel iteration test suite");
> BOOST_MESSAGE("Gau?-Seidel tests");
> test->add(BOOST_TEST_CASE(&GaussSeidelTest4));
> return test;
> }

This file may look like this:

#include "../GaussSeidel.h"

 #include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

BOOST_AUTO_TEST_SUITE( Gau?_Seidel_iteration_test_suite )

BOOST_AUTO_TEST_CASE( GaussSeidelTest4 )
{
  DenseMatrix A(3, 3);
  Vektor x(3);
  Vektor y(3);
  GaussSeidel gs1("damped Gau?-Seidel (SOR)", 0.5, 1); // 1 iteration
  gs1.solve(A, x ,y);
  BOOST_CHECK_CLOSE(X[0], -108.0/240, 1E-10);
  BOOST_CHECK_CLOSE(X[1], -27.0/240, 1E-10);
  BOOST_CHECK_CLOSE(X[2], 38.0/240, 1E-10);
}

BOOST_AUTO_TEST_SUITE_END()

Don't you think this look a bit neater? And it works both with static and
shared library. All you need is to define BOOST_TEST_DYN_LINK in makefile
during compilation to be able to link with shared library. And this works
for both *nix and NT.

Gennadiy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net