Added checks on resolution & start time restrictions, and tests for them

This commit is contained in:
GPayne
2020-02-10 22:07:39 -07:00
parent 99021221e9
commit 016baa7ce7
3 changed files with 260 additions and 56 deletions
+135 -21
View File
@@ -34,19 +34,24 @@
namespace openspace::globebrowsing {
TimeQuantizer::TimeQuantizer(const std::string& start, const std::string& end,
/*TimeQuantizer::TimeQuantizer(const std::string& start, const std::string& end,
double resolution)
: _start(start)
, _resolution(resolution)
, _timerange(start, end)
{
//setStartEndRange(start, end);
}
}*/
TimeQuantizer::TimeQuantizer(const std::string& start, const std::string& end,
const std::string& resolution)
: TimeQuantizer(start, end, parseTimeResolutionStr(resolution))
{}
: _start(start)
, _timerange(start, end)
//TimeQuantizer(start, end, parseTimeResolutionStr(resolution))
{
verifyStartTimeRestrictions();
_resolution = parseTimeResolutionStr(resolution);
}
double TimeQuantizer::parseTimeResolutionStr(const std::string& resolutionStr) {
const char unit = resolutionStr.back();
@@ -60,12 +65,15 @@ double TimeQuantizer::parseTimeResolutionStr(const std::string& resolutionStr) {
throw ghoul::RuntimeError("Cannot convert " + numberString + " to number");
}
else {
verifyResolutionRestrictions(static_cast<int>(value), unit);
return computeSecondsFromResolution(static_cast<int>(value), unit);
}
}
void TimeQuantizer::setStartEndRange(const std::string& start, const std::string& end) {
_start.setTime(start);
verifyStartTimeRestrictions();
_timerange.setStart(start);
_timerange.setEnd(end);
}
@@ -74,6 +82,93 @@ void TimeQuantizer::setResolution(const std::string& resolutionString) {
_resolution = parseTimeResolutionStr(resolutionString);
}
void TimeQuantizer::verifyStartTimeRestrictions() {
if (_start.day() < 1 || _start.day() > 28) {
throw ghoul::RuntimeError(
"Invalid start day value of " + std::to_string(_start.day()) +
" for day of month. Valid days are 1 - 28."
);
}
if (_start.hour() != 0 || _start.minute() != 0 || _start.second() != 0) {
throw ghoul::RuntimeError(
"Invalid start time value of " + std::to_string(_start.hour()) + ":" +
std::to_string(_start.minute()) + ":" + std::to_string(_start.second()) +
". Time must be 00:00:00."
);
}
}
void TimeQuantizer::verifyResolutionRestrictions(const int value, const char unit) {
switch (unit) {
case 'y':
break;
case 'M':
switch (value) {
case 1:
case 2:
case 3:
case 4:
case 6:
break;
default:
throw ghoul::RuntimeError(
"Invalid resolution count of " + std::to_string(value) + " for (M)onth"
+ " option. Valid counts are 1, 2, 3, 4, or 6."
);
}
break;
case 'd':
if (value < 1 || value > 28) {
throw ghoul::RuntimeError(
"Invalid resolution count of " + std::to_string(value) + " for (d)ay"
+ " option. Valid counts are 1 - 28."
);
}
break;
case 'h':
switch (value) {
case 1:
case 2:
case 3:
case 4:
case 6:
case 12:
break;
default:
throw ghoul::RuntimeError(
"Invalid resolution count of " + std::to_string(value) + " for (h)our"
+ " option. Valid counts are 1, 2, 3, 4, 6, or 12."
);
}
break;
case 'm':
switch (value) {
case 15:
case 30:
break;
default:
throw ghoul::RuntimeError(
"Invalid resolution count of " + std::to_string(value) + " for (m)inute"
+ " option. Valid counts are 1, 2, 3, 4, 6, or 12."
);
}
break;
default:
throw ghoul::RuntimeError(
"Invalid resolution unit format '" + std::to_string(unit) +
"'. Expected 'y', 'M', 'd', 'h', or 'm'."
);
}
}
double TimeQuantizer::computeSecondsFromResolution(const int valueIn, const char unit) {
double value = static_cast<double>(valueIn);
// convert value to seconds, based on unit.
@@ -85,7 +180,8 @@ double TimeQuantizer::computeSecondsFromResolution(const int valueIn, const char
case 'd':
value *= 24.0;
[[fallthrough]];
case 'h': value *= 60.0;
case 'h':
value *= 60.0;
[[fallthrough]];
case 'm':
value *= 60.0;
@@ -100,8 +196,8 @@ double TimeQuantizer::computeSecondsFromResolution(const int valueIn, const char
default:
throw ghoul::RuntimeError(
"Invalid unit format '" + std::string(1, unit) +
"'. Expected 'y', 'd', 'h', 'm' or 's'."
"Invalid resolution unit format '" + std::to_string(unit) +
"'. Expected 'y', 'M', 'd', 'h', 'm' or 's'."
);
}
return value;
@@ -132,7 +228,7 @@ bool TimeQuantizer::quantize(Time& t, bool clamp) {
_resolutionUnit, error, _resolution);
}
error = diff(quantized, unquantized);
bool hasSettled = (lastIncr == 1 && lastDecr == 1);
bool hasSettled = (lastIncr == 1 && lastDecr == 1 && error >= 0.0);
iterations++;
if (hasSettled || iterations > iterationLimit) {
break;
@@ -160,7 +256,7 @@ void TimeQuantizer::doFirstApproximation(DateTime& quantized, DateTime& unQ,
double value, char unit)
{
double minYearsToAdjust;
double minIncrementsToAdjust;
double minIncrementsAdjust;
bool isSimMonthPastQuantizedMonth;
double error = 0.0;
int originalHour, originalMinute, originalSecond;
@@ -171,8 +267,8 @@ void TimeQuantizer::doFirstApproximation(DateTime& quantized, DateTime& unQ,
case 'y':
minYearsToAdjust = static_cast<double>(unQ.year()) -
static_cast<double>(_start.year());
minIncrementsToAdjust = minYearsToAdjust / value;
quantized.setYear(_start.year() + static_cast<int>(minIncrementsToAdjust));
minIncrementsAdjust = minYearsToAdjust / value;
quantized.setYear(_start.year() + static_cast<int>(minIncrementsAdjust) * value);
break;
case 'M':
@@ -217,6 +313,12 @@ void TimeQuantizer::doFirstApproximation(DateTime& quantized, DateTime& unQ,
quantized.decrementOnce(1, 'd');
}
break;
default:
throw ghoul::RuntimeError(
"Invalid unit format in doFirstApproximation '" + std::to_string(unit) +
"'. Expected 'y', 'M', 'd', 'h', or 'm'."
);
}
}
@@ -302,28 +404,34 @@ void DateTime::incrementOnce(int value, char unit) {
case 'm':
if (singleIncrement(_minute, value, 0, 59))
break;
//fall-through...
//else fall-through if overflow...
case 'h':
if (singleIncrement(_hour, value, 0, 23))
break;
//fall-through...
//else fall-through if overflow...
case 'd':
if (singleIncrement(_day, value, 1, monthSize(_month, _year)))
break;
//fall-through...
//else fall-through if overflow...
case 'M':
inBounds = singleIncrement(_month, value, 1, 12);
_day = std::clamp(_day, 1, monthSize(_month, _year));
if (inBounds)
break;
//fall-through...
//else fall-through if overflow...
case 'Y':
case 'y':
_year += value;
break;
default:
throw ghoul::RuntimeError(
"Invalid unit format in TQ incrementOnce '" + std::to_string(unit) +
"'. Expected 'y', 'M', 'd', 'h', or 'm'."
);
}
}
@@ -355,12 +463,12 @@ void DateTime::decrementOnce(int value, char unit) {
case 'm':
if (singleDecrement(_minute, value, 0, 59))
break;
//fall-through...
//else fall-through if underflow...
case 'h':
if (singleDecrement(_hour, value, 0, 23))
break;
//fall-through...
//else fall-through if underflow...
case 'd':
if (singleDecrement(_day, value, 1,
@@ -368,18 +476,24 @@ void DateTime::decrementOnce(int value, char unit) {
{
break;
}
//fall-through...
//else fall-through if underflow...
case 'M':
inBounds = singleDecrement(_month, value, 1, 12);
_day = std::clamp(_day, 1, monthSize(_month, _year));
if (inBounds)
break;
//fall-through...
//else fall-through if underflow...
case 'Y':
case 'y':
_year -= value;
break;
default:
throw ghoul::RuntimeError(
"Invalid unit format in TQ decrementOnce '" + std::to_string(unit) +
"'. Expected 'y', 'M', 'd', 'h', or 'm'."
);
}
}
+3 -1
View File
@@ -338,7 +338,7 @@ public:
* \params end the ISO8601 date/time string (YYYY-MM-DDTHH:mm:ss) for end
* \params resolution the number of seconds between the temporal data set updates
*/
TimeQuantizer(const std::string& start, const std::string& end, double resolution);
//TimeQuantizer(const std::string& start, const std::string& end, double resolution);
/*
* Constructor that initializes with formatted strings for start & ends date/times,
@@ -407,6 +407,8 @@ public:
std::vector<std::string> quantized(Time& start, Time& end);
private:
void verifyStartTimeRestrictions();
void verifyResolutionRestrictions(const int value, const char unit);
double diff(DateTime& from, DateTime& to);
void doFirstApproximation(DateTime& q, DateTime& unQ, double value, char unit);
RangedTime _timerange;
+122 -34
View File
@@ -69,15 +69,37 @@ int loadLSKKernel() {
}
static void singleTimeTest(openspace::Time& t,
openspace::globebrowsing::TimeQuantizer& tq, bool clamp,
const std::string& input, const std::string& expected)
openspace::globebrowsing::TimeQuantizer& tq, bool clamp,
const std::string& input, const std::string& expected)
{
t.setTime(input);
tq.quantize(t, clamp);
EXPECT_EQ(t.ISO8601(), expected);
}
TEST_F(TimeQuantizerTest, Basic) {
static void singleResolutionTest(openspace::globebrowsing::TimeQuantizer& tq,
std::string resolution, std::string expectedType,
bool expectFailure)
{
std::string res;
std::string search = "Invalid resolution ";
try {
tq.setResolution(resolution);
}
catch (const ghoul::RuntimeError& e) {
res = e.message;
}
if (expectFailure) {
EXPECT_TRUE(res.find(search) != std::string::npos);
EXPECT_TRUE(res.find(expectedType) != std::string::npos);
}
else {
EXPECT_TRUE(res.find(search) == std::string::npos);
}
}
static void LoadSpiceKernel () {
loadLSKKernel();
// naif0008.tls is a text file, check if loaded.
SpiceBoolean found;
@@ -95,8 +117,40 @@ TEST_F(TimeQuantizerTest, Basic) {
);
ASSERT_TRUE(found == SPICETRUE) << "Kernel not loaded";
using namespace openspace::globebrowsing;
}
TEST_F(TimeQuantizerTest, TestYears) {
LoadSpiceKernel();
using namespace openspace::globebrowsing;
TimeQuantizer t1;
openspace::Time testT;
t1.setStartEndRange("2019-12-09T00:00:00", "2030-03-01T00:00:00");
t1.setResolution("1y");
singleTimeTest(testT, t1, true, "2020-12-08T23:59:59", "2019-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2020-12-09T00:00:00", "2020-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2021-12-08T23:59:58", "2020-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2022-12-09T00:00:02", "2022-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2022-11-08T13:00:15", "2021-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2020-12-09T00:00:00", "2020-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2024-12-08T23:59:59", "2023-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2024-12-09T00:00:01", "2024-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2020-12-31T00:00:01", "2020-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2021-01-01T00:00:00", "2020-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2020-12-31T23:59:59", "2020-12-09T00:00:00.000");
t1.setResolution("3y");
singleTimeTest(testT, t1, true, "2020-12-08T23:59:59", "2019-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2022-12-09T00:00:00", "2022-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2028-12-08T23:59:59", "2025-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2028-12-09T00:00:01", "2028-12-09T00:00:00.000");
}
TEST_F(TimeQuantizerTest, TestDays) {
LoadSpiceKernel();
using namespace openspace::globebrowsing;
TimeQuantizer t1;
openspace::Time testT;
@@ -117,48 +171,64 @@ TEST_F(TimeQuantizerTest, Basic) {
singleTimeTest(testT, t1, true, "2019-12-08T23:59:00", "2019-12-09T00:00:00.000");
singleTimeTest(testT, t1, true, "2019-12-05T14:29:00", "2019-12-09T00:00:00.000");
t1.setStartEndRange("2017-01-28T08:00:00", "2020-09-01T08:00:00");
t1.setStartEndRange("2016-05-28T00:00:00", "2021-09-01T00:00:00");
t1.setResolution("4d");
singleTimeTest(testT, t1, true, "2016-06-01T00:00:00", "2016-06-01T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-06-01T00:00:01", "2016-06-01T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-03T10:00:00", "2016-07-03T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-07T00:00:00", "2016-07-07T00:00:00.000");
singleTimeTest(testT, t1, true, "2021-11-07T00:00:00", "2021-09-01T00:00:00.000");
singleTimeTest(testT, t1, false, "2021-11-07T00:00:00", "2021-11-07T00:00:00.000");
t1.setStartEndRange("2019-02-21T00:00:00", "2021-09-01T00:00:00");
t1.setResolution("11d");
singleTimeTest(testT, t1, true, "2020-03-01T00:30:00", "2020-03-01T00:00:00.000");
singleTimeTest(testT, t1, true, "2019-03-04T00:00:02", "2019-03-04T00:00:00.000");
}
TEST_F(TimeQuantizerTest, TestMonths) {
LoadSpiceKernel();
using namespace openspace::globebrowsing;
TimeQuantizer t1;
openspace::Time testT;
t1.setStartEndRange("2017-01-28T00:00:00", "2020-09-01T00:00:00");
t1.setResolution("1M");
singleTimeTest(testT, t1, true, "2017-03-03T05:15:45", "2017-02-28T08:00:00.000");
singleTimeTest(testT, t1, true, "2017-03-29T08:15:45", "2017-03-28T08:00:00.000");
singleTimeTest(testT, t1, true, "2017-03-03T05:15:45", "2017-02-28T00:00:00.000");
singleTimeTest(testT, t1, true, "2017-03-29T00:15:45", "2017-03-28T00:00:00.000");
t1.setStartEndRange("2016-01-17T08:00:00", "2020-09-01T08:00:00");
t1.setStartEndRange("2016-01-17T00:00:00", "2020-09-01T00:00:00");
t1.setResolution("2M");
singleTimeTest(testT, t1, true, "2016-01-27T05:15:45", "2016-01-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-03-16T08:15:45", "2016-01-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-03-17T18:00:02", "2016-03-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-05-18T08:00:02", "2016-05-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-11-17T10:15:45", "2016-11-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2017-01-18T05:15:45", "2017-01-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-01-27T05:15:45", "2016-01-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-03-16T08:15:45", "2016-01-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-03-17T18:00:02", "2016-03-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-05-18T00:00:02", "2016-05-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-11-17T10:15:45", "2016-11-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2017-01-18T05:15:45", "2017-01-17T00:00:00.000");
t1.setResolution("3M");
singleTimeTest(testT, t1, true, "2016-04-16T05:15:45", "2016-01-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-27T05:15:45", "2016-07-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2017-10-17T08:01:00", "2017-10-17T08:00:00.000");
singleTimeTest(testT, t1, true, "2016-04-16T05:15:45", "2016-01-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-27T05:15:45", "2016-07-17T00:00:00.000");
singleTimeTest(testT, t1, true, "2017-10-17T00:01:00", "2017-10-17T00:00:00.000");
t1.setStartEndRange("2016-05-29T03:00:00", "2021-09-01T08:00:00");
t1.setStartEndRange("2016-05-28T00:00:00", "2021-09-01T00:00:00");
t1.setResolution("6M");
singleTimeTest(testT, t1, true, "2016-11-29T03:00:05", "2016-11-29T03:00:00.000");
singleTimeTest(testT, t1, true, "2017-05-29T04:15:45", "2017-05-29T03:00:00.000");
singleTimeTest(testT, t1, true, "2017-10-17T08:01:00", "2017-05-29T03:00:00.000");
singleTimeTest(testT, t1, true, "2016-11-28T00:00:05", "2016-11-28T00:00:00.000");
singleTimeTest(testT, t1, true, "2017-05-30T04:15:45", "2017-05-28T00:00:00.000");
singleTimeTest(testT, t1, true, "2017-10-17T05:01:00", "2017-05-28T00:00:00.000");
}
t1.setStartEndRange("2016-05-29T03:00:00", "2021-09-01T08:00:00");
t1.setResolution("4d");
singleTimeTest(testT, t1, true, "2016-06-02T03:00:00", "2016-06-02T03:00:00.000");
singleTimeTest(testT, t1, true, "2016-06-02T03:00:01", "2016-06-02T03:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-04T13:00:00", "2016-07-04T03:00:00.000");
singleTimeTest(testT, t1, true, "2016-07-08T03:00:00", "2016-07-08T03:00:00.000");
t1.setStartEndRange("2019-02-21T03:00:00", "2021-09-01T08:00:00");
t1.setResolution("11d");
singleTimeTest(testT, t1, true, "2020-03-01T03:30:00", "2020-03-01T03:00:00.000");
singleTimeTest(testT, t1, true, "2019-03-04T03:00:02", "2019-03-04T03:00:00.000");
TEST_F(TimeQuantizerTest, TestTimes) {
LoadSpiceKernel();
using namespace openspace::globebrowsing;
TimeQuantizer t1;
openspace::Time testT;
t1.setStartEndRange("2019-02-21T00:00:00", "2021-09-01T00:00:00");
t1.setResolution("2h");
@@ -190,4 +260,22 @@ TEST_F(TimeQuantizerTest, Basic) {
singleTimeTest(testT, t1, true, "2019-02-28T22:59:59", "2019-02-28T22:45:00.000");
}
TEST_F(TimeQuantizerTest, TestResolutionError) {
LoadSpiceKernel();
using namespace openspace::globebrowsing;
TimeQuantizer t1;
singleResolutionTest(t1, "29d", "(d)ay option.", true);
singleResolutionTest(t1, "0d", "(d)ay option.", true);
singleResolutionTest(t1, "5h", "(h)our option.", true);
singleResolutionTest(t1, "11h", "(h)our option.", true);
singleResolutionTest(t1, "12h", "(h)our option.", false);
singleResolutionTest(t1, "78y", "(y)ear option.", false);
singleResolutionTest(t1, "12m", "(m)inute option.", true);
singleResolutionTest(t1, "1m", "(m)inute option.", true);
singleResolutionTest(t1, "0m", "(m)inute option.", true);
singleResolutionTest(t1, "15m", "(m)inute option.", false);
singleResolutionTest(t1, "30m", "(m)inute option.", false);
singleResolutionTest(t1, "31m", "(m)inute option.", true);
singleResolutionTest(t1, "10s", "unit format", true);
}