Versions Compared

Key

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

...

  • 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 determine the argument type, and use the EXPECT_EQ assertion 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.

  • NotExistingFile

    Sometimes it may happen that the given file cannot be opened due to a bad filename or nonexistent. The test suite below:

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

    In this case, it is expected to throw a runtime_error exception with a message containing the name of the file that was not found.

    Code Block
    languagecpp
    firstline32
    titleoperations.cpp :: get_triangle_area()
    linenumberstrue
    if (base_file_stream.is_open()) {
         ...
    }
    else throw std::runtime_error("Could not open file: " + file_base);

    Inside the function it just checks if the file could be opened and if not, throws a runtime_error exception with a message pointing to the name of the file not found.