<?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=Vkumar3</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=Vkumar3"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vkumar3"/>
	<updated>2026-05-06T12:46:03Z</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_vb&amp;diff=9670</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9670"/>
		<updated>2007-11-20T01:55:30Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion Support in Eiffel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
* NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
* StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
* ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Design by Contract Assertions. There is a language and environment support assertions in Eiffel. And unlike other languages, in Eiffel if an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
* http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
* http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
* http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
* http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
&lt;br /&gt;
* http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
&lt;br /&gt;
* http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
&lt;br /&gt;
* http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9669</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9669"/>
		<updated>2007-11-20T01:52:43Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Supported Types of Assertions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
* NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
* StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
* ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
* http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
* http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
* http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
* http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
&lt;br /&gt;
* http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
&lt;br /&gt;
* http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
&lt;br /&gt;
* http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9668</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9668"/>
		<updated>2007-11-20T01:51:55Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
* http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
* http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
* http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
* http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
&lt;br /&gt;
* http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
&lt;br /&gt;
* http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
&lt;br /&gt;
* http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9667</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9667"/>
		<updated>2007-11-20T01:51:42Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
* http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
* http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
* http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
&lt;br /&gt;
http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
&lt;br /&gt;
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
&lt;br /&gt;
http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9665</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9665"/>
		<updated>2007-11-20T01:50:53Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
&lt;br /&gt;
http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
&lt;br /&gt;
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
&lt;br /&gt;
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
&lt;br /&gt;
http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9663</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9663"/>
		<updated>2007-11-20T01:50:39Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
&lt;br /&gt;
http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
&lt;br /&gt;
http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9662</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9662"/>
		<updated>2007-11-20T01:50:17Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Example of Assertion Method: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Assertion_%28computing%29&lt;br /&gt;
http://glu.ttono.us/articles/2005/10/30/why-and-how-ruby-and-rails-unit-testing&lt;br /&gt;
http://www.ntecs.de/old-hp/s-direktnet/Data/eiffel/EiffelCpp.txt&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Further Reading=&lt;br /&gt;
http://www.jot.fm/issues/issue_2002_08/article1/&lt;br /&gt;
http://www2.sys-con.com/itsg/virtualcd/java/archives/0402/mahmoud/index.html&lt;br /&gt;
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html&lt;br /&gt;
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/ruby-unit.html&lt;br /&gt;
http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9657</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9657"/>
		<updated>2007-11-20T01:43:00Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion Support in Eiffel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
Eiffel has an inbuilt support for Assertions. And unlike other languages, in Eiffel in an Assertion fails, there is a way to handle that situation. In other words the assertions has the capability to handle exceptions which is not present in most of the OO languages.&lt;br /&gt;
&lt;br /&gt;
===Usage of Assertions in Eiffel===&lt;br /&gt;
&lt;br /&gt;
The following example displays a simple use of Assertion in Eiffel :&lt;br /&gt;
&lt;br /&gt;
 square_root (x: REAL): REAL is&lt;br /&gt;
 /*Returns the positive square root of x. require*/&lt;br /&gt;
   positive_argument: x &amp;gt;= 0.0;&amp;lt;BR&amp;gt;&lt;br /&gt;
 do&lt;br /&gt;
 /* Code to calculate square root */&lt;br /&gt;
 ensure&lt;br /&gt;
   correct_root: Result * Result = x;&lt;br /&gt;
   positive_root: Result &amp;gt;= 0;&lt;br /&gt;
 end;&lt;br /&gt;
&lt;br /&gt;
The above code defines a function which returns a positive square root of the argument. The code also states that the method takes a real argument which is greater than or equal to zero. Result is a special variable that is used to return values from the methods. The ensure keyword makes sure that the result is correctly calculated and also it is positive.&lt;br /&gt;
&lt;br /&gt;
Eiffel also supports '''Invariant''' clauses. As mentioned at the top of the article, these apply to entire classes , and are used to state rules that always hole. For example, a stack class with an ''empty'' flag and a ''count'' of items held might have the following invariant clause:&lt;br /&gt;
&lt;br /&gt;
 invariant&lt;br /&gt;
 count &amp;gt;= 0;&lt;br /&gt;
 empty = (count = 0);&lt;br /&gt;
&lt;br /&gt;
The above code states that the count value, i.e. the number of items on the stack can never be less than zero. The second line states that the empty flag can be true only if the count is zero.&lt;br /&gt;
&lt;br /&gt;
===Types of Assertion in Eiffel===&lt;br /&gt;
1). The assertion in Eiffel is closely related to the exception mechanism, which can be independently found in other OO languages like Java. An exception is raised whenever an assertion fails in Eiffel. The exception can caught by a method or can be passed back to the caller. If the exception is not handled anywhere in the hierarchy then the program fails. On the other hand, debuggers can also be used to catch these exceptions.&lt;br /&gt;
&lt;br /&gt;
2). In Eiffel a contract can be defined between clients and servers. Therefore assertions help in avoiding misunderstandings between coders and speed up integration.&lt;br /&gt;
&lt;br /&gt;
3). When new classes are derived from existing ones by inheritance there are design rules which specify the relationship between the assertions in the parent and child classes. These rules ensure that the child can correctly be substituted for the parent in all cases. &lt;br /&gt;
&lt;br /&gt;
4).Eiffel also supports assertions at runtime and thus provides faster debugging capabilities.&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9635</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9635"/>
		<updated>2007-11-20T01:20:10Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion Support in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in Eiffel==&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9631</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9631"/>
		<updated>2007-11-20T01:13:37Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion Support in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
