* make size, date ranges work even if specified e->b. add unit tests.

This commit is contained in:
Dirk-Jan C. Binnema 2011-05-21 09:14:02 +03:00
parent e607aed8de
commit fa3ee3c058
2 changed files with 34 additions and 19 deletions

View File

@ -51,9 +51,21 @@ public:
normalize_date (begin);
normalize_date (end);
// note, we'll have to compare the *completed*
// versions of begin and end to if the were specified
// in the opposite order; however, if that is true, we
// have to complete begin, end 'for real', as the
// begin date is completed to the begin of the
// interval, and the to the end of the interval
// ie. begin: 2008 -> 200801010000
// end: 2008 -> 200812312359
if (complete_date12(begin,true) >
complete_date12(end, false))
std::swap (begin, end);
complete_date (begin, 12, true);
complete_date (end, 12, false);
begin = complete_date12(begin,true);
end = complete_date12(end, false);
return (Xapian::valueno)MU_MSG_PSEUDO_FIELD_ID_DATESTR;
}
@ -106,25 +118,23 @@ private:
char k = date[i];
if (std::isdigit(k))
cleanup += date[i];
// else if (k != ':' && k != '-' && k != '/' && k != '.' &&
// k != ',' && k != '_')
// throw std::runtime_error ("error in date str");
}
date = cleanup;
}
void complete_date (std::string& date, size_t len,
bool is_begin) const {
std::string complete_date12 (const std::string date, bool is_begin) const {
std::string compdate;
const std::string bsuffix ("00000101000000");
const std::string esuffix ("99991231235959");
const size_t size = 12;
if (is_begin)
date = std::string (date + bsuffix.substr (date.length()));
compdate = std::string (date + bsuffix.substr (date.length()));
else
date = std::string (date + esuffix.substr (date.length()));
compdate = std::string (date + esuffix.substr (date.length()));
date = date.substr (0, len);
return compdate.substr (0, size);
}
};
@ -143,9 +153,13 @@ public:
if (!substitute_size (begin) || !substitute_size (end))
return Xapian::BAD_VALUENO;
begin = Xapian::sortable_serialise(atol(begin.c_str()));
end = Xapian::sortable_serialise(atol(end.c_str()));
/* swap if b > e */
if (begin > end)
std::swap (begin, end);
begin = Xapian::sortable_serialise (atol(begin.c_str()));
end = Xapian::sortable_serialise (atol(end.c_str()));
return (Xapian::valueno)MU_MSG_FIELD_ID_SIZE;
}
private:

View File

@ -311,11 +311,12 @@ test_mu_query_dates (void)
QResults queries[] = {
{ "date:20080731..20080804", 5},
/* { "date:20080804..20080731", 5}, */
{ "date:20080804..20080731", 5},
{ "date:2008-07/31..2008@08:04", 5},
{ "date:2008-0731..20080804 s:gcc", 1},
{ "date:2008-08-11-08-03..now", 1},
{ "date:2008-08-11-08-05..now", 0}
{ "date:2008-08-11-08-03..today", 1},
{ "date:2008-08-11-08-05..now", 0},
};
xpath = fill_database ();
@ -339,7 +340,7 @@ test_mu_query_sizes (void)
QResults queries[] = {
{ "size:0b..2m", 12},
{ "size:2k..4k", 2},
/* { "size:2m..0b", 12}, */
{ "size:2m..0b", 12}
};
xpath = fill_database ();
@ -373,10 +374,10 @@ main (int argc, char *argv[])
test_mu_query_accented_chars_02);
g_test_add_func ("/mu-query/test-mu-query-wildcards",
test_mu_query_wildcards);
g_test_add_func ("/mu-query/test-mu-query-sizes",
test_mu_query_sizes);
g_test_add_func ("/mu-query/test-mu-query-dates",
test_mu_query_dates);
g_test_add_func ("/mu-query/test-mu-query-sizes",
test_mu_query_sizes);
g_log_set_handler (NULL,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,