Skip to main content

Statement testing with Chat GPT

ChatGPT 4.5 fighting with insertSort function

In the example below we consider statement testing using chatgpt. The chatbot's task is to identify statements for the insertSort function - and to create labels and a graph. 

For this task, GPT - 4.5 in the free version is used. The graph is also generated using GPT - the "Create image" function.

Prompt: 

Identify statements for the insertSort function. Assign a tag to for each identified statement. Present the list of statements with tags.
ChatGPT answered:

Here’s a breakdown of a canonical insertSort (in-place insertion sort) in pseudocode, with each statement identified and tagged:

  1. function insertSort(A[0…n-1])
    Tag: FunctionDeclaration

  2. for i ← 1 to n-1 do
    Tag: ForLoop

    • Initialization (i ← 1)
      Tag: LoopInit

    • Condition (i ≤ n-1)
      Tag: LoopCondition

    • Update (i ← i + 1)
      Tag: LoopUpdate

  3. key ← A[i]
    Tag: Assignment

  4. j ← i − 1
    Tag: Assignment

  5. while j ≥ 0 and A[j] > key do
    Tag: WhileLoop

    • Condition (j ≥ 0)
      Tag: Comparison

    • Condition (A[j] > key)
      Tag: Comparison

  6. A[j + 1] ← A[j]
    Tag: Shift

  7. j ← j − 1
    Tag: Decrement

  8. end while
    Tag: LoopEnd

  9. A[j + 1] ← key
    Tag: Insertion

  10. end for
    Tag: LoopEnd


Legend of Tags

  • FunctionDeclaration: Introduces the function or method.

  • ForLoop / WhileLoop: Entry to a loop structure.

  • LoopInit / LoopCondition / LoopUpdate: The three parts of a for loop.

  • Assignment: Storing a value into a variable.

  • Comparison: Evaluating a relational expression.

  • Shift: Moving an array element to make room.

  • Decrement: Reducing a counter by 1.

  • Insertion: Placing the key element into its sorted position.

  • LoopEnd: Marks the end of a loop block.

This mapping helps you see exactly what each line of the algorithm is doing and how it contributes to insertion sort’s overall flow.

image.png



   

 Task 2 (student 2) 

 Using a Chat Bot generate the Control Flow Graph in the DOT language for the insertSort function. Do not force the Chat Bot to create the instruction list. 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 the Chat Bot. Did it help them co correct the CFG (now the CFG should be correct)? Discuss the result. 

 Task 3. (student 3) 

Convince a Chat Bot to write an instruction test for the function insertSort in the C language. 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.