Skip to content

Commit

Permalink
Merge pull request #2 from david-grs/master
Browse files Browse the repository at this point in the history
asdf
  • Loading branch information
HoneyMonster7 authored May 10, 2019
2 parents 1c5362b + 90885ba commit b4b63e6
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "course03/gtest"]
path = course03/gtest
url = [email protected]:google/googletest.git
[submodule "course03/meeting03/gtest"]
path = course03/meeting03/gtest
url = [email protected]:google/googletest.git
18 changes: 18 additions & 0 deletions course03/meeting03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.1)

include(gtest.cmake)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -Wall -Wextra -fsanitize=undefined")
add_executable(main main.cc date.h date.cc)

find_package(Boost 1.58 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

find_package (Threads)
target_link_libraries(main
gtest
${CMAKE_THREAD_LIBS_INIT}
${Boost_LIBRARIES}
)
add_test(main main)

70 changes: 70 additions & 0 deletions course03/meeting03/date.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "date.h"

#include <sstream>
#include <array>
#include <stdexcept>

Date::Date() :
Date(1, 1, 1970)
{}

Date::Date(int day, int month, int year) :
mDay(day),
mMonth(month),
mYear(year)
{
if (!IsValid())
{
throw std::runtime_error("invalid date");
}
}

/* NOT NEEDED
Date::Date(const Date& other) :
Date(other.mDay, other.mMonth, other.mYear)
{}
Date& Date::operator=(const Date& other)
{
mDay = other.mDay;
mMonth = other.mMonth;
mYear = other.mYear;
if (!IsValid())
{
throw std::runtime_error("invalid date");
}
return *this;
}
*/

bool Date::IsValid() const
{
static const std::array<int, 12> MonthDays{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

return mDay > 0 && mMonth > 0 && mMonth <= 12
&& mYear != 0 // no year 0 in gregorian calendar
&& (mDay <= MonthDays[mMonth - 1] || (mDay == 29 && mMonth == 2 && IsLeapYear()));
}

bool Date::IsLeapYear() const
{
return IsLeapYear(mYear);
}

bool Date::IsLeapYear(int year)
{
if (year < 1)
{
++year;
}

return (year % 4 == 0 && year % 100 != 0) || year % 400 ==0;
}

std::ostream& operator<<(std::ostream& stream, const Date& date)
{
stream << date.GetDay() << "/" << date.GetMonth() << "/" << date.GetYear();
return stream;
}
31 changes: 31 additions & 0 deletions course03/meeting03/date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <iosfwd>

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

// rule of zero: no dtor, no copy ctor, no assignment op
// rule of three:
//Date(const Date&);
//Date& operator=(const Date&);

int GetDay() const { return mDay; }
int GetMonth() const { return mMonth; }
int GetYear() const { return mYear; }

bool IsLeapYear() const;
static bool IsLeapYear(int);

private:
bool IsValid() const;

int mDay;
int mMonth;
int mYear;
};

std::ostream& operator<<(std::ostream&, const Date&);
1 change: 1 addition & 0 deletions course03/meeting03/gtest
Submodule gtest added at 9997a8
51 changes: 51 additions & 0 deletions course03/meeting03/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "date.h"

#include <gtest/gtest.h>

#include <iostream>

/*
TEST(Date, Init)
{
Date d;
EXPECT_EQ(1, d.GetDay());
EXPECT_EQ(1, d.GetMonth());
EXPECT_EQ(1970, d.GetYear());
}
TEST(Date, Invalid)
{
EXPECT_ANY_THROW(Date(1, 1, 0));
}
class DateTest : public testing::Test
{
public:
DateTest()
{
}
Date mDate;
};
TEST_F(DateTest, LeapYear)
{
}
*/

int main()
{
Date d{1,2,2019};
//std::cout << Date::IsLeapYear(2019) << std::endl;

Date d2{d};

Date d3;
d3 = d;
std::cout << d2.GetDay() << std::endl;

return 0;
}

0 comments on commit b4b63e6

Please sign in to comment.