Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54484 - in sandbox: boost/algorithm libs/algorithm/clamp/test
From: marshall_at_[hidden]
Date: 2009-06-28 19:04:50


Author: marshall
Date: 2009-06-28 19:04:50 EDT (Sun, 28 Jun 2009)
New Revision: 54484
URL: http://svn.boost.org/trac/boost/changeset/54484

Log:
Updated per Olaf's suggestions
Text files modified:
   sandbox/boost/algorithm/clamp.hpp | 8 +-
   sandbox/libs/algorithm/clamp/test/clamp_test.cpp | 86 +++++++++++++++++++--------------------
   2 files changed, 46 insertions(+), 48 deletions(-)

Modified: sandbox/boost/algorithm/clamp.hpp
==============================================================================
--- sandbox/boost/algorithm/clamp.hpp (original)
+++ sandbox/boost/algorithm/clamp.hpp 2009-06-28 19:04:50 EDT (Sun, 28 Jun 2009)
@@ -20,23 +20,23 @@
 
 namespace boost { namespace algorithm {
 
-/// \fn clamp ( V lo, V hi, V val )
+/// \fn clamp ( V val, V lo, V hi )
 /// \brief Returns the value "val" brought into the range [ lo, hi ]
 /// If the value is greater than "hi", return hi. If the value is
 /// less than "lo", return lo. Otherwise, return the original value.
 ///
+/// \param val The value to be clamped
 /// \param lo The low point of the range to be clamped to
 /// \param hi The high point of the range to be clamped to
-/// \param val The value to be clamped
 ///
   template<typename V>
- V clamp ( V lo, V hi, V val )
+ V clamp ( V val, V lo, V hi )
   {
 // return val >= hi ? hi : val <= lo ? lo : val;
 // Alternately,
 // return std::max ( std::min ( val, hi ), lo );
 // Rewritten to only use operator <
- return val < lo ? lo : val < hi ? val : hi;
+ return val < lo ? lo : hi < val ? hi : val;
   }
 
 }}

Modified: sandbox/libs/algorithm/clamp/test/clamp_test.cpp
==============================================================================
--- sandbox/libs/algorithm/clamp/test/clamp_test.cpp (original)
+++ sandbox/libs/algorithm/clamp/test/clamp_test.cpp 2009-06-28 19:04:50 EDT (Sun, 28 Jun 2009)
@@ -16,32 +16,32 @@
 {
 
 // Inside the range, equal to the endpoints, and outside the endpoints.
- BOOST_CHECK_EQUAL ( 3, ba::clamp ( 1, 10, 3 ));
- BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 10, 1 ));
- BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 10, 0 ));
- BOOST_CHECK_EQUAL ( 10, ba::clamp ( 1, 10, 10 ));
- BOOST_CHECK_EQUAL ( 10, ba::clamp ( 1, 10, 11 ));
+ BOOST_CHECK_EQUAL ( 3, ba::clamp ( 3, 1, 10 ));
+ BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 1, 10 ));
+ BOOST_CHECK_EQUAL ( 1, ba::clamp ( 0, 1, 10 ));
+ BOOST_CHECK_EQUAL ( 10, ba::clamp ( 10, 1, 10 ));
+ BOOST_CHECK_EQUAL ( 10, ba::clamp ( 11, 1, 10 ));
     
 // Negative numbers
- BOOST_CHECK_EQUAL ( -3, ba::clamp ( -10, -1, -3 ));
- BOOST_CHECK_EQUAL ( -1, ba::clamp ( -10, -1, -1 ));
- BOOST_CHECK_EQUAL ( -1, ba::clamp ( -10, -1, 0 ));
- BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -1, -10 ));
- BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -1, -11 ));
+ BOOST_CHECK_EQUAL ( -3, ba::clamp ( -3, -10, -1 ));
+ BOOST_CHECK_EQUAL ( -1, ba::clamp ( -1, -10, -1 ));
+ BOOST_CHECK_EQUAL ( -1, ba::clamp ( 0, -10, -1 ));
+ BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -10, -1 ));
+ BOOST_CHECK_EQUAL ( -10, ba::clamp ( -11, -10, -1 ));
 
 // Mixed positive and negative numbers
- BOOST_CHECK_EQUAL ( 5, ba::clamp ( -10, 10, 5 ));
- BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, 10, -10 ));
- BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, 10, -15 ));
- BOOST_CHECK_EQUAL ( 10, ba::clamp ( -10, 10, 10 ));
- BOOST_CHECK_EQUAL ( 10, ba::clamp ( -10, 10, 15 ));
+ BOOST_CHECK_EQUAL ( 5, ba::clamp ( 5, -10, 10 ));
+ BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -10, 10 ));
+ BOOST_CHECK_EQUAL ( -10, ba::clamp ( -15, -10, 10 ));
+ BOOST_CHECK_EQUAL ( 10, ba::clamp ( 10, -10, 10 ));
+ BOOST_CHECK_EQUAL ( 10, ba::clamp ( 15, -10, 10 ));
 
 // Unsigned
