Assertion (software development): Difference between revisions

Content deleted Content added
Changing short description from "In computer programming, statement that a predicate is always true at that point in code execution" to "Statement that a predicate is always true at that point in code execution"
 
(6 intermediate revisions by 5 users not shown)
Line 1:
{{shortShort description|In computer programming, statementStatement that a predicate is always true at that point in code execution}}
{{About|the computer programming concept|assertions in the context of the [[Security Assertion Markup Language]] (SAML) open standard|Security Assertion Markup Language#Assertions}}
 
Line 9:
 
The following code contains two assertions, <code>x > 0</code> and <code>x > 1</code>, and they are indeed true at the indicated points during execution:
<syntaxhighlight lang="cjava">
x = 1;
assert x > 0;
Line 20:
The example above uses the notation for including assertions used by [[C. A. R. Hoare]] in his 1969 article.<ref>[[C. A. R. Hoare]], [http://lambda-the-ultimate.org/node/1912 An axiomatic basis for computer programming], ''[[Communications of the ACM]]'', 1969.</ref> That notation cannot be used in existing mainstream programming languages. However, programmers can include unchecked assertions using the [[Comment (computer programming)|comment feature]] of their programming language. For example, in [[C++]]:
 
<syntaxhighlight lang="Cc">
x = 5;
x = x + 1;
Line 30:
[[Library (computing)|Libraries]] may provide assertion features as well. For example, in C using [[glibc]] with C99 support:
 
<syntaxhighlight lang="Cc">
#include <assert.h>
 
Line 60:
 
<syntaxhighlight lang="java">
int total = countNumberOfUsers();
if (total % 2 == 0) {
// total is even
} else {
// total is odd and non-negative
assert total % 2 == 1;
}
</syntaxhighlight>
 
Line 124:
Some languages, including [[C (programming language)|C]], [[ZPE_Programming_Environment|YASS]] and [[C++]], can completely remove assertions at compile time using the [[preprocessor]].
 
Similarly, launching the [[Python (programming language)|Python]] interpreter with "{{Mono|-O}}" (for "optimize") as an argument will cause the Python code generator to not emit any bytecode for asserts.<ref>[https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assert-stmt Official Python Docs, ''assert statement'']</ref>
 
Java requires an option to be passed to the run-time engine in order to ''enable'' assertions. Absent the option, assertions are bypassed, but they always remain in the code unless optimised away by a [[Just-in-time compilation|JIT compiler]] at run-time or [[dead_code_elimination|excluded at compile time]] via the programmer manually placing each assertion behind an <code>if (false)</code> clause.
 
Programmers can build checks into their code that are always active by bypassing or manipulating the language's normal assertion-checking mechanisms.
Line 137:
 
<syntaxhighlight lang="c">
int *ptr = malloc(sizeof(int) * 10);
assert(ptr);
// use ptr
...
</syntaxhighlight>
 
Line 150:
 
<syntaxhighlight lang="c">
int *ptr;
// Statement below fails if malloc() returns NULL,
// but is not executed at all when compiling with -NDEBUG!
assert(ptr = malloc(sizeof(int) * 10));
// use ptr: ptr isn't initialised when compiling with -NDEBUG!
...
</syntaxhighlight>
 
Line 183:
 
* ''[http://discovery.ucl.ac.uk/4991/1/4991.pdf A historical perspective on runtime assertion checking in software development]'' by Lori A. Clarke, David S. Rosenblum in: ACM SIGSOFT Software Engineering Notes 31(3):25-37, 2006
* ''[httphttps://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1203056 Assertions: a personal perspective]'' by C.A.R. Hoare in: IEEE Annals of the History of Computing, Volume: 25, Issue: 2 (2003), Page(s): 14 - 2514–25
* ''[http://queue.acm.org/detail.cfm?id=2220317 My Compiler Does Not Understand Me]'' by Poul-Henning Kamp in: ACM Queue 10(5), May 2012
* ''[https://blog.regehr.org/archives/1091 Use of Assertions]'' by John Regehr