| 1 | /** @file api_percentages.cc |
| 2 | * @brief Tests of percentage calculations. |
| 3 | */ |
| 4 | /* Copyright 2008 Lemur Consulting Ltd |
| 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_percentages.h" |
| 24 | |
| 25 | #include <xapian.h> |
| 26 | |
| 27 | #include "apitest.h" |
| 28 | #include "testutils.h" |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | // ####################################################################### |
| 33 | // # Tests start here |
| 34 | |
| 35 | // Test that percentages reported are the same regardless of which part of the |
| 36 | // mset is returned, for sort-by-value search. Regression test for a bug in |
| 37 | // 1.0.7 and earlier with returned percentages. |
| 38 | DEFINE_TESTCASE(consistency3, backend) { |
| 39 | Xapian::Database db(get_database("apitest_sortconsist")); |
| 40 | Xapian::Enquire enquire(db); |
| 41 | enquire.set_query(Xapian::Query("foo")); |
| 42 | enquire.set_sort_by_value(1, 0); |
| 43 | Xapian::doccount lots = 3; |
| 44 | Xapian::MSet bigmset = enquire.get_mset(0, lots); |
| 45 | TEST_EQUAL(bigmset.size(), lots); |
| 46 | for (Xapian::doccount start = 0; start < lots; ++start) { |
| 47 | tout << *bigmset[start] << ":" << bigmset[start].get_weight() << ":" << bigmset[start].get_percent() << "%" << endl; |
| 48 | for (Xapian::doccount size = 0; size < lots - start; ++size) { |
| 49 | Xapian::MSet mset = enquire.get_mset(start, size); |
| 50 | if (mset.size()) { |
| 51 | TEST_EQUAL(start + mset.size(), |
| 52 | min(start + size, bigmset.size())); |
| 53 | } else if (size) { |
| 54 | TEST(start >= bigmset.size()); |
| 55 | } |
| 56 | for (Xapian::doccount i = 0; i < mset.size(); ++i) { |
| 57 | TEST_EQUAL(*mset[i], *bigmset[start + i]); |
| 58 | TEST_EQUAL_DOUBLE(mset[i].get_weight(), |
| 59 | bigmset[start + i].get_weight()); |
| 60 | TEST_EQUAL_DOUBLE(mset[i].get_percent(), |
| 61 | bigmset[start + i].get_percent()); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | TEST(0); |
| 66 | return true; |
| 67 | } |