Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added course03/homework00/.DS_Store
Binary file not shown.
Binary file added course03/homework00/julian/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions course03/homework00/julian/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmake-build-debug
7 changes: 7 additions & 0 deletions course03/homework00/julian/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5.1)

project(cpp_class_hw1_julianbrendl LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
Empty file.
5 changes: 5 additions & 0 deletions course03/homework00/julian/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(LIBRARY_NAME hw1)

add_executable(${LIBRARY_NAME} date.h date.cc test.cc)

target_include_directories(${LIBRARY_NAME} PUBLIC .)
86 changes: 86 additions & 0 deletions course03/homework00/julian/src/date.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "date.h"
#include <stdexcept>

namespace CppCourse { namespace Homework1 {

Date::Date(int year, int month, int day)
: mYear(year),
mMonth(month),
mDay(day)
{
if (mYear < 0)
throw std::runtime_error("Year is not valid.");

if (mMonth < 0 || mMonth > 12)
throw std::runtime_error("Month is not valid.");

// Ignoring special cases like leap years, or 30 vs 31 etc.
if (mDay < 0 || mDay > 31)
throw std::runtime_error("Day is not valid.");
}

int Date::GetYear() const
{
return mYear;
}

int Date::GetMonth() const
{
return mMonth;
}

int Date::GetDay() const
{
return mDay;
}

std::string Date::ToString() const
{
return std::to_string(mYear) + "-" + std::to_string(mMonth) + "-" + std::to_string(mDay);
}

bool Date::operator== (const Date& other) const
{
return mYear == other.mYear && mMonth == other.mMonth && mDay == other.mDay;
}

bool Date::operator!= (const Date& other) const
{
return !(*this == other);
}

bool Date::operator< (const Date& other) const
{
return *this != other && !(*this > other);
}

bool Date::operator> (const Date& other) const
{
if (mYear == other.mYear)
{
if (mMonth == other.mMonth)
{
return mDay > other.mDay;
}
else
{
return mMonth > other.mMonth;
}
}
else
{
return mYear > other.mYear;
}
}

bool Date::operator<= (const Date& other) const
{
return !(*this > other);
}

bool Date::operator>= (const Date& other) const
{
return !(*this < other);
}

}};
31 changes: 31 additions & 0 deletions course03/homework00/julian/src/date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <string>

namespace CppCourse { namespace Homework1 {

class Date
{
public:
Date(int year, int month, int day);

int GetYear() const;
int GetMonth() const;
int GetDay() const;

std::string ToString() const;

bool operator== (const Date& other) const;
bool operator!= (const Date& other) const;
bool operator< (const Date& other) const;
bool operator> (const Date& other) const;
bool operator<= (const Date& other) const;
bool operator>= (const Date& other) const;

private:
int mYear;
int mMonth;
int mDay;
};

}};
113 changes: 113 additions & 0 deletions course03/homework00/julian/src/test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "date.h"
#include <iostream>

typedef CppCourse::Homework1::Date Date;

void TestEqual(const Date& date1, const Date& date2)
{
std::cout << "Test equal: ";
bool result = date1 == date2;

std::cout << date1.ToString() << " == " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestNotEqual(const Date& date1, const Date& date2)
{
std::cout << "Test not equal: ";
bool result = date1 != date2;

std::cout << date1.ToString() << " != " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestLessThan(const Date& date1, const Date& date2)
{
std::cout << "Test less than: ";
bool result = date1 < date2;

std::cout << date1.ToString() << " < " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestGreaterThan(const Date& date1, const Date& date2)
{
std::cout << "Test greater than: ";
bool result = date1 > date2;

std::cout << date1.ToString() << " > " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestLessThanEqual(const Date& date1, const Date& date2)
{
std::cout << "Test <=: ";
bool result = date1 <= date2;

std::cout << date1.ToString() << " <= " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestGreaterThanEqual(const Date& date1, const Date& date2)
{
std::cout << "Test >=: ";
bool result = date1 >= date2;

std::cout << date1.ToString() << " >= " << date2.ToString() << ": " << std::to_string(result) << std::endl;
}

void TestCompareDates(const Date& base, const Date& lessThanBase, const Date& greaterThanBase)
{
TestEqual(base, base);
TestEqual(base, lessThanBase);
TestEqual(base, greaterThanBase);

std::cout << std::endl;

TestNotEqual(base, base);
TestNotEqual(base, lessThanBase);
TestNotEqual(base, greaterThanBase);

std::cout << std::endl;

TestLessThan(base, base);
TestLessThan(base, lessThanBase);
TestLessThan(base, greaterThanBase);

std::cout << std::endl;

TestGreaterThan(base, base);
TestGreaterThan(base, lessThanBase);
TestGreaterThan(base, greaterThanBase);

std::cout << std::endl;

TestLessThanEqual(base, base);
TestLessThanEqual(base, lessThanBase);
TestLessThanEqual(base, greaterThanBase);

std::cout << std::endl;

TestGreaterThanEqual(base, base);
TestGreaterThanEqual(base, lessThanBase);
TestGreaterThanEqual(base, greaterThanBase);
}

int main(int argc, char** argv)
{
std::cout << "Starting tests" << std::endl;

Date baseDay {2019, 01, 02};
Date lessThanBaseDay {2019, 01, 01};
Date greaterThanBaseDay {2019, 01, 03};

Date baseMonth {2019, 02, 01};
Date lessThanBaseMonth {2019, 01, 01};
Date greaterThanBaseMonth {2019, 03, 01};

Date baseDayYear {2020, 01, 01};
Date lessThanBaseDayYear {2019, 01, 01};
Date greaterThanBaseDayYear {2021, 01, 01};

std::cout << std::endl << "COMPARE DATES DAY" << std::endl;
TestCompareDates(baseDay, lessThanBaseDay, greaterThanBaseDay);
std::cout << std::endl << "COMPARE DATES MONTH" << std::endl;
TestCompareDates(baseMonth, lessThanBaseMonth, greaterThanBaseMonth);
std::cout << std::endl << "COMPARE DATES YEAR" << std::endl;
TestCompareDates(baseDayYear, lessThanBaseDayYear, greaterThanBaseDayYear);
}