<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kkrish</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kkrish"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Kkrish"/>
	<updated>2026-06-07T11:48:52Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10146</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10146"/>
		<updated>2007-11-28T20:39:37Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions in XUnit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of xUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions. In the example listed above the ''assert.h'' standard library provides the ''assert()'' function. If the testing framework is ''CPPUnit'', then the corresponding assert function is ''CPPUNIT_ASSERT(expression)''.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support. Some of the commonly used assert statements in Ruby are ''assert(), assert_equal(), assert_not_equal(), assert_nil(), assert_not_nil(), assert_same(), assert_not_same()''.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
There are no unit testing frameworks for Eiffel. Design by Contract is the recommended way to test Eiffel code.&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;br /&gt;
&lt;br /&gt;
==&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10145</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10145"/>
		<updated>2007-11-28T20:38:44Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for Assertions in Various O-O Programming Languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of xUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions. In the example listed above the ''assert.h'' standard library provides the ''assert()'' function. If the testing framework is ''CPPUnit'', then the corresponding assert function is ''CPPUNIT_ASSERT(expression)''.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support. Some of the commonly used assert statements in Ruby are ''assert(), assert_equal(), assert_not_equal(), assert_nil(), assert_not_nil(), assert_same(), assert_not_same()''.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
There are no unit testing frameworks for Eiffel. Design by Contract is the recommended way to test Eiffel code.&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;br /&gt;
&lt;br /&gt;
==&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10138</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10138"/>
		<updated>2007-11-28T20:35:16Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions in various o-o programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Support for Assertions in Various O-O Programming Languages=&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions. In the example listed above the ''assert.h'' standard library provides the ''assert()'' function. If the testing framework is ''CPPUnit'', then the corresponding assert function is ''CPPUNIT_ASSERT(expression)''.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support. Some of the commonly used assert statements in Ruby are ''assert(boolean, [message]), assert_equal(expected, actual, [message]), assert_not_equal(expected, actual, [message]), assert_nil(object, [message]), assert_not_nil(object, [message]), assert_same(expected, actual, [message]), assert_not_same(expected, actual, [message])''.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
There are no unit testing frameworks for Eiffel. Design by Contract is the recommended way to test Eiffel code.&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10137</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10137"/>
		<updated>2007-11-28T20:34:31Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Support for assertions in various o-o programming languages=&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions. In the example listed above the ''assert.h'' standard library provides the ''assert()'' function. If the testing framework is ''CPPUnit'', then the corresponding assert function is ''CPPUNIT_ASSERT(expression)''.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support. Some of the commonly used assert statements in Ruby are ''assert(boolean, [message]), assert_equal(expected, actual, [message]), assert_not_equal(expected, actual, [message]), assert_nil(object, [message]), assert_not_nil(object, [message]), assert_same(expected, actual, [message]), assert_not_same(expected, actual, [message])''.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
There are no unit testing frameworks for Eiffel. Design by Contract is the recommended way to test Eiffel code.&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10136</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10136"/>
		<updated>2007-11-28T20:33:28Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Eiffel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions. In the example listed above the ''assert.h'' standard library provides the ''assert()'' function. If the testing framework is ''CPPUnit'', then the corresponding assert function is ''CPPUNIT_ASSERT(expression)''.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support. Some of the commonly used assert statements in Ruby are ''assert(boolean, [message]), assert_equal(expected, actual, [message]), assert_not_equal(expected, actual, [message]), assert_nil(object, [message]), assert_not_nil(object, [message]), assert_same(expected, actual, [message]), assert_not_same(expected, actual, [message])''.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
There are no unit testing frameworks for Eiffel. Design by Contract is the recommended way to test Eiffel code.&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10123</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10123"/>
		<updated>2007-11-28T20:20:00Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* == */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit ====&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10120</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10120"/>
		<updated>2007-11-28T20:18:45Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in xUnit&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework. The pyUnit framework uses the built-in Python ''assert'' statement for assertions.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10119</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10119"/>
		<updated>2007-11-28T20:16:48Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions in XUnit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides the necessary assertion support.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10117</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10117"/>
		<updated>2007-11-28T20:15:52Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
