📄 type_traits_test.cpp
字号:
typedef unsigned char unsigned_char; CPPUNIT_ASSERT( is_integer(unsigned_char()) == 1 );# if defined (_STLP_HAS_WCHAR_T) CPPUNIT_ASSERT( is_integer(wchar_t()) == 1 );# endif CPPUNIT_ASSERT( is_integer(short()) == 1 ); typedef unsigned short unsigned_short; CPPUNIT_ASSERT( is_integer(unsigned_short()) == 1 ); CPPUNIT_ASSERT( is_integer(int()) == 1 ); typedef unsigned int unsigned_int; CPPUNIT_ASSERT( is_integer(unsigned_int()) == 1 ); CPPUNIT_ASSERT( is_integer(long()) == 1 ); typedef unsigned long unsigned_long; CPPUNIT_ASSERT( is_integer(unsigned_long()) == 1 );# if defined (_STLP_LONG_LONG) typedef _STLP_LONG_LONG long_long; CPPUNIT_ASSERT( is_integer(long_long()) == 1 ); typedef unsigned _STLP_LONG_LONG unsigned_long_long; CPPUNIT_ASSERT( is_integer(unsigned_long_long()) == 1 );# endif CPPUNIT_ASSERT( is_integer(float()) == 0 ); CPPUNIT_ASSERT( is_integer(double()) == 0 );# if !defined ( _STLP_NO_LONG_DOUBLE ) typedef long double long_double; CPPUNIT_ASSERT( is_integer(long_double()) == 0 );# endif CPPUNIT_ASSERT( is_integer(any) == 0 ); CPPUNIT_ASSERT( is_integer(any_pointer) == 0 );#endif}#if defined (STLPORT)template <typename _Type>int is_rational(_Type) { typedef typename _IsRational<_Type>::_Ret _Ret; return type_to_value(_Ret());}#endifvoid TypeTraitsTest::rational(){#if defined (STLPORT) CPPUNIT_ASSERT( is_rational(bool()) == 0 ); CPPUNIT_ASSERT( is_rational(char()) == 0 ); typedef signed char signed_char; CPPUNIT_ASSERT( is_rational(signed_char()) == 0 ); typedef unsigned char unsigned_char; CPPUNIT_ASSERT( is_rational(unsigned_char()) == 0 );# if defined (_STLP_HAS_WCHAR_T) CPPUNIT_ASSERT( is_rational(wchar_t()) == 0 );# endif CPPUNIT_ASSERT( is_rational(short()) == 0 ); typedef unsigned short unsigned_short; CPPUNIT_ASSERT( is_rational(unsigned_short()) == 0 ); CPPUNIT_ASSERT( is_rational(int()) == 0 ); typedef unsigned int unsigned_int; CPPUNIT_ASSERT( is_rational(unsigned_int()) == 0 ); CPPUNIT_ASSERT( is_rational(long()) == 0 ); typedef unsigned long unsigned_long; CPPUNIT_ASSERT( is_rational(unsigned_long()) == 0 );# if defined (_STLP_LONG_LONG) typedef _STLP_LONG_LONG long_long; CPPUNIT_ASSERT( is_rational(long_long()) == 0 ); typedef unsigned _STLP_LONG_LONG unsigned_long_long; CPPUNIT_ASSERT( is_rational(unsigned_long_long()) == 0 );# endif CPPUNIT_ASSERT( is_rational(float()) == 1 ); CPPUNIT_ASSERT( is_rational(double()) == 1 );# if !defined ( _STLP_NO_LONG_DOUBLE ) typedef long double long_double; CPPUNIT_ASSERT( is_rational(long_double()) == 1 );# endif CPPUNIT_ASSERT( is_rational(any) == 0 ); CPPUNIT_ASSERT( is_rational(any_pointer) == 0 );#endif}#if defined (STLPORT)template <typename _Type>int is_pointer_type(_Type) { return type_to_value(_IsPtrType<_Type>::_Ret());}#endifvoid TypeTraitsTest::pointer_type(){#if defined (STLPORT) CPPUNIT_ASSERT( is_pointer_type(int_val) == 0 ); CPPUNIT_ASSERT( is_pointer_type(int_pointer) == 1 ); CPPUNIT_ASSERT( is_pointer_type(int_const_pointer) == 1 ); CPPUNIT_ASSERT( is_pointer_type(int_volatile_pointer) == 1 ); CPPUNIT_ASSERT( is_pointer_type(int_const_volatile_pointer) == 1 ); CPPUNIT_ASSERT( is_pointer_type(int_ref) == 0 ); CPPUNIT_ASSERT( is_pointer_type(int_const_ref) == 0 ); CPPUNIT_ASSERT( is_pointer_type(any) == 0 ); CPPUNIT_ASSERT( is_pointer_type(any_pointer) == 1 );#endif}void TypeTraitsTest::reference_type(){#if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) CPPUNIT_ASSERT( type_to_value(_IsRefType<int>::_Ret()) == 0 ); CPPUNIT_ASSERT( type_to_value(_IsRefType<int*>::_Ret()) == 0 ); CPPUNIT_ASSERT( type_to_value(_IsRefType<int&>::_Ret()) == 1 ); CPPUNIT_ASSERT( type_to_value(_IsRefType<int const&>::_Ret()) == 1 ); CPPUNIT_ASSERT( type_to_value(_IsRefType<int const volatile&>::_Ret()) == 1 ); CPPUNIT_ASSERT( type_to_value(_IsOKToSwap(int_pointer, int_pointer, __true_type(), __true_type())._Answer()) == 1 ); CPPUNIT_ASSERT( type_to_value(_IsOKToSwap(int_pointer, int_pointer, __false_type(), __false_type())._Answer()) == 0 );#endif}#if defined (STLPORT)template <typename _Tp1, typename _Tp2>int are_both_pointer_type (_Tp1, _Tp2) { return type_to_value(_BothPtrType<_Tp1, _Tp2>::_Answer());}#endifvoid TypeTraitsTest::both_pointer_type(){#if defined (STLPORT) CPPUNIT_CHECK( are_both_pointer_type(int_val, int_val) == 0 ); CPPUNIT_CHECK( are_both_pointer_type(int_pointer, int_pointer) == 1 ); CPPUNIT_CHECK( are_both_pointer_type(int_const_pointer, int_const_pointer) == 1 ); CPPUNIT_CHECK( are_both_pointer_type(int_volatile_pointer, int_volatile_pointer) == 1 ); CPPUNIT_CHECK( are_both_pointer_type(int_const_volatile_pointer, int_const_volatile_pointer) == 1 ); CPPUNIT_CHECK( are_both_pointer_type(int_ref, int_ref) == 0 ); CPPUNIT_CHECK( are_both_pointer_type(int_const_ref, int_const_ref) == 0 ); CPPUNIT_CHECK( are_both_pointer_type(any, any) == 0 ); CPPUNIT_CHECK( are_both_pointer_type(any_pointer, any_pointer) == 1 );#endif}#if defined (STLPORT)template <typename _Tp1, typename _Tp2>int is_ok_to_use_memcpy(_Tp1 val1, _Tp2 val2) { return type_to_value(_UseTrivialCopy(val1, val2)._Answer());}#endifvoid TypeTraitsTest::ok_to_use_memcpy(){#if defined (STLPORT) && !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) CPPUNIT_CHECK( is_ok_to_use_memcpy(int_pointer, int_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_const_pointer, int_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_pointer, int_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_const_pointer, int_const_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_const_pointer, int_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_const_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_const_volatile_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(int_pointer, any_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pointer, int_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pointer, any_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pointer, any_const_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pod_pointer, int_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pod_pointer, any_pod_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(any_pod_pointer, any_pod_const_pointer) == 0 ); vector<float> **pvf = 0; vector<int> **pvi = 0; CPPUNIT_CHECK( is_ok_to_use_memcpy(pvf, pvi) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memcpy(pvi, pvf) == 0 );#endif}#if defined (STLPORT)template <typename _Tp1, typename _Tp2>int is_ok_to_use_memmove(_Tp1 val1, _Tp2 val2) { return type_to_value(_UseTrivialUCopy(val1, val2)._Answer());}#endifvoid TypeTraitsTest::ok_to_use_memmove(){#if defined (STLPORT) && !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) CPPUNIT_CHECK( is_ok_to_use_memmove(int_pointer, int_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_const_pointer, int_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_pointer, int_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_const_pointer, int_const_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_const_pointer, int_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_const_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_const_volatile_pointer, int_const_volatile_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(int_pointer, any_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pointer, int_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pointer, any_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pointer, any_const_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pod_pointer, int_pointer) == 0 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pod_pointer, any_pod_pointer) == 1 ); CPPUNIT_CHECK( is_ok_to_use_memmove(any_pod_pointer, any_pod_const_pointer) == 0 );#endif}#if defined (STLPORT)template <typename _Tp>int has_trivial_destructor(_Tp) { typedef typename __type_traits<_Tp>::has_trivial_destructor _TrivialDestructor; return type_to_value(_TrivialDestructor());}struct DestructorMonitor{ ~DestructorMonitor() { ++nb_destructor_call; } static size_t nb_destructor_call;};size_t DestructorMonitor::nb_destructor_call = 0;# if defined (_STLP_USE_NAMESPACES)namespace std {# endif _STLP_TEMPLATE_NULL struct __type_traits<DestructorMonitor> { typedef __true_type has_trivial_default_constructor; typedef __true_type has_trivial_copy_constructor; typedef __true_type has_trivial_assignment_operator; typedef __true_type has_trivial_destructor; typedef __true_type is_POD_type; };# if defined (_STLP_USE_NAMESPACES)}# endif#endifvoid TypeTraitsTest::trivial_destructor(){#if defined (STLPORT) CPPUNIT_CHECK( has_trivial_destructor(int_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(int_const_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(int_volatile_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(int_const_volatile_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(any_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(any) == 0 ); CPPUNIT_CHECK( has_trivial_destructor(any_pointer) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(any_pod) == 1 ); CPPUNIT_CHECK( has_trivial_destructor(string()) == 0 ); //Check of the meta information impact in a container implementation { vector<DestructorMonitor> v(10); DestructorMonitor::nb_destructor_call = 0; } CPPUNIT_CHECK( DestructorMonitor::nb_destructor_call == 0 );#endif}#if defined (STLPORT)template <typename _Tp>int is_POD_type(_Tp) { typedef typename __type_traits<_Tp>::is_POD_type _IsPODType; return type_to_value(_IsPODType());}#endifvoid TypeTraitsTest::is_POD(){#if defined (STLPORT) CPPUNIT_CHECK( is_POD_type(int_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(int_const_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(int_volatile_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(int_const_volatile_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(any_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(any) == 0 ); CPPUNIT_CHECK( is_POD_type(any_pointer) == 1 ); CPPUNIT_CHECK( is_POD_type(any_pod) == 1 ); CPPUNIT_CHECK( is_POD_type(string()) == 0 );#endif}#if defined (STLPORT)template <typename _Tp>int is_stlport_class(_Tp) { typedef _IsSTLportClass<_Tp> _STLportClass;# if !defined (__BORLANDC__) typedef typename _STLportClass::_Ret _Is;# else typedef typename __bool2type<_STLportClass::_Is>::_Ret _Is;# endif return type_to_value(_Is());}#endifvoid TypeTraitsTest::stlport_class(){#if defined (STLPORT) CPPUNIT_CHECK( is_stlport_class(allocator<char>()) == 1 );# if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) CPPUNIT_CHECK( is_stlport_class(string()) == 1 );# endif CPPUNIT_CHECK( is_stlport_class(any) == 0 );#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -