CSC/ECE 517 Fall 2007/wiki3 1 sa

From Expertiza_Wiki
Revision as of 17:40, 19 November 2007 by Kkrish (talk | contribs) (→‎Python)
Jump to navigation Jump to search

Support for assertions in various o-o programming languages

Topic :Compare the support for assertions in various o-o programming languages. How well is it integrated with the language (instead of being supplied by libraries)? How many kinds of assertions are supported? How are assertions used in the various flavors of XUnit testing frameworks?

Introduction

Definition

"In computer programming, an assertion is a predicate (i.e., a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. Assertions are used to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the code is expected to be executed. A postcondition — placed at the end — describes the expected state at the end of execution. "[1]

Assertions are essentially a way to implement testing in computer programming and are specified by the programmer to check for correctness in a program. Assertions are a systematic way to check that the internal state of a program is as the programmer expected, with the goal of catching bugs. In particular, they are good for catching false assumptions that were made while writing the code.

Following is a very simple example of an assertion in Java.

if (i % 4 == 0) {
       ...
   } else if (i % 4 == 1) {
       ...
   } else if (i % 4 == 2) {
       ...
   } else {
       assert i % 4 == 3 : i;
       ...
   }

In the above example we know that if the first three conditions are false then i % 4 has to evaluate to 3 and this is asserted using assert. Note that even in this case the assertion may fail if i is negative.

Depending on the programming language, assertions can either be a part of the design process or they could be statements which are checked at runtime. In this page we are trying to research the support for assertions present in varios o-o programming languages. The research is based on two main criteria:

  • How well the support for assertions is integrated with the language.
  • How many different kinds of assertions the language supports.

Support for assertions on various o-o programming languages

Java

Support for assertions

Java, when developed initially, did not have any built-in support for assertions. This was probably because it's exception handling feature was very powerful. However the (beta) release 1.4 of Java 2 included a built-in assertion facility. An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program

Types of assertions

C++

Support for assertions

Types of assertions

Ruby

Support for assertions

Types of assertions

Python

Support for Assertions

Assertions in Python are built into the language and are implemented using the assert statement.


Types of Assertion

Runtime Assertions

The example below checks whether a value of a variable is always positive using assertions in Python.

x=5
assert x>0 #will be true as long as x>0

An AssertionError is raised whenever an assertions fails and usually terminates the program.

The assert statement can also be used for type checking since python is a dynamically typed language. This is useful in making sure the type of the arguments to an function are correct.

from types import *

def foo(x,name)
 assert type(x) is IntType, "id is not an integer: %s" % `id`
 assert type(name) is StringType, "name is not String: %s" % `name`

The string expression at the end of the assert statement is printed out only if the assertion fails.

Assertions during Developmental Cycles

Assertions in python are executed only in debug mode and can be disabled by passing the "-O" option to the python interpreter. This allows the developer to turn off assertions in the release builds to avoid unnecessary performance slowdowns.

Assertions in Design by Contract

Python does not have native support for assertions in Design by Contract(DBC). However, there are third party libraries like PyDBC or Contracts for Python which add support for DBC.

SmallTalk

Support for assertions

Types of assertions

Eiffel

Support for assertions

Types of assertions

XUnit testing frameworks and assertions

References

[1] Wikipedia Page Assertions