Assertions: Difference between revisions

From Rosetta Code
Content added Content deleted
(add E example)
Line 59: Line 59:
return 0;
return 0;
}</lang>
}</lang>

=={{header|E}}==

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:

<lang e>
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
</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==

Revision as of 15:01, 7 February 2009

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.

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

Ada

Using pragma Assert: <lang ada> pragma Assert (A = 42, "Oops!"); </lang> The behavior of pragma is controlled by pragma Assertion_Policy. Another way is to use the predefined package Ada.Assertions: <lang ada> with Ada.Assertions; use Ada.Assertions; ... Assert (A = 42, "Oops!"); </lang> The procedure Assert propagates Assertion_Error when condition is false.

ALGOL 68

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 preclude:

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

C

<lang c>#include <assert.h>

int main(){

  int a;
  /* ...input or change a here */
  assert(a == 42); /* aborts program when a is not 42 */
  return 0;

}</lang>

E

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:

<lang e> 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 </lang>

Haskell

<lang haskell>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</lang>

Java

<lang java5>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

}</lang>

J

   assert n = 42

Modula-3

ASSERT is a pragma, that creates a run-time error if it returns FALSE. <lang modula3><*ASSERT a = 42*></lang>

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

OCaml

<lang ocaml>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 *)</lang>

Python

<lang python>a = 5

  1. ...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</lang>