ASSERT statement

Start of changeThe ASSERT statement asserts whether a condition is true or false, compares two values and determines if they are equal, or asserts whether a statement should be executed or not.End of change

Read syntax diagramSkip visual syntax diagram
>>-ASSERT------------------------------------------------------->

>--+-TRUE--(--test-expression--)----------------------+-+--------------------------------+-><
   +-FALSE--(--test-expression--)---------------------+ '-TEXT--(--display-expression--)-'   
   +-COMPARE--(--actual_exp--,expected_exp--+----+--)-+                                      
   |                                        '-,z-'    |                                      
   '-UNREACHABLE--------------------------------------'                                      

TRUE(test-expression)
Asserts that test-expression is true when one or more bits in test-expression have the value '1'B.
FALSE(test-expression)
Asserts that test-expression is false when all the bits in test-expression have the value '0'B.
Start of changeCOMPARE(actual_exp,expected_exp,z)End of change
Start of changeAsserts that the value of actual_exp is equal to the value of expected_exp. z is an optional argument which names an operator.End of change
TEXT(display-expression)
Passes the display-expression to the assertion routine if the assert fails.
UNREACHABLE
Asserts that the statement cannot be reached, because it is bypassed by a proceeding statement, such as a GOTO, RETURN, or SIGNAL statement.
display-expression
A scalar CHARACTER expression.
test-expression
A computational scalar expression that is to be, if necessary, converted to BIT.
Start of changeactual_expEnd of change
Start of changeA computational expression that is evaluated and possibly converted. It must be a scalar expression and must have the same type as expected_exp.End of change
Start of changeexpected_expEnd of change
Start of changeA computational expression that is evaluated and possibly converted. It must be a scalar expression and must have the same type as actual_exp.End of change
Start of changezEnd of change
Start of changeA CHAR(2) constant. When uppercased, the constant must have one of these values: EQ, LE, LT, GT, GE, or NE. If you do not specify z, EQ is the default value.
EQ
Equal to
LE
Less than or equal to
LT
Less than
GT
Greater than
GE
Greater than or equal to
NE
Not equal to
End of change
Start of changeIf the assertion fails, compiled code calls routine IBMPASU for the ASSERT UNREACHABLE statement, IBMPAST for the ASSERT TRUE and FALSE statements, and IBMPASC for the ASSERT COMPARE statement. These routines must use the OPTLINK linkage.
Note:
  • Under the ASSERT(CONDITION) compiler option, the ASSERTION condition will be raised.
  • The compiled code calls the routines IBMPASU/IBMPAST/IBMPASC, if ASSERT(ENTRY) compiler option is used.
End of change
Start of changeCompiled code calls the IBMPASU and IBMPAST routines with the following BYVALUE parameters: End of change
Start of change Compiled code calls the IBMPASC routine for the ASSERT COMPARE statement with the following BYVALUE parameters: End of change
Start of changeThe strings representing the actual and expected expressions depend on the type of those expressions. If the commands have: End of change

Example: The usage of the ASSERT TRUE, ASSERT FALSE and ASSERT UNREACHABLE statements

Start of changeThe following example shows the usage of the ASSERT TRUE, ASSERT FALSE and ASSERT UNREACHABLE statements. You must code the routines that are used in this example. End of change

asserts: package;

	main: proc options(main);

		dcl n fixed bin;

		n = 1;
		assert true( n> 0 );
		assert true( n= 2 ) text('n not equal to 2');
		assert unreachable;

	end;

	ibmpasu:
		proc( packagename_ptr, procname_ptr, assert_sourceline,
		 	   text_addr, text_length )
		ext( '_IBMPASU')
		options( byvalue linkage(optlink) );

		dcl packagename_ptr   pointer;
		dcl procname_ptr      pointer;
		dcl assert_sourceline fixed bin(31);
		dcl text_addr         pointer;
		dcl text_length       fixed bin(31);

		dcl assert_packagename char(100) var based(packagename_ptr);
		dcl assert_procname char(100) var based(procname_ptr);
		dcl assert_text char(text_length) based(text_addr);

		put skip edit( 'unreachable code hit on line ',
			               trim(assert_sourceline),
	            		   ' in ',
			               assert_packagename,
			               ':', assert_procname )
			           ( a );
		if text_length = 0 then;
		else
		   put skip list( assert_text );
	end;    

  ibmpast:
		proc( packagename_ptr, procname_ptr, assert_sourceline,
		      text_addr, text_length )
		ext( '_IBMPAST')
		options( byvalue linkage(optlink) );

		dcl packagename_ptr   pointer;
		dcl procname_ptr      pointer;
		dcl assert_sourceline fixed bin(31);
		dcl text_addr         pointer;
		dcl text_length       fixed bin(31);

		dcl assert_packagename char(100) var based(packagename_ptr);
		dcl assert_procname char(100) var based(procname_ptr);
		dcl assert_text char(text_length) based(text_addr);


		put skip edit( 'conditional assertion failed on line ',
			               trim(assert_sourceline),
			               ' in ',
			                assert_packagename,
			                ':', assert_procname )
			              ( a );
		if text_length = 0 then;
		else
		   put skip list( assert_text );
	end;

