CloudFlare suffered a massive security issue affecting all of its customers, including Rosetta Code. All passwords not changed since February 19th 2017 have been expired, and session cookie longevity will be reduced until late March.--Michael Mol (talk) 05:15, 25 February 2017 (UTC)

Assertions

From Rosetta Code
Task
Assertions
You are encouraged to solve this task according to the task description, using any language you may know.

Assertions are a way of breaking out of code when there is an error or an unexpected input.

Some languages throw exceptions and some treat it as a break point.


Task

Show an assertion in your language by asserting that an integer variable is equal to 42.

Ada[edit]

Using pragma Assert:

pragma Assert (A = 42, "Oops!");

The behavior of pragma is controlled by pragma Assertion_Policy. Another way is to use the predefined package Ada.Assertions:

with Ada.Assertions;  use Ada.Assertions;
...
Assert (A = 42, "Oops!");

The procedure Assert propagates Assertion_Error when condition is false.

Aime[edit]

integer x;
 
x = 41;
if (x != 42) {
error("x is not 42");
}

Executing the program will produce on standard error:

aime: assert: 5: x is not 42

ALGOL 68[edit]

The "Revised Report on the Algorithmic Language - ALGOL 68" suggest that ASSERT may be made available by a particular implementation, quote: "Pragmats may ... convey to the implementation some piece of information affecting some aspect of the meaning of the program which is not defined by this Report,..."

Example given[1]:

INT a, b; read((a, b)) PR ASSERT a >= 0 & b > 0 PR;

This works with neither ELLA ALGOL 68 nor ALGOL 68G.

The standard alternative would be to implement the assertions as an exception as per the Exceptions sample code.

In ELLA ALGOL 68 the ASSERT is implemented as an operator in the environment prelude:

OP      ASSERT = (VECTOR [] CHAR assertion,BOOL valid) VOID:
IF NOT valid
THEN type line on terminal(assertion);
terminal error( 661 {invalid assertion } )
FI;

And can be "USEd" as follows:

PROGRAM assertions CONTEXT VOID
USE standard,environment
BEGIN
INT a := 43;
"Oops!" ASSERT ( a = 42 )
END
FINISH

ALGOL W[edit]

Assertions were added to the 1972 version of Algol W. If the tested condition is false, the program terminates. In the following, the write does not get executed.

begin
integer a;
a := 43;
assert a = 42;
write( "this won't appear" )
end.

Apex[edit]

Asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.

 
String myStr = 'test;
System.assert(myStr == 'something else', 'Assertion Failed Message');
 

Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt.

 
Integer i = 5;
System.assertEquals(6, i, 'Expected 6, received ' + i);
 

Asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.

 
Integer i = 5;
System.assertNotEquals(5, i, 'Expected different value than ' + i);
 

You can’t catch an assertion failure using a try/catch block even though it is logged as an exception.

AWK[edit]

AWK doesn't have a built-in assert statement. It could be simulated using a user-defined assert() function defined as below. The BEGIN section shows some examples of successful and failed "assertions".

 
BEGIN {
meaning = 6 * 7
assert(meaning == 42, "Integer mathematics failed")
assert(meaning == 42)
meaning = strtonum("42 also known as forty-two")
assert(meaning == 42, "Built-in function failed")
meaning = "42"
assert(meaning == 42, "Dynamic type conversion failed")
meaning = 6 * 9
assert(meaning == 42, "Ford Prefect's experiment failed")
print "That's all folks"
exit
}
 
# Errormsg is optional, displayed if assertion fails
function assert(cond, errormsg){
if (!cond) {
if (errormsg != "") print errormsg
exit 1
}
}
 

The above example produces the output below, and sets the program's exit code to 1 (the default is 0)

Ford Prefect's experiment failed

AutoHotkey[edit]

Exceptions[edit]

Works with: AutoHotkey_L
a := 42
Assert(a > 10)
Assert(a < 42) ; throws exception
 
Assert(bool){
If !bool
throw Exception("Expression false", -1)
}

Legacy versions[edit]

if (a != 42)
{
OutputDebug, "a != 42" ; sends output to a debugger if connected
ListVars ; lists values of local and global variables
Pause ; pauses the script, use ExitApp to exit instead
}

Axe[edit]

A=42??Returnʳ

BBC BASIC[edit]

      PROCassert(a% = 42)
END
 
DEF PROCassert(bool%)
IF NOT bool% THEN ERROR 100, "Assertion failed"
ENDPROC

Brat[edit]

squish import :assert :assertions
 
assert_equal 42 42
assert_equal 13 42 #Raises an exception

C[edit]

#include <assert.h>
 
