Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74394 - trunk/libs/test/example
From: gennadiy.rozental_at_[hidden]
Date: 2011-09-15 23:10:48


Author: rogeeff
Date: 2011-09-15 23:10:47 EDT (Thu, 15 Sep 2011)
New Revision: 74394
URL: http://svn.boost.org/trac/boost/changeset/74394

Log:
added missing examples
Added:
   trunk/libs/test/example/est_example1.cpp (contents, props changed)
   trunk/libs/test/example/est_example2.cpp (contents, props changed)
   trunk/libs/test/example/unit_test_example_14.cpp (contents, props changed)

Added: trunk/libs/test/example/est_example1.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/test/example/est_example1.cpp 2011-09-15 23:10:47 EDT (Thu, 15 Sep 2011)
@@ -0,0 +1,56 @@
+// (C) Copyright Gennadiy Rozental 2005-2010.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/test for the library home page.
+
+// Boost.Test
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+#include <boost/test/exception_safety.hpp>
+#include <boost/test/mock_object.hpp>
+using namespace boost::itest;
+
+// Boost
+#include <boost/bind.hpp>
+
+//____________________________________________________________________________//
+
+// Here is the function we are going to use to see all execution path variants
+
+template<typename Employee,typename Ostr>
+typename Employee::string_type
+validate_and_return_salary( Employee const& e, Ostr& os )
+{
+ if( e.Title() == "CEO" || e.Salary() > 100000 )
+ os << e.First() << " " << e.Last() << " is overpaid";
+
+ return e.First() + " " + e.Last();
+}
+
+//____________________________________________________________________________//
+
+// Mock object we are going to use for this test
+
+struct EmpMock : mock_object<> {
+ typedef mock_object<> mo_type;
+ typedef mo_type string_type;
+
+ string_type const& Title() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Title ); }
+ string_type const& First() const { BOOST_ITEST_MOCK_FUNC( EmpMock::First ); }
+ string_type const& Last() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Last ); }
+
+ mo_type const& Salary() const { BOOST_ITEST_MOCK_FUNC( EmpMock::Salary ); }
+};
+
+//____________________________________________________________________________//
+
+BOOST_TEST_EXCEPTION_SAFETY( test_all_exec_path )
+{
+ validate_and_return_salary( EmpMock(), mock_object<>::prototype() );
+}
+
+//____________________________________________________________________________//
+
+// EOF

Added: trunk/libs/test/example/est_example2.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/test/example/est_example2.cpp 2011-09-15 23:10:47 EDT (Thu, 15 Sep 2011)
@@ -0,0 +1,80 @@
+// (C) Copyright Gennadiy Rozental 2005-2010.
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/test for the library home page.
+
+// Boost.Test
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+#include <boost/test/exception_safety.hpp>
+#include <boost/test/mock_object.hpp>
+using namespace boost::itest;
+
+// Boost
+#include <boost/bind.hpp>
+
+//____________________________________________________________________________//
+
+// Here is an example of simple (incorrect) stack implementation
+
+template<class T>
+class stack {
+public:
+ explicit stack( int init_capacity = 10 )
+ : m_capacity( init_capacity )
+ , m_size( 0 )
+ , m_v( BOOST_ITEST_NEW(T)[m_capacity] )
+ {
+ BOOST_ITEST_SCOPE( stack::stack );
+ }
+ ~stack()
+ {
+ delete[] m_v;
+ }
+
+ void push( T const& element )
+ {
+ BOOST_ITEST_SCOPE( stack::push );
+
+ if( m_size == m_capacity ) {
+ m_capacity *= 2;
+ T* new_buffer = BOOST_ITEST_NEW( T )[m_capacity];
+ for( unsigned i = 0; i < m_size; i++ ) {
+ new_buffer[i] = m_v[i];
+ }
+ delete [] m_v;
+ m_v = new_buffer;
+ }
+ m_v[m_size++] = element;
+ }
+ unsigned size() { return m_size; }
+
+private:
+ unsigned m_capacity;
+ unsigned m_size;
+ T* m_v;
+};
+
+//____________________________________________________________________________//
+
+BOOST_TEST_EXCEPTION_SAFETY( test_stack_push )
+{
+ stack<mock_object<> > st( 2 );
+
+ for( unsigned i = 0; i < 3; ++i ) {
+ try {
+ st.push( mock_object<>::prototype() );
+ }
+ catch( ... ) {
+ // this invariant checks that in case of failed push number of elements doesn't change
+ BOOST_CHECK_EQUAL( i, st.size() );
+ throw;
+ }
+ }
+}
+
+//____________________________________________________________________________//
+
+// EOF

