Added Catch2 tests, moved Age-class to own library

This commit is contained in:
Michael Mandl 2018-11-24 19:55:16 +01:00
parent d1d635bd95
commit a2b85eba83
15 changed files with 79 additions and 4 deletions

66
source/Age/Age.cpp Normal file
View file

@ -0,0 +1,66 @@
#include "Age.h"
#include <QDebug>
#include <sstream>
Age::Age(unsigned int years, unsigned int months)
: m_years(years)
, m_months(months)
{
}
Age::Age(const QDate &birth, const QDate &reference)
{
if (reference < birth)
{
qDebug() << "test (" << reference << ") before birth (" << birth << ")";
m_years = 0;
m_months = 0;
return;
}
int years = reference.year() - birth.year();
int months = reference.month() - birth.month();
if (months == 0 && reference.day() < birth.day())
{
months--;
}
if (months < 0)
{
years--;
months = (months + 12) % 12;
}
m_years = years;
m_months = months;
}
bool Age::operator<(const Age &cmp) const
{
if (m_years == cmp.m_years)
{
return m_months < cmp.m_months;
}
return m_years < cmp.m_years;
}
unsigned int Age::years() const
{
return m_years;
}
unsigned int Age::months() const
{
return m_months;
}
std::string Age::toString() const
{
std::ostringstream result;
result << m_years << ";" << m_months;
return result.str();
}

22
source/Age/Age.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <QDate>
class Age
{
private:
unsigned int m_years = 0;
unsigned int m_months = 0;
public:
Age() = default;
Age(unsigned int years, unsigned int months);
Age(const QDate &birth, const QDate &reference);
bool operator<(const Age &cmp) const;
unsigned int years() const;
unsigned int months() const;
std::string toString() const;
};

23
source/Age/CMakeLists.txt Normal file
View file

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.6)
project(Age LANGUAGES CXX)
find_package(Qt5Core REQUIRED)
add_library(${PROJECT_NAME}
Age.cpp
)
set_target_properties(${PROJECT_NAME}
PROPERTIES CXX_STANDARD 14
)
target_include_directories(${PROJECT_NAME}
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
)