Skip to main content

Decision testing with MS Copilot

Task 1 (student 1) 

Using MS Copilot  identify statements for the RegulaFalsi function. Assign a tag to for each identified statement.  Present the list of statements with tags. Write the prompt and the chatbot answer. Discuss the result. 

 Does it properly recognized that the FOR instruction consists of 3 statements?  Did it handle properly nested IFs?

  • Function Declaration - Tag: Definition

  • Variable Initialization - Tag: Initialization

  • FOR Loop Components:

    • Loop Initialization - Tag: Loop Setup

    • Loop Condition - Tag: Loop Condition

    • Loop Update - Tag: Loop Update

  • Nested IF Statements:

    • Outer IF Condition - Tag: Decision Making

    • Inner IF Condition - Tag: Decision Making

    • Inner ELSE Block - Tag: Alternative Path

    • Outer ELSE Block - Tag: Alternative Path

  • Mathematical Computation - Tag: Calculation

  • Return Statement - Tag: Output
    The FOR loop correctly consists of three statements: initialization, condition, and update. If the AI properly recognizes this, it demonstrates an understanding of structured loops.Nested IFs are tricky because they involve conditional branching within another condition. Proper handling requires ensuring the logical flow of execution remains intact, and the AI must not misinterpret the nested structure.

 Task 2 (student 2) 

 Using MS Copilot generate the Control Flow Graph in the DOT language for the RegulaFalsi function. Write the prompt and the chatbot answer. Discuss the result.  

 Does the CFG has the same structure as the CFG created by a human in the previous section of the book? 

 When the Task 1 is finished, use the generated instruction list with tags as the hint for Copilot. Did it help them co correct the CFG (now the CFG should be correct)? Discuss the result. 

digraph RegulaFalsi_CFG {
    node [shape=rectangle];

    start;
    readInputs [label="Read inputs: a, b, tol, max_iter"];
    computeFaFb [label="Compute fa = f(a), fb = f(b)"];
    checkRootInterval [label="if (fa * fb >= 0)"];
    returnNoRoot [label="return -1"];
    loopInit [label="for (int i = 0; i < max_iter; ++i)"];
    computeC [label="Compute c = (a * fb - b * fa) / (fb - fa)"];
    computeFc [label="Compute fc = f(c)"];
    checkTolerance [label="if (fabs(fc) < tol)"];
    returnRoot [label="return c"];
    checkSign [label="if (fa * fc < 0)"];
    updateB [label="b = c; fb = fc"];
    updateA [label="a = c; fa = fc"];
    returnMaxIter [label="return -1"];

    start -> readInputs;
    readInputs -> computeFaFb;
    computeFaFb -> checkRootInterval;
    checkRootInterval -> returnNoRoot [label="True"];
    checkRootInterval -> loopInit [label="False"];
    loopInit -> computeC [label="True"];
    computeC -> computeFc;
    computeFc -> checkTolerance;
    checkTolerance -> returnRoot [label="True"];
    checkTolerance -> checkSign [label="False"];
    checkSign -> updateB [label="True"];
    checkSign -> updateA [label="False"];
    updateB -> loopInit;
    updateA -> loopInit;
    loopInit -> returnMaxIter [label="False"];
}

image.png

 Task 3. (student 3) 

Convince Copilot to write a decision test for the function RegulaFalsi in the C language (I think you need to explain to the chatbot what a decision test is). Write the prompt and the chatbot answer. Discuss the result, compare it with the code written by a human in the previous section of the book.

Prompt:

create a decision test for a function implementing regula falsi algorithm written in C, the decision test must cover 100% of decisions in the algorithm

Result:

To achieve 100% decision coverage in testing a Regula Falsi implementation, we need to ensure that all branching conditions in the algorithm are exercised. This includes:

  • Cases where the root exists within the interval.

  • Scenarios where the function values at endpoints do not bracket a root.

  • Convergence criteria being met or hitting iteration limits.

  • Handling edge cases like exact root discovery and numerical precision issues.

Example Decision Test for Regula Falsi

Below is a comprehensive test suite covering these cases.

c
#include <stdio.h>
#include <math.h>

// Function prototype for Regula Falsi
double regulaFalsi(double (*func)(double), double a, double b, double tol, int max_iter);

// Sample function: f(x) = x^3 - x - 2
double func(double x) {
    return x * x * x - x - 2;
}

// Test suite covering all decisions
void testRegulaFalsi() {
    // Case 1: Normal case (root exists within interval)
    double root1 = regulaFalsi(func, 1, 2, 1e-6, 100);
    if (fabs(root1 - 1.521) < 1e-3)
        printf("Test Case 1 Passed: Root found correctly\n");
    else
        printf("Test Case 1 Failed: Incorrect root\n");

    // Case 2: Function values do not bracket a root
    double root2 = regulaFalsi(func, 2, 3, 1e-6, 100);
    if (isnan(root2))
        printf("Test Case 2 Passed: Correctly handled non-bracketing case\n");
    else
        printf("Test Case 2 Failed: Incorrect handling\n");

    // Case 3: Convergence criteria met (early termination)
    double root3 = regulaFalsi(func, 1, 2, 1e-12, 10);
    if (fabs(root3 - 1.521) < 1e-6)
        printf("Test Case 3 Passed: Converged within tolerance\n");
    else
        printf("Test Case 3 Failed: Did not converge correctly\n");

    // Case 4: Iteration limit reached
    double root4 = regulaFalsi(func, 1, 2, 1e-12, 2);
    if (fabs(root4 - 1.521) > 1e-3)
        printf("Test Case 4 Passed: Correctly stopped at iteration limit\n");
    else
        printf("Test Case 4 Failed: Incorrect iteration handling\n");
}

int main() {
    testRegulaFalsi();
    return 0;
}

What This Covers

  • Case 1: Standard scenario where a root is correctly found.

  • Case 2: Interval does not bracket a root, ensuring proper error handling.

  • Case 3: Convergence criteria ensures termination upon reaching tolerance.

  • Case 4: Algorithm correctly stops at the iteration limit without unnecessary processing.

This test suite guarantees that all decision paths in the algorithm are exercised, ensuring robustness.

Would you like to incorporate additional error-handling scenarios or numerical precision checks?