structure5
Class Assert

java.lang.Object
  extended by structure5.Assert

public class Assert
extends java.lang.Object

A library of assertion testing and debugging procedures.

This class of static methods provides basic assertion testing facilities. An assertion is a condition that is expected to be true at a certain point in the code. Each of the assertion-based routines in this class perform a verification of the condition, and do nothing (aside from testing side-effects) if the condition holds. If the condition fails, however, the assertion throws an exception and prints the associated message, that describes the condition that failed. Basic support is provided for testing general conditions, and pre- and postconditions. There is also a facility for throwing a failed condition for code that should not be executed.

Features similar to assertion testing are expected to be incorporated into the Java 2 language beginning in SDK 1.4.

The debugging facilities provide control that is slightly improved over print statements.

Since:
Java Structures, 1st edition

Field Summary
protected static int debugLevel
          The current level of debugging; generally 0 upward.
 
Method Summary
static void condition(boolean test, java.lang.String message)
          Test a general condition.
static void debug(int level, java.lang.String message)
          Set up a debugging message at a specific level.
static void debug(java.lang.String message)
          Set up a level 1 debugging message.
static void debugging()
          Increase the verbosity of the debugging messages.
static int debugLevel(int level)
          Explictly set the debugging level (0 = none)
static void fail(java.lang.String message)
          Indicate certain failure.
static void invariant(boolean test, java.lang.String message)
          Test a loop invariant.
static void post(boolean test, java.lang.String message)
          Test a postcondition.
static void pre(boolean test, java.lang.String message)
          Test a precondition.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debugLevel

protected static int debugLevel
The current level of debugging; generally 0 upward. A level of 0 is generally considered off.

Method Detail

debugging

public static void debugging()
Increase the verbosity of the debugging messages.

Postcondition:
debugging level is increased

debugLevel

public static int debugLevel(int level)
Explictly set the debugging level (0 = none)

Parameters:
level - the desired level of verosity
Returns:
the old level
Precondition:
level >= 0
Postcondition:
the level of debugging is set to level

debug

public static void debug(java.lang.String message)
Set up a level 1 debugging message.

Parameters:
message - a string to be printed if debugging level is 1 or more
Postcondition:
prints message if the debugging level is 1 or more

debug

public static void debug(int level,
                         java.lang.String message)
Set up a debugging message at a specific level.

Parameters:
level - the level that triggers the printing of message
message - the message to be printed at the desired level
Precondition:
level >= 1 and message is non-null
Postcondition:
prints message if debugging level is level or more

pre

public static void pre(boolean test,
                       java.lang.String message)
Test a precondition. If the assertion fails the message indicates that a precondition failed. A precondition is something you require to be true for the method to be executed correctly.

Parameters:
test - A boolean expression describing precondition.
message - A string describing precondition.
Precondition:
Result of precondition test
Postcondition:
Does nothing if test true, otherwise abort w/message

post

public static void post(boolean test,
                        java.lang.String message)
Test a postcondition. If the assertion fails, the message indicates that a postcondition failed. A postcondition is something expected to be true after a method invocation, provided the preconditions are met.

Parameters:
test - A boolean expression describing postcondition.
message - A string describing postcondition.
Precondition:
Result of postcondition test
Postcondition:
Does nothing if test true, otherwise abort w/message

condition

public static void condition(boolean test,
                             java.lang.String message)
Test a general condition. If the assertion fails, the message indicates that a general condition failed. The condition may be anything that needs to be verified during the course of program execution.

Parameters:
test - A boolean expression describing the condition.
message - A string describing the condition.
Precondition:
result of general condition test
Postcondition:
does nothing if test true, otherwise abort w/message

invariant

public static void invariant(boolean test,
                             java.lang.String message)
Test a loop invariant. If the assertion fails, the message indicates that an invariant failed. The condition may be anything that needs to be verified during the course of program execution.

Parameters:
test - A boolean expression describing the condition.
message - A string describing the condition.
Precondition:
result of an invariant test
Postcondition:
does nothing if test true, otherwise abort w/message

fail

public static void fail(java.lang.String message)
Indicate certain failure. Stops the program with a message indicating why failure occurred.

Parameters:
message - A string describing the reason for failure.
Postcondition:
Throws error with message