Wiki:
In computer programming,AssertionIs a predicate (a true-false statement) placed in a program to indicate that the developerThinksThat the predicate is always true at that place. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort.
In programming,Yan(Assertion) Is a typeOne worker(For exampleA true or false legal statement), In orderResult of the verification code development cycle-When the program runs to the statement location, the corresponding statement should be true. If the statement is not true, the program will abort the statement and generate a warning message.
Programmers can use assertions 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 programmer expects the code to execute. A postcondition-placed at the end-describes the expected state at the end of execution.
Programmers can use the statementIndicator Program, Provide programsPositive. For example, before a program (Configure a condition first), Which indicates the programPre-renewal. Or add the statement (Subsequent Condition), Which indicates the programResult of the expiration period after the row is renewed.
In versions ages such as Eiffel, assertions form part of the design process; other versions ages, such as C and Java, use them only to check assumptions at runtime. in both cases, they can be checked for validity at runtime but can usually also be suppressed.
When developing an Eiffel statement program, the statement is a part of the design process. Program languages such as C-producer and Java are mainly used inSeasonal releaseCheck whether the statement is correct. You can also useYan XiaoyanInRenewal periodCheck your statement. In either case, you can check whether the statement is valid or not.About the capabilities of the website threat inspector.
Baidu Encyclopedia:
1. Programming assertions:
When writing code, we always make assumptions that assertions are used
Capture these assumptions in the code. Assertions are expressed as some
Boolean expressionThe programmer believes that the expression value is true at a specific point in the program and can enable and disable Assertion Verification at any time.
Enable assertions during testing and disable assertions during deployment. Similarly, after the program is put into operation, the end user can re-enable the assertion in case of problems. Using assertions, you can create code that is more stable, has better quality, and is not prone to errors. You can use assertions to interrupt the current operation when the value is false.
Unit tests must use assertions (JUnit/junitx). Besides
Type check and unit testAssertions also provide an excellent way to determine whether various features are maintained in the program. Use assertions
Contractual DesignA step closer.
2. assertion features:
Precondition assertion: Features required before Code Execution
Post-condition assertions: Features required after Code Execution
Unchanged assertion: Unchangeable features before and after Code Execution
3. assertion method:
- Assert expression1
- Assert expression1: expression2
Expression1 should always be one
Boolean ValueExpression2 is the failure message output when the assertion fails.
String. If expression1 is false, an assertionerror is thrown.
ErrorInstead of an exception, that is, an unchecked exception. assertionerror is not captured because it is an error, this will make your system unstable.
4. Usage:1. Do not use assertion
Common method parameter checkAnd the parameters of common methods always need to be executed. 2. assertion statement
It cannot have any boundary effect.Do not use assertion statements to modify variables or change the return values of methods.
Where the program will not arrivePlace assertions: assert false 2. assertions can be used
Check the parameters passed to the private Method. (For public methods, because they are provided to external interfaces, there must be corresponding parameter tests in the methods to ensure code robustness.) 3. Execute
Prerequisites and prerequisites4. Use assertions
Check the unchanged status of the classMake sure that the status of a variable is met in any situation. (For example, the age attribute must be greater than 0 and smaller than a proper value)
Supplement:
Several principles of assertions are used:
- Use assertions to capture situations that should not occur. Do not confuse the differences between illegal and wrong situations. The latter must exist and be handled.
- Use assertions to confirm the function parameters.
- When writing a function, you need to repeat it and ask yourself: "What assumptions do I plan to make ?" Once the assumptions are determined, we need to use assertions to check the assumptions.
- In general, textbooks encourage programmers to design error-proof programs, but remember that this programming style will conceal errors. When programming against errors, if "impossible to happen" does happen, Use assertions to trigger an alarm.
Programs are generally divided
Debug and release versionsThe debug version is used for internal debugging, And the release version is released to users.
Assert is a macro that only works in the debug version.It is used to check the situation where "should not" occurs.
Usage:
Assert macroThe prototype is defined in
Assert. hThe function is to terminate the program execution if its condition returns an error.
assert()Is a diagnostic macro used to dynamically identify the logical error conditions of a program. Its prototype is:
1 #include "assert.h" 2 void assert( int expression );
If the macro parameter evaluate result is a non-zero value, no action is performed. If it is a zero value, print it with a wide character.Diagnose messagesAnd then callabort(). Diagnostic messages include:
- Source file name (in
stdlib.hMacro declared in__FILE__Value)
- The row number of the source file (in
stdlib.hMacro declared in__LINE__Value)
- Function Name (macro declared in stdlib. h)
__func__Value), which is a new feature of c99.
- Expression in which the result is 0
The display target of diagnostic information depends onType of the called Program. If yesConsole ProgramThe diagnostic information is displayed inStderr Device; If yesWindow-based program,assert()GenerateWindows MessageBoxTo display the diagnosis information.
The disadvantage of using assert is that frequent calls will greatly affect program performance and increase additional overhead.
After debugging, you can insert a statement that contains # include# Define ndebug to disable assert callsThe sample code is as follows:
1 #include 2 #define NDEBUG 3 #include
Assertion (software development) -- assertion