int main(){
int a;
/* ...input or change a here */
assert(a == 42); /* aborts program when a is not 42, unless the NDEBUG macro was defined */
 
return 0;
}

To turn off assertions, simply define the NDEBUG macro before where <assert.h> is included.

There is no mechanism to add a custom "message" with your assertion, like in other languages. However, there is a "trick" to do this, by simply logical-AND-ing your condition with a string constant message, like in the following. Since a string constant is guaranteed to be non-NULL (and hence evaluated as True), and since AND-ing with True is an identity operation for a boolean, it will not alter the behavior of the assertion, but it will get captured in the debug message that is printed:

assert(a == 42 && "Error message");

This trick only works with messages written directly in the source code (i.e. cannot be a variable or be computed), however, since the assertion message is captured by the macro at compile-time.

C#[edit]

using System.Diagnostics;
 
Debug.Assert(a == 42);

C++[edit]

Translation of: C
#include <cassert> // assert.h also works
 
int main()
{
int a;
// ... input or change a here
 
assert(a == 42); // Aborts program if a is not 42, unless the NDEBUG macro was defined
// when including <cassert>, in which case it has no effect
}

Note that assert does not get a std:: prefix because it's a macro.

Clojure[edit]

 
(let [i 42]
(assert (= i 42)))
 

Common Lisp[edit]

(let ((x 42))
(assert (and (integerp x) (= 42 x)) (x)))

Component Pascal[edit]

Works with BlackBox Component Builder

 
MODULE Assertions;
VAR
x: INTEGER;
PROCEDURE DoIt*;
BEGIN
x := 41;
ASSERT(x = 42);
END DoIt;
END Assertions.
 
Assertions.DoIt
 

Output:

TRAP 0

 Assertions.DoIt   [0000001DH] 
 Kernel.Call   [00001A7CH] 
	.adr	INTEGER	1685454913
	.kind	INTEGER	0
	.n	INTEGER	0
	.p	INTEGER	0
	.par	ARRAY 256 OF INTEGER	elements
	.r	REAL	8.70603013185328E+175
	.sig	POINTER	[64760018H]
	.size	INTEGER	2287288
	.sp	INTEGER	256
	.typ	POINTER	NIL
 Meta.Item.ParamCallVal   [00002B5EH] 
	.adr	INTEGER	1685454913
	.data	ARRAY 256 OF INTEGER	elements

D[edit]

import std.exception: enforce;
 
int foo(in bool condition) pure nothrow
in {
// Assertions are used in contract programming.
assert(condition);
} out(result) {
assert(result > 0);
} body {
if (condition)
return 42;
 
// assert(false) is never stripped from the code, it generates an
// error in debug builds, and it becomes a HALT instruction in
// -release mode.
//
// It's used as a mark by the D type system. If you remove this
// line the compiles gives an error:
//
// Error: function assertions.foo no return exp;
// or assert(0); at end of function
assert(false, "This can't happen.");
}
 
void main() pure {
int x = foo(true);
 
// A regular assertion, it throws an error.
// Use -release to disable it.
// It can be used in nothrow functions.
assert(x == 42, "x is not 42");
 
// This throws an exception and it can't be disabled.
// There are some different versions of this lazy function.
enforce(x == 42, "x is not 42");
}

Dart[edit]

Dart supplies a class Expect that works similar to the Assert methods of Junit

main() {
int i=42;
int j=41;
 
Expect.equals(42,i);
Expect.equals(42,j);
}

Delphi[edit]

Assert(a = 42);

If an assertion fails, EAssertionFailed exception is raised.

The generation of assertion code can be disabled by compiler directive

{$ASSERTIONS OFF}

Here is a simple console demo app which raises and handles assertion exception:

program TestAssert;
 
{$APPTYPE CONSOLE}
 
{.$ASSERTIONS OFF} // remove '.' to disable assertions
 
uses
SysUtils;
 
var
a: Integer;
 
begin
try
Assert(a = 42);
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln;
end.

DWScript[edit]

Simple assertion, with a custom (optional) message

Assert(a = 42, 'Not 42!');

Other specialized assertions can be used in contracts, for instance this function check that the parameter (passed by reference ofr the purpose of illustration) is 42 when entering the function and when leaving the function

procedure UniversalAnswer(var a : Integer);
require
a = 42;
begin
// code here
ensure
a = 42;
end;

E[edit]

E does not have the specific feature of assertions which may be disabled by a global option. But it does have a utility to throw an exception if a condition is false:

require(a == 42)          # default message, "Required condition failed"
 
require(a == 42, "The Answer is Wrong.") # supplied message
 
require(a == 42, fn { `Off by ${a - 42}.` }) # computed only on failure

