Skip to main content

Branch testing with MS Copilot

Task 1 (student 1) 

 Using MS Copilot  identify branches for the insertSort function. Assign a tag to for each identified branch.  Present the list of statements with tags. Write the prompt and the chatbot answer. Discuss the result - compare it with the previous page of the book. 

Prompt for MS Copilot:

This is code for insert sort algorithm in C: void insertSort(int a[], int length) { int i, j, value; for (i = 1; i < length; ++i) { value = a[i]; for (j = i - 1; j >= 0 && a[j] > value; --j) { a[j + 1] = a[j]; } a[j + 1] = value; } }. Branch testing, identify branches for the insertSort function. Assign a tag to for each identified branch. Present the list of statements with tags

Answer from chatbot:

Branch testing involves identifying branches—conditional paths the code can take—within the function. In your insertSort function, we have the following branches:

Identified Branches:

  1. Loop entry conditions (for both loops)

    • for (i = 1; i < length; ++i) { → Tag: B1 (Outer loop)

    • for (j = i - 1; j >= 0 && a[j] > value; --j) { → Tag: B2 (Inner loop)

  2. Loop body execution

    • The body of the inner loop executes when the condition j >= 0 && a[j] > value is true → Tag: B3

    • The statement a[j + 1] = a[j]; occurs when the inner loop executes → Tag: B4

  3. Loop exit conditions

    • a[j + 1] = value; after the inner loop terminates → Tag: B5

Summary of Statements with Tags:

StatementTag
for (i = 1; i < length; ++i)B1
for (j = i - 1; j >= 0 && a[j] > value; --j)B2
a[j + 1] = a[j]; (executed inside inner loop)B3
Inner loop condition (j >= 0 && a[j] > value) is trueB4
a[j + 1] = value; (executed after inner loop terminates)B5

This breakdown helps in designing test cases that ensure all branches are covered. If you'd like, I can help formulate some test cases to validate each path

 Task 2 (student 2) 

 Using MS Copilot generate the Control Flow Graph in the DOT language for the insertSort function. Label the edges of the CFG. Discuss the result - compare the CFG with the graph created by a human. 

 Task 3. (student 3) 

Convince Copilot to write a branch 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.

Prompt for MS Copilot:

write a branch test for the function insertSort in the C language

Answer from chatbot:

Sure! A branch test aims to check all possible execution paths in a function, ensuring that each branch of conditional statements is tested. Here's an example of a branch test for an insertSort function in C:

#include <stdio.h>
#include <assert.h>

// Function to perform insertion sort
void insertSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;

        // Move elements that are greater than key one position ahead
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}

// Function to check if an array is sorted
int isSorted(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        if (arr[i] < arr[i - 1]) {
            return 0; // Not sorted
        }
    }
    return 1; // Sorted
}

// Branch Test Cases
void testInsertSort() {
    // Test empty array
    int arr1[] = {};
    insertSort(arr1, 0);
    assert(isSorted(arr1, 0));

    // Test single element array
    int arr2[] = {42};
    insertSort(arr2, 1);
    assert(isSorted(arr2, 1));

    // Test already sorted array
    int arr3[] = {1, 2, 3, 4, 5};
    insertSort(arr3, 5);
    assert(isSorted(arr3, 5));

    // Test reverse sorted array
    int arr4[] = {5, 4, 3, 2, 1};
    insertSort(arr4, 5);
    assert(isSorted(arr4, 5));

    // Test random unsorted array
    int arr5[] = {3, 1, 4, 1, 5, 9};
    insertSort(arr5, 6);
    assert(isSorted(arr5, 6));

    printf("All branch tests passed!\n");
}

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

This test suite covers various cases:

  • An empty array

  • A single-element array

  • Already sorted data

  • Reverse sorted data

  • Random unsorted data

It ensures that all conditional branches in insertSort are exercised. If all assertions pass, the sorting function is likely working correctly for different scenarios.