Decision testing with Chat GPT
You can use any other Chat Bot. If you do so, change the page title.
Task 1 (student 1)
Using a Chat Bot 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?
"Identify statements for the RegulaFalsi function. Assign a tag to for each identified statement. Present the list of statements with tags"
Tag | Statement Description |
---|---|
INIT_INPUTS |
Read or set the initial values of a , b , tolerance, and max iterations. |
CHECK_VALIDITY |
Check if f(a) and f(b) have opposite signs (i.e., f(a) * f(b) < 0 ). |
ITER_LOOP_START |
Start the iteration loop (usually a for or while loop). |
CALC_C |
Calculate the new approximation c using: c=af(b)−bf(a)f(b)−f(a)c = \frac{a f(b) - b f(a)}{f(b) - f(a)} |
CALC_FC |
Compute f(c) to check for root or next interval. |
CHECK_CONVERGENCE |
Check if f(c) is close enough to zero or if the interval is smaller than tolerance. |
UPDATE_INTERVAL |
If f(c) * f(a) < 0 , set b = c ; else if f(c) * f(b) < 0 , set a = c . |
INCREMENT_ITER |
Update iteration count. |
RETURN_ROOT |
Return c as the root approximation once the method converges. |
HANDLE_NO_CONVERGE |
If max iterations reached, return failure or warning. |
# INIT_INPUTS
a = initial_guess_1
b = initial_guess_2
tolerance = 1e-6
max_iterations = 100
# CHECK_VALIDITY
if f(a) * f(b) >= 0:
raise ValueError("Invalid interval")
# ITER_LOOP_START
for i in range(max_iterations):
# CALC_C
c = (a * f(b) - b * f(a)) / (f(b) - f(a))
# CALC_FC
fc = f(c)
# CHECK_CONVERGENCE
if abs(fc) < tolerance:
# RETURN_ROOT
return c
# UPDATE_INTERVAL
if f(a) * fc < 0:
b = c
else:
a = c
# HANDLE_NO_CONVERGE
raise Exception("Method did not converge")
Task 2 (student 2)
Using a Chat Bot 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?
The prompt was:
Generate the Control Flow Graph in the DOT language for the RegulaFalsi function.
double FalsiMethod(double (*f)(double), double a, double b, double e, int m) { double c, fc; int n, side = 0; /* starting values at endpoints of interval */ double fa = f(a); double fb = f(b); for (n = 0; n < m; n++) { c = (fa * b - fb * a) / (fa - fb); if (fabs(b - a) < e * fabs(b + a)) break; fc = f(c); if (fc * fb > 0) { /* fc and fb have same sign, copy c to b */ b = c; fb = fc; if (side == -1) fa /= 2; side = -1; } else if (fa * fc > 0) { /* fc and fa have same sign, copy c to a */ a = c; fa = fc; if (side == +1) fb /= 2; side = +1; } else { /* fc * f_ very small (looks like zero) */ break; } } return c; }
The answer:
digraph RegularFalsiCFG {
rankdir=TB;
node [fontname="Courier", fontsize=10];
Entry [shape=oval, label="Entry"];
Init [shape=box, label="c, fc;\nn, side=0;\nfa=f(a);\nfb=f(b);"];
ForCond [shape=diamond, label="n < m"];
ComputeC [shape=box, label="c = (fa*b - fb*a)/(fa - fb)"];
TestTol [shape=diamond, label="|b - a| < e * |b + a|"];
ComputeFc [shape=box, label="fc = f(c)"];
TestFB [shape=diamond, label="fc * fb > 0"];
ThenFB [shape=box, label="b = c;\nfb = fc;\nif(side == -1) fa /= 2;\nside = -1;"];
TestFAFC [shape=diamond, label="fa * fc > 0"];
ThenFAFC [shape=box, label="a = c;\nfa = fc;\nif(side == +1) fb /= 2;\nside = +1;"];
BreakZero [shape=box, label="break;"];
Return [shape=oval, label="return c;"];
Entry -> Init;
Init -> ForCond;
ForCond -> ComputeC [label="true"];
ForCond -> Return [label="false"];
ComputeC -> TestTol;
TestTol -> Return [label="true"];
TestTol -> ComputeFc[label="false"];
ComputeFc -> TestFB;
TestFB -> ThenFB [label="true"];
TestFB -> TestFAFC [label="false"];
ThenFB -> ForCond;
TestFAFC -> ThenFAFC [label="true"];
TestFAFC -> BreakZero[label="false"];
ThenFAFC -> ForCond;
BreakZero -> Return;
}
Task 3. (student 3)
Convince a Chat Bot 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.
"write a DECISION TEST for the function RegulaFalsi in the C language"
#include <stdio.h>
#include <math.h>
#define EPSILON 0.0001
double f(double x) {
return x * x * x - x - 2; // Example function
}
double RegulaFalsi(double a, double b) {
if (f(a) * f(b) >= 0) {
printf("Invalid interval.\n");
return -1;
}
double c = a;
while ((b - a) >= EPSILON) {
// Find the point that touches x axis
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
// Check if the above point is root
if (fabs(f(c)) < EPSILON)
break;
// Decide the side to repeat the steps
if (f(c) * f(a) < 0)
b = c;
else
a = c;
}
return c;
}
void test_invalid_interval() {
double root = RegulaFalsi(1, 1); // f(1)*f(1) >= 0
assert(root == -1); // Should return -1 for invalid interval
printf("Test invalid interval passed.\n");
}
void test_valid_root_interval() {
double root = RegulaFalsi(1, 2); // f(1) = -2, f(2) = 4, sign change
assert(fabs(f(root)) < EPSILON); // Root should be valid
printf("Test valid root interval passed.\n");
}
void test_early_termination() {
// Modify function or input to force f(c) < EPSILON quickly
double root = RegulaFalsi(1.5, 2); // Should converge quickly
assert(fabs(f(root)) < EPSILON);
printf("Test early termination passed.\n");
}
void test_update_a_or_b() {
double a = 1, b = 2;
double root = RegulaFalsi(a, b); // We expect updates to both a and b at some point
assert(fabs(f(root)) < EPSILON);
printf("Test update a/b logic passed.\n");
}
int main() {
test_invalid_interval(); // Covers decision 1 (true)
test_valid_root_interval(); // Covers decisions 1 (false), 2 (true), 3 (false), 4 (true/false)
test_early_termination(); // Covers decision 3 (true)
test_update_a_or_b(); // Covers decision 4 (both branches)
printf("All decision tests passed.\n");
return 0;
}
No Comments