Ruby alone does not support Assertions , but if combined with ''Test::Unit'' Ruby can become quite strong as far as Assertions are concerned. Unit Testing with the help assertions makes it very easy to fix bugs while keeping regressing to a minimum.&lt;br /&gt;
&lt;br /&gt;
===How to Use Assertions in Ruby===&lt;br /&gt;
&lt;br /&gt;
Require ‘test/unit’ and set your test class to inherit from Test::Unit::TestCase&lt;br /&gt;
1). Write methods prefixed with test_&lt;br /&gt;
2). Insert assert statements according to the code logic&lt;br /&gt;
3). Run the tests&lt;br /&gt;
&lt;br /&gt;
Following is an example of how assertions can be used in ruby:&lt;br /&gt;
&lt;br /&gt;
 my_first_test.rb&amp;lt;br&amp;gt;&lt;br /&gt;
 require 'test/unit'&amp;lt;br&amp;gt;&lt;br /&gt;
 class MyFirstTest &amp;lt; Test::Unit::TestCase&lt;br /&gt;
   def test_for_truth&lt;br /&gt;
     assert true&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Another example of assertions in Ruby can be found below. Suppose we have defined a MyMath Class which performs basic mathematical functionalities. Now how do we write a Unit Test case which could test all the functionalities?&lt;br /&gt;
&lt;br /&gt;
 my_math_test.rb&lt;br /&gt;
 require 'my_math'&lt;br /&gt;
 require 'test/unit'&amp;lt;BR&amp;gt;&lt;br /&gt;
 class MyMathTest &amp;lt; Test::Unit::TestCcase&lt;br /&gt;
   def test_addition&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;2+2&amp;quot;)&lt;br /&gt;
     assert_equal 4, MyMath.run(&amp;quot;1+3&amp;quot;)&lt;br /&gt;
     assert_equal 5, MyMath.run(&amp;quot;5+0&amp;quot;)&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;-5 + 5&amp;quot;)&lt;br /&gt;
  end&amp;lt;BR&amp;gt;&lt;br /&gt;
   def test_subtraction&lt;br /&gt;
     assert_equal 0, MyMath.run(&amp;quot;2-2&amp;quot;)&lt;br /&gt;
     assert_equal 1, MyMath.run(&amp;quot;2-1&amp;quot;)&lt;br /&gt;
     assert_equal -1, MyMath.run(&amp;quot;2-3&amp;quot;)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find a comprehensive list of assert functions supported by the Test::Unit framework at [http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html this page].&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9603</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9603"/>
		<updated>2007-11-20T00:44:24Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Support in ruby==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9592</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9592"/>
		<updated>2007-11-20T00:18:33Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion support in C # */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
===Supported Types of Assertions===&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
===Example code for assertion in C#===&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9590</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9590"/>
		<updated>2007-11-20T00:15:11Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion Methods used in Xunit Testing Framework */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the test is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framework or by the test automater as Custom Assertions.&lt;br /&gt;
&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
===Implementation of Assertion===&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
&lt;br /&gt;
1)	How to call the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
2)	Which method to call among all the Assertion Methods.&amp;lt;br&amp;gt;&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
&lt;br /&gt;
a) Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&amp;lt;br&amp;gt;&lt;br /&gt;
b) These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &amp;lt;br&amp;gt;&lt;br /&gt;
c) Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Assertion Messages===&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails. In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
&lt;br /&gt;
Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1). Single Outcome Assertions: For example '''Fail'''. It does not take any arguments since it behaves the same all the time. For example:&lt;br /&gt;
&lt;br /&gt;
 fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
 unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2). Stated Outcome Assertions: Such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression). These evaluate a single argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assertNotNull( a );&lt;br /&gt;
 assertTrue( b &amp;gt; c );&lt;br /&gt;
 assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3). Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }. These evaluate a block of code and a single expected exception argument. For example:&lt;br /&gt;
&lt;br /&gt;
 assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
 {&lt;br /&gt;
   flight.setMileage(-1122) &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
4). Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. For example:&lt;br /&gt;
&lt;br /&gt;
 Assert.AreEqual( x, y );&lt;br /&gt;
&lt;br /&gt;
5). Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. For example:&lt;br /&gt;
&lt;br /&gt;
 assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
&lt;br /&gt;
===Example of Assertion Method:===&lt;br /&gt;
 /*&lt;br /&gt;
  * Asserts that two objects are equal. If they are not&lt;br /&gt;
  * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
 &lt;br /&gt;
  static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Example3.jpg&amp;diff=9587</id>
		<title>File:Example3.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Example3.jpg&amp;diff=9587"/>
		<updated>2007-11-20T00:05:45Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Example2.jpg&amp;diff=9586</id>
		<title>File:Example2.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Example2.jpg&amp;diff=9586"/>
		<updated>2007-11-20T00:05:33Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Example1.jpg&amp;diff=9585</id>
		<title>File:Example1.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Example1.jpg&amp;diff=9585"/>
		<updated>2007-11-20T00:05:18Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9583</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9583"/>
		<updated>2007-11-20T00:04:06Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion support in C # */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
