threads: update unit tests

This commit is contained in:
Dirk-Jan C. Binnema 2020-12-29 10:46:13 +02:00
parent 90e35a9732
commit 9a1e749cc5
1 changed files with 239 additions and 269 deletions

View File

@ -31,66 +31,51 @@
#include "test-mu-common.hh"
#include "mu-query.hh"
#include "utils/mu-str.h"
#include "utils/mu-utils.hh"
using namespace Mu;
struct _tinfo {
const char *threadpath;
const char *msgid;
const char *subject;
struct tinfo {
std::string threadpath;
std::string msgid;
std::string subject;
};
typedef struct _tinfo tinfo;
static void
assert_tinfo_equal (const tinfo *expected, const tinfo *actual)
assert_tinfo_equal (const tinfo& expected, const tinfo& actual)
{
g_assert_cmpstr (expected->threadpath,==,actual->threadpath);
g_assert_cmpstr (expected->subject,==,actual->subject);
g_assert_cmpstr (expected->msgid,==,actual->msgid);
assert_equal (expected.threadpath, actual.threadpath);
assert_equal (expected.subject, actual.subject);
assert_equal (expected.msgid, actual.msgid);
}
static void
tinfo_init_from_iter (tinfo *item, MuMsgIter *iter)
tinfo_init_from_item (tinfo& item, QueryResults::const_iterator& it)
{
MuMsg *msg;
const MuMsgIterThreadInfo *ti;
const auto& qmatch{it.query_match()};
msg = mu_msg_iter_get_msg_floating (iter);
g_assert (msg);
ti = mu_msg_iter_get_thread_info (iter);
if (!ti)
g_print ("%s: thread info not found\n", mu_msg_get_msgid (msg));
g_assert (ti);
item->threadpath = ti->threadpath;
item->subject = mu_msg_get_subject (msg);
item->msgid = mu_msg_get_msgid (msg);
item.threadpath = qmatch.thread_path;
item.subject = qmatch.sort_key;
item.msgid = *it.message_id();
if (g_test_verbose())
g_print ("%s %s %s\n",
item->threadpath, item->subject, item->msgid);
item.threadpath, item.subject, item.msgid);
}
static void
foreach_assert_tinfo_equal (MuMsgIter *iter, const tinfo items[], guint n_items)
foreach_assert_tinfo_equal (QueryResults& qres,
const tinfo items[], guint n_items)
{
guint u;
u = 0;
while (!mu_msg_iter_is_done (iter) && u < n_items) {
tinfo ti;
tinfo_init_from_iter (&ti, iter);
g_assert (u < n_items);
assert_tinfo_equal (&items[u], &ti);
size_t u{};
for (auto&& item: qres) {
tinfo ti{};
tinfo_init_from_item (ti, item);
assert_tinfo_equal (items[u], ti);
++u;
mu_msg_iter_next (iter);
}
g_assert (u == n_items);
g_assert_cmpuint(u, ==, n_items);
}
static std::string
@ -131,16 +116,16 @@ run_and_get_results_full (const std::string& xpath, const std::string& expr,
Mu::Query q{store};
const auto myflags{flags | Mu::QueryFlags::Threading};
auto res = q.run (expr, sort_field, myflags);
g_assert_true(!!res);
auto res{q.run(expr, sort_field, myflags)};
g_assert(!!res);
return std::move(res.value());
return *res;
}
static QueryResults
run_and_get_results (const std::string& xpath, const char *query)
{
return run_and_get_results_full (xpath, query, MU_MSG_FIELD_ID_DATE);
return run_and_get_results_full (xpath, query, MU_MSG_FIELD_ID_SUBJECT);
}
static void
@ -166,19 +151,13 @@ test_mu_threads_01 (void)
const auto xpath{make_database(MU_TESTMAILDIR3)};
g_assert (!xpath.empty());
auto res{run_and_get_results (xpath, "abc")};
g_assert_false(res.empty());
#waning fixme
//foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items));
auto qres{run_and_get_results (xpath, "abc")};
foreach_assert_tinfo_equal (qres, items, G_N_ELEMENTS (items));
}
static void
test_mu_threads_rogue (void)
{
MuMsgIter *iter;
tinfo *items;
tinfo items1 [] = {
{"0", "cycle0@msg.id", "cycle0"},
{"0:0", "cycle0.0@msg.id", "cycle0.0"},
@ -194,45 +173,36 @@ test_mu_threads_rogue (void)
};
const auto xpath{make_database (MU_TESTMAILDIR3)};
g_assert_false (xpath.empty());
auto res{run_and_get_results (xpath, "def")};
g_assert_false(res.empty());
/* due to the random order in files can be indexed, there are two possible ways
* for the threads to be built-up; both are okay */
if (g_strcmp0 (mu_msg_get_msgid(mu_msg_iter_get_msg_floating (iter)),
"cycle0@msg.id") == 0)
items = items1;
else
items = items2;
auto& items = (res.begin().message_id() == "cycle0@msg.id") ? items1 : items2;
//foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items1));
foreach_assert_tinfo_equal (res, items, G_N_ELEMENTS (items1));
}
static MuMsgIter*
query_testdir (const char *query, MuMsgFieldId sort_field, gboolean descending)
static QueryResults
query_testdir (const std::string& query, MuMsgFieldId sort_field, gboolean descending)
{
const auto flags{descending ? QueryFlags::Descending : QueryFlags::None};
const auto xpath{make_database(MU_TESTMAILDIR3)};
g_assert_false (xpath.empty());
auto iter = run_and_get_iter_full (xpath, query, sort_field, flags);
g_assert (iter != NULL);
g_assert (!mu_msg_iter_is_done (iter));
auto qres{run_and_get_results_full (xpath, query, sort_field, flags)};
g_assert_false (qres.empty());
return iter;
return qres;
}
static void
check_sort_by_subject (const char *query, const tinfo expected[],
check_sort_by_subject (const std::string& query,
const tinfo expected[],
guint n_expected, gboolean descending)
{
MuMsgIter *iter;
iter = query_testdir (query, MU_MSG_FIELD_ID_SUBJECT, descending);
foreach_assert_tinfo_equal (iter, expected, n_expected);
mu_msg_iter_destroy (iter);
auto qres{query_testdir (query, MU_MSG_FIELD_ID_SUBJECT, descending)};
foreach_assert_tinfo_equal (qres, expected, n_expected);
}
static void