#
#/*
#	openisis - an open implementation of the CDS/ISIS database
#	Version 0.8.x (patchlevel see file Version)
#	Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org
#
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library 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
#	Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with this library; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#	see README for more information
#EOH */
#
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..6\n"; }
END {print "not ok 1\n" unless $loaded;}
use OpenIsis;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

sub pok { print $_[1] ? '' : 'not ', 'ok ', $_[0], ' (', $_[2], ") \n"; }

OpenIsis::log(0,'');
pok( 2, 1, 'unfortunately the effect of logging cannot be tested here' );

$db = OpenIsis::open( "cds", "-dbpath", "../db/cds" );
pok( 3, $db >= 0, $db );

$maxrow = OpenIsis::maxRowid( $db );
pok( 4, $maxrow > 0, $maxrow );

$row = OpenIsis::read( $db, 28 );
for (keys(%$row)) {
	next if 'mfn' eq $_;
	$a = $row->{$_};
	OpenIsis::MHL( @$a );
	# print STDERR "\t$_=",join( "\n\t", @$a ),"\n";
}
pok( 5, "HASH" eq ref($row), $row );

$row = OpenIsis::subfields( '^ap. 233-238 ^billus.' );
# for (keys(%$row)) { print STDERR "\t$_=$row->{$_}\n"; }
pok( 6, "HASH" eq ref($row) && 'p. 233-238 ' eq $row->{'a'}, $row->{'a'} );

@res = OpenIsis::query( $db, 'plant * water' );
# print STDERR "mfns for 'plant * water': ", join(',',@res), "\n";
pok( 7, 2 == @res, scalar(@res) );
@res = OpenIsis::query( $db, 'devel$' );
# print STDERR "mfns for 'devel$': ", join(',',@res), "\n";
pok( 8, 18 == @res, scalar(@res) );

# test error code
$nodb = OpenIsis::open( "foo", "-dbpath", "/no/such/database" );
pok( 9, $nodb == $OpenIsis::ERR_BADF, $nodb );

# not really tests
for (@res) {
	$row = OpenIsis::read( $db, $_ );
	$title = $row->{'24'};
	OpenIsis::MHL( @$title );
	# print STDERR "mfn ", $row->{'mfn'}, " title: ", $title->[0], "\n";
}
@res = OpenIsis::terms( $db, 'a$' );
# print STDERR 'terms for "a$": ', join('; ',@res), "\n";

$str = OpenIsis::2html( 'lt: < gt: > amp: & quot: "' );
# print STDERR "\t$str\n";

@res = OpenIsis::query( $db, 'Dacca' );
for (@res) {
	$row = OpenIsis::read( $db, $_, 'V24, V26, V26^*[..], V71,' );
	for (sort(keys(%$row))) {
		$vals = $row->{$_};
		# for $v (@$vals) { print STDERR "\t$_=$v\n"; }
	}
}
