#!/usr/bin/perl -w
# Copyright (C) 2007,2009 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

use strict;

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

my %uncopyableclasses = (
    'BM25Weight' => '',
    'BoolWeight' => '',
    'TradWeight' => '',
    'ValuePostingSource' => '0',
    'ValueWeightPostingSource' => '0',
    'DecreasingValueWeightPostingSource' => '0',
    'ValueCountMatchSpy' => '0',
    'ValueMapPostingSource' => '0',
    'FixedWeightPostingSource' => '0'
);

my %copyableclasses = (
    'Database' => '',
    'DateValueRangeProcessor' => '0',
    'Document' => '',
    'ESet' => '',
    'ESetIterator' => '',
    'Enquire' => 'Xapian::InMemory::open()',
    'MSet' => '',
    'MSetIterator' => '',
    'MultiValueKeyMaker' => '',
    'NumberValueRangeProcessor' => '0, ""',
    'PositionIterator' => '',
    'PostingIterator' => '',
    'Query' => '',
    'QueryParser' => '',
    'Registry' => '',
    'RSet' => '',
    'SimpleStopper' => '',
    'Stem' => '',
    'StringValueRangeProcessor' => '0',
    'TermGenerator' => '',
    'TermIterator' => '',
    'ValueIterator' => '',
    'WritableDatabase' => ''
);

my %no_get_description = (
    'DateValueRangeProcessor' => 1,
    'MultiValueKeyMaker' => 1,
    'NumberValueRangeProcessor' => 1,
    'Registry' => 1,
    'StringValueRangeProcessor' => 1,
    'BM25Weight' => 1,
    'BoolWeight' => 1,
    'TradWeight' => 1
);

print <<'END';
/** @file api_generated.cc
 * @brief test common features of API classes
 */
END
print $generated_warning;
print <<'END';
/* Copyright (C) 2007,2009 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>

#include "api_generated.h"

#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;
    $class = "Xapian::$class";
    print "    $class $object$params;\n";
    print "    (void)$object.get_description();\n" if $get_description;
}

print <<'END';
    return true;
}

/// 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 "    (void)$object.get_description();\n" if $get_description;
    print "    $class copy_$object($object);\n";
    print "    $object = copy_$object;\n";
    if ($params =~ /InMemory/) {
	print "#endif\n";
    }
    print "\n";
}

print <<'END';
    return true;
}
END