Ruby uses the ''Test::Unit'' framework for unit testing and the ''Test::Unit::Assertions'' provides assertion support as described above.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10113</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10113"/>
		<updated>2007-11-28T20:12:52Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in XUnit ====&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].  Most of them are ports of JUnit to C++. Since, assertions are not native to C++, each testing unit defines its own assert macros and functions.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby supports assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
[[Image:Comparison.PNG]]&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;br /&gt;
*[http://xunitpatterns.com/Assertion%20Method.html xUnit Patterns]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10091</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10091"/>
		<updated>2007-11-28T18:53:54Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures that the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10090</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10090"/>
		<updated>2007-11-28T18:53:19Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. Here, the state of the code to be executed before and after execution is explicitly defined using pre-conditions and post-conditions. These conditions are also extracted and included in the documentation of the class which ensures the documentation stays in sync with the code.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10088</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10088"/>
		<updated>2007-11-28T18:43:24Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually result in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10087</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10087"/>
		<updated>2007-11-28T18:42:23Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails and will usually terminate the program.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10086</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10086"/>
		<updated>2007-11-28T18:41:52Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Runtime Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
In the example above, the expression ''x&amp;gt;0'' will evaluate to ''true'' and therefore, no ''AssertionError'' is raised.&lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10085</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10085"/>
		<updated>2007-11-28T18:40:38Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions during Developmental Cycle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in Python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10083</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10083"/>
		<updated>2007-11-28T18:36:22Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement according to the official Python documentation is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax. The exception ''AssertionError'' is thrown whenever an assertion fails.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10082</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10082"/>
		<updated>2007-11-28T18:32:40Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions during Developmental Cycle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.However, since assertions in Ruby are part of the unit testing framework, they are expected to be used only in the test fixture and not in the actual development code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit::Assertions]&lt;br /&gt;
*[http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing Why and How: Ruby (and Rails) Unit Testing]&lt;br /&gt;
*[http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt WHY EIFFEL IS BETTER THAN C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10076</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10076"/>
		<updated>2007-11-28T18:27:11Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown with the pertinent information on where the assertion failed which, if required, can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10075</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10075"/>
		<updated>2007-11-28T18:23:24Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case where the assert false statement is executed when none of the case statements are true.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework. An assertion failure will result in the exception ''AssertionFailedError'' being thrown which can be ''rescued'' by the programmer.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10071</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10071"/>
		<updated>2007-11-28T18:19:10Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions during Developmental Cycles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define ''NDEBUG'' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case as the preprocessor will remove all the debugging code.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10070</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10070"/>
		<updated>2007-11-28T18:16:43Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Runtime Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example checks if the difference between ''x'' and ''y'' is greater than zero. In the example above, an assertion error will be thrown resulting in the termination of the program.&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10066</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10066"/>
		<updated>2007-11-28T18:12:20Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client or the entity using the particular method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs the programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''. The ''assert'' macro evaluates an expression and prints out a error message to standard error if the expression evaluates to zero. The actual message is dependent on the implementation and varies from compiler to compiler. In most cases, an assertion failure results in the termination of the program using ''abort''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failure will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] below illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html Programming with Assertions]&lt;br /&gt;
*[http://www.codeproject.com/cs/design/DbCwithXCSharp.asp Design by Contract with Extensible C#]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10037</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=10037"/>
		<updated>2007-11-28T17:41:13Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in various o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple switch-case example can be formulated as follows where an assertion is used for the default case.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failure will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] bellow illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=xUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
