Software Testing Techniques: C Code Examples

Software Testing Techniques in C

Triangle Classification

This section demonstrates how to classify triangles based on their side lengths using C code.


#include <stdio.h>

// Function to check if sides form a triangle
int isValidTriangle(int a, int b, int c) {
    return (a + b > c) && (a + c > b) && (b + c > a);
}

// Function to determine the type of triangle
void determineTriangleType(int a, int b, int c) {
    if (!isValidTriangle(a, b, c)) {
        printf("Not a triangle.\n");
        return;
    }
    if (a == b && b == c) {
        printf("Equilateral triangle.\n");
    } else if (a == b || b == c || a == c) {
        printf("Isosceles triangle.\n");
    } else {
        printf("Scalene triangle.\n");
    }
}

int main() {
    int a, b, c;

    printf("Enter the sides of the triangle: ");
    scanf("%d %d %d", &a, &b, &c);
    printf("Testing with sides (%d, %d, %d):\n", a, b, c);

    determineTriangleType(a, b, c);

    return 0;
}

Commission Calculation with Boundary Value Analysis

This code calculates sales commissions and demonstrates Boundary Value Analysis for testing.


#include <stdio.h>

// Function to calculate commission
float calculateCommission(int sales) {
    if (sales <= 5000) {
        return 0.0;
    } else if (sales <= 10000) {
        return sales * 0.05;
    } else if (sales <= 20000) {
        return sales * 0.10;
    } else {
        return sales * 0.15;
    }
}

int main() {
    // Test cases using Boundary Value Analysis
    int testCases[] = {5000, 5001, 10000, 10001, 20000, 20001};
    int numCases = sizeof(testCases) / sizeof(testCases[0]);

    printf("Boundary Value Analysis Test Cases:\n");
    printf("Sales\tCommission\n");

    for (int i = 0; i < numCases; i++) {
        int sales = testCases[i];
        float commission = calculateCommission(sales);
        printf("%d\t%.2f\n", sales, commission);
    }

    return 0;
}

Next Date Calculation with Boundary Value Analysis

This section shows how to calculate the next date and uses Boundary Value Analysis for testing.


#include <stdio.h>
#include <stdbool.h>

// Function to check if a year is a leap year
bool isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// Function to get the number of days in a given month
int getDaysInMonth(int month, int year) {
    switch (month) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 4: case 6: case 9: case 11:
            return 30;
        case 2:
            return isLeapYear(year) ? 29 : 28;
        default:
            return 0; // Invalid month
    }
}

// Function to calculate the next date
void getNextDate(int day, int month, int year) {
    int daysInMonth = getDaysInMonth(month, year);

    if (day < 1 || day > daysInMonth || month < 1 || month > 12 || year < 1) {
        printf("Invalid date: %d-%d-%d\n", day, month, year);
        return;
    }

    if (day < daysInMonth) {
        day++;
    } else {
        day = 1;
        if (month < 12) {
            month++;
        } else {
            month = 1;
            year++;
        }
    }
    printf("Next date: %02d-%02d-%04d\n", day, month, year);
}

int main() {
    // Test cases using Boundary Value Analysis
    int testCases[][3] = {
        {31, 12, 2023}, // End of year
        {28, 2, 2023},  // Non-leap year end of February
        {29, 2, 2024},  // Leap year end of February
        {30, 4, 2023},  // End of April
        {31, 1, 2023},  // End of January
        {1, 1, 2023}   // Start of the year
    };
    int numCases = sizeof(testCases) / sizeof(testCases[0]);

    printf("Boundary Value Analysis Test Cases:\n");
    for (int i = 0; i < numCases; i++) {
        int day = testCases[i][0];
        int month = testCases[i][1];
        int year = testCases[i][2];
        printf("Input date: %02d-%02d-%04d -> ", day, month, year);
        getNextDate(day, month, year);
    }

    return 0;
}

Triangle Classification with Equivalence Class Testing

This code classifies triangles and demonstrates Equivalence Class Testing.


#include <stdio.h>

void classifyTriangle(int a, int b, int c) {
    // Check for invalid inputs
    if (a <= 0 || b <= 0 || c <= 0) {
        printf("Invalid Triangle: Sides must be positive.\n");
        return;
    }    if (a + b <= c || a + c <= b || b + c <= a) {
        printf("Not a Triangle: Violates triangle inequality.\n");
        return;
    }    if (a == b && b == c) {
        printf("Equilateral Triangle\n");
    } else if (a == b || b == c || a == c) {
        printf("Isosceles Triangle\n");
    } else {
        printf("Scalene Triangle\n");
    }
}

int main() {
    // Test cases using Equivalence Class Testing
    int testCases[][3] = {
        {3, 3, 3},  // Equilateral
        {3, 3, 5},  // Isosceles
        {3, 4, 5},  // Scalene
        {0, 3, 5},  // Invalid: Side is zero
        {-3, 4, 5}, // Invalid: Negative side
        {1, 2, 3},  // Invalid: Triangle inequality violation
        {5, 5, 10}  // Invalid: Triangle inequality violation
    };
    int numCases = sizeof(testCases) / sizeof(testCases[0]);

    printf("Triangle Classification Test Cases:\n");
    for (int i = 0; i < numCases; i++) {
        int a = testCases[i][0];
        int b = testCases[i][1];
        int c = testCases[i][2];
        printf("Input Sides: %d, %d, %d -> ", a, b, c);
        classifyTriangle(a, b, c);
    }

    return 0;
}

