Tips and Tricks for the ABAP® Programming Language
Small random number generator for test data generation
A small random number generator can be quite helpful for the construction of test data (there are certainly also function modules which covers this topic). If you start the report with the default parameters, 100 “draws with put back” are carried out for different data types. After a sorting the results can be judged visually. If the P_Test parameter input was ‘N’, a small test volume for the respective data types is generated.
The parameters:
- P_Anzahl : Number of the loop cycles for random number generation.
- PDAT_C05 : For a C field (extend 5 bytes) 5 letters will be generated.
- PDAT_C30 : For a C field (extend 30 Bytes) 25 letters will be generated.
- PDAT_I04 : The content of an I field is filled with 4 digits.
- PDAT_P10 : A P field (extend 15 Bytes) is loaded with 20 digits.
- P_SORT13 : Different insights into the sequence and dispersion of the generated values can be gained by a sorting key (1, 2 or 3) (no attempt to prove something mathematically ).
- P_TEST : In the case of ‘N’, an overview of the generated values can be given per data type. I- and P-fields can contain a ‘0’ at the leading locations . C-fields can contain several blanks. In the case of ‘J’, a summary of the single values is generated (letters, digits, blank characters) to judge the “draw results”.
The subprogram ERZEUGEN-PSEUDOZUFALL is called through e.g. “PERFORM ERZEUGEN-PSEUDOZUFALL USING target field anzahl_zeichen x”.
If letter should be generated, an ‘A’ had to be inserted for the x, and in the case of digits, a ‘N’ had to be used.
By using this routine in your program you can create test data for the data types C, N, I and P in a simple manner. Through generation of a 8 number-digit and addition/subtraction to an D field with today’s date valid dates can also be generated .The same is correspondingly valid for a 6 digit T-field.
ABAP™-Source-Code
You can copy and paste the source code directly into the ABAP™-Workbench.
REPORT Y9030010 LINE-SIZE 130. "Release 3.1G, 4.5A ************************************************************************ * Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com * * You can use or modify this report for your own work as long * as you don't try to sell or republish it. * In no event will the author be liable for indirect, special, * Incidental, or consequental damages (if any) arising out of * the use of this report. * *//////////////////////////////////////////////////////////////////////* MOVE: 'Example to create test data within an internal table itab' TO SY-TITLE. *//////////////////////////////////////////////////////////////////////* *************** Parameter ********************** PARAMETERS: P_ANZAHL(4) TYPE N DEFAULT '0010', PDAT_C05(1) TYPE C DEFAULT 'J', PDAT_C30(1) TYPE C DEFAULT 'J', PDAT_I04(1) TYPE C DEFAULT 'J', PDAT_P10(1) TYPE C DEFAULT 'J', P_SORT13(1) TYPE N DEFAULT '1', "only 1, 2, 3 P_TEST(1) TYPE C DEFAULT 'N'. *************** Internal tables ********************** DATA: BEGIN OF ITAB1 OCCURS 100, CFELD5(5) TYPE C, CFELD(30) TYPE C, IFELD4 TYPE I, PFELD10(15) TYPE P, END OF ITAB1. * DATA: BEGIN OF ITAB2 OCCURS 100, LFDIND TYPE I, ZUFALL TYPE F, E_RAND TYPE F, ABC(1) TYPE C, END OF ITAB2. * *************** Declaration of variables ********************** DATA: E_RAND TYPE F. *//////////////////////////////////////////////////////////////////////* ************* Main Section ******************* *//////////////////////////////////////////////////////////////////////* * PERFORM GENERATE-ZUFALL. CASE P_SORT13. WHEN '1'. SORT ITAB2 ASCENDING BY ABC. "1 WHEN '2'. SORT ITAB2 ASCENDING BY ZUFALL. "2 WHEN '3'. SORT ITAB2 ASCENDING BY LFDIND. "3 WHEN OTHERS. SORT ITAB2 ASCENDING BY ABC. "1 ENDCASE. * IF P_TEST EQ 'J'. PERFORM DISPLAY-ITAB21. "test ELSE. PERFORM DISPLAY-ITAB1. ENDIF. * *//////////////////////////////////////////////////////////////////////* ************* Subroutines ******************* *//////////////////////////////////////////////////////////////////////* ************************************************************************ * Generation of table contents * ************************************************************************ FORM GENERATE-ZUFALL. * *BREAK-POINT 111. WRITE: /1 'Creating of random chars for the test volume' COLOR 5. WRITE: /1 'Number of runs:' COLOR 7, P_ANZAHL. ULINE. SKIP. *....................................................................... GET RUN TIME FIELD E_RAND. "1. Aufruf DO P_ANZAHL TIMES. IF PDAT_C05 EQ 'J'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-CFELD5 5 'A'. ENDIF. * IF PDAT_C30 EQ 'J'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-CFELD 7 'A'. * PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-CFELD 25 'A'. ENDIF. * IF PDAT_I04 EQ 'J'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-IFELD4 4 'N'. "max 9 ENDIF. * IF PDAT_P10 EQ 'J'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-PFELD10 8 'N'. * PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-PFELD10 20 'N'. ENDIF. APPEND ITAB1. * ENDDO. ENDFORM. ************************************************************************ ************************************************************************ * Creating of test data * ************************************************************************ FORM ERZEUGEN-PSEUDOZUFALL USING TARGET LAENGE TYP. DATA: Z_P(16) TYPE P VALUE '999999999999999999996467'. DATA: Z_A(16) TYPE P VALUE '671354420908421773035669'. DATA: Z_C(16) TYPE P. DATA: Z_TIME(31) TYPE N. DATA: Z_ZUFALL TYPE F. * *....................................................................... FIELD-SYMBOLS: <FS1>, <FS2>. DATA: AUSW_NUM(10) TYPE C VALUE '0123456789'. DATA: AUSW_ABC(27) TYPE C VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '. DATA: Z_AUSWAHL(30) TYPE N. DATA: BEGINN TYPE I. DATA: NFELD(30) TYPE N. DATA: CFELD(30) TYPE C. DATA: Z_OFFSET(2) TYPE N. ************************************************************************ IF E_RAND EQ 0. GET RUN TIME FIELD Z_TIME. MOVE SY-UZEIT TO E_RAND. ADD Z_TIME+25(6) TO E_RAND. ADD Z_TIME+26(3) TO E_RAND. ENDIF. ************************************************************************ DO LAENGE TIMES. * E_RAND = ( Z_A * E_RAND + 1 ) MOD Z_P. Z_ZUFALL = E_RAND / Z_P. *....................................................................... *.....corresponding to the fieldlength: Create a random number.......... *....................................................................... IF TYP EQ 'N'. DO LAENGE TIMES. Z_ZUFALL = Z_ZUFALL * 10. ENDDO. MOVE Z_ZUFALL TO Z_AUSWAHL. Z_OFFSET = 29 - LAENGE + 1. ASSIGN Z_AUSWAHL+Z_OFFSET(1) TO <FS2>. "can be directly used IF P_TEST EQ 'J'. "only for test MOVE SY-INDEX TO ITAB2-LFDIND. " MOVE E_RAND TO ITAB2-E_RAND. " MOVE Z_ZUFALL TO ITAB2-ZUFALL. " WRITE <FS2> TO ITAB2-ABC. " APPEND ITAB2. " ELSE. " WRITE <FS2> TO NFELD+BEGINN. ADD 1 TO BEGINN. ENDIF. " ENDIF. ************************************************************************ IF TYP EQ 'A'. MOVE E_RAND TO Z_AUSWAHL. Z_AUSWAHL = Z_AUSWAHL MOD 27. "26 letters and blank ASSIGN AUSW_ABC+Z_AUSWAHL(1) TO <FS1>. IF P_TEST EQ 'J'. "only for test MOVE SY-INDEX TO ITAB2-LFDIND. " MOVE E_RAND TO ITAB2-E_RAND. " MOVE Z_ZUFALL TO ITAB2-ZUFALL. " WRITE <FS1> TO ITAB2-ABC. " APPEND ITAB2. " ELSE. " WRITE <FS1> TO CFELD+BEGINN. ADD 1 TO BEGINN. ENDIF. " ENDIF. *----------------------------------------------------------------------- E_RAND = E_RAND / 10000000000000. E_RAND = E_RAND / 10000000. ENDDO. * IF TYP EQ 'N'. MOVE NFELD TO TARGET. ENDIF. IF TYP EQ 'A'. MOVE CFELD TO TARGET. ENDIF. * ENDFORM. ************************************************************************ * Output of the generated table ************************************************************************ FORM DISPLAY-ITAB1. * MOVE SPACE TO ITAB1. WRITE: /1 'CFELD' COLOR 6, * 15 'CFELD30' COLOR 6, 7 'CFELD30' COLOR 6, 44 'I-FELD' COLOR 6, * 57 'PFELD10' COLOR 6. 76 'PFELD10' COLOR 6. ULINE. SKIP. * LOOP AT ITAB1. WRITE: /1 ITAB1-CFELD5, 7 ITAB1-CFELD, 40 ITAB1-IFELD4, 54 ITAB1-PFELD10. ENDLOOP. ENDFORM. ************************************************************************ * Display letter distribution ************************************************************************ FORM DISPLAY-ITAB21. DATA: Z_DOPPELT TYPE I. DATA: ZE_RAND LIKE ITAB2-E_RAND. DATA: ZE_ABC LIKE ITAB2-ABC. *....................................................................... SKIP. ULINE. WRITE: /1 'The distribution frequency to judge the quality' COLOR 6. ULINE. SKIP. * MOVE SPACE TO ITAB2. LOOP AT ITAB2. IF SY-INDEX EQ 1. MOVE ITAB2-ABC TO ZE_ABC. ELSE. IF ITAB2-ABC EQ ZE_ABC. ADD 1 TO Z_DOPPELT. MOVE ITAB2-ABC TO ZE_ABC. ELSE. IF ZE_ABC CO '0123456789'. WRITE: /1 'Number for digits:', 24 ZE_ABC, 29 Z_DOPPELT. ELSE. WRITE: /1 'Number for letters:', 24 ZE_ABC, 34 Z_DOPPELT. ENDIF. MOVE ITAB2-ABC TO ZE_ABC. CLEAR Z_DOPPELT. ENDIF. ENDIF. ENDLOOP. * IF ZE_ABC CO '0123456789'. WRITE: /1 'Number for digits:', 24 ZE_ABC, 29 Z_DOPPELT. ELSE. WRITE: /1 'Number for letters:', 24 ZE_ABC, 34 Z_DOPPELT. ENDIF. MOVE ITAB2-ABC TO ZE_ABC. ENDFORM. ************************************************************************ ************************************************************************ ******************* END OF PROGRAM *************************************