EchoLisp[edit]

 
(assert (integer? 42)) → #t ;; success returns true
 
;; error and return to top level if not true;
(assert (integer? 'quarante-deux))
error: assert : assertion failed : (#integer? 'quarante-deux)
 
;; assertion with message (optional)
(assert (integer? 'quarante-deux) "☝️ expression must evaluate to the integer 42")
💥 error: ☝️ expression must evaluate to the integer 42 : assertion failed : (#integer? 'quarante-deux)
 

ECL[edit]

 
ASSERT(a = 42,'A is not 42!',FAIL);

Eiffel[edit]

Works with: SmartEiffel
version 2.4

There are many assertion types in Eiffel, one is the following:

File called main.e:

class MAIN
creation main
feature main is
local
test: TEST;
do
create test;
 
io.read_integer;
test.assert(io.last_integer);
end
end

Another file called test.e:

class TEST
feature assert(val: INTEGER) is
require
val = 42;
do
print("Thanks for the 42!%N");
end
end

Elixir[edit]

ExUnit.start
 
defmodule AssertionTest do
use ExUnit.Case
 
def return_5, do: 5
 
test "not equal" do
assert 42 == return_5
end
end
Output:
  1) test not equal (AssertionTest)
     test.exs:8
     Assertion with == failed
     code: 42 == return_5
     lhs:  42
     rhs:  5
     stacktrace:
       test.exs:9: (test)



Finished in 0.1 seconds (0.1s on load, 0.01s on tests)
1 test, 1 failure

Randomized with seed 869000

Emacs Lisp[edit]

Assertion can be loaded from cl.el:

(require 'cl)
(let ((x 41))
(assert (= x 42) t "the answer is not right"))

Erlang[edit]

Erlang doesn't have an assert statement. However, it is single assignment, and its assignment operator won't complain if you reassign the exact same value to an existing variable but will throw an exception otherwise.

1> N = 42.
42
2> N = 43.
** exception error: no match of right hand side value 43
3> N = 42.
42
4> 44 = N.
** exception error: no match of right hand side value 42
5> 42 = N.
42

As such, the behavior of Erlang's assignment operator is extremely similar to a regular assert in other languages.

Euphoria[edit]

type fourty_two(integer i)
return i = 42
end type
 
fourty_two i
 
i = 41 -- type-check failure

Factor[edit]

Throw an exception if the value on the top of the stack is not equal to 42:

USING: kernel ;
42 assert=

FBSL[edit]

One needs to DECLARE the asserter variable at the top of script.

This implementation evaluates the expression given to the function and displays a message if it evaluates to false.

#APPTYPE CONSOLE
 
DECLARE asserter
 
FUNCTION Assert(expression)
DIM cmd AS STRING = "DIM asserter AS INTEGER = (" & expression & ")"
EXECLINE(cmd, 1)
IF asserter = 0 THEN PRINT "Assertion: ", expression, " failed"
END FUNCTION
 
Assert("1<2")
Assert("1>2")
 
PAUSE

Output

Assertion: 1>2 failed

Press any key to continue...

Forth[edit]

variable a
: assert a @ 42 <> throw ;
 
41 a ! assert

FreeBASIC[edit]

' FB 1.05.0 Win64
' requires compilation with -g switch
 
Dim a As Integer = 5
Assert(a = 6)
'The rest of the code will not be executed
Print a
Sleep
Output:
assert.bas(5): assertion failed at __FB_MAINPROC__: a =6

F#[edit]

F# provides an assert function that is only enabled when the program is compiled with DEBUG defined. When an assertion fails, a dialog box is shown with the option to enter the debugger.

let test x =
assert (x = 42)
 
test 43

GAP[edit]

# See section 7.5 of reference manual
 
# GAP has assertions levels. An assertion is tested if its level
# is less then the global level.
 
# Set global level
SetAssertionLevel(10);
 
a := 1;
Assert(20, a > 1, "a should be greater than one");
# nothing happens
 
a := 1;
Assert(4, a > 1, "a should be greater than one");
# error
 
# Show current global level
AssertionLevel();
# 10

Go[edit]

Assertions are a feature consciously omitted from Go. For cases where you want feedback during development, the following code should provide a similar purpose. While it is simply an if statement and a panic, the technique does have some properties typical of assertions. For one, the predicate of an if statement in Go is required to be of boolean type. Specifically, ints are not tacitly tested for zero, pointers are not tested for nil: the expression must be boolean, as the WP article mentions is typical of assertions. Also, it provides a good amount of information should the predicate evaluate to true. First, a value of any type can be passed to the panic, and by default is displayed, followed by a stack trace which includes the location of the panic in the source code—function name, file name, and line number.

package main
 
func main() {
x := 43
if x != 42 {
panic(42)
}
}

Output:

panic: 42

panic PC=0x2b772d1a1048
runtime.panic+0xa7 /pool/go/src/pkg/runtime/proc.c:1032
        runtime.panic(0x40e820, 0x2a)
main.main+0x48 /pool/test.go:8
        main.main()
runtime.mainstart+0xf /pool/go/src/pkg/runtime/amd64/asm.s:77
        runtime.mainstart()
runtime.goexit /pool/go/src/pkg/runtime/proc.c:148
        runtime.goexit()

Groovy[edit]

def checkTheAnswer = {
assert it == 42 : "This: " + it + " is not the answer!"
}

Test program:

println "before 42..."
checkTheAnswer(42)
println "before 'Hello Universe'..."
checkTheAnswer("Hello Universe")

Output:

before 42...
before 'Hello Universe'...
java.lang.AssertionError: This: Hello Universe is not the answer!. Expression: (it == 42). Values: it = Hello Universe
	at ConsoleScript80$_run_closure1.doCall(ConsoleScript80:2)
	at ConsoleScript80.run(ConsoleScript80:8)

Haskell[edit]

import Control.Exception
 
main = let a = someValue in
assert (a == 42) -- throws AssertionFailed when a is not 42
somethingElse -- what to return when a is 42

Icon and Unicon[edit]

...
runerr(n,( expression ,"Assertion/error - message.")) # Throw (and possibly trap) an error number n if expression succeeds.
...
stop(( expression ,"Assertion/stop - message.")) # Terminate program if expression succeeds.
...

There are no 'assertions', which can be turned on/off by the compiler. We can emulate them by prefixing a stop statement with a check on a global variable:

 
$define DEBUG 1 # this allows the assertions to go through
 
procedure check (a)
if DEBUG then stop (42 = a, " is invalid value for 'a'")
write (a)
end
 
procedure main ()
check (10)
check (42)
check (12)
end
 

This produces the output:

10
42 is invalid value for 'a'

Changing the define to: $define DEBUG &fail turns off the assertion checking.

J[edit]

   assert n = 42

Java[edit]

public static void main(String[] args){
int a;
//...input or change a here
assert a == 42;//throws an AssertionError when a is not 42
assert a == 42 : "Error message"; //throws an AssertionError
//when a is not 42 with "Error message" for the message
//the error message can be any non-void expression
}

Note: assertion checking is disabled by default when you run your program with the java command. You must provide the -ea (short for -enableassertions) flag in order to enable them.

Julia[edit]

#assert() function takes expression as 1st argument, failed-assertion message as optional 2nd argument
julia> assert(x==42,"x is not 42")
ERROR: assertion failed: x is not 42
[email protected] macro checks the supplied conditional expression, with the expression returned in the failed-assertion message
julia> @assert x==42
ERROR: assertion failed: :((x==42))
#Julia also has type assertions of the form, x::Type which can be appended to a variable for type-checking at any point
julia> x::String
ERROR: type: typeassert: expected String, got Int32

Kotlin[edit]

Assertions need to be enabled using java's -ea option for an AssertionError to be thrown when the condition is false.

// version 1.0.6 (assert.kt)
 
fun main(args: Array<String>) {
val a = 42
assert(a == 43)
}
Output:
Exception in thread "main" java.lang.AssertionError: Assertion failed
        at AssertKt.main(assert.kt:5)

Lasso[edit]

local(a) = 8
fail_if(
#a != 42,
error_code_runtimeAssertion,
error_msg_runtimeAssertion + ": #a is not 42"
)
Output:
-9945 Runtime assertion: #a is not 42

Liberty BASIC[edit]

Liberty BASIC has no exceptions or user-defined error messages, but we could break program if condition is not met. We can even make it spell "AssertionFailed". In a way.

 
a=42
call assert a=42
print "passed"
 
a=41
call assert a=42
print "failed (we never get here)"
end
 
sub assert cond
if cond=0 then 'simulate error, mentioning "AssertionFailed"
AssertionFailed(-1)=0
end if
end sub
 
Output:
passed

Stops with error message

Output:
RuntimeError: Subscript out of range: -1, AssertionFailed() 

Lingo[edit]

Lingo has no assert statement, but the abort command (that exits the full call stack) allows to implement something like it as global function:

-- in a movie script
on assert (ok, message)
if not ok then
if not voidP(message) then _player.alert(message)
abort -- exits from current call stack, i.e. also from the caller function
end if
end
 
-- anywhere in the code
on test
x = 42
assert(x=42, "Assertion 'x=42' failed")
put "this shows up"
x = 23
assert(x=42, "Assertion 'x=42' failed")
put "this will never show up"
end

Lisaac[edit]

? { n = 42 };

Lua[edit]

a = 5
assert (a == 42)
assert (a == 42,'\''..a..'\' is not the answer to life, the universe, and everything')

Maple[edit]

(Taken from Lua, above.)

a := 5:
ASSERT( a = 42 );
ASSERT( a = 42, "a is not the answer to life, the universe, and everything" );

Mathematica / Wolfram Language[edit]

Assert[var===42]

MATLAB / Octave[edit]

assert(x == 42,'x = %d, not 42.',x);

Sample Output:

x = 3;
assert(x == 42,'Assertion Failed: x = %d, not 42.',x);
??? Assertion Failed: x = 3, not 42.
 

Metafont[edit]

Metafont has no really an assert built in, but it can easily created:

def assert(expr t) = if not (t): errmessage("assertion failed") fi enddef;

This assert macro uses the errmessage built in to show the "error". The errmessage gives the error message and asks the user what to do.

Usage example:

n := 41;
assert(n=42);
message "ok";

Output (failed assertion):

This is METAFONT, Version 2.71828 (Web2C 7.5.5)
(./assert.mf
! assertion failed.
<to be read again> 
                   ;
l.4 assert(n=42);
                 
?

Modula-3[edit]

ASSERT is a pragma, that creates a run-time error if it returns FALSE.

<*ASSERT a = 42*>

Assertions can be ignored in the compiler by using the -a switch.

Nemerle[edit]

A basic assertion uses the assert keyword:

assert (foo == 42, $"foo == $foo, not 42.")

Assertion violations throw an AssertionException with the line number where the assertion failed and the message provided as the second parameter to assert.

Nemerle also provides macros in the Nemerle.Assertions namespace to support preconditions, postconditions and class invariants:

using Nemerle.Assertions;
 
class SampleClass
{
public SomeMethod (input : list[int]) : int
requires input.Length > 0 // requires keyword indicates precondition,
// there can be more than one condition per method
{ ... }
 
public AnotherMethod (input : string) : list[char]
ensures value.Length > 0 // ensures keyword indicates postcondition
{ ... } // value is a special symbol that indicates the method's return value
}

The design by contract macros throw Nemerle.AssertionException's unless another Exception is specified using the otherwise keyword after the requires/ensures statement. For further details on design by contract macros, see here.

Nim[edit]

var a = 42
assert(a == 42)

Assertions may be disabled by compiling with --assertions:off.

Oberon-2[edit]

Oxford Oberon-2

 
MODULE Assertions;
VAR
a: INTEGER;
BEGIN
a := 40;
ASSERT(a = 42);
END Assertions.
 

Output:

Runtime error: assertion failed (0) on line 6 in module Assertions
In procedure Assertions.%main
   called from MAIN

Objective-C[edit]

For use within an Objective-C method:

NSAssert(a == 42, @"Error message");

If you want to use formatting arguments, you need to use the assertion macro corresponding to your number of formatting arguments:

NSAssert1(a == 42, @"a is not 42, a is actually %d", a); # has 1 formatting arg, so use NSAssert"1"

Within a regular C function you should use NSCAssert or NSCAssertN instead.

To turn off assertions, define the NS_BLOCK_ASSERTIONS macro.

OCaml[edit]

let a = get_some_value () in
assert (a = 42); (* throws Assert_failure when a is not 42 *)
(* evaluate stuff to return here when a is 42 *)

It is possible to compile with the parameter -noassert then the compiler won't compile the assertion checks.

Oforth[edit]

In Oforth, assertions are handled as tests.

Assertions are checked only if oforth is launched using --a command line. Default value is to not check assertions.

If an assertion is ko (and if oforth is launched using --a), an exception is raised.

: testInteger(n, m)
assert: [ n isInteger ]
assert: [ n 42 == ]
 
System.Out "Assertions are ok, parameters are : " << n << ", " << m << cr ;
Output:
testInteger(41, 43)
[1:interpreter] ExRuntime : Assertion failed into <#testInteger>

testInteger(42, 43)
Assertions are ok, parameters are : 42, 43

Oz[edit]

Oz does not have an assert statement. But if different values are assigned to the same dataflow variable, an exception will be thrown (similar to Erlang).

declare
proc {PrintNumber N}
N=42 %% assert
{Show N}
end
in
{PrintNumber 42} %% ok
{PrintNumber 11} %% throws

Output:

%***************************** failure **************************
%**
%** Tell: 11 = 42
%**
%** Call Stack:
%** procedure 'PrintNumber' in file "Oz<8>", line 3, column 0, PC = 18600220
%**--------------------------------------------------------------

PARI/GP[edit]

PARI can use any of the usual C methods for making assertions. GP has no built-in assertions.

Translation of: C
#include <assert.h>
#include <pari/pari.h>
 
void
test()
{
GEN a;
// ... input or change a here
 
assert(equalis(a, 42)); /* Aborts program if a is not 42, unless the NDEBUG macro was defined */
}

More common is the use of pari_err_BUG in such cases:

if (!equalis(a, 42)) pari_err_BUG("this_function_name (expected a = 42)");

Pascal[edit]

See Delphi

Perl[edit]

While not exactly an assertion, a common Perl idiom is to use or die to throw an exception when a certain statement is false.

print "Give me a number: ";
chomp(my $a = <>);
 
$a == 42 or die "Error message\n";
 
# Alternatives
die "Error message\n" unless $a == 42;
die "Error message\n" if not $a == 42;
die "Error message\n" if $a != 42;

This idiom is typically used during file operations:

open my $fh, '<', 'file'
or die "Cannot open file: $!\n"; # $! contains the error message from the last error

It is not needed whith the "autodie" pragma:

use autodie;
open my $fh, '<', 'file'; # automatically throws an exception on failure

Some third-party modules provide other ways of using assertions in Perl:

use Carp::Assert;
assert($a == 42);

There is also a number of ways to test assertions in test suites, for example:

is $a, 42;
ok $a == 42;
cmp_ok $a, '==', 42, 'The answer should be 42';
# etc.

Perl 6[edit]

my $a = (1..100).pick;
$a == 42 or die '$a ain\'t 42';
Works with: pugs

Note: This example uses an experimental feature, and does not work in the primary Perl 6 compiler, Rakudo.

# with a (non-hygienic) macro
macro assert ($x) { "$x or die 'assertion failed: $x'" }
assert('$a == 42');

Phix[edit]

User defined types allow the value to be automatically tested whenever it changes, and can be disabled using the "without type_check" directive:

type int42(object i)
return i=42
end type
 
int42 i
 
i = 41 -- type-check failure

When a type check occurs, program execution halts and if the program was run from the editor, it automatically jumps to the offending source file and line.

Note that, under "without type_check", the run-time reserves the right to continue to perform limited type checking, for example were the type declared as int42(integer i) then ensuring that i is an integer may allow subsequent optimisations to be applied, and therefore, despite the compiler directive, integer() could still be enforced even though "=42" would not.

You can also use constants to reduce code output on release versions:

global constant DEBUG = 0  -- (or any other identifier name can be used)
global procedure assert(integer flag, string msg)
if DEBUG then
if not flag then
{} = message_box(msg,"failed assertion",MB_OK) -- or
puts(1,msg) -- , and/or
crash(msg) -- crash/ex.err report -- or
trace(1) -- start debugging
end if
end if
end function
 
assert(i=42,"i is not 42!!")

Note that while the body of assert() and the call to it are suppressed, the calculation of the expression (i=42) may still generate code; sometimes further improvements to the compiler may be possible, sometimes the asserts may need "if DEBUG" around them. Also note that, as things stand, the constants 42 and "i is not 42!!" will be created in the executable file whatever DEBUG is set to, though again there is nothing to prevent the compiler from being enhanced to avoid emitting such unnecessary values, one day.

Lastly, I find the following trivial idioms to be spectacularly effective in Phix, the first line terminates with a divide by zero, whereas the second produces a slightly more user-friendly, and therefore potentially less developer-friendly message:

if i!=42 then ?9/0 end if
if i!=42 then crash("i is not 42!!") end if

Again, if the application was run from Edita, on error it automatically jumps to the offending file and line.

PHP[edit]

<?php
$a = 5
#...input or change $a here
assert($a == 42) # when $a is not 42, take appropriate actions,
# which is set by assert_options()
?>

PicoLisp[edit]

The 'assert' function, in combination with the tilde read macro, generates code only in debug mode:

...
~(assert (= N 42)) # Exists only in debug mode
...

Other possibilities are either to break into an error handler:

(let N 41
(unless (= N 42) (quit "Incorrect N" N)) ) # 'quit' throws an error
41 -- Incorrect N
?

or to stop at a debug break point, allowing to continue with the program:

(let N 41
(unless (= N 42) (! setq N 42)) ) # '!' is a breakpoint
(setq N 42) # Manually fix the value
! # Hit ENTER to leave the breakpoint
-> 42

PL/I[edit]

 
/* PL/I does not have an assert function as such, */
/* but it is something that can be implemented in */
/* any of several ways. A straight-forward way */
/* raises a user-defined interrupt. */
 
on condition (assert_failure) snap
put skip list ('Assert failure');
....
if a ^= b then signal condition(assert_failure);
 
/* Another way is to use the preprocessor, thus: */
%assert: procedure (a, b) returns (character);
return ('if ' || a || '^=' || b ||
' then signal condition(assert_failure);');
%end assert;
%activate assert;
 
assert(a, 42);
 

Prolog[edit]

Works with: SWI Prolog
 
test(A):-
assertion(A==42).
 

PureBasic[edit]

PureBasic does not have a native function for assertion, but allows for the definition of one.

The Macro below will only be included in the code if is compiled in debug mode, if so it will test the condition and if it fails it will inform with the message defined by the programmer, the line where it happened and in which source code file.

Macro Assert(TEST,MSG="Assert: ")
CompilerIf #PB_Compiler_Debugger
If Not (TEST)
Debug MSG+" Line="+Str(#PB_Compiler_Line)+" in "+#PB_Compiler_File
CallDebugger
EndIf
CompilerEndIf
EndMacro

A implementation as defined above could be;

A=42
Assert(A=42,"Assert that A=42")
A=42-1
Assert(A=42)

Where the second test would fail resulting in a message to the programmer with cause (if given by programmer), code line & file.

Python[edit]

a = 5
#...input or change a here
assert a == 42 # throws an AssertionError when a is not 42
assert a == 42, "Error message" # throws an AssertionError
# when a is not 42 with "Error message" for the message
# the error message can be any expression

It is possible to turn off assertions by running Python with the -O (optimizations) flag.

R[edit]

stopifnot(a==42)

Racket[edit]

Racket has higher-order assertions known as contracts that can protect any values including functions and objects. Contracts are typically applied on the imports or exports of a module.

#lang racket
 
(define/contract x
(=/c 42) ; make sure x = 42
42)
 
(define/contract f
(-> number? (or/c 'yes 'no)) ; function contract
(lambda (x)
(if (= 42 x) 'yes 'no)))
 
(f 42)  ; succeeds
(f "foo") ; contract error!
 

If typical assertion checking (i.e. error unless some boolean condition holds) is needed, that is also possible:

#lang racket
 
(define x 80)
(unless (= x 42)
(error "a is not 42")) ; will error
 

REXX[edit]

/* REXX ***************************************************************
* There's no assert feature in Rexx. That's how I'd implement it
* 10.08.2012 Walter Pachl
**********************************************************************/

x.=42
x.2=11
Do i=1 By 1
Call assert x.i,42
End
Exit
assert:
Parse Arg assert_have,assert_should_have
If assert_have\==assert_should_have Then Do
Say 'Assertion fails in line' sigl
Say 'expected:' assert_should_have
Say ' found:' assert_have
Say sourceline(sigl)
Say 'Look around'
Trace ?R
Nop
Signal Syntax
End
Return
Syntax: Say 'program terminated'

Output:

Assertion fails in line 8
expected: 42
   found: 11
  Call assert x.i,42
Look around
    Here I enter Say i
2
    and then I press just enter
program terminated    

RLaB[edit]

RLaB does not have a special function to deal with assertions. The following workaround will do the trick:

 
// test if 'a' is 42, and if not stop the execution of the code and print
// some error message
if (a != 42)
{
stop("a is not 42 as expected, therefore I stop until this issue is resolved!");
}
 

Ring[edit]

 
x = 42
assert( x = 42 )
assert( x = 100 )
 

Ruby[edit]

This uses test/unit from the standard library.

require "test/unit/assertions"
include Test::Unit::Assertions
 
n = 5
begin
assert_equal(42, n)
rescue Exception => e
# Ruby 1.8: e is a Test::Unit::AssertionFailedError
# Ruby 1.9: e is a MiniTest::Assertion
puts e
end
Output:
<42> expected but was
<5>.

Rust[edit]

 
let x = 42;
assert!(x == 42);
assert_eq!(x, 42);
 

Sather[edit]

class MAIN is
main is
i ::= 41;
assert i = 42; -- fatal
-- ...
end;
end;

(The current GNU Sather compiler v1.2.3 I am using to test the code seems to ignore the assertion and no fatal error is raised, despite Sather should, see e.g. here).

Scala[edit]

These two are the same thing, and are tagged @elidable(ASSERTION):

assert(a == 42)
assert(a == 42, "a isn't equal to 42")
assume(a == 42)
assume(a == 42, "a isn't equal to 42")

The next one does the same thing as above, but it is not tagged. Often used as a pre-condition checker on class constructors.

require(a == 42)
require(a == 42, "a isn't equal to 42")

This one checks a value and returns it for further use (here shown being printed). It uses assert, which, as explained, gets tagged.

println(a.ensuring(a == 42))
println(a.ensuring(a == 42, "a isn't equal to 42"))
println(a.ensuring(_ == 42))
println(a.ensuring(_ == 42, "a isn't equal to 42"))

Scheme[edit]

Works with: Scheme version RRS
Translation of: Common Lisp
(let ((x 42))
(assert (and (integer? x) (= x 42))))

SETL[edit]

assert( n = 42 );

Sidef[edit]

var num = pick(0..100);
assert_eq(num, 42); # dies when "num" is not 42
Output:
assert_eq: 26 == 42 is false at assertions.sf line 2.

Slate[edit]

load: 'src/lib/assert.slate'.
define: #n -> 7.
assert: n = 42 &description: 'That is not the Answer.'.

raises an AssertionFailed condition (an Error).

Smalltalk[edit]

foo := 41.
...
self assert: (foo == 42).

In TestCase and subclasses, a number of check methods are inherited; among them:

self assert: (... somethingMustEvaluateToTrue.. )
self should:[ some code ] raise: someException "ensures that an exception is raised
Works with: Smalltalk/X

Object also implements assert:; these are evaluated dynamically, but can be disabled via a flag setting. Also the compiler can be instructed to ignore them for production code (which is not normally done; disabled instead by default):

self assert: (... somethingMustEvaluateToTrue.. ) "implemented in Object"

the implementation in Object raises an AssertionFailedError exception, which usually opens a debugger when in the IDE, but can be caught in deployed apps.

SPARK[edit]

Works with SPARK GPL 2010

Assertions are analysed statically, before compilation or execution. They can appear in various places:

inline in the code, either
-# check X = 42;
or
-# assert X = 42;
as a precondition on an operation:
procedure P (X : in out Integer);
--# derives X from *;
--# pre X = 42;
or as a postcondition on an operation:
procedure P (X : in out Integer);
--# derives X from *;
--# post X = 42;

Example:

X := 7;
--# check X = 42;

produces the following output:

H1:    true .
       ->
C1:    false .

which is an unprovable theorem that tells you that there is a guaranteed failure.

Swift[edit]

var a = 5
//...input or change a here
assert(a == 42) // aborts program when a is not 42
assert(a == 42, "Error message") // aborts program
// when a is not 42 with "Error message" for the message
// the error message must be a static string

In release mode assertion checks are turned off.

Tcl[edit]

Library: Tcllib (Package: control)
package require control
 
set x 5
control::assert {$x == 42}

Produces the output:

assertion failed: $x == 42

UNIX Shell[edit]

Works with: bash

Assertions are not builtin commands, but we can add a function easily.

assert() {
if test ! $1; then
[[ $2 ]] && echo "$2" >&2
exit 1
fi
}
x=42
assert "$x -eq 42" "that's not the answer"
((x--))
assert "$x -eq 42" "that's not the answer"
echo "won't get here"

Vala[edit]

int a = 42;
int b = 33;
assert (a == 42);
assert (b == 42); // will break the program with "assertion failed" error
 

VBScript[edit]

Definition[edit]

sub Assert( boolExpr, strOnFail )
if not boolExpr then
Err.Raise vbObjectError + 99999, , strOnFail
end if
end sub
 

Invocation[edit]

dim i
i = 43
Assert i=42, "There's got to be more to life than this!"

Output[edit]

>cscript "C:\foo\assert.vbs"
C:\foo\assert.vbs(3, 3) (null): There's got to be more to life than this!

Visual Basic[edit]

VB's Assert only fires when run from within the IDE. When compiled, all Debug lines are ignored.

Debug.Assert i = 42

XPL0[edit]

XPL0 does not have an assert command. The equivalent is usually synthesized something like this.

proc Fatal(Str);        \Display error message and terminate program
char Str;
[\return; uncomment this if "assertions" are to be disabled
SetVid(3); \set normal text display if program uses graphics
Text(0, Str); \display error message
ChOut(0, 7); \sound the bell
exit 1; \terminate the program; pass optional error code to DOS
];
 
if X#42 then Fatal("X#42");

zkl[edit]

n:=42; (n==42) or throw(Exception.AssertionError);
n=41; (n==42) or throw(Exception.AssertionError("I wanted 42!"));
Output:
Stack trace for VM#1 ():
   Cmd.__constructor addr:38  args(0) reg(1) 
   startup.__constructor addr:2242  args(0) reg(1) ER
   startup.__constructor addr:2178  args(0) reg(22) 
Exception thrown: AssertionError(I wanted 42!)