From 92c9ebb0d656235026b6d9ebaaf60709ea5bcef4 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 14 Jan 2015 07:27:24 -0800 Subject: [PATCH] Optimize calls to unit_test::suite::expect: This changes expect and unexpected to receive the reason text as a template argument, allowing the std::string conversion of char const* parameters to take place only if the condition evaluates to false. This cuts all calls to malloc and free on tests that pass. --- beast/unit_test/suite.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/beast/unit_test/suite.h b/beast/unit_test/suite.h index 2e70df7af..1b54a6ec6 100644 --- a/beast/unit_test/suite.h +++ b/beast/unit_test/suite.h @@ -164,9 +164,17 @@ public: logged if the condition is false. @return `true` if the test condition indicates success. */ + template + bool + expect (Condition shouldBeTrue, + String const& reason); + template bool - expect (Condition shouldBeTrue, std::string const& reason = ""); + expect (Condition shouldBeTrue) + { + return expect (shouldBeTrue, ""); + } /** Return the argument associated with the runner. */ std::string const& @@ -177,9 +185,17 @@ public: // DEPRECATED // @return `true` if the test condition indicates success (a false value) + template + bool + unexpected (Condition shouldBeFalse, + String const& reason); + template bool - unexpected (Condition shouldBeFalse, std::string const& reason = ""); + unexpected (Condition shouldBeFalse) + { + return unexpected (shouldBeFalse, ""); + } /** Record a successful test condition. */ template @@ -324,10 +340,11 @@ suite::operator() (runner& r) run (r); } -template +template inline bool -suite::expect (Condition shouldBeTrue, std::string const& reason) +suite::expect (Condition shouldBeTrue, + String const& reason) { if (shouldBeTrue) pass(); @@ -336,10 +353,11 @@ suite::expect (Condition shouldBeTrue, std::string const& reason) return shouldBeTrue; } -template +template inline bool -suite::unexpected (Condition shouldBeFalse, std::string const& reason) +suite::unexpected (Condition shouldBeFalse, + String const& reason) { if (! shouldBeFalse) pass();