Branch data Line data Source code
1 : : /** @file api_collapse.cc
2 : : * @brief Test collapsing during the match.
3 : : */
4 : : /* Copyright (C) 2009 Olly Betts
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 2 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program; if not, write to the Free Software
18 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : : */
20 : :
21 : : #include <config.h>
22 : :
23 : : #include "api_collapse.h"
24 : :
25 : : #include <xapian.h>
26 : :
27 : : #include "apitest.h"
28 : : #include "testutils.h"
29 : :
30 : : using namespace std;
31 : :
32 : : /// Simple test of collapsing with collapse_max > 1.
33 : 13 : DEFINE_TESTCASE(collapsekey5,backend) {
34 : 13 : Xapian::Database db(get_database("apitest_simpledata"));
35 : 13 : Xapian::Enquire enquire(db);
36 : : // "this" matches all documents.
37 : 13 : enquire.set_query(Xapian::Query("this"));
38 : :
39 : 13 : Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());
40 : :
41 [ + + ]: 143 : for (Xapian::valueno slot = 0; slot < 10; ++slot) {
42 : 130 : map<string, Xapian::doccount> tally;
43 [ + + ]: 910 : for (Xapian::docid did = 1; did <= db.get_doccount(); ++did) {
44 : 780 : ++tally[db.get_document(did).get_value(slot)];
45 : : }
46 : :
47 [ + + ]: 1040 : for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
48 : 910 : tout << "Collapsing on slot " << slot << " max " << cmax << endl;
49 : 910 : enquire.set_collapse_key(slot, cmax);
50 : 910 : Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
51 : :
52 : : // Check the collapse MSet size is as expected.
53 : 910 : Xapian::doccount expect_size = 0;
54 : 910 : map<string, Xapian::doccount>::const_iterator i;
55 [ + + ]: 3822 : for (i = tally.begin(); i != tally.end(); ++i) {
56 [ + - ][ + + ]: 2912 : if (i->first.empty() || i->second <= cmax) {
[ + + ]
57 : 2548 : expect_size += i->second;
58 : : } else {
59 : 364 : expect_size += cmax;
60 : : }
61 : : }
62 [ - + ][ # # ]: 910 : TEST_EQUAL(mset.size(), expect_size);
63 : :
64 : : // Check that the right number of documents with each collapse key
65 : : // value are left after collapsing.
66 : 910 : map<string, Xapian::doccount> seen;
67 [ + + ]: 5590 : for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
68 : 4680 : const string & key = j.get_collapse_key();
69 [ - + # # ]: 4680 : TEST(tally.find(key) != tally.end());
70 : 4680 : ++seen[key];
71 : 910 : }
72 [ + + ]: 3822 : for (i = tally.begin(); i != tally.end(); ++i) {
73 [ + - ][ + + ]: 2912 : if (i->first.empty() || i->second <= cmax) {
[ + + ]
74 [ - + ][ # # ]: 2548 : TEST_EQUAL(seen[i->first], i->second);
75 : : } else {
76 [ - + ][ # # ]: 364 : TEST_EQUAL(seen[i->first], cmax);
77 : : }
78 : : }
79 : : }
80 : : }
81 : :
82 : 13 : return true;
83 : : }
|