Add copy constructor and assignment operator to GncDate

This commit is contained in:
Geert Janssens 2017-05-02 23:09:23 +02:00
parent 5070037314
commit 7df29b572a
3 changed files with 55 additions and 3 deletions

View File

@ -509,9 +509,17 @@ GncDate::GncDate(const std::string str, const std::string fmt) :
m_impl(new GncDateImpl(str, fmt)) {}
GncDate::GncDate(std::unique_ptr<GncDateImpl> impl) :
m_impl(std::move(impl)) {}
GncDate::GncDate(const GncDate& a) :
m_impl(new GncDateImpl(*a.m_impl)) {}
GncDate::GncDate(GncDate&&) = default;
GncDate::~GncDate() = default;
GncDate&
GncDate::operator=(const GncDate& a)
{
m_impl.reset(new GncDateImpl(*a.m_impl));
return *this;
}
GncDate&
GncDate::operator=(GncDate&&) = default;

View File

@ -244,9 +244,23 @@ class GncDate
* - fmt doesn't specify a year, yet a year was found in the string
*/
GncDate(const std::string str, const std::string fmt);
/** Construct a GncDate from a GncDateImpl.
*/
GncDate(std::unique_ptr<GncDateImpl> impl);
/** Copy constructor.
*/
GncDate(const GncDate&);
/** Move constructor.
*/
GncDate(GncDate&&);
/** Default destructor.
*/
~GncDate();
/** Copy assignment operator.
*/
GncDate& operator=(const GncDate&);
/** Move assignment operator.
*/
GncDate& operator=(GncDate&&);
/** Set the date object to the computer clock's current day. */
void today();

View File

@ -37,6 +37,22 @@ TEST(gnc_date_constructors, test_ymd_constructor)
EXPECT_FALSE(date.isnull());
}
TEST(gnc_date_constructors, test_copy_constructor)
{
GncDate a(2045, 11, 13);
GncDate b(a);
EXPECT_FALSE(a.isnull());
EXPECT_TRUE (a == b);
}
TEST(gnc_date_constructors, test_move_constructor)
{
GncDate a(2045, 11, 13);
GncDate b(std::move(a));
EXPECT_TRUE(a.isnull());
EXPECT_TRUE (b.format("%Y-%m-%d") == "2045-11-13");
}
typedef struct
{
const char* date_fmt;
@ -46,9 +62,6 @@ typedef struct
int exp_day;
} parse_date_data;
/* parse_date
* time64 parse_date (const char* date_str, int format)// C: 14 in 7 SCM: 9 in 2 Local: 1:0:0
*/
TEST(gnc_date_constructors, test_str_format_constructor)
{
auto today = GncDate();
@ -215,6 +228,23 @@ TEST(gnc_date_operators, test_more_less_than)
EXPECT_FALSE (c >= a);
}
TEST(gnc_date_operators, test_copy_assignment)
{
GncDate a(2017, 1, 6);
GncDate b;
b = a;
EXPECT_TRUE (a == b);
}
TEST(gnc_date_operators, test_move_assignment)
{
GncDate a(2045, 11, 13);
GncDate b;
b = std::move(a);
EXPECT_TRUE(a.isnull());
EXPECT_TRUE (b.format("%Y-%m-%d") == "2045-11-13");
}
TEST(gnc_datetime_constructors, test_default_constructor)
{
GncDateTime atime;