Converted (almost) all for-loops with iterators or indices to range-based

This commit is contained in:
Phil Nash
2017-04-25 11:06:52 +01:00
parent 073377a4e4
commit 1f3ba8a0b6
19 changed files with 136 additions and 220 deletions

View File

@@ -62,17 +62,13 @@ namespace Catch {
template<typename ContainerT>
inline void deleteAll( ContainerT& container ) {
typename ContainerT::const_iterator it = container.begin();
typename ContainerT::const_iterator itEnd = container.end();
for(; it != itEnd; ++it )
delete *it;
for( auto p : container )
delete p;
}
template<typename AssociativeContainerT>
inline void deleteAllValues( AssociativeContainerT& container ) {
typename AssociativeContainerT::const_iterator it = container.begin();
typename AssociativeContainerT::const_iterator itEnd = container.end();
for(; it != itEnd; ++it )
delete it->second;
for( auto const& kvp : container )
delete kvp.second;
}
bool startsWith( std::string const& s, std::string const& prefix );

View File

@@ -93,8 +93,8 @@ namespace Catch {
{
if( !data.testsOrTags.empty() ) {
TestSpecParser parser( ITagAliasRegistry::get() );
for( std::size_t i = 0; i < data.testsOrTags.size(); ++i )
parser.parse( data.testsOrTags[i] );
for( auto const& testOrTags : data.testsOrTags )
parser.parse( testOrTags );
m_testSpec = parser.testSpec();
}
}

View File

@@ -88,11 +88,9 @@ public:
operator T () const {
size_t overallIndex = getCurrentContext().getGeneratorIndex( m_fileInfo, m_totalSize );
typename std::vector<const IGenerator<T>*>::const_iterator it = m_composed.begin();
typename std::vector<const IGenerator<T>*>::const_iterator itEnd = m_composed.end();
for( size_t index = 0; it != itEnd; ++it )
size_t index = 0;
for( auto generator : m_composed )
{
const IGenerator<T>* generator = *it;
if( overallIndex >= index && overallIndex < index + generator->size() )
{
return generator->getValue( overallIndex-index );

View File

@@ -62,10 +62,8 @@ namespace Catch {
}
bool moveNext() {
std::vector<IGeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin();
std::vector<IGeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end();
for(; it != itEnd; ++it ) {
if( (*it)->moveNext() )
for( auto generator : m_generatorsInOrder ) {
if( generator->moveNext() )
return true;
}
return false;

View File

@@ -42,12 +42,10 @@ namespace Catch
bool LegacyReporterAdapter::assertionEnded( AssertionStats const& assertionStats ) {
if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) {
for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end();
it != itEnd;
++it ) {
if( it->type == ResultWas::Info ) {
ResultBuilder rb( it->macroName.c_str(), it->lineInfo, "", ResultDisposition::Normal );
rb << it->message;
for( auto const& messageInfo : assertionStats.infoMessages ) {
if( messageInfo.type == ResultWas::Info ) {
ResultBuilder rb( messageInfo.macroName.c_str(), messageInfo.lineInfo, "", ResultDisposition::Normal );
rb << messageInfo.message;
rb.setResultType( ResultWas::Info );
AssertionResult result = rb.build();
m_legacyReporter->Result( result );

View File

@@ -35,11 +35,8 @@ namespace Catch {
tagsAttr.setIndent( 6 );
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
it != itEnd;
++it ) {
for( auto const& testCaseInfo : matchedTestCases ) {
matchedTests++;
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
Colour::Code colour = testCaseInfo.isHidden()
? Colour::SecondaryText
: Colour::None;
@@ -63,11 +60,8 @@ namespace Catch {
testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec();
std::size_t matchedTests = 0;
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
it != itEnd;
++it ) {
for( auto const& testCaseInfo : matchedTestCases ) {
matchedTests++;
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
if( startsWith( testCaseInfo.name, '#' ) )
Catch::cout() << '"' << testCaseInfo.name << '"' << std::endl;
else
@@ -84,10 +78,8 @@ namespace Catch {
}
std::string all() const {
std::string out;
for( std::set<std::string>::const_iterator it = spellings.begin(), itEnd = spellings.end();
it != itEnd;
++it )
out += "[" + *it + "]";
for( auto const& spelling : spellings )
out += "[" + spelling + "]";
return out;
}
std::set<std::string> spellings;
@@ -106,29 +98,20 @@ namespace Catch {
std::map<std::string, TagInfo> tagCounts;
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
it != itEnd;
++it ) {
for( std::set<std::string>::const_iterator tagIt = it->getTestCaseInfo().tags.begin(),
tagItEnd = it->getTestCaseInfo().tags.end();
tagIt != tagItEnd;
++tagIt ) {
std::string tagName = *tagIt;
for( auto const& testCase : matchedTestCases ) {
for( auto const& tagName : testCase.getTestCaseInfo().tags ) {
std::string lcaseTagName = toLower( tagName );
std::map<std::string, TagInfo>::iterator countIt = tagCounts.find( lcaseTagName );
auto countIt = tagCounts.find( lcaseTagName );
if( countIt == tagCounts.end() )
countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first;
countIt->second.add( tagName );
}
}
for( std::map<std::string, TagInfo>::const_iterator countIt = tagCounts.begin(),
countItEnd = tagCounts.end();
countIt != countItEnd;
++countIt ) {
for( auto const& tagCount : tagCounts ) {
std::ostringstream oss;
oss << " " << std::setw(2) << countIt->second.count << " ";
Text wrapper( countIt->second.all(), TextAttributes()
oss << " " << std::setw(2) << tagCount.second.count << " ";
Text wrapper( tagCount.second.all(), TextAttributes()
.setInitialIndent( 0 )
.setIndent( oss.str().size() )
.setWidth( CATCH_CONFIG_CONSOLE_WIDTH-10 ) );
@@ -143,18 +126,18 @@ namespace Catch {
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
IReporterRegistry::FactoryMap::const_iterator itBegin = factories.begin(), itEnd = factories.end(), it;
std::size_t maxNameLen = 0;
for(it = itBegin; it != itEnd; ++it )
maxNameLen = (std::max)( maxNameLen, it->first.size() );
for( auto const factoryKvp : getRegistryHub().getReporterRegistry().getFactories() )
maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() );
for(it = itBegin; it != itEnd; ++it ) {
Text wrapper( it->second->getDescription(), TextAttributes()
for( auto const factoryKvp : getRegistryHub().getReporterRegistry().getFactories() ) {
Text wrapper( factoryKvp.second->getDescription(), TextAttributes()
.setInitialIndent( 0 )
.setIndent( 7+maxNameLen )
.setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) );
Catch::cout() << " "
<< it->first
<< factoryKvp.first
<< ':'
<< std::string( maxNameLen - it->first.size() + 2, ' ' )
<< std::string( maxNameLen - factoryKvp.first.size() + 2, ' ' )
<< wrapper << '\n';
}
Catch::cout() << std::endl;

View File

@@ -55,8 +55,8 @@ namespace Matchers {
template<typename ArgT>
struct MatchAllOf : MatcherBase<ArgT> {
virtual bool match( ArgT const& arg ) const CATCH_OVERRIDE {
for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
if (!m_matchers[i]->match(arg))
for( auto matcher : m_matchers ) {
if (!matcher->match(arg))
return false;
}
return true;
@@ -65,10 +65,13 @@ namespace Matchers {
std::string description;
description.reserve( 4 + m_matchers.size()*32 );
description += "( ";
for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
if( i != 0 )
bool first = true;
for( auto matcher : m_matchers ) {
if( first )
first = false;
else
description += " and ";
description += m_matchers[i]->toString();
description += matcher->toString();
}
description += " )";
return description;
@@ -85,8 +88,8 @@ namespace Matchers {
struct MatchAnyOf : MatcherBase<ArgT> {
virtual bool match( ArgT const& arg ) const CATCH_OVERRIDE {
for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
if (m_matchers[i]->match(arg))
for( auto matcher : m_matchers ) {
if (matcher->match(arg))
return true;
}
return false;
@@ -95,10 +98,13 @@ namespace Matchers {
std::string description;
description.reserve( 4 + m_matchers.size()*32 );
description += "( ";
for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
if( i != 0 )
bool first = true;
for( auto matcher : m_matchers ) {
if( first )
first = false;
else
description += " or ";
description += m_matchers[i]->toString();
description += matcher->toString();
}
description += " )";
return description;

View File

@@ -40,8 +40,8 @@ namespace Matchers {
// !TBD: see note in EqualsMatcher
if (m_comparator.size() > v.size())
return false;
for (size_t i = 0; i < m_comparator.size(); ++i)
if (std::find(v.begin(), v.end(), m_comparator[i]) == v.end())
for ( auto const& comparator : m_comparator )
if (std::find(v.begin(), v.end(), comparator) == v.end())
return false;
return true;
}

View File

@@ -60,10 +60,8 @@ namespace Catch {
bool hasUntestedSections() const {
if( m_state == Unknown )
return true;
for( SubSections::const_iterator it = m_subSections.begin();
it != m_subSections.end();
++it)
if( (*it)->hasUntestedSections() )
for( auto subSection : m_subSections )
if( subSection->hasUntestedSections() )
return true;
return false;
}
@@ -75,10 +73,8 @@ namespace Catch {
}
RunningSection* findOrAddSubSection( std::string const& name, bool& changed ) {
for( SubSections::const_iterator it = m_subSections.begin();
it != m_subSections.end();
++it)
if( (*it)->getName() == name )
for( auto subSection : m_subSections )
if( subSection->getName() == name )
return *it;
RunningSection* subSection = new RunningSection( this, name );
m_subSections.push_back( subSection );

View File

@@ -27,14 +27,12 @@ namespace Catch {
std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const {
std::string expandedTestSpec = unexpandedTestSpec;
for( std::map<std::string, TagAlias>::const_iterator it = m_registry.begin(), itEnd = m_registry.end();
it != itEnd;
++it ) {
std::size_t pos = expandedTestSpec.find( it->first );
for( auto const& registryKvp : m_registry ) {
std::size_t pos = expandedTestSpec.find( registryKvp.first );
if( pos != std::string::npos ) {
expandedTestSpec = expandedTestSpec.substr( 0, pos ) +
it->second.tag +
expandedTestSpec.substr( pos + it->first.size() );
registryKvp.second.tag +
expandedTestSpec.substr( pos + registryKvp.first.size() );
}
}
return expandedTestSpec;

View File

@@ -99,9 +99,9 @@ namespace Catch {
testCaseInfo.lcaseTags.clear();
std::ostringstream oss;
for( std::set<std::string>::const_iterator it = tags.begin(), itEnd = tags.end(); it != itEnd; ++it ) {
oss << '[' << *it << ']';
std::string lcaseTag = toLower( *it );
for( auto const& tag : tags ) {
oss << '[' << tag << ']';
std::string lcaseTag = toLower( tag );
testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) );
testCaseInfo.lcaseTags.insert( lcaseTag );
}

View File

@@ -68,17 +68,15 @@ namespace Catch {
void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ) {
std::set<TestCase> seenFunctions;
for( std::vector<TestCase>::const_iterator it = functions.begin(), itEnd = functions.end();
it != itEnd;
++it ) {
std::pair<std::set<TestCase>::const_iterator, bool> prev = seenFunctions.insert( *it );
for( auto const function : functions ) {
std::pair<std::set<TestCase>::const_iterator, bool> prev = seenFunctions.insert( function );
if( !prev.second ) {
std::ostringstream ss;
ss << Colour( Colour::Red )
<< "error: TEST_CASE( \"" << it->name << "\" ) already defined.\n"
<< "error: TEST_CASE( \"" << function.name << "\" ) already defined.\n"
<< "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << '\n'
<< "\tRedefined at " << it->getTestCaseInfo().lineInfo << std::endl;
<< "\tRedefined at " << function.getTestCaseInfo().lineInfo << std::endl;
throw std::runtime_error(ss.str());
}
@@ -88,11 +86,9 @@ namespace Catch {
std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ) {
std::vector<TestCase> filtered;
filtered.reserve( testCases.size() );
for( std::vector<TestCase>::const_iterator it = testCases.begin(), itEnd = testCases.end();
it != itEnd;
++it )
if( matchTest( *it, testSpec, config ) )
filtered.push_back( *it );
for( auto const& testCase : testCases )
if( matchTest( testCase, testSpec, config ) )
filtered.push_back( testCase );
return filtered;
}
std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ) {

View File

@@ -64,8 +64,8 @@ namespace Catch {
bool matches( TestCaseInfo const& testCase ) const {
// All patterns in a filter must match for the filter to be a match
for( std::vector<Ptr<Pattern> >::const_iterator it = m_patterns.begin(), itEnd = m_patterns.end(); it != itEnd; ++it ) {
if( !(*it)->matches( testCase ) )
for( auto const& pattern : m_patterns ) {
if( !pattern->matches( testCase ) )
return false;
}
return true;
@@ -78,8 +78,8 @@ namespace Catch {
}
bool matches( TestCaseInfo const& testCase ) const {
// A TestSpec matches if any filter matches
for( std::vector<Filter>::const_iterator it = m_filters.begin(), itEnd = m_filters.end(); it != itEnd; ++it )
if( it->matches( testCase ) )
for( auto const& filter : m_filters )
if( filter.matches( testCase ) )
return true;
return false;
}