CSC/ECE 517 Fall 2010/ch6 6b yc: Difference between revisions
No edit summary |
No edit summary |
||
Line 28: | Line 28: | ||
In Java, <code>%</code> is the ''remainder'' operator (or ''modulus'') — if its first operand is negative, the result can also be negative. Here, we have assumed that <code>total</code> is non-negative, so that the remainder of a division with 2 will always be 0 or 1. The assertion makes this assumption explicit — if <code>NumberOfUsers</code> does return a negative value, the program may have a bug. | In Java, <code>%</code> is the ''remainder'' operator (or ''modulus'') — if its first operand is negative, the result can also be negative. Here, we have assumed that <code>total</code> is non-negative, so that the remainder of a division with 2 will always be 0 or 1. The assertion makes this assumption explicit — if <code>NumberOfUsers</code> does return a negative value, the program may have a bug. | ||
=References= | == Different Types of Assertions == | ||
Assertions are meant to encode the key properties of O-O programs. They can be classified into the following types. <br/> | |||
* Preconditions: This defines what must be true when a method is invoked. | |||
* Postconditions: This defines what must be true after a method completes successfully. | |||
* Class invariants: This defines what must be true about each instance of a class. | |||
= References = | |||
*[1] B. Meyer, Object Oriented Software Construction, Prentice Hall, 1997. | *[1] B. Meyer, Object Oriented Software Construction, Prentice Hall, 1997. | ||
*[2] C. A. R. Hoare, An Axiomatic Basis for Computer Programming, Communications of the ACM, Vol. 12, No. 10, pp. 576–580,583, October 1969. | *[2] C. A. R. Hoare, An Axiomatic Basis for Computer Programming, Communications of the ACM, Vol. 12, No. 10, pp. 576–580,583, October 1969. |
Revision as of 15:00, 17 November 2010
Problem Statement
"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
"An imperfect solution is better than none. - B. Meyer "[1]
Assertions are formal constraints on software systems which are inserted as annotations in the source program text. They had their origin in program verification [2]. Program correctness is usually defined in relation to a specification and assertions can encode the semantic properties of a specification. Using assertions to show program correctness is in general a non-trivial task and therefore it is hardly followed in practice. However, many key properties of a program can still be encoded in a simple assertion language. In such a scenario, if a program executes without any assertion violation, it can give some confidence about the program’s correctness. In a sense, assertions test a program without using any test data.
Example
Following is a simple example of an assertion in Java.
int total = NumberOfUsers(); if (total % 2 == 0) { // total is even } else { // total is odd assert(total % 2 == 1); }
In Java, %
is the remainder operator (or modulus) — if its first operand is negative, the result can also be negative. Here, we have assumed that total
is non-negative, so that the remainder of a division with 2 will always be 0 or 1. The assertion makes this assumption explicit — if NumberOfUsers
does return a negative value, the program may have a bug.
Different Types of Assertions
Assertions are meant to encode the key properties of O-O programs. They can be classified into the following types.
- Preconditions: This defines what must be true when a method is invoked.
- Postconditions: This defines what must be true after a method completes successfully.
- Class invariants: This defines what must be true about each instance of a class.
References
- [1] B. Meyer, Object Oriented Software Construction, Prentice Hall, 1997.
- [2] C. A. R. Hoare, An Axiomatic Basis for Computer Programming, Communications of the ACM, Vol. 12, No. 10, pp. 576–580,583, October 1969.