#18 The ASSERT statement

not accepted
Edward Hart

Per Simon's suggestions in the original Contributions thread [7ac18850], here's an updated version of the ASSERT patch.

Note: the attached patch deletes trailing whitespace from the cobc and libcob changelogs.

What is ASSERT?

For the last few weeks, I've been slowly trying to get my head round the GNU COBOL source code, and to help me understand I've been working on adding a new statement to GNU COBOL: ASSERT. Okay, it's another non-standard extension, but I think it will be useful.

There aren't any docs on it because this is just an extension I made up. I just liked C's assert macro and thought it would be neat to implement a COBOL version. I don't expect this to be integrated into GNU COBOL.

ASSERT tests a condition; if it is false, EC-IMP-FAILED-ASSERT is raised, the location of the failed assertion is displayed on stderr and the program terminates. The ASSERT syntax diagram is:

ASSERT conditional-expression [WITH MESSAGE literal-1] [FOR DEBUGGING],

where literal-1 is a string. The MESSAGE clause is used to define a message which will be displayed with the ASSERT location and the DEBUGGING clause specifies that the ASSERT will only be active if the compiler switch ‑fdebug‑assert is present. If -fsource-location is used, FUNCTION EXCEPTION-LOCATION is used to show the location of a failed ASSERT. If not, the displayed location is just the file and line number. Using -debug implicity sets -fsource-location and -fdebug-assert.

There is one known bug: if only the MESSAGE clause is used and the program is compiled without -fsource-location, the assertion's location will be one line further down than it should be.

Why would I want to use ASSERT?

Standard COBOL programming style favours large programs which are difficult to test, which makes modifying them difficult. ASSERT documents assumptions that you make when writing code—things you always expect to be true. Such things might be "a month has 31 days at most", "the name field will not be blank" or "the year will not be greater than 2000". Assertions help when modifying code as it allows you to say with confidence "This change has not broken this module." If you change something and an assertion fails, you then know that you have broken the code and (hopefully) how you have done it. And, when you find a bug, you can use ASSERTs to make sure it never happens again and/or to find the source of the bad data causing it.

An equivalent, standard-conforming copybook

But what if you like assertions, but not non-standard extensions? In that case, COPY comes to the rescue:

           IF NOT ( cond )
               STOP RUN RETURNING 1

Using the above copybook provides the same functionality, while remaining standard-conforming. Here's how it's used:

           COPY assert REPLACING ==cond== BY ==year-num < 2000== 
               ==message== BY =="Help! Year >= 2000!"==

For assertions which should only be used for debugging, use the following copybook and >>DEFINE debug-assert to activate:

       >>IF debug-assert IS DEFINED
           IF NOT ( cond )
               STOP RUN RETURNING 1
1 Attachments


Discussion: 7ac18850


  • Simon Sobisch

    Simon Sobisch - 2014-08-28
    • Group: Test --> not accepted
  • Simon Sobisch

    Simon Sobisch - 2014-08-28

    Just to add the information from the referenced Discussion:

    First: the patch is very well done.
    The bad news: even if the patch is that good I'm not positive to integrate it for now as there is a non-standard (even in view of "other vendors") extension that can be coded in standard ways already (with not much more code) - as the alternate COPY shows.


  • Edward Hart

    Edward Hart - 2015-04-12

    I'd like to re-open this ticket as I've found out that ASSERT has actually been implemented! In particular, Elastic COBOL has an ASSERT statement. It is defined on page 300 of their language reference:*

    The ASSERT statement asserts that a condition is always true. An ASSERT is useful for debugging programs and asserting that they behave as intended. It is enabled only when compiling with the -assert flag, so normally the ASSERT does nothing, not degrading the speed or size of the program. Liberal usage of ASSERT is encouraged so when a problem does arise, the program may be compiled with -assert to help discover if anything which should always be true in fact is false.

    General format
    ASSERT condition-1 [AS display-parameter-1]

    Syntax rules
    1. condition-1 is a condition which should always be true. If it is false upon execution when compiled with -assert, an exception is thrown to be made visible to the user.
    2. Display-parameter-1 is the same as the parameter to a simple DISPLAY, and it may contain references to any identifiers mentioned in condition-1 among other items. Its message will be visible and should help in diagnosing the problem.

    • You should give that reference a read---the sheer number of extensions makes Micro Focus' reference look lean and simple.
    Last edit: Edward Hart 2015-04-12
  • Brian Tiffin

    Brian Tiffin - 2015-09-06

    I like this feature. Voting to accept.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks