Code: Alles auswählen.
REPORT zknobel_coverage.
CLASS lcx DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
*--------------------------------------------------------------------*
* Class to test - no changes allowed here
*--------------------------------------------------------------------*
CLASS lcl_coverage_test DEFINITION DEFERRED.
CLASS lcl_test DEFINITION FINAL FRIENDS lcl_coverage_test.
PUBLIC SECTION.
METHODS: constructor IMPORTING iv_seed TYPE i
iv_max TYPE i
iv_count TYPE i
RAISING lcx,
get_odd_max RETURNING VALUE(rv_max) TYPE i,
get_log_sum RETURNING VALUE(rv_sum) TYPE f.
DATA: mo_random TYPE REF TO cl_abap_random,
mt_numbers TYPE STANDARD TABLE OF i WITH NON-UNIQUE DEFAULT KEY,
mt_r_odd TYPE RANGE OF i,
mv_range_is_filled TYPE abap_bool.
ENDCLASS.
*--------------------------------------------------------------------*
* Unit testclass - you may change as much as you like
*--------------------------------------------------------------------*
CLASS lcl_coverage_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PRIVATE SECTION.
METHODS check_coverage FOR TESTING.
ENDCLASS.
CLASS lcl_test IMPLEMENTATION.
METHOD constructor.
IF iv_count < 0.
RAISE EXCEPTION TYPE lcx.
ENDIF.
mo_random = cl_abap_random=>create( iv_seed ).
mv_range_is_filled = abap_false.
APPEND VALUE #( sign = 'I' option = 'EQ' low = 1 ) TO mt_r_odd.
DO iv_count TIMES.
APPEND mo_random->intinrange( low = 0
high = iv_max
) TO mt_numbers ASSIGNING FIELD-SYMBOL(<lv_number>).
IF <lv_number> MOD 2 = 1.
APPEND VALUE #( sign = 'I' option = 'EQ' low = <lv_number> ) TO mt_r_odd.
ENDIF.
ENDDO.
SORT mt_r_odd.
DELETE ADJACENT DUPLICATES FROM mt_r_odd.
IF lines( mt_r_odd ) > 0.
mv_range_is_filled = abap_true.
ENDIF.
ENDMETHOD.
METHOD get_odd_max.
DATA: rr_data TYPE REF TO data.
FIELD-SYMBOLS: <lv_min_i> TYPE i.
rr_data = cl_abap_exceptional_values=>get_min_value( rv_max ).
ASSIGN rr_data->* TO <lv_min_i>.
rv_max = <lv_min_i>.
IF lines( mt_numbers ) = 0
OR mv_range_is_filled = abap_false.
RETURN.
ENDIF.
LOOP AT mt_numbers ASSIGNING FIELD-SYMBOL(<lv_number>) WHERE table_line IN mt_r_odd.
IF <lv_number> > rv_max.
rv_max = <lv_number>.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD get_log_sum.
LOOP AT mt_numbers ASSIGNING FIELD-SYMBOL(<lv_number>).
TRY.
rv_sum = rv_sum + 1 / <lv_number>.
CATCH cx_root.
CONTINUE.
ENDTRY.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
*--------------------------------------------------------------------*
* Unzureichende Unit tests
*--------------------------------------------------------------------*
CLASS lcl_coverage_test IMPLEMENTATION.
METHOD check_coverage.
DATA(lo_test) = NEW lcl_test( iv_seed = 0
iv_max = 10000
iv_count = 10
).
lo_test->get_odd_max( ).
lo_test->get_log_sum( ).
ENDMETHOD.
ENDCLASS.
Folgende Benutzer bedankten sich beim Autor ewx für den Beitrag (Insgesamt 4):
black_adept • a-dead-trousers • Tron • Murdock
Folgende Benutzer bedankten sich beim Autor jocoder für den Beitrag:
a-dead-trousers
Code: Alles auswählen.
* Main program
submit (sy-repid) and return.
Code: Alles auswählen.
TRY.
NEW lcl_test( iv_seed = 0
iv_max = 10
iv_count = - 100
).
CATCH cx_root.
ENDTRY.
Code: Alles auswählen.
* Log Sum --> CATCH-Block
NEW lcl_test( iv_seed = 0
iv_max = 0
iv_count = 1
)->get_log_sum( ).
Code: Alles auswählen.
* get_odd_max: RETURN
NEW lcl_test( iv_seed = 0
iv_max = 10
iv_count = 0
)->get_odd_max( ).
Code: Alles auswählen.
* get_odd_max: --> Body of LOOP
NEW lcl_test( iv_seed = 0
iv_max = 1
iv_count = 10000
)->get_odd_max( ).
Code: Alles auswählen.
* Log Sum --> Do not enter loop
NEW lcl_test( iv_seed = 0
iv_max = 10
iv_count = 0
)->get_log_sum( ).
Code: Alles auswählen.
IF table is not initial. LOOP at TABLE... ohne Bedingung
Folgende Benutzer bedankten sich beim Autor black_adept für den Beitrag (Insgesamt 4):
ewx • a-dead-trousers • qyurryus • Tron