- BOOST_CHECK_EQUAL ( 5U, ba::clamp ( 1U, 10U, 5U ));
- BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 1U, 10U, 1U ));
- BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 1U, 10U, 0U ));
- BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 1U, 10U, 10U ));
- BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 1U, 10U, 15U ));
+ BOOST_CHECK_EQUAL ( 5U, ba::clamp ( 5U, 1U, 10U ));
+ BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 1U, 1U, 10U ));
+ BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 0U, 1U, 10U ));
+ BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 10U, 1U, 10U ));
+ BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 15U, 1U, 10U ));
 }
 
 
@@ -49,25 +49,25 @@
 {
 
 // Inside the range, equal to the endpoints, and outside the endpoints.
- BOOST_CHECK_EQUAL ( 3.0, ba::clamp ( 1.0, 10.0, 3.0 ));
- BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 1.0, 10.0, 1.0 ));
- BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 1.0, 10.0, 0.0 ));
- BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 1.0, 10.0, 10.0 ));
- BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 1.0, 10.0, 11.0 ));
+ BOOST_CHECK_EQUAL ( 3.0, ba::clamp ( 3.0, 1.0, 10.0 ));
+ BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 1.0, 1.0, 10.0 ));
+ BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 0.0, 1.0, 10.0 ));
+ BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 10.0, 1.0, 10.0 ));
+ BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 11.0, 1.0, 10.0 ));
     
 // Negative numbers
- BOOST_CHECK_EQUAL ( -3.f, ba::clamp ( -10.f, -1.f, -3.f ));
- BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( -10.f, -1.f, -1.f ));
- BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( -10.f, -1.f, 0.f ));
- BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -1.f, -10.f ));
- BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -1.f, -11.f ));
+ BOOST_CHECK_EQUAL ( -3.f, ba::clamp ( -3.f, -10.f, -1.f ));
+ BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( -1.f, -10.f, -1.f ));
+ BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( 0.f, -10.f, -1.f ));
+ BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -10.f, -1.f ));
+ BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -11.f, -10.f, -1.f ));
 
 // Mixed positive and negative numbers
- BOOST_CHECK_EQUAL ( 5.f, ba::clamp ( -10.f, 10.f, 5.f ));
- BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, 10.f, -10.f ));
- BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, 10.f, -15.f ));
- BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( -10.f, 10.f, 10.f ));
- BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( -10.f, 10.f, 15.f ));
+ BOOST_CHECK_EQUAL ( 5.f, ba::clamp ( 5.f, -10.f, 10.f ));
+ BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -10.f, 10.f ));
+ BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -15.f, -10.f, 10.f ));
+ BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( 10.f, -10.f, 10.f ));
+ BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( 15.f, -10.f, 10.f ));
 
 }
 
@@ -79,9 +79,7 @@
     custom & operator = ( const custom &rhs ) { v = rhs.v; return *this; }
     
     bool operator < ( const custom &rhs ) const { return v < rhs.v; }
- bool operator == ( const custom &rhs ) const { return v == rhs.v; }
- bool operator <= ( const custom &rhs ) const { return v <= rhs.v; }
- bool operator >= ( const custom &rhs ) const { return v >= rhs.v; }
+ bool operator == ( const custom &rhs ) const { return v == rhs.v; } // need this for the test
     
     std::ostream & print ( std::ostream &os ) const { return os << v; }
     
@@ -95,14 +93,14 @@
 {
 
 // Inside the range, equal to the endpoints, and outside the endpoints.
- BOOST_CHECK_EQUAL ( custom( 3), ba::clamp ( custom(1), custom(10), custom( 3)));
- BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom(1), custom(10), custom( 1)));
- BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom(1), custom(10), custom( 0)));
- BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(1), custom(10), custom(10)));
- BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(1), custom(10), custom(11)));
+ BOOST_CHECK_EQUAL ( custom( 3), ba::clamp ( custom( 3), custom(1), custom(10)));
+ BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom( 1), custom(1), custom(10)));
+ BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom( 0), custom(1), custom(10)));
+ BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(10), custom(1), custom(10)));
+ BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(11), custom(1), custom(10)));
 
 // Fail!!
-// BOOST_CHECK_EQUAL ( custom(1), ba::clamp ( custom(1), custom(10), custom(11)));
+// BOOST_CHECK_EQUAL ( custom(1), ba::clamp ( custom(11), custom(1), custom(10)));
 }
 
 int test_main( int , char* [] )


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk