Граничный анализ: природа корней квадратного уравнения

Опубликовано: 11 Января, 2022

Рассмотрим задачу для определения природы корней квадратного уравнения, где входами являются 3 переменные (a, b, c), а их значения могут быть из интервала [0, 100]. Выход может быть одним из следующих в зависимости от значений переменных:

  • Не квадратное уравнение,
  • Настоящие корни,
  • Мнимые корни,
  • Равные корни

Наша цель - разработать контрольные примеры граничных значений.

Анализ граничных значений - это метод тестирования программного обеспечения, при котором тесты предназначены для включения представителей граничных значений в диапазон. Анализ граничных значений имеет в общей сложности 4 * n + 1 различных тестовых случаев , где n - количество переменных в задаче.

Здесь мы должны рассмотреть все три переменные и разработать все возможные тестовые примеры. У нас будет 13 тестовых примеров при n = 3.

    Квадратное уравнение будет иметь вид: ax 2 + bx + c = 0

  • Корни действительны, если (b 2 - 4ac)> 0
  • Корни мнимые, если (b 2 - 4ac) <0
  • Корни равны, если (b 2 - 4ac) = 0
  • Equation is not quadratic if a = 0

How do we design the test cases ?
For each variable we consider below 5 cases:

  • amin = 0
  • amin+1 = 1
  • anominal = 50
  • amax-1 = 99
  • amax = 100

When we are considering these 5 cases for a variable, rest of the variables have the nominal values, like in the above case where the value of ‘a’ is varying from 0 to 100, the value of ‘b’ and ‘c’ will be taken as the nominal or average value. Similarly, when the values of variable ‘b’ are changing from 0 to 100, the values of ‘a’ and ‘c’ will be nominal or average i.e 50.

The possible test cases for the nature of roots of a Quadratic Equation in a Boundary Value Analysis can be:

Below is the program that verifies the test cases considered in the table shown above. The program takes user-defined inputs so that you can check for any of the test cases mentioned above.

C++

// C++ program to check the nature of the roots
  
#include <bits/stdc++.h>
using namespace std;
  
// BVA for nature of roots of a quadratic equation
void nature_of_roots(int a, int b, int c)
{
  
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0) {
        cout << "Not a Quadratic Equation"
             << endl;
        return;
    }
  
    int D = b * b - 4 * a * c;
  
    // If D > 0, it will be Real Roots
    if (D > 0) {
        cout << "Real Roots" << endl;
    }
  
    // If D == 0, it will be Equal Roots
    else if (D == 0) {
        cout << "Equal Roots" << endl;
    }
  
    // If D < 0, it will be Imaginary Roots
    else {
        cout << "Imaginary Roots" << endl;
    }
}
  
// Function to check for all testcases
void checkForAllTestCase()
{
  
    cout << "Testcase"
         << " a b c Actual Output"
         << endl;
    cout << endl;
    int a, b, c;
    int testcase = 1;
    while (testcase <= 13) {
        if (testcase == 1) {
            a = 0;
            b = 50;
            c = 50;
        }
        else if (testcase == 2) {
            a = 1;
            b = 50;
            c = 50;
        }
        else if (testcase == 3) {
            a = 50;
            b = 50;
            c = 50;
        }
        else if (testcase == 4) {
            a = 99;
            b = 50;
            c = 50;
        }
        else if (testcase == 5) {
            a = 100;
            b = 50;
            c = 50;
        }
        else if (testcase == 6) {
            a = 50;
            b = 0;
            c = 50;
        }
        else if (testcase == 7) {
            a = 50;
            b = 1;
            c = 50;
        }
        else if (testcase == 8) {
            a = 50;
            b = 99;
            c = 50;
        }
        else if (testcase == 9) {
            a = 50;
            b = 100;
            c = 50;
        }
        else if (testcase == 10) {
            a = 50;
            b = 50;
            c = 0;
        }
        else if (testcase == 11) {
            a = 50;
            b = 50;
            c = 1;
        }
        else if (testcase == 12) {
            a = 50;
            b = 50;
            c = 99;
        }
        else if (testcase == 13) {
            a = 50;
            b = 50;
            c = 100;
        }
        cout << " " << testcase << " "
             << a << " " << b << " "
             << c << " ";
        nature_of_roots(a, b, c);
        cout << endl;
        testcase++;
    }
}
  
