1 /** @file
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.
DEFINE_TESTCASE(collapsekey5,backend)33 DEFINE_TESTCASE(collapsekey5, backend) {
34     Xapian::Database db(get_database("apitest_simpledata"));
35     Xapian::Enquire enquire(db);
36     // "this" matches all documents.
37     enquire.set_query(Xapian::Query("this"));
38 
39     Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());
40 
41     for (Xapian::valueno slot = 0; slot < 10; ++slot) {
42 	map<string, Xapian::doccount> tally;
43 	for (Xapian::docid did = 1; did <= db.get_doccount(); ++did) {
44 	    ++tally[db.get_document(did).get_value(slot)];
45 	}
46 
47 	for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
48 	    tout << "Collapsing on slot " << slot << " max " << cmax << endl;
49 	    enquire.set_collapse_key(slot, cmax);
50 	    Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
51 
52 	    // Check the collapse MSet size is as expected.
53 	    Xapian::doccount expect_size = 0;
54 	    map<string, Xapian::doccount>::const_iterator i;
55 	    for (i = tally.begin(); i != tally.end(); ++i) {
56 		if (i->first.empty() || i->second <= cmax) {
57 		    expect_size += i->second;
58 		} else {
59 		    expect_size += cmax;
60 		}
61 	    }
62 	    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 	    map<string, Xapian::doccount> seen;
67 	    for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
68 		const string & key = j.get_collapse_key();
69 		TEST(tally.find(key) != tally.end());
70 		++seen[key];
71 	    }
72 	    for (i = tally.begin(); i != tally.end(); ++i) {
73 		if (i->first.empty() || i->second <= cmax) {
74 		    TEST_EQUAL(seen[i->first], i->second);
75 		} else {
76 		    TEST_EQUAL(seen[i->first], cmax);
77 		}
78 	    }
79 	}
80     }
81 }
82