&lt;br /&gt;
[[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError.&lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9581</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9581"/>
		<updated>2007-11-20T00:03:02Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertion support in C # */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
In C#, one can use Assert Method by using either '''Debug''' or '''Trace''' Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the '''Design by Contract Approach''' for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
&lt;br /&gt;
'''Imperative Assertion:''' It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 [[Image:Example1.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''Declarative Assertions:''' The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
&lt;br /&gt;
– RequiresAttribute for pre conditions&lt;br /&gt;
– EnsuresAttribute for post conditions&lt;br /&gt;
&lt;br /&gt;
Declarative assertions are applied for asbtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
&lt;br /&gt;
'''RequiresAttribute:'''  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example2.jpg]]&lt;br /&gt;
&lt;br /&gt;
'''EnsuresAttribute:'''  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
[[Image:Example3.jpg]]&lt;br /&gt;
&lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
&lt;br /&gt;
• NotNullAttribute&lt;br /&gt;
&lt;br /&gt;
– Expands into “parameter != null” for parameters&amp;lt;br&amp;gt;&lt;br /&gt;
– Expands into “result != null” for returned values&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
– Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages of using Debug.Assert'''&lt;br /&gt;
&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Assert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
&lt;br /&gt;
 // unsafe code&lt;br /&gt;
 Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
&lt;br /&gt;
'''Assert Arguments''' &lt;br /&gt;
&lt;br /&gt;
Trace, Assert and Debug. Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
&lt;br /&gt;
 Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
 Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
 Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
 Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
&lt;br /&gt;
 using System.Diagnostics;&lt;br /&gt;
 class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&amp;lt;BR&amp;gt;&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
==Assertion Methods used in Xunit Testing Framework==&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9569</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9569"/>
		<updated>2007-11-19T23:53:11Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertions support in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
===Integration of Assertion in Java===&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
===Declaration of Assertion===&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9568</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9568"/>
		<updated>2007-11-19T23:51:28Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Example of Assertion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
 /* Defining a object of type TreeNode */&lt;br /&gt;
 TreeNode root;&amp;lt;br&amp;gt;&lt;br /&gt;
 /* Asserting that the root is not null */&lt;br /&gt;
 Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
'''Integration of Assertion in Java'''&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Declaration of Assertion'''&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9567</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9567"/>
		<updated>2007-11-19T23:50:29Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertions support in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
'''Integration of Assertion in Java'''&lt;br /&gt;
&lt;br /&gt;
The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Declaration of Assertion'''&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9566</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9566"/>
		<updated>2007-11-19T23:43:46Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertions support in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readability of the program. In Java, assertions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluated at the beginning of method call, placing it in the beginning of the class is appropriate but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readability of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
&lt;br /&gt;
The Java programming language has very less support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
&lt;br /&gt;
'''a) Built in:'''  Here, support for assertions is directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The syntax of assertion command is checked by the compiler and there is a runtime environment in order to check the assertion at the time of execution. This approach helps to integrate assertions into the programming language and the compiler messages are consistent. Also debugging tools can consider assertions (display stack traces and accurate line numbers).&lt;br /&gt;
&lt;br /&gt;
'''b) Preprocessing:'''  This is the most common approach for the support of assertions. Here the assertions are not placed in the program but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assertion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
'''Integration of Assertion in Java'''&lt;br /&gt;
&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyword '''“assert”''' to the language, and '''AssertionError''' class. As mentioned above, assertion is a Boolean expression which must be true during the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
&lt;br /&gt;
'''Declaration of Assertion'''&lt;br /&gt;
&lt;br /&gt;
Assertion is declared in two forms&lt;br /&gt;
&lt;br /&gt;
 assert Expression;&lt;br /&gt;
&lt;br /&gt;
 assert Expression1  :  Expression2;&lt;br /&gt;
&lt;br /&gt;
In both of the above forms, the condition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
&lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
&lt;br /&gt;
 Assert Salary &amp;gt; 3000;&lt;br /&gt;
 Assert isStatusEnabled ();&lt;br /&gt;
&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
 Class AssertionDemo{&lt;br /&gt;
   &lt;br /&gt;
   public double withdrawMoney(double balance , double amount){&lt;br /&gt;
     assert balance &amp;gt;= amount;&lt;br /&gt;
     return balance – amount;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   Public static void main(String args[]){&lt;br /&gt;
     System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
     System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In the above example, the method ''withdrawMoney'' is called and the paramters ''double'' and ''amount''. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9560</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9560"/>
		<updated>2007-11-19T23:33:57Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Analysis of Support for Assertion in different Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
&lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9559</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9559"/>
		<updated>2007-11-19T23:33:40Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Analysis of Support for Assertion in different Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
The analysis basically focuses on the following few factors &lt;br /&gt;
1. Level of integration of Assertions in the language&lt;br /&gt;
2. How wide is the functionality provided by assertions.&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9552</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9552"/>
		<updated>2007-11-19T23:14:14Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Integration of Assertion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9551</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9551"/>
		<updated>2007-11-19T23:13:56Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Example of Assertion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
==Integration of Assertion==&lt;br /&gt;
&lt;br /&gt;
Assertions support can be put inside the language at many levels. Different languages support assertions at different levels, hence it becomes important to have the knowledge of these levels before delving into the specifics of each language. Following lists and describes these levels briefly:&lt;br /&gt;
&lt;br /&gt;
1. '''Assertions in design by contract:''' Assertions can be a part of the documentation. They describe the state of the code before executing a particular statement and the state achieved after executing that statement. They can also specify certain invariants of a class.&lt;br /&gt;
2. '''Assertions for Runtime Checking:''' Assertions can be used in the actual code implementation to find out if the state of the code is consistent and holds valid values for all the variables taking part in the computations at that point. The main advantage of this approach is that the bug is discovered as soon as it is induced in the system, instead of finding it out later with the help of side effects.&lt;br /&gt;
3. '''Assertions during the development cycle:''' Normally assertions are enabled during the development cycle by the programmer to find the bugs as quickly as possible. This helps in faster development and less number of bugs later on.&lt;br /&gt;
4. '''Static assertions:''' Static assertions are performed at compile time and report a more evident kind of bugs which are very easy to spot and fix.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9550</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9550"/>
		<updated>2007-11-19T22:57:05Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Problem Statement */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Problem Statement =&lt;br /&gt;
''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;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9549</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9549"/>
		<updated>2007-11-19T22:56:42Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
==Types of Assertions==&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
==Example of Assertion==&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
= Analysis of Support for Assertion in different Programming Languages =&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9548</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9548"/>
		<updated>2007-11-19T22:56:02Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
=Types of Assertions=&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
=Example of Assertion=&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9547</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9547"/>
		<updated>2007-11-19T22:55:39Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
=Types of Assertions=&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
=Example of Assertion=&lt;br /&gt;
&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9545</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9545"/>
		<updated>2007-11-19T22:53:37Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Assertions support in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9544</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9544"/>
		<updated>2007-11-19T22:52:38Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Analysis of Support for Assertion in different Programming Languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Assertions support in Java ==&lt;br /&gt;
&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
&lt;br /&gt;
== Assertion support in C # ==&lt;br /&gt;
&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9541</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9541"/>
		<updated>2007-11-19T22:51:33Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
In the remaining sections, we compare different OO languages and analyze the support of assertion in the languages.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9540</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9540"/>
		<updated>2007-11-19T22:50:05Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9539</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9539"/>
		<updated>2007-11-19T22:49:47Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
/* Defining a object of type TreeNode */&lt;br /&gt;
TreeNode root;&lt;br /&gt;
/* Asserting that the root is not null */&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9538</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9538"/>
		<updated>2007-11-19T22:48:51Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
'''Example of Assertion'''&lt;br /&gt;
As stated above Assertion returns a boolean value, if its false, indicates a bug in the code. It prevents the flow of control to fall into an invalid state which would otherwise can cause serious failures.&lt;br /&gt;
&lt;br /&gt;
Following is a simple example of assertion:&lt;br /&gt;
&lt;br /&gt;
TreeNode root;&lt;br /&gt;
&lt;br /&gt;
Assert root != null;&lt;br /&gt;
&lt;br /&gt;
This asserts that root is not null. If it is then it throws a AssertionError. In a way, if any line after the assert statement is executed then it can assume that root is not null.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9536</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9536"/>
		<updated>2007-11-19T22:42:34Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Problem Statement ==&lt;br /&gt;
''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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9535</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9535"/>
		<updated>2007-11-19T22:41:55Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
''== Problem Statement ==&lt;br /&gt;
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;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9534</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9534"/>
		<updated>2007-11-19T22:27:35Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9533</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9533"/>
		<updated>2007-11-19T22:27:23Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9532</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9532"/>
		<updated>2007-11-19T22:27:05Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
1) '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9531</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9531"/>
		<updated>2007-11-19T22:26:33Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes a very tedious task. It becomes difficult to track the problem or able to point out the location of bug in the solution. This is where assertions come into picture. Assertions are Boolean expressions that checks for proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
The syntax of assertion is ''&amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;);'' where  ''&amp;lt;assertion_type&amp;gt;'' indicates what is the assertion made, ''&amp;lt;condition&amp;gt;'' indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
&lt;br /&gt;
'''Types of Assertions:'''&lt;br /&gt;
10 '''Preconditions:''' This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
2) '''Postconditions:'''  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
3) '''Invariants:'''  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
4) '''Data assertions:''' This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Analysis of Support for Assertion in different Programming Languages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9529</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9529"/>
		<updated>2007-11-19T22:20:48Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes very tedious. It becomes difficult to track the problem or unable to get the solution to the solution. Hence it becomes very time consuming. This is where assertions come into the picture. Assertions are Boolean expressions that checks for the proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The syntax of the assertion is &amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;); where  &amp;lt;assertion_type&amp;gt; indicates what is the assertion made, &amp;lt;condition&amp;gt; indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
Types of Assertions:&lt;br /&gt;
1)	Preconditions: This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
2)	Postconditions:  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
3)	Invariants:  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
4)	Data assertions: This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9528</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 1 vb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_1_vb&amp;diff=9528"/>
		<updated>2007-11-19T22:20:25Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Sometimes debugging and testing of the code becomes very tedious. It becomes difficult to track the problem or unable to get the solution to the solution. Hence it becomes very time consuming. This is where assertions come into the picture. Assertions are Boolean expressions that checks for the proper state of the program, method calls for invocation, change in state of data in the class, error handling and the method code for computation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The syntax of the assertion is &amp;lt;assertion_type&amp;gt;(&amp;lt;condition&amp;gt;, &amp;lt;message&amp;gt;); where  &amp;lt;assertion_type&amp;gt; indicates what is the assertion made, &amp;lt;condition&amp;gt; indicates a boolean expression that helps to determine whether the assertion is violated or not; &amp;lt;message&amp;gt; will display an error information when the assertion is violated.&lt;br /&gt;
