mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-03 05:45:39 +02:00
Refactor implementation of case-insensitivity in tags
By not materializing the lower cased tags ahead of time, we save allocations at the cost of worsened performance when comparing two tags. Since there are rarely many tags, and commonly they are not compared even if present, this is almost always a win. The new implementation also improves the robustness of the code responsible for handling tags in a case-insensitive manner.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <catch2/catch_test_spec.hpp>
|
||||
#include <catch2/interfaces/catch_interfaces_testcase.hpp>
|
||||
#include <catch2/internal/catch_string_manip.hpp>
|
||||
#include <catch2/internal/catch_case_insensitive_comparisons.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
@@ -103,8 +104,13 @@ namespace Catch {
|
||||
}
|
||||
} // end unnamed namespace
|
||||
|
||||
bool operator<( Tag const& lhs, Tag const& rhs ) {
|
||||
return lhs.original < rhs.original;
|
||||
bool operator<( Tag const& lhs, Tag const& rhs ) {
|
||||
Detail::CaseInsensitiveLess cmp;
|
||||
return cmp( lhs.original, rhs.original );
|
||||
}
|
||||
bool operator==( Tag const& lhs, Tag const& rhs ) {
|
||||
Detail::CaseInsensitiveEqualTo cmp;
|
||||
return cmp( lhs.original, rhs.original );
|
||||
}
|
||||
|
||||
Detail::unique_ptr<TestCaseInfo>
|
||||
@@ -126,7 +132,6 @@ namespace Catch {
|
||||
// (including optional hidden tag and filename tag)
|
||||
auto requiredSize = originalTags.size() + sizeOfExtraTags(_lineInfo.file);
|
||||
backingTags.reserve(requiredSize);
|
||||
backingLCaseTags.reserve(requiredSize);
|
||||
|
||||
// We cannot copy the tags directly, as we need to normalize
|
||||
// some tags, so that [.foo] is copied as [.][foo].
|
||||
@@ -172,9 +177,8 @@ namespace Catch {
|
||||
}
|
||||
|
||||
// Sort and prepare tags
|
||||
toLowerInPlace(backingLCaseTags);
|
||||
std::sort(begin(tags), end(tags), [](Tag lhs, Tag rhs) { return lhs.lowerCased < rhs.lowerCased; });
|
||||
tags.erase(std::unique(begin(tags), end(tags), [](Tag lhs, Tag rhs) {return lhs.lowerCased == rhs.lowerCased; }),
|
||||
std::sort(begin(tags), end(tags));
|
||||
tags.erase(std::unique(begin(tags), end(tags)),
|
||||
end(tags));
|
||||
}
|
||||
|
||||
@@ -195,9 +199,6 @@ namespace Catch {
|
||||
std::string combined("#");
|
||||
combined += extractFilenamePart(lineInfo.file);
|
||||
internalAppendTag(combined);
|
||||
// TBD: Running this over all tags again is inefficient, but
|
||||
// simple enough. In practice, the overhead is small enough.
|
||||
toLowerInPlace(backingLCaseTags);
|
||||
}
|
||||
|
||||
std::string TestCaseInfo::tagsAsString() const {
|
||||
@@ -223,13 +224,7 @@ namespace Catch {
|
||||
backingTags += tagStr;
|
||||
const auto backingEnd = backingTags.size();
|
||||
backingTags += ']';
|
||||
backingLCaseTags += '[';
|
||||
// We append the tag to the lower-case backing storage as-is,
|
||||
// because we will perform the lower casing later, in bulk
|
||||
backingLCaseTags += tagStr;
|
||||
backingLCaseTags += ']';
|
||||
tags.emplace_back(StringRef(backingTags.c_str() + backingStart, backingEnd - backingStart),
|
||||
StringRef(backingLCaseTags.c_str() + backingStart, backingEnd - backingStart));
|
||||
tags.emplace_back(StringRef(backingTags.c_str() + backingStart, backingEnd - backingStart));
|
||||
}
|
||||
|
||||
bool operator<( TestCaseInfo const& lhs, TestCaseInfo const& rhs ) {
|
||||
|
@@ -25,13 +25,21 @@
|
||||
|
||||
namespace Catch {
|
||||
|
||||
/**
|
||||
* A **view** of a tag string that provides case insensitive comparisons
|
||||
*
|
||||
* Note that in Catch2 internals, the square brackets around tags are
|
||||
* not a part of tag's representation, so e.g. "[cool-tag]" is represented
|
||||
* as "cool-tag" internally.
|
||||
*/
|
||||
struct Tag {
|
||||
Tag(StringRef original_, StringRef lowerCased_):
|
||||
original(original_), lowerCased(lowerCased_)
|
||||
constexpr Tag(StringRef original_):
|
||||
original(original_)
|
||||
{}
|
||||
StringRef original, lowerCased;
|
||||
StringRef original;
|
||||
|
||||
friend bool operator<( Tag const& lhs, Tag const& rhs );
|
||||
friend bool operator< ( Tag const& lhs, Tag const& rhs );
|
||||
friend bool operator==( Tag const& lhs, Tag const& rhs );
|
||||
};
|
||||
|
||||
struct ITestInvoker;
|
||||
@@ -79,7 +87,7 @@ namespace Catch {
|
||||
std::string name;
|
||||
StringRef className;
|
||||
private:
|
||||
std::string backingTags, backingLCaseTags;
|
||||
std::string backingTags;
|
||||
// Internally we copy tags to the backing storage and then add
|
||||
// refs to this storage to the tags vector.
|
||||
void internalAppendTag(StringRef tagString);
|
||||
|
@@ -38,15 +38,13 @@ namespace Catch {
|
||||
|
||||
TestSpec::TagPattern::TagPattern( std::string const& tag, std::string const& filterString )
|
||||
: Pattern( filterString )
|
||||
, m_tag( toLower( tag ) )
|
||||
, m_tag( tag )
|
||||
{}
|
||||
|
||||
bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const {
|
||||
return std::find_if(begin(testCase.tags),
|
||||
end(testCase.tags),
|
||||
[&](Tag const& tag) {
|
||||
return tag.lowerCased == m_tag;
|
||||
}) != end(testCase.tags);
|
||||
return std::find( begin( testCase.tags ),
|
||||
end( testCase.tags ),
|
||||
Tag( m_tag ) ) != end( testCase.tags );
|
||||
}
|
||||
|
||||
bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const {
|
||||
|
@@ -13,6 +13,7 @@
|
||||
#include <catch2/interfaces/catch_interfaces_testcase.hpp>
|
||||
#include <catch2/interfaces/catch_interfaces_reporter_factory.hpp>
|
||||
#include <catch2/internal/catch_move_and_forward.hpp>
|
||||
#include <catch2/internal/catch_case_insensitive_comparisons.hpp>
|
||||
|
||||
#include <catch2/internal/catch_context.hpp>
|
||||
#include <catch2/catch_config.hpp>
|
||||
@@ -32,12 +33,12 @@ namespace Catch {
|
||||
auto const& testSpec = config.testSpec();
|
||||
std::vector<TestCaseHandle> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
|
||||
|
||||
std::map<StringRef, TagInfo> tagCounts;
|
||||
std::map<StringRef, TagInfo, Detail::CaseInsensitiveLess> tagCounts;
|
||||
for (auto const& testCase : matchedTestCases) {
|
||||
for (auto const& tagName : testCase.getTestCaseInfo().tags) {
|
||||
auto it = tagCounts.find(tagName.lowerCased);
|
||||
auto it = tagCounts.find(tagName.original);
|
||||
if (it == tagCounts.end())
|
||||
it = tagCounts.insert(std::make_pair(tagName.lowerCased, TagInfo())).first;
|
||||
it = tagCounts.insert(std::make_pair(tagName.original, TagInfo())).first;
|
||||
it->second.add(tagName.original);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user