Assert
Function Name: assert
Skill: test a condition and possibly terminate the program
Usage: void assert (int test );
Program example:
# Include <assert. h>
# Include <stdio. h>
# Include <stdlib. h>
Struct ITEM {
Int key;
Int value;
};
/* Add item to list, make sure list is not null */
Void additem (struct ITEM * itemptr ){
Assert (itemptr! = NULL );
/* Add item to list */
}
Int main (void)
{
Additem (NULL );
Return 0;
}
Assert can be in the following two forms:
Assert ExprEssion1;
Assert ExprEssion1: ExprEssion2;
ExprEssion1 should always generate a Boolean value.
ExprEssion2 can be any expression that generates a value. This value is used to generate a String message that displays more debugging information.
Assertions are disabled by default. To enable assertions during compilation, use the source 1.4 flag:
Javac-source 1.4 Test. java
To enable assertions at run time, you can use-enableassertions or-ea flag.
To disable assertions during running, use the-da or-disableassertions flag.
To enable assertions in the system class, you can use the-esa or-dsa tag. You can also enable or disable Assertion Based on the package.
You can place assertions on any location that is not expected to arrive normally. Assertions can be used to verify parameters passed to private methods. However, assertions should not be used to verify the parameters passed to the public method, because public methods must check their parameters whether or not assertions are enabled. However, you can use assertions to test the post-condition either in a public method or in a non-public method. In addition, assertions should not change the state of the program in any way.
Assert () function usage
The assert macro prototype is defined in <assert. h>. Its function is to terminate program execution if its condition returns an error. The prototype definition is as follows:
# Include <assert. h>
Void assert (int exprEssion );
The role of assert is to calculate the expression exprEssion. If its value is false (that is, 0), It prints an error message to stderr first,
Then, terminate the program by calling abort.
See the following program list badptr. c:
# Include <stdio. h>
# Include <assert. h>
# Include <stdlib. h>
Int main (void)
{
FILE * fp;
Fp = fopen ("test.txt", "w"); // open a file in writable mode. If it does not exist, create a file with the same name.
Assert (fp); // so there will be no error
Fclose (fp );
Fp = fopen ("noexitfile.txt", "r"); // open a file in read-only mode. If it does not exist, the file fails to be opened.
Assert (fp); // an error occurs here
Fclose (fp); // the program will never be executed here
Return 0;
}
[Root @ localhost error_process] # gcc badptr. c
[Root @ localhost error_process] #./a. out
A. out: badptr. c: 14: main: Assertion 'fp' failed.
Abandoned
The disadvantage of using assert is that frequent calls will greatly affect program performance and increase additional overhead.
After debugging, you can disable the assert call by inserting # define NDEBUG before the # include <assert. h> statement. The sample code is as follows:
# Include <stdio. h>
# Define NDEBUG
# Include <assert. h>
Usage summary and precautions:
1) Check the validity of input parameters at the beginning of the Function
For example:
Int resetBufferSize (int nNewSize)
{
// Function: Change the buffer size,
// Parameter: New length of the nNewSize Buffer
// Return value: Current Buffer Length
// Note: keep the original information unchanged. nNewSize <= 0 indicates clearing the buffer.
Assert (nNewSize> = 0 );
Assert (nNewSize <= MAX_BUFFER_SIZE );
...
}
2) Each assert only tests one condition, because when multiple conditions are verified simultaneously, if the assert fails, it is impossible to intuitively determine which condition fails.
Bad: assert (nOffset> = 0 & nOffset + nSize <= m_nInfomationSize );
Good: assert (nOffset> = 0 );
Assert (nOffset + nSize <= m_nInfomationSize );
3) You cannot use statements that change the environment, because assert takes effect only on DEBUG. If so, problems will occur when the program is actually running.
Error: assert (I ++ <100)
This is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed.
Correct: assert (I <100)
I ++;
4) assert and the following statement should be empty for logical and visual consistency.
5) In some cases, assert cannot replace conditional filtering.
Form: http://luzihai1111.blog.163.com/blog/static/12622981120097313222227/