Types of Assertions:&lt;br /&gt;
1)	Preconditions: This defines the condition to be satisfied in order to call a method. These conditions are checked at the entry point of method.&lt;br /&gt;
2)	Postconditions:  This defines what the method does. This condition is evaluated at the exit point of the method.&lt;br /&gt;
3)	Invariants:  This defines state-space consistency for a class. It is evaluated at the entry and exit point of a class.&lt;br /&gt;
4)	Data assertions: This defines conditions that must be met at a particular line of code. These conditions are evaluated at that particular line of code.&lt;br /&gt;
Assertions support in Java&lt;br /&gt;
It is important to place the assertion statements in the appropriate location for better readibility of the program. In Java, asssetions are placed in the class definition section of the program. Placing the preconditions and postconditions at the start of the class makes them easy to identify. Since precondition is evaluted at the beginning of method call, placing it in the beginning of the class is appropritae but postconditions are treated in a different way. Since they are evaluated at the exit point of method, it must be placed at the top of the method (or before the return statement of the method) and have an assertion tool evaluate the postcondition at all the appropriate conditions. Hence this will improve the readibility of the program. Similarly, invariant is placed in the class definition and evaluated for every entry and exit point of every public method declared in the class.&lt;br /&gt;
The Java programming language has very little support for assertions but there are different approaches which help to support assertions in java language. They are as follows:&lt;br /&gt;
a)	Built in:  Here, support for assertions are directly included in the Java programming language. Java contains language constructs to formulate the assertions.  The synatx of assertion command is check by the compiler and there is a runtime environment inorder to check the assertin at the runtime. This approach helps to integrate assertions into the programming language and the compiler messages are consitent, debuggint tools can consider assertions (display stack traces and accuarte line numbers).&lt;br /&gt;
b)	Preprocessing:  This is the most common approach for support of assertions. Here the assertions are not placed in the porgram but are incorporated into the program as comments.  There is preprocessor which can interpret the comments and convert the comments into programming code.  The advantage of using this approach is to separate the assetion from programming logic. This approach is useful for languages that do not support assertions. The shortcoming of this approach is that original code may be altered and hence the line numbers of compilers do not match with the line numbers of the program. This approach is implemented in Java programming language since it has very little support for assertions.&lt;br /&gt;
&lt;br /&gt;
The iContract Tool for Java programming uses this approach. Programmers add assertion code in the JavaDoc in the form of comments. Then iContract tool translates these comments into assertion code. Another example is Jass (Java with Assertions). The developers write the assertions in Jass and use a preprocessor to translate them into java code.&lt;br /&gt;
&lt;br /&gt;
Integration of Assertion in Java&lt;br /&gt;
 The assertion is supported in J2SE 1.4. This is supported by adding the keyboard “assert” to the language, and AssertionError class. As mentioned above, assertion is a Boolean expression which must be true during the execution at the runtime. This facility can be enabled and disabled at runtime.&lt;br /&gt;
 Declaration of Assertion:  Assertion is declared in two forms&lt;br /&gt;
