Checking your program with assert statements

C

In C there is an <assert.h> header that defines an assert macro.

assert(expr); evaluates expr, and if it is false, prints an error message which will at least include the file name and line number, and stops the program.

C assertion checking is enabled by default. When you have finished testing your program, you can disable the assert statements without deleting them by adding -DNDEBUG on the command line.

Java

In Java, there is an assert statement with two forms:

assert expr;
assert expr : message;

In either case, the Boolean expression expr is evaluated. If it is not true, an AssertionError exception is thrown. The two forms do

throw newAssertionError();
throw newAssertionError(message);

respectively.

Java assertion checking is disabled by default. When you want to test your program, you must pass the -ea or -enableassertions runtime option to the java(1) command. (Not the javac command.)

C#

In C# there is a class System.Diagnostics.Debug.

Debug.Assert(expr);
Debug.Assert(expr, message1);
Debug.Assert(expr, message1, message2);

The Boolean expression expr is evaluated. If it is false, the system pops up a dialogue box showing the message(s) if any and the call stack.

This only happens in a “debug” build. In a “release” build, these statements have no effect.

The Debug class has methods you can use for tracing as well.

Python

In Python, there is an assert statement with two forms:

assert expr
assert expr, message

This is very close to Java. These forms are equivalent to

if __debug__:
   if not (expr):
      raise AssertionError

and

if __debug__:
   if not (expr):
      raise AssertionError(message)

respectively.

Python assertion checking is enabled by default. You may not assign to __debug__. Using the -O (optimise) command line option turns debugging off, amongst other things, and no code will be generated for assert statements then.

Ruby

Ruby is what you use when Python isn't slow enough.

There is a Ruby Gem called solid_assert which can be used to add Python-like assertions to Ruby programs. If I am reading the documentation correctly, it always evaluates the expression, but do note the “if” there.

Alternatively, you can just add

class AssertionError < RuntimeError
end

def assert &block
    raise AssertionError unless yield
end

to your code, after which

assert {expr}

will work.

AWK

There are no built-in assertions in AWK.

It is customary to add something like this

function err(msg) {
    print FILENAME ":" FNR ": " msg >"/dev/stderr"
    exit 1
}

to your program, after which

    if (!(expr)) err(message)

will do the trick.

Ada

In Ada,

pragma Assert(expr);
pragma Assert(expr, message);

are availlable. If you use the GNAT compiler, which is an optional part of the GCC suite, assertion checking is disabled by default and enabled by the -gnats command line option.

Perl5

The Perl idiom for assertion checking is

die unless expr;

or

expr or die;

These checks are always enabled.