Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Inline sort + unique for <= 2 tags

Commit
ee4b6eadb7f82b4a866324a3c29637d743880bcc
Author
Martin Hořeňovský ·
Parents
2f03f0079fc8

Files · Download .tar.gz

Changes

 src/catch2/catch_test_case_info.cpp | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/catch2/catch_test_case_info.cpp b/src/catch2/catch_test_case_info.cpp
index 925db045..4ccc3d52 100644
--- a/src/catch2/catch_test_case_info.cpp
+++ b/src/catch2/catch_test_case_info.cpp
@@ -14,6 +14,7 @@
 #include <cassert>
 #include <cctype>
 #include <algorithm>
+#include <utility>
 
 namespace Catch {
 
@@ -205,10 +206,21 @@ namespace Catch {
             internalAppendTag("."_sr);
         }
 
-        // Sort and prepare tags
-        std::sort(begin(tags), end(tags));
-        tags.erase(std::unique(begin(tags), end(tags)),
-                   end(tags));
+        // Tests usually have small number of tags, so we explicitly handle
+        // the 1 and 2 tags cases for better performance in low-opt builds.
+        switch ( tags.size() ) {
+        case 1:
+            break;
+        case 2:
+            if ( tags[0] == tags[1] ) { tags.pop_back(); }
+            else if ( tags[1] < tags[0] ) { std::swap( tags[0], tags[1] ); }
+            break;
+        default:
+            std::sort( begin( tags ), end( tags ) );
+            tags.erase( std::unique( begin( tags ), end( tags ) ),
+                        end( tags ) );
+        }
+
     }
 
     bool TestCaseInfo::isHidden() const {