Use range-based for-loops

This commit is contained in:
Alexander Grund 2020-07-11 14:45:10 +02:00
parent 5ef17d0e33
commit 6702d68940
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
6 changed files with 51 additions and 67 deletions

View file

@ -162,9 +162,8 @@ namespace detail
~expectation()
{
for( sequences_cit it = sequences_.begin();
it != sequences_.end(); ++it )
(*it)->remove( this );
for( auto& sequence: sequences_)
sequence->remove( this );
}
void invoke( const std::shared_ptr< invocation >& i )
@ -220,14 +219,14 @@ namespace detail
bool invoke() const
{
for( sequences_cit it = sequences_.begin();
it != sequences_.end(); ++it )
if( ! (*it)->is_valid( this ) )
for( auto& sequence: sequences_)
{
if( ! sequence->is_valid( this ) )
return false;
}
bool result = invocation_->invoke();
for( sequences_cit it = sequences_.begin();
it != sequences_.end(); ++it )
(*it)->invalidate( this );
for( auto& sequence: sequences_)
sequence->invalidate( this );
return result;
}
@ -251,18 +250,13 @@ namespace detail
}
private:
typedef std::vector<
std::shared_ptr< sequence_impl >
> sequences_type;
typedef sequences_type::const_iterator sequences_cit;
std::shared_ptr< invocation > invocation_;
std::shared_ptr<
matcher_base<
void( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, T) )
>
> matcher_;
sequences_type sequences_;
std::vector< std::shared_ptr<sequence_impl> > sequences_;
const char* file_;
int line_;
};

View file

@ -52,14 +52,19 @@ namespace detail
virtual ~function_impl()
{
if( valid_ && exceptions_ >= exceptions() )
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
if( ! it->verify() )
{
for( const auto& expectation: expectations_ )
{
if( ! expectation.verify() )
{
error_type::fail( "untriggered expectation",
boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
it->file(), it->line() );
expectation.file(), expectation.line() );
}
}
}
if( context_ )
context_->remove( *this );
}
@ -67,16 +72,17 @@ namespace detail
virtual bool verify() const
{
lock _( mutex_ );
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
if( ! it->verify() )
for( const auto& expectation: expectations_ )
{
if( ! expectation.verify() )
{
valid_ = false;
error_type::fail( "verification failed",
boost::unit_test::lazy_ostream::instance()
<< lazy_context( this )
<< lazy_expectations( this ),
it->file(), it->line() );
expectation.file(), expectation.line() );
}
}
return valid_;
}
@ -228,29 +234,30 @@ namespace detail
{
lock _( mutex_ );
valid_ = false;
for( expectations_cit it = expectations_.begin();
it != expectations_.end(); ++it )
if( it->is_valid(
for( const auto& expectation: expectations_ )
{
if( expectation.is_valid(
BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ) )
{
if( ! it->invoke() )
if( ! expectation.invoke() )
{
error_type::fail( "sequence failed",
MOCK_FUNCTION_CONTEXT, it->file(), it->line() );
MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() );
return error_type::abort();
}
if( ! it->valid() )
if( ! expectation.valid() )
{
error_type::fail( "missing action",
MOCK_FUNCTION_CONTEXT, it->file(), it->line() );
MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() );
return error_type::abort();
}
valid_ = true;
error_type::call( MOCK_FUNCTION_CONTEXT, it->file(), it->line() );
if( it->functor() )
return it->functor()(
error_type::call( MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() );
if( expectation.functor() )
return expectation.functor()(
BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) );
return it->trigger();
return expectation.trigger();
}
}
error_type::fail( "unexpected call", MOCK_FUNCTION_CONTEXT );
return error_type::abort();
@ -297,18 +304,14 @@ namespace detail
{}
friend std::ostream& operator<<( std::ostream& s, const lazy_expectations& e )
{
for( expectations_cit it = e.impl_->expectations_.begin();
it != e.impl_->expectations_.end(); ++it )
s << std::endl << *it;
for( const auto& expectation: e.impl_->expectations_ )
s << std::endl << expectation;
return s;
}
const function_impl* impl_;
};
typedef std::list< expectation_type > expectations_type;
typedef typename expectations_type::const_iterator expectations_cit;
expectations_type expectations_;
std::list< expectation_type > expectations_;
context* context_;
mutable bool valid_;
const int exceptions_;

View file

@ -36,27 +36,21 @@ namespace detail
bool verify() const
{
bool valid = true;
for( verifiables_cit it = verifiables_.begin();
it != verifiables_.end(); ++it )
if( ! (*it)->verify() )
for( const auto* verifiable: verifiables_ )
if( ! verifiable->verify() )
valid = false;
return valid;
}
void reset()
{
const verifiables_t verifiables = verifiables_;
for( verifiables_cit it = verifiables.begin();
it != verifiables.end(); ++it )
if( std::find( verifiables_.begin(), verifiables_.end(), *it )
!= verifiables_.end() )
(*it)->reset();
const auto verifiables = verifiables_;
for( auto* verifiable: verifiables )
if( std::find( verifiables_.begin(), verifiables_.end(), verifiable ) != verifiables_.end() )
verifiable->reset();
}
private:
typedef std::vector< verifiable* > verifiables_t;
typedef verifiables_t::const_iterator verifiables_cit;
verifiables_t verifiables_;
std::vector< verifiable* > verifiables_;
};
}
} // mock

View file

@ -58,7 +58,7 @@ namespace detail
virtual void serialize( std::ostream& s, const verifiable& v ) const
{
lock _( mutex_ );
children_cit it = children_.find( &v );
const auto it = children_.find( &v );
if( it != children_.end() )
s << it->second;
else
@ -78,12 +78,9 @@ namespace detail
}
private:
typedef std::map< const verifiable*, child > children_t;
typedef children_t::const_iterator children_cit;
group group_;
parent parent_;
children_t children_;
std::map< const verifiable*, child > children_;
const std::shared_ptr< mutex > mutex_;
};
}

View file

@ -33,7 +33,7 @@ namespace detail
boost::unit_test::const_string name )
{
scoped_lock _( mutex_ );
children_t::iterator it = children_.lower_bound( &v );
auto it = children_.lower_bound( &v );
if( it == children_.end() ||
children_.key_comp()( &v, it->first ) )
it = children_.insert( it,
@ -67,7 +67,7 @@ namespace detail
virtual void serialize( std::ostream& s, const verifiable& v ) const
{
scoped_lock _( mutex_ );
children_cit it = children_.find( &v );
const auto it = children_.find( &v );
if( it != children_.end() )
s << it->second;
else
@ -120,11 +120,8 @@ namespace detail
child child_;
};
typedef std::map< const verifiable*, counter_child > children_t;
typedef children_t::const_iterator children_cit;
parents_t parents_;
children_t children_;
std::map< const verifiable*, counter_child > children_;
group group_;
mutable mutex mutex_;

View file

@ -48,8 +48,7 @@ namespace detail
void invalidate( const void* e )
{
lock _( mutex_ );
elements_type::iterator it =
std::find( elements_.begin(), elements_.end(), e );
const auto it = std::find( elements_.begin(), elements_.end(), e );
if( it != elements_.end() )
elements_.erase( elements_.begin(), it );
}