// Driver Code
int main()
{
    checkForAllTestCase();
    return 0;
}

Java

// Java program to check the nature of the roots
import java.util.*;
  
class GFG
{
  
// BVA for nature of roots of a quadratic equation
static void nature_of_roots(int a, int b, int c)
{
  
    // If a = 0, D/2a will yield exception
    // Hence it is not a valid Quadratic Equation
    if (a == 0)
    {
        System.out.print("Not a Quadratic Equation"
            +" ");
        return;
    }
  
    int D = b * b - 4 * a * c;
  
    // If D > 0, it will be Real Roots
    if (D > 0) {
        System.out.print("Real Roots" +" ");
    }
  
    // If D == 0, it will be Equal Roots
    else if (D == 0) {
        System.out.print("Equal Roots" +" ");
    }
  
    // If D < 0, it will be Imaginary Roots
    else {
        System.out.print("Imaginary Roots" +" ");
    }
}
  
// Function to check for all testcases
static void checkForAllTestCase()
{
  
    System.out.print("Testcase"
        + " a b c Actual Output"
        +" ");
    System.out.println();
    int a, b, c;
    a = b = c = 0;
    int testcase = 1;
    while (testcase <= 13) {
        if (testcase == 1) {
            a = 0;
            b = 50;
            c = 50;
        }
        else if (testcase == 2) {
            a = 1;
            b = 50;
            c = 50;
        }
        else if (testcase == 3) {
            a = 50;
            b = 50;
            c = 50;
        }
        else if (testcase == 4) {
            a = 99;
            b = 50;
            c = 50;
        }
        else if (testcase == 5) {
            a = 100;
            b = 50;
            c = 50;
        }
        else if (testcase == 6) {
            a = 50;
            b = 0;
            c = 50;
        }
        else if (testcase == 7) {
            a = 50;
            b = 1;
            c = 50;
        }
        else if (testcase == 8) {
            a = 50;
            b = 99;
            c = 50;
        }
        else if (testcase == 9) {
            a = 50;
            b = 100;
            c = 50;
        }
        else if (testcase == 10) {
            a = 50;
            b = 50;
            c = 0;
        }
        else if (testcase == 11) {
            a = 50;
            b = 50;
            c = 1;
        }
        else if (testcase == 12) {
            a = 50;
            b = 50;
            c = 99;
        }
        else if (testcase == 13) {
            a = 50;
            b = 50;
            c = 100;
        }
        System.out.print(" " + testcase+ " "
            + a+ " " + b+ " "
            + c+ " ");
        nature_of_roots(a, b, c);
        System.out.println();
        testcase++;
    }
}
  
// Driver Code
public static void main(String[] args)
{
    checkForAllTestCase();
}
}
  
// This code is contributed by 29AjayKumar

Python3

# Python3 program to check the nature of the roots
  
# BVA for nature of roots of a quadratic equation
def nature_of_roots(a, b, c):
  
    # If a = 0, D/2a will yield exception
    # Hence it is not a valid Quadratic Equation
    if (a == 0):
        print("Not a Quadratic Equation");
        return;
      
    D = b * b - 4 * a * c;
  
    # If D > 0, it will be Real Roots
    if (D > 0):
        print("Real Roots");
      
    # If D == 0, it will be Equal Roots
    elif(D == 0):
        print("Equal Roots");
      
    # If D < 0, it will be Imaginary Roots
    else:
        print("Imaginary Roots");
      
# Function to check for all testcases
def checkForAllTestCase():
  
    print("Testcase a b c Actual Output");
    print();
    a = b = c = 0;
    testcase = 1;
    while (testcase <= 13):
        if (testcase == 1):
            a = 0;
            b = 50;
            c = 50;
        elif(testcase == 2):
            a = 1;
            b = 50;
            c = 50;
        elif(testcase == 3):
            a = 50;
            b = 50;
            c = 50;
        elif(testcase == 4):
            a = 99;
            b = 50;
            c = 50;
        elif(testcase == 5):
            a = 100;
            b = 50;
            c = 50;
        elif(testcase == 6):
            a = 50;
            b = 0;
            c = 50;
        elif(testcase == 7):
            a = 50;
            b = 1;
            c = 50;