复制代码
- //:Assert.java
- //Assertiontoolfordebugging
- packagecom.bruceeckel.tools.debug;
- publicclassAssert{
- privatestaticvoidperr(Stringmsg){
- System.err.println(msg);
- }
- publicfinalstaticvoidis_true(booleanexp){
- if(!exp)perr("Assertionfailed");
- }
- publicfinalstaticvoidis_false(booleanexp){
- if(exp)perr("Assertionfailed");
- }
- publicfinalstaticvoid
- is_true(booleanexp,Stringmsg){
- if(!exp)perr("Assertionfailed:"+msg);
- }
- publicfinalstaticvoid
- is_false(booleanexp,Stringmsg){
- if(exp)perr("Assertionfailed:"+msg);
- }
- }///:~
复制代码
- //:Assert.java
- //Turningofftheassertionoutput
- //soyoucanshiptheprogram.
- packagecom.bruceeckel.tools;
- publicclassAssert{
- publicfinalstaticvoidis_true(booleanexp){}
- publicfinalstaticvoidis_false(booleanexp){}
- publicfinalstaticvoid
- is_true(booleanexp,Stringmsg){}
- publicfinalstaticvoid
- is_false(booleanexp,Stringmsg){}
- }///:~
复制代码
- //:TestAssert.java
- //Demonstratingtheassertiontool
- packagec05;
- //Commentthefollowing,anduncommentthe
- //subsequentlinetochangeassertionbehavior:
- importcom.bruceeckel.tools.debug.*;
- //importcom.bruceeckel.tools.*;
- publicclassTestAssert{
- publicstaticvoidmain(String[]args){
- Assert.is_true((2+2)==5);
- Assert.is_false((1+1)==2);
- Assert.is_true((2+2)==5,"2+2==5");
- Assert.is_false((1+1)==2,"1+1!=2");
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |