#include "test.h"

#ifndef NO_WILDCARD_QUERY

	void testFuzzyQuery(CuTest *tc){
		RAMDirectory ram;
		
		//---
		WhitespaceAnalyzer an;
		IndexWriter* writer = _CLNEW IndexWriter(&ram, &an, true);

		//---  
		Document *doc = 0;
		//****
		doc = _CLNEW Document();
		doc->add(*Field::UnStored(_T("body"), _T("test")));
		writer->addDocument(doc);
		_CLDELETE(doc);
		//****
		doc = _CLNEW Document();
		doc->add(*Field::UnStored(_T("body"), _T("home")));
		writer->addDocument(doc);
		_CLDELETE(doc);
		//****
		doc = _CLNEW Document();
		doc->add(*Field::UnStored(_T("body"), _T("pc linux")));
		writer->addDocument(doc);
		_CLDELETE(doc);
		//****
		doc = _CLNEW Document();
		doc->add(*Field::UnStored(_T("body"), _T("tested")));
		writer->addDocument(doc);
		_CLDELETE(doc);
		//****
		doc = _CLNEW Document();
		doc->add(*Field::UnStored(_T("body"), _T("source")));
		writer->addDocument(doc);
		_CLDELETE(doc);

		//---
		writer->optimize();
		writer->close();
		_CLDELETE(writer);

		//---
		IndexSearcher searcher (&ram);

		//---
		Term* term = _CLNEW Term(_T("body"), _T("test~"));
		Query* query = _CLNEW FuzzyQuery(term);
		Hits* result = searcher.search(query);
		
    	CLUCENE_ASSERT(result && result->length() > 0);

		//---
		_CLDELETE(result);
		_CLDELETE(query);
		_CLDECDELETE(term);
		searcher.close();
		ram.close();
	}


	void testAsterisk(CuTest *tc){
		RAMDirectory indexStore;
		SimpleAnalyzer an;
		IndexWriter* writer = _CLNEW IndexWriter(&indexStore, &an, true);
		Document doc1;
		Document doc2;
		doc1.add(*Field::Text(_T("body"), _T("metal")));
		doc2.add(*Field::Text(_T("body"), _T("metals")));

		writer->addDocument(&doc1);
		writer->addDocument(&doc2);
		writer->optimize();
    	writer->close();
		_CLDELETE(writer);
        //////////////////////////////////////////////////
		
		IndexReader* reader = IndexReader::open(&indexStore);
		IndexSearcher* searcher = _CLNEW IndexSearcher(reader);

		Term* term = _CLNEW Term(_T("body"), _T("metal"));
			Query* query1 = _CLNEW TermQuery(term);
		_CLDECDELETE(term);

		term = _CLNEW Term(_T("body"), _T("metal*"));
		Query* query2 = _CLNEW WildcardQuery(term);
		_CLDECDELETE(term);
			
		term = _CLNEW Term(_T("body"), _T("m*tal"));
		Query* query3 = _CLNEW WildcardQuery(term);
		_CLDECDELETE(term);
			
		term = _CLNEW Term(_T("body"), _T("m*tal*"));
		Query* query4 = _CLNEW WildcardQuery(term);
		_CLDECDELETE(term);

    	Hits* result = NULL;
	
    	result = searcher->search(query1);
    	CLUCENE_ASSERT(1 == result->length());
		_CLDELETE(result);
		_CLDELETE(query1);

    	result = searcher->search(query2);
    	CLUCENE_ASSERT(2 == result->length());
		_CLDELETE(result);
		_CLDELETE(query2);
	
    	result = searcher->search(query3);
    	CLUCENE_ASSERT(1 == result->length());
		_CLDELETE(result);
		_CLDELETE(query3);
	
    	result = searcher->search(query4);
    	CLUCENE_ASSERT(2 == result->length());
		_CLDELETE(result);
		_CLDELETE(query4);

		indexStore.close();
		searcher->close();
		reader->close();
		_CLDELETE(reader);
		_CLDELETE(searcher);
	}

	void testQuestionmark(CuTest *tc){
		RAMDirectory indexStore;
		SimpleAnalyzer an;
		IndexWriter* writer = _CLNEW IndexWriter(&indexStore, &an, true);
		Document doc1;
		Document doc2;
		Document doc3;
		Document doc4;
		doc1.add(*Field::Text(_T("body"), _T("metal")));
		doc2.add(*Field::Text(_T("body"), _T("metals")));
		doc3.add(*Field::Text(_T("body"), _T("mXtals")));
		doc4.add(*Field::Text(_T("body"), _T("mXtXls")));
		writer->addDocument(&doc1);
		writer->addDocument(&doc2);
		writer->addDocument(&doc3);
		writer->addDocument(&doc4);
    	writer->optimize();
		writer->close();
		_CLDELETE(writer);
        //////////////////////////////////////////////////////
    	
		IndexReader* reader = IndexReader::open(&indexStore);
		IndexSearcher* searcher = _CLNEW IndexSearcher(reader);
		
		Term* term = _CLNEW Term(_T("body"), _T("m?tal"));
		Query* query1 = _CLNEW WildcardQuery(term); 
		_CLDECDELETE(term);

			term = _CLNEW Term(_T("body"), _T("metal?"));
		Query* query2 = _CLNEW WildcardQuery(term);
			_CLDECDELETE(term);

			term = _CLNEW Term(_T("body"), _T("metals?"));
		Query* query3 = _CLNEW WildcardQuery(term);
		_CLDECDELETE(term);

		term = _CLNEW Term(_T("body"), _T("m?t?ls"));
		Query* query4 = _CLNEW WildcardQuery(term);
		_CLDECDELETE(term);


    	Hits* result = NULL;
	
    	result = searcher->search(query1);
    	CLUCENE_ASSERT(1 == result->length());
		_CLDELETE(result);
		_CLDELETE(query1);

    	result = searcher->search(query2);
    	CLUCENE_ASSERT(2 == result->length());
		_CLDELETE(result);
		_CLDELETE(query2);
	
    	result = searcher->search(query3);
    	CLUCENE_ASSERT(1 == result->length());
		_CLDELETE(result);
		_CLDELETE(query3);
	
    	result = searcher->search(query4);
    	CLUCENE_ASSERT(3 == result->length());
		_CLDELETE(result);
		_CLDELETE(query4);
	
		indexStore.close();
		reader->close();
		searcher->close();
		_CLDELETE(reader);
		_CLDELETE(searcher);
	}
#else
	void _NO_WILDCARD_QUERY(CuTest *tc){
		CuNotImpl(tc,_T("Wildcard"));
	}
	void _NO_FUZZY_QUERY(CuTest *tc){
		CuNotImpl(tc,_T("Fuzzy"));
	}
#endif


CuSuite *testwildcard(void)
{
	CuSuite *suite = CuSuiteNew(_T("CLucene Wildcard Test"));

	#ifndef NO_FUZZY_QUERY
		SUITE_ADD_TEST(suite, testFuzzyQuery);
	#else
		SUITE_ADD_TEST(suite, _NO_FUZZY_QUERY);
	#endif
	#ifndef NO_WILDCARD_QUERY
		SUITE_ADD_TEST(suite, testQuestionmark);
		SUITE_ADD_TEST(suite, testAsterisk);
	#else
		SUITE_ADD_TEST(suite, _NO_WILDCARD_QUERY);
    #endif

    return suite; 
}
// EOF
