#! /bin/sh
#
# Test for namazu. Query operation.
#
LOG=`pwd`/test-log
IDX=`pwd`/idx8
TMP=`pwd`/tmp
echo '  *** starting ' $0 >>$LOG

cd ../src

M0="$TMP.0"      # no hit
M1="$TMP.1"      # 1.txt
M2="$TMP.2"      # 2.txt
M3="$TMP.3"      # 3.txt

M12="$TMP.12"    # 1.txt and 2.txt
M13="$TMP.13"    # 1.txt and 3.txt
M23="$TMP.23"    # 2.txt and 3.txt
M123="$TMP.123"    # 1.txt and 2.txt 3.txt

cat /dev/null > $M0
head -1  $IDX/NMZ.r > $M1
head -2  $IDX/NMZ.r | tail -1 > $M2
head -3  $IDX/NMZ.r | tail -1 > $M3

cat $M1 $M2 > $M12
cat $M1 $M3 > $M13
cat $M2 $M3 > $M23
cat $M1 $M2 $M3 > $M123

#
# Check WORD searching.
# Matches: 1.txt
#
./namazu -l 'foo' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1 # error if not matched

#
# Check WORD searching.
# Matches: 1.txt 2.txt
#
./namazu -l 'bar' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1

#
# Check PHRASE searching.
# Matches: 1.txt 2.txt
#
./namazu -l '"bar baz"' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1

# isomorphism
./namazu -l '{bar baz}' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1


#
# Check PHRASE searching.
# Matches: 1.txt
#
./namazu -l '"foo bar baz qux quux"' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

#
# Check PHRASE searching.
# Matches: no hit
#
./namazu -l '"bar foo"' $IDX | sort > $TMP
cmp $TMP $M0 || exit 1

#
# Check PHRASE searching.
# Matches: no hit
#
./namazu -l '"foo bar qux"' $IDX | sort > $TMP
cmp $TMP $M0 || exit 1


#
# Check WORD searching.
# Matches: 1.txt 2.txt 3.txt
#
./namazu -l 'baz' $IDX | sort > $TMP
cmp $TMP $M123 || exit 1

#
# Check PREFIX searching.
# Matches: 1.txt
#
./namazu -l 'f*' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

#
# Check SUFFIX searching.
# Matches: 3.txt
#
./namazu -l '*ult' $IDX | sort > $TMP
cmp $TMP $M3 || exit 1

#
# Check SUBSTRING searching.
# Matches: 1.txt and 2.txt 3.txt
#
./namazu -l '*u*' $IDX | sort > $TMP
cmp $TMP $M123 || exit 1

#
# Check REGEX searching.
# Matches: 2.txt 3.txt
#
./namazu -l '/c.*e/' $IDX | sort > $TMP
cmp $TMP $M23 || exit 1

#
# Check REGEX searching.
# Matches: 1.txt 2.txt 3.txt
#
./namazu -l '/(foo|corge|grault)/' $IDX | sort > $TMP
cmp $TMP $M123 || exit 1

#
# Check FIELD-specified searching.
# Matches: 1.txt
#
./namazu -l '+subject:foo' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

#
# Check FIELD-specified searching.
# Matches: 1.txt 2.txt 3.txt
#
./namazu -l '+to:test@namazu.org' $IDX | sort > $TMP
cmp $TMP $M123 || exit 1

#
# Check FIELD-specified searching with REGEX.
# Matches: 2.txt 3.txt
#
./namazu -l '+from:/test@(bar|baz).namazu.org/' $IDX | sort > $TMP
cmp $TMP $M23 || exit 1

#
# Check AND searching.
# Matches: 1.txt
#
./namazu -l 'foo bar' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

# isomorphism
./namazu -l 'foo & bar' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

# isomorphism
./namazu -l 'foo and bar' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

#
# Check OR searching.
# Matches: 1.txt, 2.txt
#
./namazu -l 'foo or bar' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1

# isomorphism
./namazu -l 'foo | bar' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1

#
# Check NOT searching. 
# Matches: 2.txt
#
./namazu -l 'bar not foo' $IDX | sort > $TMP
cmp $TMP $M2 || exit 1

# isomorphism
./namazu -l 'bar ! foo' $IDX | sort > $TMP
cmp $TMP $M2 || exit 1


#
# Check complex searching. 
# Matches: 1.txt
#
./namazu -l '( foo or baz ) and foo' $IDX | sort > $TMP
cmp $TMP $M1 || exit 1

#
# Check complex searching. 
# Matches: 2.txt
#
./namazu -l '( foo or bar ) not foo' $IDX | sort > $TMP
cmp $TMP $M2 || exit 1

#
# Check complex searching. 
# Matches: 1.txt 2.txt
#
./namazu -l '( foo or baz ) not grault' $IDX | sort > $TMP
cmp $TMP $M12 || exit 1

#
# Check complex searching. 
# Matches: 3.txt
#
./namazu -l '( foo or baz ) not foo and grault' $IDX | sort > $TMP
cmp $TMP $M3 || exit 1

#
# Check complex searching. 
# Matches: no hit
#
./namazu -l '( foo or bar or baz ) and foo and corge' $IDX | sort > $TMP
cmp $TMP $M0 || exit 1

#
# Check complex searching. 
# Matches: 1.txt 2.txt 3.txt
#
./namazu -l '( /(foo|grault)/ or ( bar and corge ) ) and /q.*x/' \
	$IDX | sort > $TMP
cmp $TMP $M123 || exit 1

rm -f $TMP $TMP.*

exit 0
