summaryrefslogtreecommitdiffstats
path: root/indexlib/tests/ifile-test.cpp
blob: 9b9f92832d032d6e5ec044f4d110b17d8eab160a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <boost/test/unit_test.hpp>
#include "ifile.h"
#include <string>
#include <stdarg.h>

using namespace ::boost::unit_test;
namespace ifile_test {
//using indexlib::detail::ifile;
const char* fname = "ifile-test-delete-me";
void cleanup() {
	ifile::remove( fname );
}

inline
bool check_results( const ifile& ifi, const char* str, ... ) {
	const char* s;
	va_list args;
	va_start( args, str );
	std::vector<unsigned> res = ifi.search( str )->list();
	unsigned i = 0;

	while ( s = va_arg( args, const char* ) ) {
		if ( i == res.size() ) return false;
		if ( std::string( s ) != ifi.lookup_docname( res[ i++ ] ) ) return false;
	}
	va_end( args );
	return i == res.size();
}


inline
unsigned count_results( const ifile& ifi, const char* str ) {
	return ifi.search( str )->list().size();
}

void simple() {
	cleanup();
	ifile ifi( fname );
	ifi.add( "this", "doc" );
	BOOST_CHECK_EQUAL( ifi.search( "this" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "this" )->list()[ 0 ], 0 );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( ifi.search( "this" )->list()[ 0 ] ), "doc" );
	ifi.add( "that", "doc2" );
	BOOST_CHECK_EQUAL( ifi.search( "this" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "this" )->list()[ 0 ], 0 );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( ifi.search( "this" )->list()[ 0 ] ), "doc" );
	
	BOOST_CHECK_EQUAL( ifi.search( "that" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "that" )->list()[ 0 ], 1 );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( ifi.search( "that" )->list()[ 0 ] ), "doc2" );
}

void ndocs() {
	cleanup();
	ifile ifi( fname );
	ifi.add( "one", "one" );
	ifi.add( "one", "two" );
	BOOST_CHECK_EQUAL( ifi.ndocs(), 2 );

	ifi.add( "one", "three" );
	ifi.add( "one", "four" );

	BOOST_CHECK_EQUAL( ifi.ndocs(), 4 );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( 0 ), std::string( "one" ) );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( 1 ), std::string( "two" ) );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( 2 ), std::string( "three" ) );
	BOOST_CHECK_EQUAL( ifi.lookup_docname( 3 ), std::string( "four" ) );
}

void space() {
	cleanup();
	ifile ifi( fname );

	ifi.add( "one two three", "doc" );
	BOOST_CHECK_EQUAL( ifi.search( "two" )->list().size(), 1 );
}

void numbers() {
	cleanup();
	ifile ifi( fname );

	ifi.add( "one 123 123456789 four444 five", "doc" );
	BOOST_CHECK_EQUAL( ifi.search( "123" )->list().size(), 1 );
	BOOST_CHECK_EQUAL( ifi.search( "123456789" )->list().size(), 1 );
	BOOST_CHECK_EQUAL( ifi.search( "four444" )->list().size(), 1 );
	BOOST_CHECK_EQUAL( ifi.search( "five" )->list().size(), 1 );
}

void partial() {
	cleanup();
	ifile ifi( fname );
	ifi.add( "longword", "doc_0" );

	BOOST_CHECK_EQUAL( ifi.search( "l" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "long" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "longword" )->list().size(), 1u );

	BOOST_CHECK_EQUAL( ifi.search( "longword" )->list().size(), 1u );

	ifi.add( "longnord", "doc_1" );
	BOOST_CHECK_EQUAL( ifi.search( "l" )->list().size(), 2u );
	BOOST_CHECK_EQUAL( ifi.search( "long" )->list().size(), 2u );
	BOOST_CHECK_EQUAL( ifi.search( "longw" )->list().size(), 1u );
	BOOST_CHECK_EQUAL( ifi.search( "longn" )->list().size(), 1u );
}

void several() {
	cleanup();
	ifile ifi( fname );
	ifi.add( "one two three four", "0" );
	ifi.add( "two three four", "1" );
	ifi.add( "something else", "2" );
	ifi.add( "something two", "3" );
	ifi.add( "two something four", "4" );
	ifi.add( "else something", "5" );
	ifi.add( "else four", "6" );

	BOOST_CHECK_EQUAL( count_results( ifi, "one" ), 1u );
	BOOST_CHECK_EQUAL( count_results( ifi, "one two three four" ), 1u );
	BOOST_CHECK_EQUAL( count_results( ifi, "two three four" ), 2u );

	BOOST_CHECK_EQUAL( count_results( ifi, "one two" ), 1u );
	BOOST_CHECK_EQUAL( count_results( ifi, "one" ), 1u );

	BOOST_CHECK_EQUAL( count_results( ifi, "something else" ), 2u );
	BOOST_CHECK_EQUAL( count_results( ifi, "something two" ), 2u );
}

void remove_doc() {
	cleanup();
	ifile ifi( fname );
	ifi.add( "one two three four", "0" );
	ifi.add( "two three four", "1" );
	ifi.add( "three four five", "2" );
	ifi.remove_doc( "1" );

	BOOST_CHECK( check_results( ifi, "one", "0", NULL ) );
	BOOST_CHECK( check_results( ifi, "two", "0", NULL ) );
	BOOST_CHECK( check_results( ifi, "three", "0", "2", NULL ) );
	BOOST_CHECK_EQUAL( count_results( ifi, "four" ), 0u );
}

test_suite* get_suite() {
	test_suite* test = BOOST_TEST_SUITE( "Ifile tests" );
	test->add( BOOST_TEST_CASE( &simple ) );
	test->add( BOOST_TEST_CASE( &ndocs ) );
	test->add( BOOST_TEST_CASE( &space ) );
	//test->add( BOOST_TEST_CASE( &numbers ) );
	test->add( BOOST_TEST_CASE( &partial ) );
	test->add( BOOST_TEST_CASE( &several ) );
	test->add( BOOST_TEST_CASE( &remove) );
	return test;
}

} // namespace