assert Expression;&lt;br /&gt;
assert Expression1  :  Expression2;&lt;br /&gt;
In both of the above forms, the comdition must be true else it will throw an AssertionError at runtime. &lt;br /&gt;
Some of the examples are as follows:&lt;br /&gt;
Assert Salary&amp;gt;3000;&lt;br /&gt;
Assert isStatusEnabled ();&lt;br /&gt;
Code using assertion: &lt;br /&gt;
//AssertionDemo.java&lt;br /&gt;
Class AssertionDemo{&lt;br /&gt;
Public static void main(String args[]){&lt;br /&gt;
System.out.println( withdrawMoney(1000,500) );&lt;br /&gt;
System.out.println( withdrawMoney(1000,2000) );&lt;br /&gt;
}&lt;br /&gt;
public double withdrawMoney(double balance , double amount){&lt;br /&gt;
assert balance &amp;gt;= amount;&lt;br /&gt;
return balance – amount;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
In the above example, the method withdrawMoney is called and the paramters double and amount are passed. The method contains an assertion statement that checks if the balance is greater or equal to the amount. If the condition if false, then AssertionError is thrown.&lt;br /&gt;
Assertion support in C #&lt;br /&gt;
Assertion checks for conditions that arealways true. Assertions are useful for development and are disabled during runtime. It is useful for testing and debugging. Hence, it helps to main the code in an efficient way. In C#, one can use Assert Method by using either Debug or Trace Class (belongs to System.Diagnostics namespace).  &lt;br /&gt;
C# follows the Design by Contract Approach for implementing assertions. In order to provide assertions in C#, Contract Sharp has been developed which is syntax directed, code development tool with a GUI that’s helps C# programmers in using assertions in method and class.&lt;br /&gt;
C# supports imperative (namespace is System.Diagnostics.Debug) and declarative assertions&lt;br /&gt;
Imperative Assertion: It is supported using the namespace System.Diagnostic.Debug. If there is an aassertion violation, it thrown an AssertionError dialog. This functionality is completely customizable. Example is shown below: &lt;br /&gt;
 &lt;br /&gt;
Declarative Assertions:&lt;br /&gt;
The assertion is declared in compilation attribute and hence is a part of meta data. It can contain preconditions and postconditons.&lt;br /&gt;
–	RequiresAttribute for pre conditions&lt;br /&gt;
–	EnsuresAttribute for post conditions&lt;br /&gt;
Declarative assertions are applied for abtract and interface members and are inherited. They need to be enabled at the assembly level.&lt;br /&gt;
RequiresAttribute:  The condition statement is passed to the compilation attribute and then the C# calls the method Debug.Assert(). This can be enabled and disabled at the assembly level.&lt;br /&gt;
 &lt;br /&gt;
EnsuresAttribute:  Similarly the condition to asset is passed to compilation attribute but it is checked for every return statement of the method. This function call is useful for method with many return statements. The local variable “result” indicates the returned expression&lt;br /&gt;
 &lt;br /&gt;
Some of the built-in Assertion attributes:&lt;br /&gt;
•	NotNullAttribute&lt;br /&gt;
–	Expands into “parameter != null” for parameters&lt;br /&gt;
–	Expands into “result != null” for returned values&lt;br /&gt;
•	PositiveAttribute&lt;br /&gt;
•	StrictPositiveAttribute&lt;br /&gt;
•	ValidIndexAttribute&lt;br /&gt;
–	Expands into “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Count” or “entity &amp;gt;= 0 &amp;amp;&amp;amp; entity &amp;lt; Length” or&lt;br /&gt;
Disadvantages of using Debug.Assert&lt;br /&gt;
While using Debug.Assert, one must ensure that code inside the Asssert does not affect the results of the program if assert is removed else it might introduce an error when the program is compiled. For example :&lt;br /&gt;
// unsafe code&lt;br /&gt;
Debug.Assert (meas(i) != 0 );&lt;br /&gt;
&lt;br /&gt;
If we build the program in release mode, the call to meas is removed and hence counter will not be updated (since meas function increments the counter)&lt;br /&gt;
Assert Arguments &lt;br /&gt;
Trace. Assert and Debug.Assert takes up to three arguments. The first argument is mandatory while others are optional. First argument is the condition to be met. Example is below&lt;br /&gt;
Debug.Assert (stacksize &amp;gt; 0 );&lt;br /&gt;
Trace.Assert (stacksize &amp;gt; 0 ); &lt;br /&gt;
&lt;br /&gt;
If the assert takes in two or more arguments, one argument is the condition while others are information or messages to be displayed. Example is below: &lt;br /&gt;
&lt;br /&gt;
Debug.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
Trace.Assert ( stacksize &amp;gt; 0, &amp;quot;Out of stack space&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
Below is the code for assertion in C#&lt;br /&gt;
using System.Diagnostics;&lt;br /&gt;
class ConfigFile {&lt;br /&gt;
    bool isFileOpen;&lt;br /&gt;
    public void Open(string strFile) {&lt;br /&gt;
        // Pre-conditions&lt;br /&gt;
        Debug.Assert(!isFileOpen, &amp;quot;Config file already open.&amp;quot;,&lt;br /&gt;
            &amp;quot;You can only call Open() once.&amp;quot;);&lt;br /&gt;
        Debug.Assert(strFile.Length &amp;gt; 0); // check if the length of the    file is more than 0&lt;br /&gt;
        isFileOpen = true;&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        ConfigFile file = new ConfigFile();&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;);&lt;br /&gt;
        file.Open(&amp;quot;Joe.xml&amp;quot;); // Causes an assertion!&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The main function calls the open method which contains the assertion statement. If the condition of the assertion is satisfied (checks if the file contains contents), the file is opened else it will thrown an AssertionError. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Assertion Methods used in Xunit Testing Framework&lt;br /&gt;
&lt;br /&gt;
Assertion Method is a way to obtain an outcome that is executable by computer and useful to user. The outcome of the teat is expressed as a series of assertion statements that specifies the condition that would state the test as passed. These assertions are implemented by calling Assertion Methods which are provided by Test Automation Framwork or by the test automater as Custom Assertions.&lt;br /&gt;
Assertion reduces the complexity of the conditional test logics by moving the complex code into Test Utility Methods. Hence these methods can be used again and again. All members of Xunity Testing Framework provide Assertion Methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementation of Assertion&lt;br /&gt;
&lt;br /&gt;
The important features for implemenation are:&lt;br /&gt;
1)	How to call the Assertion Methods.&lt;br /&gt;
2)	Which method to call among all the Assertion Methods&lt;br /&gt;
3)	What information to be displayed in the Assertion Message.&lt;br /&gt;
&lt;br /&gt;
Assertion Methods are named according to how they are accessed. Some of the common variations for accessing Assertion Methods are:&lt;br /&gt;
a)	Assertion Methods are inherited from Testcase Superclass provided by the framework. Hence they are invoked as though that is provided locally on TestCase Class.&lt;br /&gt;
b)	These methods can be provided using globally accessible class or module. They are invoked using class or module name. For eg : Assert.assertTrue(x) &lt;br /&gt;
c)	Assertion methods are provided as mixins or macros. For eg Ruby TestUnit.&lt;br /&gt;
&lt;br /&gt;
Assertion Messages&lt;br /&gt;
&lt;br /&gt;
This is an optional argument in Assertion Methods that provides text message to user when the assertion fails.  In Xunit, it is last argument in the list while in Junit, it is the first argument.&lt;br /&gt;
 Some of the basic Assertion Methods in Xunit Family is below:&lt;br /&gt;