Added: trunk/libs/test/example/unit_test_example_14.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/test/example/unit_test_example_14.cpp 2011-09-15 23:10:47 EDT (Thu, 15 Sep 2011)
@@ -0,0 +1,112 @@
+#define BOOST_TEST_TOOL_REPORT_WARN_FAILURE( assertion_descr ) \
+ std::cout << "Condition " << assertion_descr << " is not satisfied\n"
+
+#include <boost/test/prod_tools.hpp>
+#include <boost/exception.hpp>
+#include <boost/noncopyable.hpp>
+
+#include <iostream>
+
+void report( void (*test_func)())
+{
+ try {
+ (*test_func)();
+ } catch ( std::runtime_error const& x ) {
+ std::cout << *boost::get_error_info<boost::throw_file>(x)
+ << '('
+ << *boost::get_error_info<boost::throw_line>(x)
+ << "): Error in '" << *boost::get_error_info<boost::throw_function>(x)
+ << "': " << x.what() << std::endl;
+ }
+}
+
+//------------------------------------------------------------------//
+
+void do_check()
+{
+ int i = 62;
+ BOOST_CHECK( i == 144 );
+}
+
+//------------------------------------------------------------------//
+
+void do_check_msg()
+{
+ int i = 62;
+ BOOST_CHECK_MESSAGE( i == 144, "Expecting i == 62" );
+}
+
+//------------------------------------------------------------------//
+
+void do_check_eq()
+{
+ int i = 62;
+ BOOST_CHECK_EQUAL( i, 62 );
+ BOOST_CHECK_EQUAL( i, 144 );
+}
+
+//------------------------------------------------------------------//
+
+void do_check_le()
+{
+ int i = 62;
+ BOOST_CHECK_LE( i, 16 );
+}
+
+//------------------------------------------------------------------//
+
+void do_bitwise_eq()
+{
+ int i = 62;
+
+ BOOST_CHECK_BITWISE_EQUAL( i, 144 );
+}
+
+//------------------------------------------------------------------//
+
+int goo()
+{
+ static int i = 0;
+ return i++;
+}
+
+struct Foo : boost::noncopyable {
+ static int copy_counter;
+
+ Foo( int i_ = 0 ) : i(i_) {}
+ Foo( Foo const& ) { copy_counter++; }
+
+ int i;
+};
+
+int Foo::copy_counter = 0;
+
+bool operator==( Foo const&, Foo const& ) { return true; }
+std::ostream& operator<<( std::ostream& os, Foo const& ) { return os << "Foo"; }
+
+bool some_pred( Foo const& foo1, Foo const& foo2, Foo const& foo3, Foo const& foo4 )
+{
+ return foo1.i + foo2.i != foo3.i + foo4.i;
+}
+
+void do_check_pred()
+{
+ BOOST_CHECK_PREDICATE( some_pred, (Foo( 1 ))(Foo( 4 ))(Foo( 2 ))(Foo( 3 )) );
+ BOOST_CHECK_EQUAL( Foo::copy_counter, 0 );
+}
+
+//------------------------------------------------------------------//
+
+int main()
+{
+ report( &do_check );
+ report( &do_check_msg );
+ report( &do_check_eq );
+ report( &do_bitwise_eq );
+ report( &do_check_pred );
+ return 0;
+}
+
+//------------------------------------------------------------------//
+
+// EOF


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk