Fixed compare errors.

When values are equal operators: 'operator<', 'operator>', 'operator>=' and
'operator<=' produce incorrect result. Add test test_compare.cpp.
This commit is contained in:
Ruslan Baratov
2013-09-25 22:13:42 +04:00
parent f3126f0b40
commit a07f5be2e7
2 changed files with 50 additions and 4 deletions

View File

@@ -508,7 +508,7 @@ typename boost::enable_if<
bool
>::type
operator<(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs >= lhs;
return rhs > lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
@@ -516,7 +516,7 @@ typename boost::enable_if<
bool
>::type
inline operator>(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs <= lhs;
return rhs < lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
@@ -540,7 +540,7 @@ typename boost::enable_if<
bool
>::type
inline operator>=(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs < lhs;
return rhs <= lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
@@ -548,7 +548,7 @@ typename boost::enable_if<
bool
>::type
inline operator<=(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs > lhs;
return rhs >= lhs;
}
// addition

View File

@@ -0,0 +1,46 @@
// Copyright (c) 2013 Ruslan Baratov
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "../include/safe_integer.hpp"
#include <cassert>
template <class T>
void test_equal(T test_val) {
namespace num = boost::numeric;
num::safe<T> x = test_val;
num::safe<T> y = test_val;
assert(x == y);
assert(x >= y);
assert(x <= y);
assert(!(x < y));
assert(!(x > y));
}
template <class T>
void test_non_equal(T test_val) {
namespace num = boost::numeric;
num::safe<T> x(test_val);
num::safe<T> y(test_val + 1);
assert(x != y);
assert(!(x >= y));
assert(x <= y);
assert(x < y);
assert(!(x > y));
}
int main() {
test_equal<unsigned>(0);
test_equal<unsigned>(3);
test_equal<unsigned short>(14);
test_equal<int>(3);
test_equal<uint64_t>(45);
test_non_equal<unsigned>(342);
test_non_equal<signed char>(33);
test_non_equal<int32_t>(-988);
}