Start of changeThe following example shows the usage of the ASSERT COMPARE statement. You must code the routines that are used in this example.End of change

Start of change

Example: The usage of the ASSERT COMPARE statement

asserts: package;                                                             
                                                                                
   main: proc options(main);                                                    
                                                                                
     dcl n fixed bin;                                                           
                                                                                
     n = 1;                                                                     
     assert compare(n,1);                                                       
     assert compare(n,2) text("n not equal to 2");                                
     assert unreachable;                                                        
   end;                                                                         
                                                                                
   ibmpasc:                                                                     
    proc( packagename_ptr, procname_ptr, assert_sourceline,                     
    actual_addr, actual_length,                                                 
    expected_addr, expected_length,                                             
    text_addr, text_length )                                                    
    ext( '_IBMPASC')                                                            
    options( byvalue linkage(optlink) );                                        
                                                                                
    dcl packagename_ptr   pointer;                                              
    dcl procname_ptr      pointer;                                              
    dcl assert_sourceline fixed bin(31);                                        
    dcl actual_addr       pointer;                                              
    dcl actual_length     fixed bin(31);                                        
    dcl expected_addr     pointer;                                              
    dcl expected_length   fixed bin(31);                                        
    dcl text_addr         pointer;                                              
    dcl text_length       fixed bin(31);                                        
                                                                                
    dcl assert_packagename char(100) var based(packagename_ptr);                
    dcl assert_procname char(100) var based(procname_ptr);                      
    dcl assert_text char(text_length) based(text_addr);                         
    dcl actual_text char(actual_length) based(actual_addr);                     
    dcl expected_text char(expected_length)                                     
                                        based(expected_addr);                   
                                                                                
    put skip edit( 'compare code hit on line ',                                 
                   trim(assert_sourceline),                                     
                   ' in ',                                                      
                   assert_packagename,                                          
                   ':', assert_procname )                                       
              ( a );                                                            
                                                                                
    if text_length = 0 then;                                                    
    else                                                                        
       put skip list( assert_text );                                            
                                                                                
    if actual_length = 0 then;                                                  
    else                                                                        
       put skip list( actual_text );                                            
                                                                                
    if expected_length = 0 then;                                                
    else                                                                        
       put skip list( expected_text );                                          
                                                                                
  end;                    
End of change
Start of changeIf the assertion fails and the ASSERT(CONDITION) compiler option is in effect, the ASSERTION condition will be raised with an appropriate message and the values for the new ONTEXT, ONPACKAGE, ONACTUAL and ONEXPECTED built-in functions.
Note: Start of change
  • The ASSERTION condition is always enabled.
  • The implicit action for the ASSERTION condition is that a message is printed and the ERROR condition is raised.
  • The normal return for the ASSERTION condition is that execution continues with the next statement.
  • The ONCODEs associated with the ASSERTION condition are:
    Start of change
    Condition code Meaning
    430 SIGNAL ASSERTION
    431 An ASSERT TRUE/FALSE statement without a TEXT clause failed
    432 An ASSERT TRUE/FALSE statement with a TEXT clause failed
    433 An ASSERT UNREACHABLE statement without a TEXT clause failed
    434 An ASSERT UNREACHABLE statement with a TEXT clause failed
    435 An ASSERT COMPARE statement without a TEXT clause failed
    436 An ASSERT COMPARE statement with a TEXT clause failed
    End of change
End of change
End of change