I am trying to reverse given string in COBOL.
IBM documentation
I refered to IBM documentation here: https://www.ibm.com/docs/en/cobol-zos/6.1?topic=functions-transforming-reverse-order-reverse
This is my code which expects to take string and return its reversed version:
identification division.
program-id. solution.
data division.
linkage section.
01 str.
05 len pic 99.
05 chars pic a(30).
01 result.
05 len pic 99.
05 chars pic a(30).
procedure division using str result.
move function reverse(str) to result.
goback.
end program solution.
Outcome from the test (example) - taking string "hello", return nothing
Testing "hello"
Expected and actual have different lengths
expected: 5
actual:
TEST
This is test I am using to verify my function:
identification division.
program-id. tests.
data division.
working-storage section.
01 a-disp pic z(7)9.
01 b-disp pic z(7)9.
01 expected.
05 len pic 99.
05 chars pic a(30).
01 str.
05 len pic 99.
05 chars pic a(20).
01 result.
05 len pic 99.
05 chars pic a(30).
procedure division.
testsuite 'Fixed Tests.'.
move 'world' to chars of str
move length of 'world' to len of str
move 'dlrow' to chars of expected
move length of 'dlrow' to len of expected
perform dotest
move 'hello' to chars of str
move length of 'hello' to len of str
move 'olleh' to chars of expected
move length of 'olleh' to len of expected
perform dotest
move 0 to len of str
move 0 to len of expected
perform dotest
move 'h' to chars of str
move length of 'h' to len of str
move 'h' to chars of expected
move length of 'h' to len of expected
perform dotest
end tests.
dotest.
testcase 'Testing ' '"' chars of str(1:len of str) '"'.
call 'solution' using str result
evaluate len of result
when <> len of expected
move len of result to a-disp
move len of expected to b-disp
initialize assertion-message
string 'Expected and actual have different lengths'
line-feed
'expected: ' function trim(b-disp)
line-feed
'actual: ' function trim(a-disp)
into assertion-message
perform assert-false
when 0
initialize assertion-message
perform assert-true
when other
expect chars of result(1:len of result)
to be
chars of expected(1:len of expected).
end-evaluate
.
end program tests.
Of course this doesn't work. FUNCTION REVERSE
takes a "string", not a group with length and "string".
Either use FUNCTION REVERSE (str(1:len))
or truncate by FUNCTION TRIM
instead of passing a length, simply coding FUNCTION REVERSE (FUNCTION TRIM (str))
.