Fix signed conversion warnings.

This commit is contained in:
Daniel James
2016-10-05 09:45:53 +01:00
parent a0dc86ecbc
commit 71d19820ac
15 changed files with 61 additions and 40 deletions

View File

@ -490,7 +490,7 @@ namespace boost { namespace unordered { namespace detail {
{ {
if(i == j) return; if(i == j) return;
std::size_t distance = std::distance(i, j); std::size_t distance = static_cast<std::size_t>(std::distance(i, j));
if(distance == 1) { if(distance == 1) {
emplace_impl( emplace_impl(
boost::unordered::detail::func::construct_value( boost::unordered::detail::func::construct_value(

View File

@ -128,7 +128,7 @@ namespace boost { namespace unordered { namespace detail {
inline std::size_t insert_size(I i, I j, typename inline std::size_t insert_size(I i, I j, typename
boost::unordered::detail::enable_if_forward<I, void*>::type = 0) boost::unordered::detail::enable_if_forward<I, void*>::type = 0)
{ {
return std::distance(i, j); return static_cast<std::size_t>(std::distance(i, j));
} }
template <class I> template <class I>

View File

@ -11,9 +11,9 @@ project unordered-test/unordered
<toolset>intel:<warnings>on <toolset>intel:<warnings>on
# Would be nice to define -Wundef, but I'm getting warnings from # Would be nice to define -Wundef, but I'm getting warnings from
# Boost.Preprocessor on trunk. # Boost.Preprocessor on trunk.
<toolset>gcc:<cxxflags>"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wconversion -Wfloat-equal -Wshadow" <toolset>gcc:<cxxflags>"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wsign-conversion -Wconversion -Wfloat-equal -Wshadow"
<toolset>darwin:<cxxflags>"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wconversion -Wfloat-equal -Wshadow" <toolset>darwin:<cxxflags>"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wsign-conversion -Wconversion -Wfloat-equal -Wshadow"
<toolset>clang:<cxxflags>"-pedantic -Wextra" <toolset>clang:<cxxflags>"-pedantic -Wstrict-aliasing -fstrict-aliasing -Wextra -Wsign-promo -Wunused-parameter -Wsign-conversion -Wconversion -Wfloat-equal -Wshadow"
; ;
#alias framework : /boost/test//boost_unit_test_framework ; #alias framework : /boost/test//boost_unit_test_framework ;

View File

@ -17,7 +17,7 @@ template <class T>
struct self_assign_base : public test::exception_base struct self_assign_base : public test::exception_base
{ {
test::random_values<T> values; test::random_values<T> values;
self_assign_base(int count = 0) : values(count) {} self_assign_base(std::size_t count = 0) : values(count) {}
typedef T data_type; typedef T data_type;
T init() const { return T(values.begin(), values.end()); } T init() const { return T(values.begin(), values.end()); }

View File

@ -17,7 +17,7 @@ template <class T>
struct self_swap_base : public test::exception_base struct self_swap_base : public test::exception_base
{ {
test::random_values<T> values; test::random_values<T> values;
self_swap_base(int count = 0) : values(count) {} self_swap_base(std::size_t count = 0) : values(count) {}
typedef T data_type; typedef T data_type;
T init() const { return T(values.begin(), values.end()); } T init() const { return T(values.begin(), values.end()); }

View File

@ -105,13 +105,13 @@ namespace test
{ {
random_values() {} random_values() {}
explicit random_values(int count, test::random_generator const& generator = explicit random_values(std::size_t count, test::random_generator const& generator =
test::default_generator) test::default_generator)
{ {
fill(count, generator); fill(count, generator);
} }
void fill(int count, test::random_generator const& generator = void fill(std::size_t count, test::random_generator const& generator =
test::default_generator) test::default_generator)
{ {
test::unordered_generator<X> gen(generator); test::unordered_generator<X> gen(generator);

View File

@ -146,14 +146,16 @@ namespace exception
UNORDERED_EPOINT("Mock hash function."); UNORDERED_EPOINT("Mock hash function.");
} }
int result;
switch(tag_) { switch(tag_) {
case 1: case 1:
return x.tag1_; result = x.tag1_;
case 2: case 2:
return x.tag2_; result = x.tag2_;
default: default:
return x.tag1_ + x.tag2_; result = x.tag1_ + x.tag2_;
} }
return static_cast<std::size_t>(result);
} }
friend bool operator==(hash const& x1, hash const& x2) { friend bool operator==(hash const& x1, hash const& x2) {

View File

@ -182,29 +182,42 @@ namespace test
explicit hash(int t = 0) : type_(t) {} explicit hash(int t = 0) : type_(t) {}
std::size_t operator()(object const& x) const { std::size_t operator()(object const& x) const {
int result;
switch(type_) { switch(type_) {
case 1: case 1:
return x.tag1_; result = x.tag1_;
case 2: case 2:
return x.tag2_; result = x.tag2_;
default: default:
return x.tag1_ + x.tag2_; result = x.tag1_ + x.tag2_;
} }
return static_cast<std::size_t>(result);
} }
std::size_t operator()(movable const& x) const { std::size_t operator()(movable const& x) const {
int result;
switch(type_) { switch(type_) {
case 1: case 1:
return x.tag1_; result = x.tag1_;
case 2: case 2:
return x.tag2_; result = x.tag2_;
default: default:
return x.tag1_ + x.tag2_; result = x.tag1_ + x.tag2_;
} }
return static_cast<std::size_t>(result);
} }
std::size_t operator()(int x) const { std::size_t operator()(int x) const {
return x; int result;
switch(type_) {
case 1:
result = x;
case 2:
result = x * 7;
default:
result = x * 256;
}
return static_cast<std::size_t>(result);
} }
friend bool operator==(hash const& x1, hash const& x2) { friend bool operator==(hash const& x1, hash const& x2) {

View File

@ -89,8 +89,9 @@ void container_test(X& r, T const&)
// size_type can represent any non-negative value type of difference_type // size_type can represent any non-negative value type of difference_type
// I'm not sure about either of these tests... // I'm not sure about either of these tests...
size_type max_diff((std::numeric_limits<difference_type>::max)()); size_type max_diff = static_cast<size_type>(
difference_type converted_diff(max_diff); (std::numeric_limits<difference_type>::max)());
difference_type converted_diff(static_cast<difference_type>(max_diff));
BOOST_TEST((std::numeric_limits<difference_type>::max)() BOOST_TEST((std::numeric_limits<difference_type>::max)()
== converted_diff); == converted_diff);

View File

@ -25,9 +25,11 @@ namespace equality_tests
return x % 1000 == y % 1000; return x % 1000 == y % 1000;
} }
int operator()(int x) const std::size_t operator()(int x) const
{ {
return alt_hash_ ? x % 250 : (x + 5) % 250; return alt_hash_ ?
static_cast<std::size_t>(x % 250) :
static_cast<std::size_t>((x + 5) % 250);
} }
}; };

View File

@ -43,19 +43,19 @@ void write_container(Container const& x)
// Make everything collide - for testing erase in a single bucket. // Make everything collide - for testing erase in a single bucket.
struct collision_hash struct collision_hash
{ {
int operator()(int) const { return 0; } std::size_t operator()(int) const { return 0; }
}; };
// For testing erase in 2 buckets. // For testing erase in 2 buckets.
struct collision2_hash struct collision2_hash
{ {
int operator()(int x) const { return x & 1; } std::size_t operator()(int x) const { return static_cast<std::size_t>(x & 1); }
}; };
// For testing erase in lots of buckets. // For testing erase in lots of buckets.
struct collision3_hash struct collision3_hash
{ {
int operator()(int x) const { return x; } std::size_t operator()(int x) const { return static_cast<std::size_t>(x); }
}; };
typedef boost::unordered_multimap<int, int, typedef boost::unordered_multimap<int, int,

View File

@ -25,9 +25,9 @@ namespace erase_tests
test::seed_t initialize_seed(85638); test::seed_t initialize_seed(85638);
int random_value(int max) { std::size_t random_value(std::size_t max) {
using namespace std; using namespace std;
return rand() % max; return static_cast<std::size_t>(rand()) % max;
} }
template <class Container> template <class Container>
@ -35,6 +35,7 @@ void erase_tests1(Container*, test::random_generator generator)
{ {
typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator; typedef BOOST_DEDUCED_TYPENAME Container::iterator iterator;
typedef BOOST_DEDUCED_TYPENAME Container::const_iterator c_iterator; typedef BOOST_DEDUCED_TYPENAME Container::const_iterator c_iterator;
typedef BOOST_DEDUCED_TYPENAME Container::difference_type difference_type;
std::cerr<<"Erase by key.\n"; std::cerr<<"Erase by key.\n";
{ {
@ -89,7 +90,7 @@ void erase_tests1(Container*, test::random_generator generator)
int iterations = 0; int iterations = 0;
while(size > 0 && !x.empty()) while(size > 0 && !x.empty())
{ {
int index = random_value((int) x.size()); std::size_t index = random_value(x.size());
c_iterator prev, pos, next; c_iterator prev, pos, next;
if(index == 0) { if(index == 0) {
prev = pos = x.begin(); prev = pos = x.begin();
@ -162,12 +163,14 @@ void erase_tests1(Container*, test::random_generator generator)
iterators.push_back(x.cend()); iterators.push_back(x.cend());
while(iterators.size() > 1) { while(iterators.size() > 1) {
int start = random_value((int) iterators.size()); std::size_t start = random_value(iterators.size());
int length = random_value((int) (iterators.size() - start)); std::size_t length = random_value(iterators.size() - start);
x.erase(iterators[start], iterators[start + length]); x.erase(iterators[start], iterators[start + length]);
iterators.erase( iterators.erase(
boost::next(iterators.begin(), start), boost::next(iterators.begin(),
boost::next(iterators.begin(), start + length)); static_cast<difference_type>(start)),
boost::next(iterators.begin(),
static_cast<difference_type>(start + length)));
BOOST_TEST(x.size() == iterators.size() - 1); BOOST_TEST(x.size() == iterators.size() - 1);
BOOST_DEDUCED_TYPENAME std::vector<c_iterator>::const_iterator BOOST_DEDUCED_TYPENAME std::vector<c_iterator>::const_iterator
@ -216,7 +219,7 @@ void erase_tests1(Container*, test::random_generator generator)
int iterations = 0; int iterations = 0;
while(size > 0 && !x.empty()) while(size > 0 && !x.empty())
{ {
int index = random_value((int) x.size()); std::size_t index = random_value(x.size());
BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next; BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
if(index == 0) { if(index == 0) {
prev = pos = x.begin(); prev = pos = x.begin();

View File

@ -24,9 +24,9 @@ namespace insert_tests {
test::seed_t initialize_seed(243432); test::seed_t initialize_seed(243432);
int random_value(int max) { std::size_t random_value(std::size_t max) {
using namespace std; using namespace std;
return rand() % max; return static_cast<std::size_t>(rand()) % max;
} }
template <class X> template <class X>
@ -319,7 +319,7 @@ void insert_tests2(X*, test::random_generator generator)
BOOST_DEDUCED_TYPENAME test::random_values<X>::iterator BOOST_DEDUCED_TYPENAME test::random_values<X>::iterator
next = it; next = it;
for (int j = random_value(20); j > 0; ++j) { for (std::size_t j = random_value(20); j > 0; ++j) {
++next; ++next;
if (next == v.end()) { break; } if (next == v.end()) { break; }
} }

View File

@ -139,7 +139,7 @@ void reserve_test1(X*, test::random_generator generator)
{ {
for (int random_mlf = 0; random_mlf < 2; ++random_mlf) for (int random_mlf = 0; random_mlf < 2; ++random_mlf)
{ {
for (int i = 1; i < 2000; i += i < 50 ? 1 : 13) for (std::size_t i = 1; i < 2000; i += i < 50 ? 1 : 13)
{ {
test::random_values<X> v(i, generator); test::random_values<X> v(i, generator);
@ -171,7 +171,7 @@ void reserve_test2(X*, test::random_generator generator)
{ {
for (int random_mlf = 0; random_mlf < 2; ++random_mlf) for (int random_mlf = 0; random_mlf < 2; ++random_mlf)
{ {
for (int i = 0; i < 2000; i += i < 50 ? 1 : 13) for (std::size_t i = 0; i < 2000; i += i < 50 ? 1 : 13)
{ {
test::random_values<X> v(i, generator); test::random_values<X> v(i, generator);

View File

@ -115,7 +115,7 @@ namespace unnecessary_copy_tests
#endif #endif
{ {
std::size_t hash_value(unnecessary_copy_tests::count_copies const& x) { std::size_t hash_value(unnecessary_copy_tests::count_copies const& x) {
return x.tag_; return static_cast<std::size_t>(x.tag_);
} }
} }