Commission Calculation with Equivalence Class Testing

This section calculates sales commissions and uses Equivalence Class Testing.


#include <stdio.h>

// Function to calculate commission
float calculateCommission(float sales) {
    if (sales <= 5000) {
        return 0.0; // No commission
    } else if (sales <= 10000) {
        return sales * 0.05; // 5% commission
    } else if (sales <= 20000) {
        return sales * 0.10; // 10% commission
    } else {
        return sales * 0.15; // 15% commission
    }
}

int main() {
    // Test cases using Equivalence Class Testing
    float testCases[] = {0, -1000, 3000, 7500, 15000, 25000};
    int numCases = sizeof(testCases) / sizeof(testCases[0]);

    printf("Commission Calculation Test Cases:\n");
    printf("Sales\t\tCommission\n");
    for (int i = 0; i < numCases; i++) {
        float sales = testCases[i];
        if (sales < 0) {
            printf("%.2f\tInvalid input: Sales cannot be negative.\n", sales);
        } else {
            float commission = calculateCommission(sales);
            printf("%.2f\t%.2f\n", sales, commission);
        }
    }

    return 0;
}

Next Date Calculation with Equivalence Class Testing

This code calculates the next date and demonstrates Equivalence Class Testing.


#include <stdio.h>
#include <stdbool.h>

// Function to check if a year is a leap year
bool isLeapYear(int year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}

// Function to get the number of days in a given month
int getDaysInMonth(int month, int year) {
    switch (month) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 4: case 6: case 9: case 11:
            return 30;
        case 2:
            return isLeapYear(year) ? 29 : 28;
        default:
            return 0; // Invalid month
    }
}

// Function to calculate the next date
void getNextDate(int day, int month, int year) {
    int daysInMonth = getDaysInMonth(month, year);

    // Validate the input date
    if (year <= 0 || month < 1 || month > 12 || day < 1 || day > daysInMonth) {
        printf("Invalid date: %02d-%02d-%04d\n", day, month, year);
        return;
    }

    // Calculate the next day
    if (day < daysInMonth) {
        day++;
    } else {
        day = 1;
        if (month < 12) {
            month++;
        } else {
            month = 1;
            year++;
        }
    }

    printf("Next date: %02d-%02d-%04d\n", day, month, year);
}

int main() {
    // Test cases using Equivalence Class Testing
    int testCases[][3] = {
        {31, 12, 2023}, // End-of-year transition
        {28, 2, 2023},  // Non-leap year February
        {29, 2, 2024},  // Leap year February
        {30, 4, 2023},  // End-of-month (30-day month)
        {31, 1, 2023},  // End-of-month (31-day month)
        {1, 1, 2023},   // Beginning of the year
        {31, 4, 2023},  // Invalid: April has 30 days
        {32, 1, 2023},  // Invalid: Day exceeds maximum for January
        {15, 13, 2023}, // Invalid: Month > 12
        {-1, 10, 2023}, // Invalid: Negative day
        {15, 0, 2023}   // Invalid: Month = 0
    };

    int numCases = sizeof(testCases) / sizeof(testCases[0]);
    printf("Next Date Program - Equivalence Class Testing:\n");
    for (int i = 0; i < numCases; i++) {
        int day = testCases[i][0];
        int month = testCases[i][1];
        int year = testCases[i][2];
        printf("Input date: %02d-%02d-%04d -> ", day, month, year);
        getNextDate(day, month, year);
    }

    return 0;
}

Binary Search with Basis Path Testing

This section demonstrates a binary search algorithm and uses Basis Path Testing.


#include <stdio.h>

// Binary Search function
int binarySearch(int arr[], int size, int target) {
    int low = 0, high = size - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2; // Prevent overflow
        if (arr[mid] == target) {
            return mid; // Target found
        } else if (arr[mid] > target) {
            high = mid - 1; // Search in the left subarray
        } else {
            low = mid + 1; // Search in the right subarray
        }
    }

    return -1; // Target not found
}

int main() {
    // Test cases for Basis Path Testing
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Test cases
    int testCases[] = {
        7,  // Path 1: Target found at mid
        3,  // Path 2: Search left subarray
        13, // Path 3: Search right subarray
        8,  // Path 4: Target not found
        1,  // Edge case: First element
        15, // Edge case: Last element
        -5, // Invalid: Below range
        20  // Invalid: Above range
    };

    int numCases = sizeof(testCases) / sizeof(testCases[0]);

    printf("Binary Search Test Cases (Basis Path Testing):\n");
    for (int i = 0; i < numCases; i++) {
        int target = testCases[i];
        int result = binarySearch(arr, size, target);

        if (result != -1) {
            printf("Target %d found at index %d.\n", target, result);
        } else {
            printf("Target %d not found in the array.\n", target);
        }
    }
    return 0;
}