Narzędzia użytkownika

Narzędzia witryny


boosttest

To jest stara wersja strony!


Opis biblioteki

Minimal testing facility

#include <boost/test/minimal.hpp>
#include <iostream>
 
int square_err( int arg )
{
	return arg*arg-1;		//blad gruby:)
}
 
int square( int arg )
{
	return arg*arg;
}
 
int cube( int arg )
{
	return arg*arg*arg;
}
 
int cube_err( int arg )
{
	return square_err( arg )*arg;
}
 
int test_main( int /*argc*/, char* /*argv*/[] )	//uwaga - zmieniona nazwa funkcji main
{
 
	int foo(3);
 
	BOOST_REQUIRE( foo == 3 );
 
	BOOST_CHECK( square( foo ) == 9 );
	BOOST_CHECK( square_err( foo ) == 9 );
 
	BOOST_CHECK( cube( foo ) == 27 );
	BOOST_CHECK( cube_err( foo ) == 27 );
 
	if( foo != 4 )
		BOOST_ERROR( "foo != 4 !!!!!" );
 
	//ponizsze 3 sprawdzenia przy bledzie spowoduja przerwanie dalszego testowania
 
	if(cube( foo ) != 4 )
		throw "cube(3) != 4";	//recznie wykryty blad, zglaszany poprzez wyjatek
					//ktory nie ma okazji byc zlapany
 
	if(cube( foo ) != 8 )
		BOOST_FAIL("cube(3) != 8");//rowniez zglasza wyjatek
 
	BOOST_REQUIRE( foo == 4 );	//blad w BOOST_REQUIRE powoduje przerwanie testow
 
	std::cout << "Ten kod nigdy nie zostanie osiagniety\n";
 
    return 0;
}

Program execution monitor

#include <boost/test/prg_exec_monitor.hpp>
 
int square_err( int arg )
{
	return arg*arg-1;		//blad gruby:)
}
 
int square( int arg )
{
	return arg*arg;
}
 
int cube( int arg )
{
	return arg*arg*arg;
}
 
int cube_err( int arg )
{
	return square_err( arg )*arg;
}
 
int cpp_main( int, char* [] )		//zmieniona nazwa !!!!!!
{
	int foo(3);
 
	if( square( foo ) != 9 )
		throw "square() error";
 
	if( square_err( foo ) != 9)
		throw "square_err() error";	//niezlapany wyjatek, spowoduje wypisanie na konsole
									//komunikatu: exception: C string: square_err() error
 
	//aby uzyskac komunikat o bledzie nalezy wyrzucic jeden z nastepujacych typow:
	//ciag znakow w stylu C (NULL-terminated)
	//instancje std::string
	//std::exception lub dowolna klase pochodna
 
	return 1;
}

Execution monitor

#include <boost/test/prg_exec_monitor.hpp>
#include <boost/test/execution_monitor.hpp>
#include <boost/test/utils/basic_cstring/io.hpp>
 
#include <iostream>
 
struct NotImplementedYetException
{};
 
struct DivisionByZeroException
{};
 
namespace
{
	class TestClass
	{
			int arg;
 
		public:
			TestClass( int a ): arg(a){}
 
			int operator() ()
			{
				if( arg > 10 )
					throw NotImplementedYetException();
 
				if( arg == 0 )
					throw DivisionByZeroException();
 
				return 10/arg;
			}
	};
 
	void translate_NotImplementedYetException( NotImplementedYetException const& ex )
	{
		std::cout<< "tej funkcji jeszcze nie zaimplementowano\n";
	}
 
	void translate_DivisionByZeroException( DivisionByZeroException const& ex )
	{
		std::cout<< "wymuszenie dzielenia przez zero\n";
	}
}		//zakonczenie lokalnej przestrzeni nazw
 
int cpp_main( int /*argc*/, char* /*argv*/[])
{
	::boost::execution_monitor monitor;
 
 
	//rejestrowanie funkcji tlumaczacych wyjatki
	monitor.register_exception_translator<NotImplementedYetException>( &translate_NotImplementedYetException );
	monitor.register_exception_translator<DivisionByZeroException>( &translate_DivisionByZeroException );
 
	try
	{
		monitor.execute( ::boost::unit_test::callback0<int>( TestClass( 20 ) ) );
		/*
		execution_monitor::execute( unit_test::callback0<int> const& F, bool catch_system_errors, int timeout )
		F - funkcja przyjmujaca zero argumentow,
		rzucany jest wyjatek boost::execution_exception przy niezlapanym wyjatku, wystapieniu bledu systemowego
		(jesli catch_system_errors jest ustawiony na true) lub po okreslonym timeoucie
		*/
	}
    catch ( boost::execution_exception const& ex )
	{
        std::cout << "Zlapano wyjatek: " << ex.what() << std::endl;
    }
 
	return 0;
}

Unit test framework

boosttest.1208303737.txt.gz · ostatnio zmienione: 2008/04/16 01:55 przez twroniak