&lt;br /&gt;
1)	Single Outcome Assertions: For example : Fail ; It does not take any arguments since it behaves the same all the time.&lt;br /&gt;
       Eg: fail( &amp;quot;Expected an exception&amp;quot; );&lt;br /&gt;
           unfinishedTest();&lt;br /&gt;
&lt;br /&gt;
2)	Stated Outcome Assertions:  such as assertNotNull(anObjectReference) and assertTrue(aBooleanExpression); these evaluate a single argument. &lt;br /&gt;
        Eg: assertNotNull( a );&lt;br /&gt;
            assertTrue( b &amp;gt; c );&lt;br /&gt;
            assertNonZero( b );&lt;br /&gt;
&lt;br /&gt;
3)	Expected Exception Assertions such as assert_raises( expectedError) { codeToExecute }; these evaluate a block of code and a single expected exception argument. &lt;br /&gt;
       Eg: assert_raises( RuntimeError, &amp;quot;Should have raised error&amp;quot;)&lt;br /&gt;
                  {flight.setMileage(-1122) }&lt;br /&gt;
&lt;br /&gt;
4)	Equality Assertions such as assertEqual(expected, actual); these compare two objects or values for equality. &lt;br /&gt;
Eg:       Assert.AreEqual( x, y );&lt;br /&gt;
5)	Fuzzy Equality Assertion such as assertEqual(expected, actual, tolerance); these determine whether two values are &amp;quot;close enough&amp;quot; to each other by using a &amp;quot;tolerance&amp;quot; or &amp;quot;comparison mask&amp;quot;. &lt;br /&gt;
       Eg: assertEquals( expectedXml, actualXml, elementsToCompare );&lt;br /&gt;
