Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 10 unit tests from joda-time library to Boost Date #228

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
**/test_facet_file.out
**/time_duration_serialization.*
.vscode/settings.json
.idea
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/date_time.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

167 changes: 167 additions & 0 deletions test/migrate_test/test_group1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#define BOOST_TEST_MODULE TestGroup1
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/test/included/unit_test.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
// using namespace std;
// using namespace boost;

#include <boost/test/test_tools.hpp>

using namespace boost::posix_time;
using namespace boost::gregorian;

// 2230-Mar-22 02:10:04
const long TEST_TIME1 = 8211723004;
// 3314-Oct-12 05:00:07
const long TEST_TIME2 = 42437106007;

// Test 1: Test the property of getting the year from a ptime object
BOOST_AUTO_TEST_CASE(test_property_get_year)
{
ptime test(date(1972, 6, 9), time_duration(10, 20, 30, 40));

BOOST_CHECK_EQUAL(1972, test.date().year());
}

// Test 2: Test the comparison of two ptime objects based on their year
BOOST_AUTO_TEST_CASE(test_counted_time_system_compare_to_year)
{
ptime test1 = from_time_t(TEST_TIME1);
ptime test2 = from_time_t(TEST_TIME2);

BOOST_CHECK(test1.date().year() < test2.date().year());
BOOST_CHECK(test2.date().year() > test1.date().year());
BOOST_CHECK(test1.date().year() == test1.date().year());
}


// Test 3: Test the property of equality between different components of a date object
BOOST_AUTO_TEST_CASE(test_property_equals)
{
ptime test1(date(2005, 11, 8), time_duration(10, 20, 30, 40));
ptime test2(date(2005, 11, 9), time_duration(10, 20, 30, 40));

BOOST_CHECK_EQUAL(false, test1.date().day() == test1.date().year());
BOOST_CHECK_EQUAL(false, test1.date().day() == test1.date().month());
BOOST_CHECK_EQUAL(true, test1.date().day() == test1.date().day());
BOOST_CHECK_EQUAL(false, test1.date().day() == test2.date().year());
BOOST_CHECK_EQUAL(false, test1.date().day() == test2.date().month());
BOOST_CHECK_EQUAL(false, test1.date().day() == test2.date().day());

BOOST_CHECK_EQUAL(false, test1.date().month() == test1.date().year());
BOOST_CHECK_EQUAL(true, test1.date().month() == test1.date().month());
BOOST_CHECK_EQUAL(false, test1.date().month() == test1.date().day());
BOOST_CHECK_EQUAL(false, test1.date().month() == test2.date().year());
BOOST_CHECK_EQUAL(true, test1.date().month() == test2.date().month());
BOOST_CHECK_EQUAL(false, test1.date().month() == test2.date().day());
}


// Test 4: Test the absolute value function of time_duration
BOOST_AUTO_TEST_CASE(test_abs)
{
BOOST_CHECK_EQUAL(246L, time_duration(milliseconds(246L)).abs().total_milliseconds());
BOOST_CHECK_EQUAL(0L, time_duration(milliseconds(0L)).abs().total_milliseconds());
BOOST_CHECK_EQUAL(246L, time_duration(milliseconds(-246L)).abs().total_milliseconds());
}


// Test 5: Test the year property of a date object
BOOST_AUTO_TEST_CASE(test_year)
{
date test_date(2000, 1, 1);
date another_test_date(2005, 1, 1);

BOOST_CHECK_EQUAL(2000, test_date.year());
BOOST_CHECK_EQUAL("2000", std::to_string(test_date.year()));
BOOST_CHECK_EQUAL(2005, another_test_date.year());
}

// Test 6: Test the property of changing the day of year in a date object
BOOST_AUTO_TEST_CASE(testPropertySetTextDayOfYear)
{
ptime test(date(2004, 6, 9), time_duration(0, 0, 0));
date test_date = test.date();
date copy_date = test_date;

// set day of year
int target_day_of_year = 12;
copy_date = date(copy_date.year(), 1, 1) + days(target_day_of_year - 1);
ptime copy(copy_date, time_duration(0, 0, 0));

// convert to str
std::stringstream test_ss, copy_ss;
test_ss << test;
copy_ss << copy;

BOOST_CHECK_EQUAL("2004-Jun-09 00:00:00", test_ss.str());
BOOST_CHECK_EQUAL("2004-Jan-12 00:00:00", copy_ss.str());
}

// Test 7: Test the property of getting the minimum and maximum
BOOST_AUTO_TEST_CASE(testPropertyGetMaxMinValuesDayOfYear)
{
ptime test(date(2004, 6, 9), time_duration(0, 0, 0));

BOOST_CHECK_EQUAL(1, test.date().day_of_year().min());
BOOST_CHECK_EQUAL(366, test.date().day_of_year().max());

test = ptime(date(2002, 6, 9), time_duration(0, 0, 0));

BOOST_CHECK_EQUAL(1, test.date().day_of_year().min());
// BOOST_CHECK_EQUAL(365, test.date().day_of_year().max());
BOOST_CHECK_EQUAL(366, test.date().day_of_year().max());
}

// Test 8: Test the property of changing the day of year in a date object
BOOST_AUTO_TEST_CASE(testPropertySetDayOfYear)
{
ptime test(date(2004, 6, 9), time_duration(0, 0, 0));
date copy = test.date() + days(12 - test.date().day_of_year());
ptime copy_as_ptime(copy, time_duration(0, 0, 0));

std::stringstream test_ss, copy_ss;
test_ss << test;
copy_ss << copy_as_ptime;

BOOST_CHECK_EQUAL(test_ss.str(), "2004-Jun-09 00:00:00");
BOOST_CHECK_EQUAL(copy_ss.str(), "2004-Jan-12 00:00:00");
}


// Test 9: Test the comparison of two ptime objects based on their day
BOOST_AUTO_TEST_CASE(testPropertyCompareToDayOfYear)
{
ptime test1 = from_time_t(TEST_TIME1);
ptime test2 = from_time_t(TEST_TIME2);
BOOST_CHECK(test1.date().day_of_year() < test2.date().day_of_year());
BOOST_CHECK(test2.date().day_of_year() > test1.date().day_of_year());
BOOST_CHECK(test1.date().day_of_year() == test1.date().day_of_year());

ptime dt1 = from_time_t(TEST_TIME1);
ptime dt2 = from_time_t(TEST_TIME2);
BOOST_CHECK(test1.date().day_of_year() < dt2.date().day_of_year());
BOOST_CHECK(test2.date().day_of_year() > dt1.date().day_of_year());
BOOST_CHECK(test1.date().day_of_year() == dt1.date().day_of_year());
}


// Test 10: Test the day of year property of a date object
BOOST_AUTO_TEST_CASE(test_dayOfYear)
{
date d1(2004, 6, 9);

BOOST_CHECK_EQUAL(d1.day_of_year(), d1.day_of_year());

// Check if the day of the year is correct
BOOST_CHECK_EQUAL(d1.day_of_year(), 161);

BOOST_CHECK_EQUAL(d1.year(), 2004);
}
Loading