• 多种方案 测试 有无符号数包括 不适用大小于符号判断符号数


    void test_符号_取补(){

    //真的要设计这么一个函数,如何保证无符号传进来,还是无符号数,这个还得思考。

    //能够在传参数的时候,判断有无符号?

    }

    #define MAX_UNSIGNED (  (1<<31)-1) //使用这个数字,对没有效果

    //#define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+(AA)>0?1:0             //0 pass

    #define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+1+(AA)>0?1:0 //加处理

    #define UNSIGNED_OR_NOT(AA) MAX_UNSIGNED+1+(AA)>0?1:0 //加处理

    #define UNSIGNED_OR_NOT_FINAL(AA)   (AA? (MAX_UNSIGNED+1+(AA)>0?1:0 ) : 0  )//多加括号

    void test_各种测试符号数(){

    /************************************************************************/

    /*  来源c expert        

    精华总结:iA>0 && ~iA>0

    */

    /************************************************************************/

          // 3 solution to find signed or not

          int iA=0,iB=-1;

          unsigned int uiA=0,uiB=1;

        if ( iA>0 && ~iA>0   )

        {

               cout<<"signed ia"<<endl;

        }

          if ( iB>0 && ~iB>0     )

          {

               cout<<"signed ib"<<endl;

          }

          if ( uiA>0 && ~uiA>0      )

          {

               cout<<"unsigned uia"<<endl;

          }

          if ( uiB>0 && ~uiB>0 )

          {

               cout<<"unsigned uib"<<endl;

          }

        //小结:上面模板验证,是非符号数

          cout<<"----------"<<endl;

          if ( UNSIGNED_OR_NOT(iA) >0) //0+1+ max,宏设计的时候,无符号返回大于

          {

               cout<<"IA sigend"<<endl;

          }

          if (UNSIGNED_OR_NOT(uiA)>0) //

          {

               cout<<"uIA UNsigend"<<endl;

          }

          if (UNSIGNED_OR_NOT(iB) >0) // 这个就是违反的案例额-1  + 1 +max..........这是本次测试的发现,单纯使用加一个最大的不一定能成功。

          {

               cout<<"Ib sigend bug!!!"<<endl;

          }

          if (UNSIGNED_OR_NOT(uiB)>0)

          {

               cout<<"uIb UNsigend"<<endl;

          }

          cout<<"+++++++++++强化版本出现,不适用大于小于符号判断"<<endl;

          //思考UNSIGNED_OR_NOT_FINAL//  //这里就不用强制转换了???

          if (UNSIGNED_OR_NOT_FINAL(iA) >0)

          {

               cout<<"IA sigend"<<endl;

          }

          if (UNSIGNED_OR_NOT_FINAL(uiA)>0) //

          {

               cout<<"uIA UNsigend"<<endl;

          }

          if (UNSIGNED_OR_NOT_FINAL(iB) >0)

          {

               cout<<"Ib sigend"<<endl;

          }

          if (UNSIGNED_OR_NOT_FINAL(uiB)>0)

          {

               cout<<"uIb UNsigend"<<endl;

          }

          cout<<"+++++++++++"<<endl;

          /************************************************************************/

          /* /-1  + 1 +max..........这是本次测试的发现,单纯使用加一个最大的不一定能成功。     还是要用大小于符号。                                                             

          下面尝试找一种方法:对于两端的,正的+最大应该负,或者负的-正的,应该魏征。。就是说:想法设法制造负数。

          target? (target+max31+1 ? :0,1) : 0

          */

          /************************************************************************/

          cout<<"边界极限测试"<<endl;

          cout<<(1<<31)-1 <<endl;

          cout<< (unsigned int) 1+ (1<<31)-1 <<endl;

          cout<< (unsigned int) 1- (1<<31)+1 <<endl<<endl;

          cout<< (unsigned int) 0+ (1<<31)-1 <<endl;

          cout<< (unsigned int) 0- (1<<31)+1 <<endl<<endl;

          cout<< (unsigned int) (1<<32)-1 + (1<<31)-1 <<endl;//已经溢出

          cout<< (unsigned int) (1<<32)-1 - (1<<31)+1 <<endl;

          cout<< (unsigned int) (1<<31)-1 + (1<<31)-1 <<endl;//已经溢出

          cout<< (unsigned int) (1<<31)-1 - (1<<31)+1 <<endl;

          cout<<"有符号数边界极限测试"<<endl; //其实,宏也可以弄范围测试

          cout<<  1+ (1<<31)-1 <<endl;

          cout<<  1- (1<<31)+1 <<endl<<endl;

          cout<<  0+ (1<<31)-1 <<endl;

          cout<<  0- (1<<31)+1 <<endl<<endl;

          cout<<  (1<<32)-1 + (1<<31)-1 <<endl;//已经溢出

          cout<<  (1<<32)-1 - (1<<31)+1 <<endl;

          cout<<  (1<<31)-1 + (1<<31)-1 <<endl;//已经溢出。。。。。。想想最大加最大多少,-2...其实1<<31 两倍就是溢出了

          cout<<  (1<<31)-1 - (1<<31)+1 <<endl;

    /************************************************************************/

    /* 暂停。。有点大。                                                                    */

    /************************************************************************/

    }

    unsigned uib

    ----------

    uIA UNsigend

    Ib sigend bug!!!

    uIb UNsigend

    +++++++++++强化版本出现,不

    Ib sigend

    uIb UNsigend

    +++++++++++

    边界 极限测试

    2147483647

    2147483648

    2147483650

    2147483647

    2147483649

    2147483646

    2147483648

    4294967294

    0

    有符号数 边界 极限测试

    -2147483648

    -2147483646

    2147483647

    -2147483647

    2147483646

    -2147483648

    -2

    0

  • 相关阅读:
    20200721_34代码如何优化以及框架的运用和构建设计
    20200719_33appium应用及H5页面元素定位下
    20200709_29多线程与分布式
    day4_day4_python
    python_day3
    python第二天
    pytihon初学第一天
    移动平均线系列
    网页爬取
    十种经典排序算法
  • 原文地址:https://www.cnblogs.com/titer1/p/2425724.html
Copyright © 2020-2023  润新知