Example of Assertion Method:&lt;br /&gt;
**&lt;br /&gt;
    * Asserts that two objects are equal. If they are not&lt;br /&gt;
    * an AssertionFailedError is thrown with the given message.*/&lt;br /&gt;
   static public void assertEquals(String message, Object expected, Object actual) {&lt;br /&gt;
      if (expected == null &amp;amp;&amp;amp; actual == null)&lt;br /&gt;
         return;&lt;br /&gt;
      if (expected != null &amp;amp;&amp;amp; expected.equals(actual))&lt;br /&gt;
         return;&lt;br /&gt;
      failNotEquals(message, expected, actual);&lt;br /&gt;
   }&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7622</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7622"/>
		<updated>2007-10-25T01:02:29Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* A Window Class Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to delegate to GameStrategy Interface and also have a simple setDifficulty() method. Whenever the difficulty changes via setDifficulty() method, the getMove() method can be delegated to GameStrategy Interface which allows two more classes called SmartStrategy and DumbStrategy to inherit from it, which themselves implement their versions of getMove(). Hence, for any difficulty level everytime the correct getMove() function is called via delegation. This can be done with delegation and not with inheritance since delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
Hence, it makes more sense to use Inheritance between Window Class and BrowserWindow Class whereas between BrowserWindow and UserSession delegation should be used.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7618</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7618"/>
		<updated>2007-10-25T01:00:00Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* A Window Class Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to delegate to GameStrategy Interface and also have a simple setDifficulty() method. Whenever the difficulty changes via setDifficulty() method, the getMove() method can be delegated to GameStrategy Interface which allows two more classes called SmartStrategy and DumbStrategy to inherit from it, which themselves implement their versions of getMove(). Hence, for any difficulty level everytime the correct getMove() function is called via delegation. This can be done with delegation and not with inheritance since delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7617</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7617"/>
		<updated>2007-10-25T00:59:22Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* The Game Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to delegate to GameStrategy Interface and also have a simple setDifficulty() method. Whenever the difficulty changes via setDifficulty() method, the getMove() method can be delegated to GameStrategy Interface which allows two more classes called SmartStrategy and DumbStrategy to inherit from it, which themselves implement their versions of getMove(). Hence, for any difficulty level everytime the correct getMove() function is called via delegation. This can be done with delegation and not with inheritance since delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7613</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7613"/>
		<updated>2007-10-25T00:52:36Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* The Game Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to have a simple setDifficulty() method. This can be done using delegation and at runtime using this method we can chance the difficulty level of the game.&lt;br /&gt;
This can be done since delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7612</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7612"/>
		<updated>2007-10-25T00:51:13Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* The Game Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to have a simple setDifficulty() method. This can be done using delegation and at runtime using this method we can chance the difficulty level of the game.&lt;br /&gt;
