/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $Id: test_check_cast.cpp,v 1.2 2009/03/09 04:56:42 xiaowei Exp $ * **************************************************************************/ /** * @file test_check_cast.cpp * @author chenxm(chenxiaoming@baidu.com) * @date 2008/11/11 13:44:18 * @version $Revision: 1.2 $ * @brief * **/ #include #include #include "bsl/check_cast.h" #include #include #define see(x) do{ \ std::cerr<<__FILE__<<":"<<__LINE__<<": " <<(#x)<<" = "<<(x)<<" ("<(val); printf("%hd\n",a); } catch (bsl::Exception& e) { printf("%s\n",e.all()); } } void test_to_char(){ { //char assert( 'a' == bsl::check_cast('a') ); //int assert( '\0'== bsl::check_cast(0) ); assert( char(127) == bsl::check_cast(127) ); assert( char(-128)== bsl::check_cast(-128) ); ASSERT_THROW( bsl::check_cast(-129), bsl::UnderflowException ); ASSERT_THROW( bsl::check_cast(128), bsl::OverflowException ); //unsigned char assert( 'c' == bsl::check_cast((unsigned char)('c'))); assert( char(127) == bsl::check_cast((unsigned char)(127))); ASSERT_THROW( bsl::check_cast(((unsigned char)128)), bsl::OverflowException ); //double assert( char(127) == bsl::check_cast(127.0) ); assert( char(-128)== bsl::check_cast(-128.0) ); ASSERT_THROW( bsl::check_cast(-129.0), bsl::UnderflowException ); ASSERT_THROW( bsl::check_cast(128.0), bsl::OverflowException ); //cstring assert( char(0) == bsl::check_cast("") ); assert( char('c') == bsl::check_cast("cat") ); } } void test_to_short(){ } void test_to_int(){ //char assert( 0 == bsl::check_cast('\0') ); assert( -1 == bsl::check_cast(char(-1))); //unsigned char assert( 0 == bsl::check_cast((unsigned char)0)); assert( 255 == bsl::check_cast((unsigned char)255)); //int assert( INT_MIN == bsl::check_cast(INT_MIN) ); assert( INT_MAX == bsl::check_cast(INT_MAX) ); //long if ( sizeof(long) > 4 ){ assert( INT_MIN == bsl::check_cast(long(INT_MIN)) ); assert( INT_MAX == bsl::check_cast(long(INT_MAX)) ); ASSERT_THROW( bsl::check_cast(long(INT_MIN)-1), bsl::UnderflowException ); ASSERT_THROW( bsl::check_cast(long(INT_MAX)+1), bsl::OverflowException ); }else{ assert( INT_MIN == bsl::check_cast(long(INT_MIN)) ); assert( INT_MAX == bsl::check_cast(long(INT_MAX)) ); } //long long assert( INT_MIN == bsl::check_cast((long long)(INT_MIN)) ); assert( INT_MAX == bsl::check_cast((long long)(INT_MAX)) ); ASSERT_THROW( bsl::check_cast((long long)(INT_MIN)-1), bsl::UnderflowException ); ASSERT_THROW( bsl::check_cast((long long)(INT_MAX)+1), bsl::OverflowException ); //double assert( INT_MIN == bsl::check_cast(double(INT_MIN)) ); assert( INT_MAX == bsl::check_cast(double(INT_MAX)) ); ASSERT_THROW( bsl::check_cast(double(INT_MIN)-1), bsl::UnderflowException ); ASSERT_THROW( bsl::check_cast(double(INT_MAX)+1), bsl::OverflowException ); //cstring } void test_to_size_t(){ assert( ULONG_MAX == bsl::check_cast((long long)ULONG_MAX) ); assert( INT_MAX == bsl::check_cast(int(INT_MAX)) ); } void test_to_long(){ } void test_to_long_long(){ } void test_to_float(){ } void test_to_double(){ } void test_to_long_double(){ } void test_interpret(){ while(true){ char buf[1024]; while(fgets(buf, 1024, stdin)){ try{ printf("result: %lld\n", bsl::check_cast(buf) ); //bsl::check_cast(int(1)); }catch(bsl::Exception& e){ printf("error: %s\n",e.what()); } } } } int main(){ test_short_to_const_char(); test_to_char(); test_to_short(); test_to_int(); test_to_long(); test_to_long_long(); test_to_float(); test_to_double(); test_to_long_double(); printf("ok...\n"); //test_interpret(); return 0; } /* vim: set ts=4 sw=4 sts=4 tw=100 */