You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 20 Next »

This tutorial focuses on creating a simple C++ Calculator project using the divide() method and testing it using the GoogleTest platform, and a complete way to do it.

1. Create The Calculator project

1.1. sources:

To present an example of using GoogleTest, we first need to create a project in C++. In the project directory named Calculator, we will create the C++ source and header files in the appropriate directories. For the tutorial, we create operations.cpp in the src directory, which contains a simple divide function and the operation.h header file. However, C++ is a statically typed language and therefore we need to supply multiple definitions of a division function according to the type of its input arguments. The output of a function should always be floating point, but for the purposes of this tutorial, we predict that the input arguments can be both integers and floating point numbers. Therefore, two definitions have been introduced for each of these types. The header file should look like this:

operations.h
#ifndef CALCULATOR_OPERATIONS_H
#define CALCULATOR_OPERATIONS_H

#include <stdexcept>

float divide(int numerator, int denominator);
float divide(float numerator, float denominator);

#endif //CALCULATOR_OPERATIONS_H

And the contents of the operation.cpp looks like this:

operations.cpp
#include "operations.h"

float divide(int numerator, int denominator){
    if(denominator == 0){
        throw std::overflow_error("Divide by zero exception!!! Verify the denominator value!");
    }
    return float(numerator) / float(denominator);
}

float divide(float numerator, float denominator){
    if(abs(denominator) <= 1e-5){
        throw std::overflow_error("Divide by zero exception!!! The denominator value close to zero!");
    }
    return numerator / denominator;
}

The presented function has a mechanism for verifying whether the divisor (denominator) is equal to zero or is close to zero in the case of a floating-point number. In case the condition evaluates to True, an exception is thrown. To implement the exception, include the <stdexcept> library. Under normal circumstances, the output value is computed and returned as floating point in both cases.

1.2. tests:

We also need to create a folder for our tests. In this tutorial, this folder is called tst. To start writing tests, we will create run_tests.cpp and test_operations.cpp. We want GoogleTest to search for test files automatically, and we do this thanks to the code stored in the run_tests.cpp source file. Typically, this code will be universal for all kinds of projects:

run_tests.cpp
#include "gtest/gtest.h"

int main(int argc, char **argv){
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

However, the actual test cases are placed in a separate file, the name of which corresponds to the name of the file that stores the code to be tested. It is good practice to name the test file using the name of the source file being tested with the keyword test appended as a prefix or suffix separated by the underscore "_" or the dash "-". The goal of this tutorial is to test the divide function stored in the operations.cpp file. Therefore, the file with the test code is named test_operations.cpp, and should look like this:

run_tests.cpp
#include <gtest/gtest.h>
#include "operations.h"

TEST(DivideOperation, PositiveInput) {
    // Integer arguments
    ASSERT_EQ(divide(10, 5), 2);
    ASSERT_FLOAT_EQ(divide(5, 10), 0.5);
    // Floating-point arguments
    ASSERT_FLOAT_EQ(divide(10.0f, 5.0f), 2.0f);
    ASSERT_FLOAT_EQ(divide(5.0f, 10.0f), 0.5f);
}

TEST(DivideOperation, NegitiveInput) {
    // Integer arguments
    ASSERT_EQ(divide(-10, -5), 2);
    ASSERT_FLOAT_EQ(divide(-5, -10), 0.5);
    // Floating-point arguments
    ASSERT_FLOAT_EQ(divide(-10.0f, -5.0f), 2.0f);
    ASSERT_FLOAT_EQ(divide(-5.0f, -10.0f), 0.5f);
}

TEST(DivideOperation, ZerioInput) {
    // Integer arguments
    EXPECT_THROW(divide(10, 0), std::overflow_error);
    // Floating-point arguments
    EXPECT_THROW(divide(10.0f, 0.0f), std::overflow_error);
}

Our project folder should look like this:

Calculator/ $: tree
.
├── src
│   ├── operations.cpp
│   └── operations.h
└── tst
    ├── run_tests.cpp
    └── test_operations.cpp


2. Build the project  - CMake

After the source and test code are ready, the project can be built and compiled. At the top of the project's folder structure, add CMakeList. The contents of the file are listed below:

cmake_minimum_required(VERSION 3.16)
project(Calculator)

set(CMAKE_CXX_STANDARD 11)

include_directories(src)
add_subdirectory(src)

enable_testing()
add_subdirectory(tst)



  • No labels