Skip to content
Snippets Groups Projects
parserTest.cpp 2.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • vcday's avatar
    vcday committed
    //
    // Created by anvia on 2/6/2018.
    //
    
    #include <string>
    #include <cassert>
    #include <iostream>
    #include "../Parser.h"
    #include "../../shared/Document.h"
    #include "../../shared/ProducerConsumerQueue.h"
    
    using namespace std;
    
    
    vcday's avatar
    vcday committed
    void testSimple ( );
    void testComplex ( );
    
    
    vcday's avatar
    vcday committed
    int main ( )
    	{
    	cout << "Testing Parser ... " << endl << endl;
    
    vcday's avatar
    vcday committed
    	testSimple ();
    	testComplex ();
    	cout << "Parser Tests Passed! :D" << endl;
    
    	}
    
    void testSimple ( )
    	{
    
    
    jsclose's avatar
    jsclose committed
    	ProducerConsumerQueue < string >  urlFrontierTest;
    
    vcday's avatar
    vcday committed
    	Document document ( "<title>This Cat Title Cat</title>" );
    
    vcday's avatar
    vcday committed
    
    
    jsclose's avatar
    jsclose committed
    	Parser parser ( &urlFrontierTest );
    
    vcday's avatar
    vcday committed
    	auto dictionary = parser.execute ( &document );
    
    vcday's avatar
    vcday committed
    
    	assert ( dictionary != nullptr );
    	assert ( dictionary->size () == 2);
    	assert ( dictionary->find ( "cat" ) != dictionary->end () );
    	assert ( dictionary->find ( "title" ) != dictionary->end () );
    	assert ( dictionary->find ( "this" ) == dictionary->end () );
    	assert ( dictionary->at ( "cat" )[ 0 ] == 0 && dictionary->at ( "cat" )[ 1 ] == 2 );
    	assert ( dictionary->at ( "title" )[ 0 ] == 1 );
    
    
    vcday's avatar
    vcday committed
    	delete dictionary;
    
    vcday's avatar
    vcday committed
    
    	}
    
    vcday's avatar
    vcday committed
    void testComplex ( )
    	{
    
    
    jsclose's avatar
    jsclose committed
    	ProducerConsumerQueue < string >  urlFrontierTest;
    
    vcday's avatar
    vcday committed
    	ifstream file("../tests/cats.html");
    	string temp;
    	string docString = "<title>Joe the Cat</title>\n";
    	docString += "<a href=\"https://www.w3schools.com/html/\">Visit our HTML tutorial</a>\n";
    	while(std::getline(file, temp)) {
    		docString += temp;
    		}
    
    	Document document ( docString );
    
    
    jsclose's avatar
    jsclose committed
    	Parser parser ( &urlFrontierTest );
    
    vcday's avatar
    vcday committed
    	auto dictionary = parser.execute ( &document );
    
    //	cout << dictionary->size () << endl;
    //	for (auto p : *dictionary)
    //		cout << p.first << endl;
    
    	assert ( dictionary != nullptr );
    	assert ( dictionary->size () == 3);
    
    	assert ( dictionary->find ( "cat" ) != dictionary->end () );
    	assert ( dictionary->find ( "story" ) != dictionary->end () );
    	assert ( dictionary->find ( "joe" ) != dictionary->end () );
    
    	assert ( dictionary->find ( "the" ) == dictionary->end () );
    	assert ( dictionary->find ( "of" ) == dictionary->end () );
    
    //	assert ( dictionary->at ( "cat" )[ 0 ] == 1 );
    //	assert ( dictionary->at ( "story" )[ 0 ] == 0 );
    //	cout << urlFrontierTest->Size () << endl;
    //	cout << urlFrontierTest->Pop () << endl;
    	delete dictionary;
    
    vcday's avatar
    vcday committed
    
    
    vcday's avatar
    vcday committed
    	}