Different programming languages support Unit testing using frameworks specific to that language. In this section we briefly discuss for some o-o programming languages.&lt;br /&gt;
* '''Java'''&lt;br /&gt;
Java uses the JUnit testing framework. Here the assert functions are responsible for all the testing. Different assertion functions can be included in the code to run the required tests. Detailed dcoumentation is available in the official website for [http://www.junit.org/ JUnit].&lt;br /&gt;
* '''C++'''&lt;br /&gt;
In C++, there is no one standardized framework but it does have several testing frameworks with different APIs. A list of these can be found [http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B here].&lt;br /&gt;
* '''Ruby'''&lt;br /&gt;
In Ruby there is no native support for assertions. This feature is implemented using in the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Test::Unit] framework&lt;br /&gt;
* '''Python'''&lt;br /&gt;
The standard Unit testing framework in Python is [http://pyunit.sourceforge.net/ PyUnit]. It is a part of the Python standard library. PyUnit is based on the JUnit framework.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
*[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
*[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
*[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
*[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
*[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
*[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
*[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;br /&gt;
*[http://www.junit.org/ JUnit]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.2B.2B Unit testing in C++]&lt;br /&gt;
*[http://pyunit.sourceforge.net/ PyUnit].&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9462</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9462"/>
		<updated>2007-11-19T19:58:56Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* External Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.[1]&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions''&lt;br /&gt;
*''Invariants''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program.&lt;br /&gt;
Java Assertion Facility is a built in kind of support since Java 1.4 release. Java supports a very simple assertion mechanism that allows to formulate correctness conditions within methods. Assertion checking can be easily enabled and disabled and traces of assertions may be eliminated completely from class files. [3]&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
Java supports runtime assertions since it is a statically typed language. It also allows specifying the pre-condition and post-condition, internal invariant type of assertions.&lt;br /&gt;
&lt;br /&gt;
A very simple swiych-case example can be formulated as follows where an assertion is used for the default case.&lt;br /&gt;
&lt;br /&gt;
 switch(color) {&lt;br /&gt;
      case Color.RED:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.YELLOW:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.BLUE:&lt;br /&gt;
        ...&lt;br /&gt;
        break;&lt;br /&gt;
      case Color.GREEN:&lt;br /&gt;
        ...&lt;br /&gt;
      default:&lt;br /&gt;
        assert false : suit; &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] bellow illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;br /&gt;
*[3] [http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html Sun Microsystems: Java Assertion Facility]&lt;br /&gt;
&lt;br /&gt;
=External Links=&lt;br /&gt;
[http://wiki.python.org/moin/UsingAssertionsEffectively Assertions in Python]&lt;br /&gt;
&lt;br /&gt;
[http://docs.python.org/ref/assert.html Assert statement in Python]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Unit Testing in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Assertions in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://archive.eiffel.com/doc/manuals/technology/contract/ Design by Contract in Eiffel]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Design_by_contract Design by Contract]&lt;br /&gt;
&lt;br /&gt;
[http://wiki.rubyonrails.org/rails/pages/HowtoUnitTest More information on Ruby Unit Testing]&lt;br /&gt;
&lt;br /&gt;
[http://www.cplusplus.com/reference/clibrary/cassert/assert.html Assertions in C++]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9415</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9415"/>
		<updated>2007-11-19T19:19:08Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Design by Contract */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-conditions and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] bellow illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9413</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9413"/>
		<updated>2007-11-19T19:17:08Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-condition and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] bellow illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9412</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9412"/>
		<updated>2007-11-19T19:16:43Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Eiffel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
Eiffel has native support for assertions through Design by Contract. &lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
====Design by Contract====&lt;br /&gt;
Eiffel supports assertions only through Design by Contract. Eiffel uses the ''require'' statement to indicate pre-condition and ''ensure'' statement for post-conditions. The example[http://archive.eiffel.com/doc/manuals/technology/contract/] bellow illustrates this using a simple dictionary insertion function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
put (x: ELEMENT; key: STRING) is&lt;br /&gt;
                     -- Insert x so that it will be retrievable through key.&lt;br /&gt;
             require&lt;br /&gt;
                     count &amp;lt;= capacity&lt;br /&gt;
                     not key.empty&lt;br /&gt;
             do&lt;br /&gt;
                     ... Some insertion algorithm ...&lt;br /&gt;
             ensure&lt;br /&gt;
                     has (x)&lt;br /&gt;
                     item (key) = x&lt;br /&gt;
                     count = old count + 1&lt;br /&gt;
             end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9407</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9407"/>
		<updated>2007-11-19T18:55:38Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9404</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9404"/>
		<updated>2007-11-19T18:50:26Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9402</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9402"/>
		<updated>2007-11-19T18:50:08Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro ''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9401</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9401"/>
		<updated>2007-11-19T18:49:58Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in C++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro''assert''.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9400</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9400"/>
		<updated>2007-11-19T18:49:43Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
There is no native support for assertions in c++. Assertions are, however, part of the standard library header ''assert.h'' which defines the macro''assert''.&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The ''assert'' macro can be used for runtime assertions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 #include&amp;lt;assert.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
   int x=3;&lt;br /&gt;
   int y=4;&lt;br /&gt;
&lt;br /&gt;
   assert((x-y)&amp;gt;0); //false. Will terminate the program.&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An assertion failue will usually result in the termination of the program. &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
The ''assert'' macro is defined only if ''NDEBUG'' is not defined. Therefore, a programmer can define 'NDEBUG' using a ''#define NDEBUG''  during the release cycle to remove all assertions in the code. There is no performance penalty in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Assertions in Design by Contract are supported in C++ using third party tools such as the DBC for C preprocessor, GNU Nana, C², or the Digital Mars C++ compiler.&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9397</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9397"/>
		<updated>2007-11-19T18:29:07Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Support for Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the ''assert'' statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9395</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9395"/>
		<updated>2007-11-19T18:27:51Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Assertions during Developmental Cycles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the assert statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9394</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9394"/>
		<updated>2007-11-19T18:27:27Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Types of assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported in different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
There are different kinds of assertions that can be implemented. A few of them have been listed below.&lt;br /&gt;
*''Basic assertions'': Annotations in the implementation of a method.&lt;br /&gt;
*''Preconditions and Postconditions'':[[Usage]]&lt;br /&gt;
*''Invariants'':&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
====Runtime Assertions ====&lt;br /&gt;
The ''Test::Unit::Assertions'' provides a rich set of assertion functions. A simple example is shown below:&lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 x=8&lt;br /&gt;
 assert x&amp;gt;0 # will return true&lt;br /&gt;
 assert_equal(x,7,&amp;quot;Equality test failed!&amp;quot;) # False. Test will fail.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
Other functions include ''assert_match'' for regular expression matching. &lt;br /&gt;
&lt;br /&gt;
 require &amp;quot;test/unit&amp;quot;&lt;br /&gt;
    &lt;br /&gt;
 assert (/[a-z]+/, &amp;quot;Match12&amp;quot;) # will fail.&lt;br /&gt;
&lt;br /&gt;
A complete list of possible runtime assertions can be found at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html RubyDoc].&lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycle ====&lt;br /&gt;
Since, Ruby does not have native support for assertions there is no way to turn off assertions during runtime unless the programmer explicitly does so.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Ruby support assertions in Design by Contract using third party libraries like Brian McCallister's DesignByContract, Ruby DBC or ruby-contract.&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the assert statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9387</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9387"/>
		<updated>2007-11-19T18:04:03Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
* Design by Contract: Assertions may be specified at the class level (invariants) or on the method level (preconditions and postconditions). An invariant is a correctness condition imposed on a class, i.e., a condition that must not be violated by any method of a class. A precondition is associated with a particular method and imposes a correctness condition on the client of the method: i.e., the client must ensure that the precondition is fulfilled; otherwise the method is not executed. A postcondition is also associated with a particular method, but it imposes a correctness condition on the implementation of the method; a violation of a postcondition indicates an error in the implementation of the method. &lt;br /&gt;
*Runtime checking: Assertions that are a part of the implementation and can be checked at run time for correctness. This is a very common usage of assertions.&lt;br /&gt;
*Development Cycle: During the development cycle, assertions are used as a powerful debugging tool. The point at which the assertion failure occurs teh programmer is immediately notified so that he can go ahead and fix the bug.&lt;br /&gt;
* Static assertions: Assertions that are checked at compile time are called static assertions.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
&lt;br /&gt;
'''1. How well the support for assertions is integrated with the language'''&lt;br /&gt;
&lt;br /&gt;
There are several ways in which assertions are supported iin different languages. Listed below are few strategies for providing this support.&lt;br /&gt;
*''Built-in support'': This means that support is directly included in the programming language. The programming language contains language constructs to formulate assertions in one way or another. The syntactical correctness of assertions is checked directly by the compiler. In addition a runtime environment must be available to perform the runtime assertion checks and also should be able to selectively enable and disable assertion checking.&lt;br /&gt;
*''Preprocessing'': The general idea here is to formulate assertions separate from the program or to include the assertions as comments. A preprocessor is used to weave the assertions into the program or to transform the comments containing assertion formulas into programming language code. The main advantage of this approach is the separation of programming logic and contracts. This is important in cases, where the programming language itself does not support assertions and the programming language must not be altered. The main disadvantage of this approach is that the original program code is changed by the preprocessor, i.e., line numbers of compiler errors do not actually fit the line numbers of the program. The same problem arises with debugging or runtime exceptions.&lt;br /&gt;
*''Metaprogramming'': According to Templ metaprogramming refers to “programming at the level of program interpretation, or in other words, to extending the interpreter of a given programming language in an application-specific way. Traditionally, this concept is available only in dynamically typed and interpreted languages” [2].&lt;br /&gt;
&lt;br /&gt;
'''2. How many different kinds of assertions the language supports'''&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for Assertions===&lt;br /&gt;
Ruby does not have native support for assertions. Assertions are supported using the ''Test::Unit'' framework. ''Test::Unit::Assertions '' contain the standard Test::Unit assertions. Please refer to the [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html Official Ruby Documentation] for more information on using the ''Test::Unit'' framework.&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the assert statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
*[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;br /&gt;
*[2] Templ J.: Metaprogramming in Oberon, ETH Dissertation No. 10655, Zurich, 1994&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9373</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9373"/>
		<updated>2007-11-19T17:45:55Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Types of Assertion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
*How well the support for assertions is integrated with the language.&lt;br /&gt;
*How many different kinds of assertions the language supports.&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the assert statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertions ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9372</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9372"/>
		<updated>2007-11-19T17:45:44Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions in various o-o programming languages=&lt;br /&gt;
==intro==&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
*How well the support for assertions is integrated with the language.&lt;br /&gt;
*How many different kinds of assertions the language supports.&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement.  The syntax for the assert statement is as follows:&lt;br /&gt;
&lt;br /&gt;
 assert_stmt::=&amp;quot;assert&amp;quot; expression [&amp;quot;,&amp;quot; expression]&lt;br /&gt;
&lt;br /&gt;
The second expression is optional. Please refer to [http://docs.python.org/ref/assert.html Python Documentation] for more information on the ''assert'' statement syntax.&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertion ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9367</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9367"/>
		<updated>2007-11-19T17:40:37Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
*How well the support for assertions is integrated with the language.&lt;br /&gt;
*How many different kinds of assertions the language supports.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions on various o-o programming languages=&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
=== Support for assertions ===&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. &lt;br /&gt;
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program&lt;br /&gt;
&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
=== Support for Assertions ===&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Types of Assertion ===&lt;br /&gt;
==== Runtime Assertions ====&lt;br /&gt;
The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
==== Assertions during Developmental Cycles ====&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==== Assertions in Design by Contract ====&lt;br /&gt;
Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
===Support for assertions===&lt;br /&gt;
===Types of assertions===&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9341</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_sa&amp;diff=9341"/>
		<updated>2007-11-19T17:22:41Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Python */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Support for assertions in various o-o programming languages'''&lt;br /&gt;
&lt;br /&gt;
''Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks? ''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &amp;quot;''In computer programming, an '''assertion''' is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution.&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;[1]&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code. &lt;br /&gt;
&lt;br /&gt;
Following is a very simple example of an assertion in Java.&lt;br /&gt;
 if (i % 4 == 0) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 1) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else if (i % 4 == 2) {&lt;br /&gt;
        ...&lt;br /&gt;
    } else {&lt;br /&gt;
        assert i % 4 == 3 : i;&lt;br /&gt;
        ...&lt;br /&gt;
    }&lt;br /&gt;
In the above example we know that if the first three conditions are false then ''i % 4'' has to evaluate to ''3'' and this is asserted using ''assert''. Note that even in this case the assertion may fail if ''i'' is negative.&lt;br /&gt;
&lt;br /&gt;
Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:&lt;br /&gt;
*How well the support for assertions is integrated with the language.&lt;br /&gt;
*How many different kinds of assertions the language supports.&lt;br /&gt;
&lt;br /&gt;
=Support for assertions on various o-o programming languages=&lt;br /&gt;
&lt;br /&gt;
==Java==&lt;br /&gt;
&lt;br /&gt;
Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility.&lt;br /&gt;
&lt;br /&gt;
==C++==&lt;br /&gt;
&lt;br /&gt;
==Ruby==&lt;br /&gt;
&lt;br /&gt;
==Python==&lt;br /&gt;
Assertions in Python are built into the language and are implemented using the ''assert'' statement. The example below checks whether a value of a variable is always positive using assertions in Python.&lt;br /&gt;
&lt;br /&gt;
 x=5&lt;br /&gt;
 assert x&amp;gt;0 #will be true as long as x&amp;gt;0&lt;br /&gt;
&lt;br /&gt;
An ''AssertionError'' is raised whenever an assertions fails and usually terminates the program. &lt;br /&gt;
&lt;br /&gt;
The ''assert'' statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.&lt;br /&gt;
&lt;br /&gt;
 from types import *&lt;br /&gt;
 &lt;br /&gt;
 def foo(x,name)&lt;br /&gt;
  assert type(x) is IntType, &amp;quot;id is not an integer: %s&amp;quot; % `id`&lt;br /&gt;
  assert type(name) is StringType, &amp;quot;name is not String: %s&amp;quot; % `name`&lt;br /&gt;
 &lt;br /&gt;
The string expression at the end of the ''assert statement'' is printed out only if the assertion fails.  &lt;br /&gt;
&lt;br /&gt;
Assertions in python are executed only in debug mode and can be disabled by passing the &amp;quot;-O&amp;quot; option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.&lt;br /&gt;
&lt;br /&gt;
==SmallTalk==&lt;br /&gt;
&lt;br /&gt;
==Eiffel==&lt;br /&gt;
&lt;br /&gt;
=XUnit testing frameworks and assertions=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
[1] [http://en.wikipedia.org/wiki/Assertion_%28computing%29 Wikipedia Page] Assertions&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7893</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7893"/>
		<updated>2007-10-28T06:39:00Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Principle of Least Astonishment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application and is not consistant across all fields with respect to how it is called. &lt;br /&gt;
&lt;br /&gt;
Other names for the principle of least astonishment include:&lt;br /&gt;
&lt;br /&gt;
1. The element of least surprise&lt;br /&gt;
&lt;br /&gt;
2. Occam's Razor&lt;br /&gt;
&lt;br /&gt;
3. The principle of parsimony&lt;br /&gt;
&lt;br /&gt;
4. The principle of minimum suprise&lt;br /&gt;
&lt;br /&gt;
It is important to remember that, when scouring the web for information on the principle of least astonishment, all of the above mentioned terms mean the same.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7892</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7892"/>
		<updated>2007-10-28T06:37:37Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Principle of Least Astonishment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like:&lt;br /&gt;
&lt;br /&gt;
1. The element of least surprise&lt;br /&gt;
&lt;br /&gt;
2. Occam's Razor&lt;br /&gt;
&lt;br /&gt;
3. The principle of parsimony&lt;br /&gt;
&lt;br /&gt;
4. The principle of minimum suprise&lt;br /&gt;
&lt;br /&gt;
It is important to remember that, when scouring the web for information on the principle of least astonishment, all of the above mentioned terms mean the same.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7891</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7891"/>
		<updated>2007-10-28T06:34:04Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Principle of Least Astonishment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like:&lt;br /&gt;
&lt;br /&gt;
1. The element of least surprise&lt;br /&gt;
&lt;br /&gt;
2. Occam's Razor&lt;br /&gt;
&lt;br /&gt;
3. The principle of parsimony&lt;br /&gt;
&lt;br /&gt;
4. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7890</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7890"/>
		<updated>2007-10-28T06:33:29Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Principle of Least Astonishment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like &lt;br /&gt;
&lt;br /&gt;
1. The element of least surprise&lt;br /&gt;
&lt;br /&gt;
2. Occam's Razor&lt;br /&gt;
&lt;br /&gt;
3. The principle of parsimony&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7889</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7889"/>
		<updated>2007-10-28T06:29:50Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Other External Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like the element of least surprise, Occam's Razor etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7888</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7888"/>
		<updated>2007-10-28T06:29:07Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* General Software Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like the element of least surprise, Occam's Razor etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].  Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other External Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7887</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7887"/>
		<updated>2007-10-28T06:28:48Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* General Software Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like the element of least surprise, Occam's Razor etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here]. &lt;br /&gt;
Ken Thompson's [http://www.faqs.org/docs/artu/ch01s06.html Basics of Unix Philosophy] gives a feel for applying the principle of least astonishment in operating system design.&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other External Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7886</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7886"/>
		<updated>2007-10-28T06:21:56Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like the element of least surprise, Occam's Razor etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the rest of this wiki talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other External Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7885</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 9 kk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_9_kk&amp;diff=7885"/>
		<updated>2007-10-28T06:21:05Z</updated>

		<summary type="html">&lt;p&gt;Kkrish: /* Applications in Other Fields */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Principle of Least Astonishment. Write a guide to the Web pages on the Principle of Least Astonishment. Which should the reader look at for easy-to-understand examples? Which give a feel for where the principle should be used? Is this principle present in fields other than programming? Is the term used consistently in other disciplines?''&lt;br /&gt;
&lt;br /&gt;
= Principle of Least Astonishment =&lt;br /&gt;
The principle of least astonishment states that when you are presented with different outcomes, one should always pick the outcome of least ambiguity or surprise. The principle has applications in a lot of fields. This wiki serves as a guide for the reader to understand more about this principle on the web. The principle of least astonishment goes by different names depending on the field of application like the element of least surprise, Occam's Razor etc.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The simplest example is in programming  where the name of a function does not indicate its functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function above is supposed to add two numbers and return the sum but actually returns the sum of squares of the two numbers which would surprise the programmer who is using it.&lt;br /&gt;
Applying the principle of least astonishment, we can either rename the function to indicate its functionality or we can rewrite the functionality itself to correspond to its name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int sum_of_squares(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return (a*a+b*b);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int add(int a, int b)&lt;br /&gt;
{&lt;br /&gt;
   return a+b&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example gives a feel for the principle of least astonishment. As this wiki is more about writing a guide to webpages on the principle of least astonishment, the next section talks about the applications of this principle, both in programming and in other fields by providing links to other pages on the web.&lt;br /&gt;
&lt;br /&gt;
== Applications in Programming ==&lt;br /&gt;
&lt;br /&gt;
=== General Software Design ===&lt;br /&gt;
[http://www.ddj.com/blog/cppblog/archives/2007/10/the_principle_o.html DDJ] has an article which introduces the user to applying the principle of least surprise to general software design by using an example of using arrays in language called APL. A very basic introduction and some examples can also be found [http://c2.com/cgi/wiki?PrincipleOfLeastAstonishment here].&lt;br /&gt;
&lt;br /&gt;
=== API Design ===&lt;br /&gt;
The principle of least astonishment also plays an important role in API or library design. For example, when designing libraries or API's we want functions which does what the function name implies it will do. [http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx Steven's Tech Talk] has a very good introduction to proper API design using the principle of least astonishment. [http://blogs.sun.com/swinger/entry/simple_kisses_and_surprises Antonio Vierio] looks at what makes an api a good api by applying the principle of least astonishment.&lt;br /&gt;
&lt;br /&gt;
=== User interface design ===&lt;br /&gt;
The principle of least astonishment is used a lot in user interface design. [http://www.joelonsoftware.com/uibook/chapters/fog0000000057.html Joel Spolsky] has an article introducing user interface design using the principle of least astonishment. The article looks at a lot of examples of using the element of least surprise by comparing user interface design in Windows and Mac OS X.&lt;br /&gt;
&lt;br /&gt;
A more general introduction to the  application of  the principle of least astonishment to UI design can be found [http://www.faqs.org/docs/artu/ch11s01.html here].&lt;br /&gt;
&lt;br /&gt;
=== Webpage Design ===&lt;br /&gt;
Webpage design like UI design also finds the application of the principle of least astonishment. [http://www.ibm.com/developerworks/web/library/us-cranky10.html IBM] has an excellent article on how this can be done.&lt;br /&gt;
&lt;br /&gt;
== Applications in Other Fields ==&lt;br /&gt;
The principle of least astonishment also finds a lot of applications in other fields. &lt;br /&gt;
&lt;br /&gt;
1. Occam's Razor is one of the most popular use of the principle of least astonishment. [http://en.wikipedia.org/wiki/Occam's_razor Occam's Razor] is commonly used to explain phenonmena in such a way that the explanation makes the fewest assumptions possible. In other words, it can be paraphrased as &amp;quot;With all things being equal, the simplest solution tends to be the right one&amp;quot;. The [http://skepdic.com/occam.html Skeptic's Dictionary] has a more critical look at this principle. The [http://www.utm.edu/research/iep/o/ockham.htm Internet Encyclopedia of Philosophy] has some interesting applications of Occam's Razor.&lt;br /&gt;
&lt;br /&gt;
2. [http://portal.acm.org/citation.cfm?id=1233682 This] paper looks at applying the principle of least astonishment to the verification of digital circuits.&lt;br /&gt;
&lt;br /&gt;
3. [http://www.mile43.com/peterson/Safety%20First.html This] page looks at applying the principle of least astonishment to riding bicycles in traffic.&lt;br /&gt;
&lt;br /&gt;
4. [http://en.wikipedia.org/wiki/Wikipedia:Guide_to_writing_better_articles#Principle_of_least_astonishment Wikipedia] has some information on applying the principle of least astonishment to writing articles.&lt;br /&gt;
&lt;br /&gt;
== Other External Links ==&lt;br /&gt;
This section has links which do not fit in with the links already provided in the other sections.&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Principle_of_least_astonishment Wikipedia entry on the principle of least astonishment]&lt;br /&gt;
&lt;br /&gt;
[http://www.canonical.org/~kragen/tao-of-programming.html#book4 Tao of Programming]&lt;br /&gt;
&lt;br /&gt;
[http://jooto.com/blog/index.php/2006/06/06/principle-of-least-astonishment/ Principle of Least Astonishment in Ruby]&lt;/div&gt;</summary>
		<author><name>Kkrish</name></author>
	</entry>
</feed>