/** @file
 * @brief Test collapsing during the match.
 */
/* Copyright (C) 2009,2026 Olly Betts
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "api_collapse.h"

#include <xapian.h>

#include "apitest.h"
#include "testutils.h"

using namespace std;

/// Simple test of collapsing with collapse_max > 1.
DEFINE_TESTCASE(collapsekey5, backend) {
    Xapian::Database db(get_database("apitest_simpledata"));
    Xapian::Enquire enquire(db);
    // "this" matches all documents.
    enquire.set_query(Xapian::Query("this"));

    Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());

    for (Xapian::valueno slot = 0; slot < 10; ++slot) {
        map<string, Xapian::doccount> tally;
        for (Xapian::docid did = 1; did <= db.get_doccount(); ++did) {
            ++tally[db.get_document(did).get_value(slot)];
        }

        for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
            tout << "Collapsing on slot " << slot << " max " << cmax << '\n';
            enquire.set_collapse_key(slot, cmax);
            Xapian::MSet mset = enquire.get_mset(0, full_mset.size());

            // Check the collapse MSet size is as expected.
            Xapian::doccount expect_size = 0;
            map<string, Xapian::doccount>::const_iterator i;
            for (i = tally.begin(); i != tally.end(); ++i) {
                if (i->first.empty() || i->second <= cmax) {
                    expect_size += i->second;
                } else {
                    expect_size += cmax;
                }
            }
            TEST_EQUAL(mset.size(), expect_size);

            // Check that the right number of documents with each collapse key
            // value are left after collapsing.
            map<string, Xapian::doccount> seen;
            for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
                const string & key = j.get_collapse_key();
                TEST(tally.find(key) != tally.end());
                ++seen[key];
            }
            for (i = tally.begin(); i != tally.end(); ++i) {
                if (i->first.empty() || i->second <= cmax) {
                    TEST_EQUAL(seen[i->first], i->second);
                } else {
                    TEST_EQUAL(seen[i->first], cmax);
                }
            }
        }
    }
}

/// Test collapsing with a percentage cut-off.
DEFINE_TESTCASE(collapsekey6, backend) {
    Xapian::Database db(get_database("apitest_simpledata"));
    Xapian::Enquire enquire(db);
    // "this" matches all documents.
    enquire.set_query(Xapian::Query("this"));

    Xapian::MSet full_mset = enquire.get_mset(0, db.get_doccount());

    for (Xapian::valueno slot = 0; slot < 10; ++slot) {
        for (Xapian::doccount cmax = db.get_doccount() + 1; cmax > 0; --cmax) {
            for (int percent = 65; percent != 100; ++percent) {
                tout << "Collapsing on slot " << slot << " max " << cmax
                     << " cutoff " << percent << '\n';
                enquire.set_collapse_key(slot, cmax);
                enquire.set_cutoff(percent);
                Xapian::MSet mset = enquire.get_mset(0, full_mset.size());
                for (Xapian::MSetIterator j = mset.begin(); j != mset.end(); ++j) {
                    TEST_REL(j.get_percent(), >=, percent);
                }
            }
        }
    }
}

/// Regression test for bug #852 in Xapian 2.0.0.
DEFINE_TESTCASE(collapsekey7, backend) {
    Xapian::Database db(get_database("apitest_simpledata"));
    Xapian::Enquire enquire(db);
    // "this" matches all documents.
    enquire.set_query(Xapian::Query("this"));
    enquire.set_collapse_key(1);
    Xapian::MSet mset = enquire.get_mset(0, 0, 1);
    tout << mset.get_matches_lower_bound() << '\n';
    TEST_REL(mset.get_matches_lower_bound(), >=, 1);
}