Therefore, delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7611</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 10 cv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_10_cv&amp;diff=7611"/>
		<updated>2007-10-25T00:50:31Z</updated>

		<summary type="html">&lt;p&gt;Vkumar3: /* The Game Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.'' &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Inheritance&amp;lt;/b&amp;gt;==&lt;br /&gt;
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. The inherited class is called the super class and new class is called the Derived Class. Here, the derived class can use the attributes and behavior of the super class and hence Inheritance is used when the derived class is a type of Super Class. In other terms, the derived class has all the properties and behavior of the super class plus some other special behaviors and attributes. Many programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Avoids rewriting the code when the functionality is already present &amp;lt;br&amp;gt;&lt;br /&gt;
- Categorizes the code well into well defined reusable logical blocks(Classes) &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance enables overriding methods therefore allows polymorphism &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Disadvantages of Inheritance&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Exposes a subclass to the details of its parent class &amp;lt;br&amp;gt;&lt;br /&gt;
- Changes in the parent class implementation would cause the subclass to change as well &amp;lt;br&amp;gt;&lt;br /&gt;
- Inheritance is static, that is the hierarchy of an object is defined at compile time and it can not be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;b&amp;gt;Delegation&amp;lt;/b&amp;gt;==&lt;br /&gt;
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding.  It is better to use delegation when a programmer's new class needs to use some of the existing class functionality and the relationship can be described as a has-a or uses-a relationship.&lt;br /&gt;
The following example describes how delegation is different from Inheritance:&lt;br /&gt;
&lt;br /&gt;
 class A {&lt;br /&gt;
   void foo() {&lt;br /&gt;
       this.bar()   // &amp;quot;this&amp;quot; is also known under the names &amp;quot;current&amp;quot; and &amp;quot;self&amp;quot; in other languages&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;a.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class B  {&lt;br /&gt;
   delegationlink A a&lt;br /&gt;
   &lt;br /&gt;
   void foo() {&lt;br /&gt;
       a.foo() // call foo() on the a-instance&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void bar() {&lt;br /&gt;
       print(&amp;quot;b.bar&amp;quot;)&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 a = new A()&lt;br /&gt;
 b = new B(a)  // establish delegation between two objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Calling b.foo() will result in b.bar to be printed on the screen. Using normal Inheritance where Class B would inherit Class A, the result would have been a.bar. This happens because, when a.foo() is called from inside foo() of class B, it is also passed the reference to the &amp;quot;this&amp;quot; pointer of class B. Therefore, when method foo() of class A tries to call this.Bar(), it refers to the Bar() method of Class B, instead of Class A.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type &amp;lt;br&amp;gt;&lt;br /&gt;
- Appears a more natural concept than Inheritance as its much more local and hence effects a specific part of the class&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Advantages of Delegation&amp;lt;/b&amp;gt;===&lt;br /&gt;
- Delegation is not statically type checked, therefore can give rise to bugs&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; Examples of Delegation and Inheritance &amp;lt;/b&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.&lt;br /&gt;
&lt;br /&gt;
=== Where Delegation makes more sense than Inheritance ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== The Game Example ====&lt;br /&gt;
&lt;br /&gt;
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.&lt;br /&gt;
&lt;br /&gt;
'''Inheritance Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelA.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
In model A, there are two classes namely &amp;quot;Dumb Player&amp;quot; and &amp;quot;Smart Player&amp;quot; which inherits from the Player Abstract Class. The client of the computer player must know when/how to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time.&lt;br /&gt;
Inheritance allows polymorphism, which provides a kind of run-time flexibility, but this model is undesirable. Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Delegation Model'''&amp;lt;BR&amp;gt;&lt;br /&gt;
[[Image:modelB.jpg]]&amp;lt;BR&amp;gt;&lt;br /&gt;
On the other hand in model B, it is possible for class ComputerPlayer to have a simple setDifficulty() method. This can be done using delegation and at runtime using this method we can chance the difficulty level of the game.&lt;br /&gt;
Therefore, delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.&lt;br /&gt;
&lt;br /&gt;
=== Where Inheritance makes more sense than Delegation ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== A Window Class Example ====&lt;br /&gt;
&lt;br /&gt;
Inheritance is generally used when an object derives all of the properties of the base object. This is done at compile time and hence cannot be changed at runtime.&lt;br /&gt;
&lt;br /&gt;
Inheritance is fundamentally used when the derived class &amp;quot;is a type&amp;quot; of the super class. In this case the derived class is a specialized set of the super class containing all the members of the super class plus its own specialized members. Whereas we use delegation where an object would like to rely upon another object to provide a set of functionalities.&lt;br /&gt;
&lt;br /&gt;
Consider a Window class, where it implements methods like Open(), Close(), Minimize(), Maximize(), Restore(), Move(). Now suppose we create another class called BrowserWindow which inherits the Window Class. This new class is a window but a special kind of window which along with the general methods (like Open,Close) also implements methods like SaveNClose(), ExitSession(), LifeTime() etc. So, in this case its valid for BrowserWindow class to inherit from Window super class. &lt;br /&gt;
&lt;br /&gt;
Now, here we can make use of delegation. Consider the function SaveNClose(). In this function the normal behavior would be close the window(in case there is no data to save), but suppose we also need to perform some other operations like saving the session or asking the user if he wants to close all of the tabs inside the window, then BrowserWindow class can delegate this functionality to another class, say UserSession. This way the BrowserWindow class does'nt have to bother about operations that should be performed on user data/session while closing the window. Note that this can only be done with the help of delegation, not inheritance. This is because, BrowserWindow is not a UserSession and hence it should not inherit UserSession class. Instead it can delegate its SaveNClose() method so that UserSession class can can perform checks on User session.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;b&amp;gt; References &amp;lt;/b&amp;gt; ==&lt;br /&gt;
[http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://media.pragprog.com/titles/pad/CodeAndDebug.pdf Practices of an Agile Developer] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.answers.com/topic/delegation-programming Delegation] &amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.atkinson.yorku.ca/~pkhaiter/Lectures4010/Lecture4010-9.ppt Systems Analysis and Design] &amp;lt;br&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=702260&amp;amp;messageID=4072925 &amp;lt;br&amp;gt;&lt;br /&gt;
http://www.perlmonks.org/index.pl?node_id=278375 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vkumar3</name></author>
	</entry>
</feed>