#!/usr/bin/perl -w
my $copyright = 'Copyright (C) 2007-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/>.

use strict;

my $generated_warning =
"/* Warning: This file is generated by $0 - do not modify directly! */\n";

my %uncopyableclasses = (
    'BB2Weight' => '',
    'BM25Weight' => '',
    'BM25PlusWeight' => '',
    'BoolWeight' => '',
    'CoordWeight' => '',
    'DiceWeight' => '',
    'DLHWeight' => '',
    'DPHWeight' => '',
    'IfB2Weight' => '',
    'IneB2Weight' => '',
    'InL2Weight' => '',
    'LM2StageWeight' => '',
    'LMAbsDiscountWeight' => '',
    'LMDirichletWeight' => '',
    'LMJMWeight' => '',
    'PL2Weight' => '',
    'PL2PlusWeight' => '',
    'TfIdfWeight' => '',
    'TradWeight' => '',
    'ValuePostingSource' => '0',
    'ValueWeightPostingSource' => '0',
    'DecreasingValueWeightPostingSource' => '0',
    'ValueCountMatchSpy' => '0',
    'ValueMapPostingSource' => '0',
    'FixedWeightPostingSource' => '0',
    'DateRangeProcessor' => '0',
    'MultiValueKeyMaker' => '',
    'NumberRangeProcessor' => '0, ""',
    'RangeProcessor' => '',
    'SimpleStopper' => '',
);

my %copyableclasses = (
    'Database' => '',
    'Document' => '',
    'ESet' => '',
    'ESetIterator' => '',
    'Enquire' => 'Xapian::Database(std::string(), Xapian::DB_BACKEND_INMEMORY)',
    'MSet' => '',
    'MSetIterator' => '',
    'PositionIterator' => '',
    'PostingIterator' => '',
    'Query' => '',
    'QueryParser' => '',
    'Registry' => '',
    'RSet' => '',
    'Stem' => '',
    'TermGenerator' => '',
    'TermIterator' => '',
    'Utf8Iterator' => '',
    'ValueIterator' => '',
    'WritableDatabase' => ''
);

my %no_get_description = (
    'DateRangeProcessor' => 1,
    'MultiValueKeyMaker' => 1,
    'NumberRangeProcessor' => 1,
    'RangeProcessor' => 1,
    'Registry' => 1,
    'BB2Weight' => 1,
    'BM25Weight' => 1,
    'BM25PlusWeight' => 1,
    'BoolWeight' => 1,
    'CoordWeight' => 1,
    'DiceWeight' => 1,
    'DLHWeight' => 1,
    'DPHWeight' => 1,
    'IfB2Weight' => 1,
    'IneB2Weight' => 1,
    'InL2Weight' => 1,
    'LM2StageWeight' => 1,
    'LMAbsDiscountWeight' => 1,
    'LMDirichletWeight' => 1,
    'LMJMWeight' => 1,
    'PL2Weight' => 1,
    'PL2PlusWeight' => 1,
    'TfIdfWeight' => 1,
    'TradWeight' => 1,
    'Utf8Iterator' => 1
);

# Classes which are STL-compatible containers.
my %container_classes = (
    'ESet' => 1,
    'MSet' => 1
);

print <<'END';
/** @file
 * @brief test common features of API classes
 */
END
print $generated_warning;
print <<"END";
/* $copyright
 *
 * 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_generated.h"

#define XAPIAN_DEPRECATED(D) D
#include <xapian.h>

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

using namespace std;

/// Check uncopyable API classes which should have a default ctor actually do.
DEFINE_TESTCASE(defaultctor1, !backend) {
END

for my $class (sort keys %uncopyableclasses) {
    my $params = $uncopyableclasses{$class};
    if ($params ne '') {
        $params = "($params)";
    }
    my $get_description = ! exists $no_get_description{$class};
    my $object = lc $class;
    my $full_class = "Xapian::$class";
    print "    $full_class $object$params;\n";
    if ($get_description) {
        print "    {\n";
        print "\tconst string& desc = $object.get_description();\n";
        print "\ttout.clear();\n";
        print "\ttout << desc << '\\n';\n";
        print "\tTEST(startswith(desc, \"$class(\") ||\n";
        print "\t     startswith(desc, \"$full_class(\"));\n";
        print "\tTEST(desc.back() == ')');\n";
        print "    }\n";
    }
}

print <<'END';
}

/// Test that API classes have a copy ctor and assignment operator.
DEFINE_TESTCASE(copyassign1, !backend) {
END

for my $class (sort keys %copyableclasses) {
    my $params = $copyableclasses{$class};
    if ($params =~ /INMEMORY/) {
        print "#ifdef XAPIAN_HAS_INMEMORY_BACKEND\n";
    }
    if ($params ne '') {
        $params = "($params)";
    }
    my $get_description = ! exists $no_get_description{$class};
    my $object = lc $class;
    $class = "Xapian::$class";
    print "    $class $object$params;\n";
    print "    TEST(!$object.get_description().empty());\n" if $get_description;
    print "    $class copy_$object($object);\n";
    print "    TEST(!copy_$object.get_description().empty());\n" if $get_description;
    print "    $class move_$object(std::move($object));\n";
    print "    TEST(!move_$object.get_description().empty());\n" if $get_description;
    print "    $object = copy_$object;\n";
    print "    TEST(!$object.get_description().empty());\n" if $get_description;
    print "    copy_$object = std::move(move_$object);\n";
    print "    TEST(!copy_$object.get_description().empty());\n" if $get_description;
    if ($params =~ /INMEMORY/) {
        print "#endif\n";
    }
    print "\n";
}

print <<'END';
}

/// Test that iterator classes have consistent iterator traits.
DEFINE_TESTCASE(itortraits1, !backend) {
END

for my $class (sort keys %copyableclasses) {
    next unless $class =~ /Iterator$/;
    print <<"END";
    {
        using traits = iterator_traits<Xapian::$class>;
        using pointer_base = std::remove_pointer<traits::pointer>::type;
        using reference_base = std::remove_reference<traits::reference>::type;
        static_assert(is_same<decltype(*std::declval<Xapian::$class>()),
                              pointer_base>::value,
                      "iterator_traits<$class>::pointer inconsistent with "
                      "type from dereferencing $class");
        static_assert(is_same<decltype(*std::declval<Xapian::$class>()),
                              reference_base>::value,
                      "iterator_traits<$class>::reference inconsistent with "
                      "type from dereferencing $class");
    }
END
}

print <<'END';
}

/// Test that container classes have consistent iterator traits.
DEFINE_TESTCASE(containertraits1, !backend) {
END

for my $class (sort keys %container_classes) {
    print <<"END";
    {
        using pointer_base =
            std::remove_pointer<Xapian::${class}::pointer>::type;
        using reference_base =
            std::remove_reference<Xapian::${class}::reference>::type;
        using value_type = Xapian::${class}::value_type;
        static_assert(is_same<value_type, pointer_base>::value,
                      "Xapian::${class}::pointer inconsistent with "
                      "Xapian::${class}::value_type");
        static_assert(is_same<value_type, reference_base>::value,
                      "Xapian::${class}::reference inconsistent with "
                      "Xapian::${class}::value_type");
    }
END
}

print <<'END';
}
END
