PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Steven Haryanto   PHP Full Text Search Index   test.php   Download  
File: test.php
Role: Unit test script
Content type: text/plain
Description: Test suite
Class: PHP Full Text Search Index
A PHP library to do full text indexing with MySQL
Author: By
Last change:
Date: 24 years ago
Size: 9,137 bytes
 

Contents

Class file image Download
<title>KwIndex Unit Test</title>
<h1>KwIndex Unit Test</h1>


<? if (!isset($step)) $step=1; if($step == 1) { ?>


<form method=POST>
<input type=hidden name=step value=2>
<table>
<tr><td>Hostname</td><td><input name=hostname value=localhost></td></tr>
<tr><td>Database name</td><td><input name=db_name value=test></td></tr>
<tr><td>Index name</td><td><input name=index_name value=kwindex></td></tr>
<tr><td>Username</td><td><input name=username value=""></td></tr>
<tr><td>Password</td><td><input name=password value=""></td></tr>
</table>
<input type=submit value="Next">
</form>


<? } elseif($step==2) { ?>


<p>Press Back if some values are incorrect.</p>
<form method=POST>
<input type=hidden name=step value=3>
<table>
<tr><td>Hostname</td><td><? echo htmlentities($hostname) ?><input type=hidden name=hostname value="<? echo htmlentities($hostname) ?>"></td></tr>
<tr><td>Database name</td><td><? echo htmlentities($db_name) ?><input type=hidden name=db_name value="<? echo htmlentities($db_name) ?>"></td></tr>
<tr><td>Index name</td><td><? echo htmlentities($index_name) ?><input type=hidden name=index_name value="<? echo htmlentities($index_name) ?>"></td></tr>
<tr><td>Username</td><td><? echo htmlentities($username) ?><input type=hidden name=username value="<? echo htmlentities($username) ?>"></td></tr>
<tr><td>Password</td><td><? echo htmlentities($password) ?><input type=hidden name=password value="<? echo htmlentities($password) ?>"></td></tr>
</table>
<input type=submit value="Next">
</form>


<? } elseif ($step==3) { ?>
<?


require "KwIndex.lib";

$docs = array(

'',

"Since the time when man's mind first busied itself with subjects beyond
his own self-preservation and the satisfaction of his bodily appetites, the
anomalous and curious have been of exceptional and persistent fascination to
him; and especially is this true of the construction and functions of the
human body."
,

"In the older works, the following authors have reported cases of pregnancy
before the appearance of menstruation: Ballonius, Vogel, Morgagni, the
anatomist of the kidney, Schenck, Bartholinus, Bierling, Zacchias,
Charleton, Mauriceau, Ephemerides, and Fabricius Hildanus."
,

"(1) The imagination-theory, or, to quote Harvey: \"Due to mental causes so
operating either on the mind of the female and so acting on her reproductive
powers, or on the mind of the male parent, and so influencing the qualities
of his semen, as to modify the nutrition and development of the
offspring.\""
,

"Wygodzky finds that the greatest number of coils of the umbilical cord ever
found to encircle a fetus are 7 (Baudelocque), 8 (Crede), and 9 (Muller and
Gray). His own case was observed this year in Wilna. The patient was a
primipara aged twenty. The last period was seen on May 10, 1894. On February
19th the fetal movements suddenly ceased. On the 20th pains set in about two
weeks before term."
,

"Warren gives an instance of a lady, Mrs. M----, thirty-two years of age,
married at fourteen, who, after the death of her first child, bore twins,
one living a month and the other six weeks. Later she again bore twins, both
of whom died. She then miscarried with triplets, and afterward gave birth to
12 living children, as follows: July 24, 1858, 1 child; June 30, 1859, 2
children; March 24, 1860, 2 children; March 1, 1861, 3 children; February
13, 1862, 4 children; making a total of 21 children in eighteen years, with
remarkable prolificity in the later pregnancies. She was never confined to
her bed more than three days, and the children were all healthy."

# text from Project Gutenberg Etext of Anomalies and Curiosities of Medicine
# by George M. Gould and Walter L. Pyle

);

class
MyKwIndex extends KwIndex {
    function &
document_sub($doc_ids) {
        global
$docs;

       
$d = array();
        while(list(
$k,$v) = each($doc_ids)) $d[$v] = $docs[$v];
        return
$d;
    }
}

$kw = new MyKwIndex(array(
                     
"index_name" => $index_name,
                     
"db_name" => $db_name,
                     
"hostname" => $hostname,
                     
"username" => $username,
                     
"password" => $password,
                     
"password" => $password
                  
));

#echo Dumper($kw->document_sub(array(1,4,3)));

echo "<p>Running tests.</p>\n";
echo
"<table>\n";


$exit=0;
while(!
$exit) {




echo
"<tr><td>1</td><td>empty_index</td><td>";
if(
$kw->empty_index()) {
    echo
"PASS";
} else {
    echo
"FAIL. Reason: ",htmlentities($kw->ERROR), ". ";
    echo
"This is a fatal failure. Stopping.\n"; $exit=1;
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>2</td><td>add_document [1]</td><td>";
if (
$kw->add_document(array(1))) {
    echo
"PASS";
} else {
    echo
"FAIL. Reason: ",htmlentities($kw->ERROR), ". ";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>3</td><td>document_count</td><td>";
$dc = $kw->document_count();
$wc = $kw->word_count();
if (
$dc == 1 && $wc == 38) {
    echo
"PASS";
} else {
    echo
"FAIL. Reason: ", htmlentities($kw->ERROR), ". ";
    echo
"Document count = $dc (expected 1), word count = $wc (expected 38)";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>4</td><td>add_document [2]</td><td>";
$kw->add_document(array(2,3,4,5));
$dc = $kw->document_count();
$wc = $kw->word_count();
if (
$dc == 5 && $wc == 189) {
    echo
"PASS";
} else {
    echo
"FAIL. Reason: ", htmlentities($kw->ERROR), ". ";
    echo
"Document count = $dc (expected 5), word count = $wc (expected 189)";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>5</td><td>remove_document</td><td>";
if (!
$kw->remove_document(array(5))) {
    echo
"FAIL. Reason: ", htmlentities($kw->ERROR), ". ";
} elseif ((
$dc = $kw->document_count()) != 4) {
    echo
"FAIL. Document count = $dc (expected 4)";
} else {
    echo
"PASS";
}
echo
"</td></tr>\n"; if($exit) break;


$docs[5] =
"According to a French authority the wife of a medical man at Fuentemajor,
in Spain, forty-three years of age, was delivered of triplets 13 times.
Puech read a paper before the French Academy in which he reports 1262 twin
births in Nimes from 1790 to 1875, and states that of the whole number in 48
cases the twins were duplicated, and in 2 cases thrice repeated, and in one
case 4 times repeated."
;

echo
"<tr><td>6</td><td>update_document</td><td>";
if (!
$kw->update_document(array(5))) {
    echo
"FAIL. Reason: ", htmlentities($kw->ERROR), ". ";
} elseif ((
$dc = $kw->document_count()) != 5 || ($wc = $kw->word_count()) != 216) {
    echo
"FAIL. Document count = $dc (expected 5), word count = $wc (expected 216)";
} else {
    echo
"PASS";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>7</td><td>common_word</td><td>";
if (!(
$stop_words = $kw->common_word())) {
    echo
"FAIL. Reason: ", htmlentities($kw->ERROR), ". ";
    echo
Dumper($stop_words);
} elseif (
sizeof($stop_words) != 4) {
    echo
"FAIL. Size of stop words = ", sizeof($stop_words), " (expected 4)";
} else {
    echo
"PASS";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>8</td><td>add_document [3]</td><td>";
$kw->empty_index();
$kw->add_document(array(1,2,3,4,5));
if ((
$dc=$kw->document_count()) == 5 && ($wc=$kw->word_count()) == 168) {
    echo
"PASS";
} else {
    echo
"FAIL. Document count = $dc (expected 5), word count = $wc (expected 168)";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>9</td><td>match_count [1]</td><td>";
$r = array(
   
$kw->match_count(array("words" => "from and", "boolean" => "AND")),
   
$kw->match_count(array("words" => "from and the", "boolean" => "AND")),
   
$kw->match_count(array("words" => "from or and the", "boolean" => "AND"))
);
if (
$r[0]==1 && $r[1]==1 && $r[2]==0) {
    echo
"PASS";
} else {
    echo
"FAIL. Result set = ($r[0], $r[1], $r[2]) (expected (1, 1, 0))";
}
echo
"</td></tr>\n"; if($exit) break;

echo
"<tr><td>10</td><td>match_count [2]</td><td>";
$r = array(
   
$kw->match_count(array("words" => "from and", "boolean" => "OR")),
   
$kw->match_count(array("words" => "from and the", "boolean" => "OR")),
   
$kw->match_count(array("words" => "from or and the", "boolean" => "OR"))
);
if (
$r[0]==5 && $r[1]==5 && $r[2]==5) {
    echo
"PASS";
} else {
    echo
"FAIL. Result set = ($r[0], $r[1], $r[2]) (expected (5, 5, 5))";
}
echo
"</td></tr>\n"; if($exit) break;


echo
"<tr><td>11</td><td>mixed test</td><td>";
if (
is_array($kw->common_word(101)) && $kw->add_document(array()) &&
   
$kw->remove_document(array()) && $kw->update_document(array()) &&
   
$kw->remove_stop_word(array())) {
    echo
"PASS";
} else {
    echo
"FAIL";
}
echo
"</td></tr>\n"; if($exit) break;

echo
"<tr><td>12</td><td>remove_index</td><td>";
if (
$kw->remove_index()) {
    echo
"PASS";
} else {
    echo
"FAIL";
}
echo
"</td></tr>\n"; if($exit) break;
$exit=1;




}
// while(!$exit)
echo "</table>\n";

echo
"<p>Done.</p>\n";

echo
"<p>If some tests do not PASS, consult the <a
href=http://steven.haryan.to/php/KwIndex.shtml>KwIndex page</a> or ask
me.</p>"
;



}
// $step == 3
?>