Catch test for .cpp file not running properly - Randomness seeded shows, and then it exits

33 views Asked by At

I want to test my class created to store price data using catch_amalgamated. My test file consisted of TEST_CASE and a few sections that tested all of the functions and operators for my price calculations. But the test program exited before it was executed.

I am using VSCode for windows with g++ compiler.

The test file compiled correctly after submitting to the terminal a following line:

g++ -o run_test_price .\test_price.cpp ..\price.cpp .\catch_amalgamated.cpp ..\exceptions\zero_price_exception.cpp

I got two warnings:

..\price.cpp: In member function 'Price& Price::operator+(const Price&) const': ..\price.cpp:42:12: warning: reference to local variable 'sum' returned [-Wreturn-local-addr] 42 | return sum; | ^~~ ..\price.cpp:40:11: note: declared here
40 | Price sum = *this; | ^~~

After that I ran the test with following command:

.\run_test_price.exe

It seemed like it worked for a moment, because it showed a line:

Randomness seeded to: 2962524401

but after that program exited. There was no information about tests failing/passing and there were no errors.

Below I added my test file and price class definition. Thank you for all the help.

TEST FILE:

#include "catch_amalgamated.hpp"
#include "..\price.hpp"
#include "..\exceptions\zero_price_exception.hpp"

#include <sstream>
#include <iostream>



TEST_CASE("Price tests", "[Price]") 
{
    Price price1(10,99);
    Price price2(35,20);
    Price price3(15,20);

    SECTION("tests of constructors and getters")
    {
        CHECK(price1.get_before_comma() == 10);
        CHECK(price2.get_before_comma() == 35);
        CHECK(price3.get_before_comma() == 15);

        CHECK(price1.get_after_comma() == 99);
        CHECK(price2.get_after_comma() == 20);
        CHECK(price3.get_after_comma() == 20);

    }

    SECTION("comparison tests")
    {
        Price another_price1(10,99);
        Price another_price2(35,20);
        Price another_price3(15,20);

        CHECK(price1 == another_price1);
        CHECK(price1 == another_price1);
        CHECK(price1 == another_price1);
        CHECK_FALSE(price1 == another_price2);
        CHECK_FALSE(price2 == another_price3);

        CHECK(price1 < price2);
        CHECK(price1 < price3);
        CHECK(price3 < price2);
        CHECK_FALSE(price2 < price1);
        
        CHECK(price2 > price1);
        CHECK(price2 > price3);
        CHECK(price3 > price1);
        CHECK_FALSE(price1 > price2);

        CHECK(price1 == another_price1);
        CHECK_FALSE(price1 == another_price2);
        CHECK_FALSE(price2 == another_price3);
    }

    SECTION("Addition test +")
    {
        CHECK(price1 + price1 == Price(21,98));
        CHECK(price1 + price2 == Price(46,19));
        CHECK(price1 + price3 == Price(26,19));

        CHECK(price2 + price2 == Price(70,40));
        CHECK(price2 + price1 == Price(46,19));
        CHECK(price2 + price3 == Price(50,40));

        CHECK(price3 + price3 == Price(50,40));
        CHECK(price3 + price1 == Price(26,19));
        CHECK(price3 + price2 == Price(50,40));
    }

    SECTION("Addition with assignment test +=")
    {
        CHECK((price1 += price2) == Price(46,19));
        CHECK(price1 == Price(46,19));
        
        CHECK((price2 += price1) == Price(81,39));
        CHECK(price2 == Price(81,39));
        
        CHECK((price3 += price2) == Price(96,59));
        CHECK(price3 == Price(96,59));
    }


    SECTION("tests of operator<<")
    {
        std::stringstream stream_price1;
        stream_price1 << price1;
        CHECK(stream_price1.str() == "10,99");
        
        std::stringstream stream_price2;
        stream_price2 << price2;
        CHECK(stream_price2.str() == "35,20");

        std::stringstream stream_price3;
        stream_price3 << price3;
        CHECK(stream_price3.str() == "15,20");
    }


    SECTION("Wrong price")
    {
        CHECK_THROWS_AS(Price(0,0), std::invalid_argument);
        CHECK_THROWS_AS(Price(-35,20), std::invalid_argument);
    }

}

PRICE CLASS DEFINITION:

#include <iostream>
#include ".\exceptions\zero_price_exception.hpp"

class Price
{
    private:
        int before_comma;
        int after_comma;
    public:
        Price(int before_comma, int after_comma);

        int get_before_comma();
        int get_after_comma();

        friend std::ostream& operator<<(std::ostream& os, const Price& price);
        Price& operator+=(const Price& second_price);
        Price& operator+(const Price& second_price) const;
        bool operator<(const Price& second_price) const;
        bool operator>(const Price& second_price) const;
        bool operator==(const Price& second_price) const;
};

std::ostream& operator<<(std::ostream& os, const Price& price);`

PRICE CLASS .CPP FILE


#include "price.hpp"

Price::Price(int before_comma, int after_comma) : before_comma(before_comma), after_comma(after_comma)
{
    //zakladamy ze cena ponizej 1 (0,55zl) jest niepoprawna
    if(before_comma <= 0 || after_comma < 0)
    {
        throw ZeroPriceException();
    }
}

int Price::get_before_comma()
{
    return before_comma;
}

int Price::get_after_comma()
{
    return after_comma;
}

Price& Price::operator+=(const Price& second_argument)
{
    after_comma += second_argument.after_comma;
    before_comma += second_argument.before_comma;

    if(after_comma >= 100)
    {
        before_comma++;
        after_comma = after_comma - 100;
    }
    
    return *this;
}

Price& Price::operator+(const Price& second_price) const
{
    Price sum = *this;
    sum += second_price;
    return sum;
}

bool Price::operator<(const Price& second_price) const
{
    if(before_comma == second_price.before_comma)
    {
        return after_comma < second_price.after_comma;
    }
    else
    {
        return before_comma < second_price.before_comma;
    }
        
}

bool Price::operator>(const Price& second_price) const
{
    return !(*this < second_price);  
}


bool Price::operator==(const Price& second_price) const
{
    return before_comma == second_price.before_comma && after_comma == second_price.after_comma;
}

std::ostream& operator<<(std::ostream& os, const Price& price)
{
    os << price.before_comma << "," << price.after_comma;
    return os;
}
0

There are 0 answers