From ccf7f2842a356e8901f8e6fe988da2f5a93db329 Mon Sep 17 00:00:00 2001 From: Billy Robert O'Neal III Date: Fri, 14 Oct 2016 14:06:45 -0700 Subject: [PATCH 1/3] Fix random_shuffle narrowing warnings Catch passes an RNG which accepts int to random_shuffle. Inside random_shuffle, the STL tries to call that RNG with the difference_type of the user provided iterators. For std::vector, this is ptrdiff_t, which on amd64 builds is wider than int. This triggers a narrowing warning because the 64 bit difference is being truncated to 32 bits. Note that this RNG implementation still does not produce a correctly uniformly shuffled result -- it's currently asserting that std::rand can produce 1000000 which is false -- but I don't know enough about how much repeatable shuffles are necessary here, so I'm leaving that alone for now. --- include/internal/catch_test_case_registry_impl.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index 04bd37fc..a70b019b 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -24,9 +24,9 @@ #endif namespace Catch { - + struct RandomNumberGenerator { - typedef int result_type; + typedef std::ptrdiff_t result_type; result_type operator()( result_type n ) const { return std::rand() % n; } From 79f01100e33cb28af06be67da9755e36566970dc Mon Sep 17 00:00:00 2001 From: Billy Robert O'Neal III Date: Fri, 14 Oct 2016 14:08:57 -0700 Subject: [PATCH 2/3] Fix transform narrowing warnings Catch passes ::tolower into std::transform with string iterators. ::tolower has the signature int(int), which triggers a stealth narrowing warning inside std::transform, because transform calls *_Dest = _Fn(*_First), which implicitly narrows an int to a char. For this particular application the narrowing is fine, so explicitly narrow in a lambda. --- include/internal/catch_common.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/internal/catch_common.hpp b/include/internal/catch_common.hpp index 2342ae61..87743ccb 100644 --- a/include/internal/catch_common.hpp +++ b/include/internal/catch_common.hpp @@ -22,7 +22,8 @@ namespace Catch { return s.find( infix ) != std::string::npos; } void toLowerInPlace( std::string& s ) { - std::transform( s.begin(), s.end(), s.begin(), ::tolower ); + std::transform( s.begin(), s.end(), s.begin(), + [](char c) { return static_cast(::tolower(c)); } ); } std::string toLower( std::string const& s ) { std::string lc = s; From c17ba0870a224d674f5c488f37b708b422956f99 Mon Sep 17 00:00:00 2001 From: Billy Robert O'Neal III Date: Fri, 14 Oct 2016 14:08:57 -0700 Subject: [PATCH 3/3] Fix transform without a lambda Catch apparently supports targeting C++03, so use an inline function instead. --- include/internal/catch_common.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_common.hpp b/include/internal/catch_common.hpp index 87743ccb..7ad5b1ca 100644 --- a/include/internal/catch_common.hpp +++ b/include/internal/catch_common.hpp @@ -21,9 +21,11 @@ namespace Catch { bool contains( std::string const& s, std::string const& infix ) { return s.find( infix ) != std::string::npos; } + char toLowerCh(char c) { + return static_cast( ::tolower( c ) ); + } void toLowerInPlace( std::string& s ) { - std::transform( s.begin(), s.end(), s.begin(), - [](char c) { return static_cast(::tolower(c)); } ); + std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); } std::string toLower( std::string const& s ) { std::string lc = s;