forked from david-grs/cppcourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from david-grs/master
asdf
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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&); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|