Exercises: Unit Testing COBOL

Exercise 34.1: Your First COBOL-Check Test Suite (Beginner)

Write a COBOL program called GRADE-CALC that accepts a numeric score (0-100) and assigns a letter grade:

  • 90-100: A
  • 80-89: B
  • 70-79: C
  • 60-69: D
  • Below 60: F

Then write a COBOL-Check test suite (.cut file) that tests: 1. A score in the middle of each grade range (95, 85, 75, 65, 50) 2. Each boundary value (90, 89, 80, 79, 70, 69, 60, 59) 3. The extremes (0 and 100) 4. An invalid score (negative number, if your PIC allows it)

Deliverables: GRADE-CALC.cbl and GRADE-CALC.cut

Exercise 34.2: Test Data Generator (Beginner)

Write a COBOL program that generates test records for a simple employee file with the following layout:

01  EMPLOYEE-REC.
    05  EMP-ID          PIC 9(6).
    05  EMP-NAME        PIC X(30).
    05  EMP-DEPT        PIC X(4).
    05  EMP-SALARY      PIC 9(7)V99.
    05  EMP-HIRE-DATE   PIC 9(8).
    05  EMP-STATUS      PIC X.
        88 EMP-ACTIVE   VALUE "A".
        88 EMP-INACTIVE VALUE "I".
        88 EMP-LEAVE    VALUE "L".

Generate at least 10 records covering: - Active, inactive, and on-leave employees - Minimum salary (0.00) and maximum salary (9999999.99) - Various departments - Hire dates in the past, present year, and future (if that's an error condition)

Exercise 34.3: Stubbing External Calls (Intermediate)

Given the following program fragment that calls an external validation subprogram:

       3000-VALIDATE-ORDER.
           CALL "CUST-VALIDATE" USING WS-CUST-ID
                                      WS-VALID-FLAG
                                      WS-CREDIT-LIMIT
           IF WS-VALID-FLAG = "Y"
               IF WS-ORDER-TOTAL > WS-CREDIT-LIMIT
                   MOVE "OVER-LIMIT" TO WS-ORDER-STATUS
               ELSE
                   MOVE "APPROVED" TO WS-ORDER-STATUS
               END-IF
           ELSE
               MOVE "INVALID-CUST" TO WS-ORDER-STATUS
           END-IF.
  1. Write a stub program for CUST-VALIDATE that always returns valid with a credit limit of $5,000.
  2. Write another stub that always returns invalid.
  3. Write test cases that use each stub to test all three paths through 3000-VALIDATE-ORDER.

Exercise 34.4: Decision Table Test Design (Intermediate)

A loan approval program uses these rules:

Condition
Credit score >= 700 Y Y Y N N N
Debt-to-income < 43% Y Y N Y N N
Loan amount < $500K Y N Y Y Y N
Action APPROVE REVIEW DENY REVIEW DENY DENY

Write COBOL-Check test cases for all six combinations. Then identify any missing combinations from the full truth table (2^3 = 8 possible combinations) and write tests for those too.

Exercise 34.5: TDD for Date Validation (Intermediate)

Using TDD (write the test first, then the code):

  1. Write COBOL-Check tests for a paragraph 5000-VALIDATE-DATE that validates a date in YYYYMMDD format. Tests should cover: - Valid dates (20251015, 20240229) - Invalid month (20251300) - Invalid day (20250132) - February 29 in a non-leap year (20250229) - February 29 in a leap year (20240229) - Invalid year (00001015)

  2. Run the tests (they should all fail).

  3. Write the 5000-VALIDATE-DATE paragraph.
  4. Run the tests again until they all pass.
  5. Refactor if needed.

Document: Write a brief narrative of your TDD experience — what surprised you? What did TDD force you to think about that you might have missed otherwise?

Exercise 34.6: Regression Test Suite (Advanced)

Build a complete regression test suite for a payroll calculation program. The program must handle:

  • Hourly employees (regular and overtime at 1.5x after 40 hours)
  • Salaried employees (fixed biweekly amount)
  • Federal tax withholding (use simplified brackets: 10% on first $10K, 22% on $10K-$40K, 32% above $40K — annualized)
  • Social Security (6.2% up to $160,200 cap)
  • Medicare (1.45%, no cap)

Write at least 20 test cases covering: - Normal calculations for both employee types - Overtime boundary (exactly 40 hours, 40.01 hours) - Tax bracket boundaries - Social Security cap (employee near and at the annual limit) - Zero hours worked - Maximum possible values

Exercise 34.7: Code Coverage Analysis (Advanced)

Take any COBOL program you have written for a previous chapter's exercise. Write a test suite for it, then manually analyze coverage:

  1. List every paragraph in the program.
  2. For each paragraph, mark whether your test suite exercises it.
  3. For each EVALUATE or IF statement, list all branches and mark which ones are tested.
  4. Calculate your statement coverage percentage and branch coverage percentage.
  5. Write additional tests to bring branch coverage above 80%.

Exercise 34.8: Custom Test Harness (Advanced)

Build a reusable custom test harness program following the pattern from Section 34.12. Your harness should include:

  1. An ASSERT-EQUAL paragraph for numeric comparisons (with tolerance)
  2. An ASSERT-STRING-EQUAL paragraph for alphanumeric comparisons
  3. An ASSERT-TRUE paragraph for 88-level conditions
  4. An ASSERT-NOT-EQUAL paragraph
  5. A REPORT-RESULTS paragraph that displays pass/fail counts
  6. A RETURN-CODE that is non-zero if any test failed

Test your harness by writing at least five tests for a calculation program of your choice.