Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This section presents several test suites for different boundary conditions. This section introduces several test suites for different boundary conditions based on the get_triangle_area() function function. It takes two filenames as a parameter, one for the length of the base of the triangle and one for its height. It reads each line and calculates the correct area of the triangle for each line. The function expects to find both files inside data directory.

  • CheckType 

    At first, it would be appropriate to check that the return type is correct. In a given project, the function returns a vector of floats, and each of its elements is a calculated area. Test suite below:

    Code Block
    languagecpp
    titleTEST(GetTriangleArea, CheckType)
    linenumberstrue
    TEST(GetTriangleArea, CheckType) {
        std::string height_file = "triangle_height.txt";
        std::string base_file = "triangle_base.txt";
        EXPECT_EQ(typeid(std::vector<float>), typeid(get_triangle_area(base_file, height_file)));
    }

    The function under test is quite complex and takes up a lot of lines, so there's no room for it here, but it's worth learning how it works. In this test suite, it is important to provide valid file names, use typeid() to to determine the argument type, and use the EXPECT_EQ assertion to to  verify that it is the same as vector<float>.

  • NegativeInput

    A triangle can only be positive lengths, so when the opposite happens, it should cover it somehow, like throwing an exception. Test suite below:

    Code Block
    languagecpp
    titleTEST(GetTriangleArea, NegativeInput)
    linenumberstrue
    TEST(GetTriangleArea, NegativeInput) {
        std::string height_file = "negative_triangle_height.txt";
        std::string base_file = "triangle_base.txt";
        EXPECT_THROW(get_triangle_area(base_file, height_file), std::invalid_argument);
    }

    A purposely prepared file, named negative_triangle_height.txt, contains a value with a negative sign inside. The test suite is waiting to catch the invalid_argument exception. Below is the part of the implementation inside the function that does this:

    Code Block
    languagecpp
    firstline46
    titleoperations.cpp :: get_triangle_area()
    linenumberstrue
    if(std::stof(line) <= 0){
                    throw std::invalid_argument("The height value must be positive!");
                }

    When reading a line from a file (because it is stored in a text file, the value must be converted using the stof() function), the condition must be that no number will be less than or equal to zero. Otherwise, an invalid_argument  exception will be thrown - it affects both files.

  • RangeError 

    It is expected to reach the same range of the number of arguments in both files, otherwise it would be impossible to calculate the area for all cases. Test suite below:

    Code Block
    languagecpp
    titleTEST(GetTriangleArea, RangeError)
    linenumberstrue
    TEST(GetTriangleArea, RangeError) {
        std::string height_file = "extra_triangle_base.txt";
        std::string base_file = "triangle_base.txt";
        EXPECT_THROW(get_triangle_area(base_file, height_file), std::range_error);
    }

    If the files happen to have a different number of arguments, the test suite should catch a range_error exception.

    Code Block
    languagecpp
    firstline56
    titleoperations.cpp :: get_triangle_area()
    linenumberstrue
    if(bases.size() != heights.size()){
            throw std::range_error("Number of given bases and heights do not match!");
        }

    Inside the function, after successfully reading the values from both files, the sizes of both containers that hold the read values are compared. If they do not match, a range_error exception is thrown.