inline typename wxImplicitConversionType<T1,T2>::value
wxMax(T1 a, T2 b)
{
- return (a > b) ? a : b;
+ typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
+
+ // Cast both operands to the same type before comparing them to avoid
+ // warnings about signed/unsigned comparisons from some compilers:
+ return static_cast<ResultType>(a) > static_cast<ResultType>(b) ? a : b;
}
template<typename T1, typename T2>
inline typename wxImplicitConversionType<T1,T2>::value
wxMin(T1 a, T2 b)
{
- return (a < b) ? a : b;
+ typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
+
+ return static_cast<ResultType>(a) < static_cast<ResultType>(b) ? a : b;
}
template<typename T1, typename T2, typename T3>
inline typename wxImplicitConversionType3<T1,T2,T3>::value
wxClip(T1 a, T2 b, T3 c)
{
- return (a < b) ? b : ((a > c) ? c : a);
+ typedef typename wxImplicitConversionType3<T1,T2,T3>::value ResultType;
+
+ if ( static_cast<ResultType>(a) < static_cast<ResultType>(b) )
+ return b;
+
+ if ( static_cast<ResultType>(a) > static_cast<ResultType>(c) )
+ return c;
+
+ return a;
}
// ----------------------------------------------------------------------------
CPPUNIT_TEST( IsPod );
CPPUNIT_TEST( IsMovable );
CPPUNIT_TEST( ImplicitConversion );
+ CPPUNIT_TEST( MinMax );
CPPUNIT_TEST_SUITE_END();
void IsPod();
void IsMovable();
void ImplicitConversion();
+ void MinMax();
DECLARE_NO_COPY_CLASS(MetaProgrammingTestCase)
};
void MetaProgrammingTestCase::ImplicitConversion()
{
- // wxImplicitConversionType<> is used to implement wxMax(). We test it
- // indirectly through that here.
-
- // test that wxMax(1.1,1) returns float, not long int
- float f = wxMax(1.1f, 1l);
- CPPUNIT_ASSERT_EQUAL( 1.1f, f);
-
#ifndef wxNO_RTTI
CPPUNIT_ASSERT(typeid(wxImplicitConversionType<char,int>::value) == typeid(int));
CPPUNIT_ASSERT(typeid(wxImplicitConversionType<int,unsigned>::value) == typeid(unsigned));
#endif
#endif // !wxNO_RTTI
}
+
+void MetaProgrammingTestCase::MinMax()
+{
+ // test that wxMax(1.1,1) returns float, not long int
+ float f = wxMax(1.1f, 1l);
+ CPPUNIT_ASSERT_EQUAL( 1.1f, f);
+
+ // test that comparing signed and unsigned correctly returns unsigned: this
+ // may seem counterintuitive in this case but this is consistent with the
+ // standard C conversions
+ CPPUNIT_ASSERT_EQUAL( 1, wxMin(-1, 1u) );
+
+ CPPUNIT_ASSERT_EQUAL( -1., wxClip(-1.5, -1, 1) );
+ CPPUNIT_ASSERT_EQUAL( 0, wxClip(0, -1, 1) );
+ CPPUNIT_ASSERT_EQUAL( 1, wxClip(2l, -1, 1) );
+}