From 820e2543b6f95b1e70335fd3b656bdd5d522a9aa Mon Sep 17 00:00:00 2001 From: Strahinja Markovic Date: Sun, 15 Apr 2012 20:10:39 -0700 Subject: [PATCH] Bugfixes and more tests --- cpp/Candidate.cpp | 2 +- cpp/Completer.cpp | 5 ++ cpp/Completer.h | 1 + cpp/Result.cpp | 13 ++-- cpp/tests/Completer_test.cpp | 128 ++++++++++++++++++++++++++--------- 5 files changed, 110 insertions(+), 39 deletions(-) diff --git a/cpp/Candidate.cpp b/cpp/Candidate.cpp index cccc933e..53916224 100644 --- a/cpp/Candidate.cpp +++ b/cpp/Candidate.cpp @@ -36,7 +36,7 @@ std::string GetWordBoundaryChars( const std::string &text ) ( i > 0 && text[ i - 1 ] == '_' && isalpha( text[ i ] ) ) ) { - result.push_back( tolower( text[ 0 ] ) ); + result.push_back( tolower( text[ i ] ) ); } } diff --git a/cpp/Completer.cpp b/cpp/Completer.cpp index 6c71cab5..9d069f27 100644 --- a/cpp/Completer.cpp +++ b/cpp/Completer.cpp @@ -21,6 +21,11 @@ namespace YouCompleteMe { +Completer::Completer( const Pylist &candidates ) +{ + AddCandidatesToDatabase( candidates ); +} + Completer::~Completer() { foreach ( Candidate* candidate, candidates_ ) diff --git a/cpp/Completer.h b/cpp/Completer.h index 0b3f4569..6005fdc7 100644 --- a/cpp/Completer.h +++ b/cpp/Completer.h @@ -37,6 +37,7 @@ class Completer { public: Completer() {} + Completer( const Pylist &candidates ); ~Completer(); void AddCandidatesToDatabase( const Pylist &candidates ); diff --git a/cpp/Result.cpp b/cpp/Result.cpp index 0bb42f1e..e3a7d2a8 100644 --- a/cpp/Result.cpp +++ b/cpp/Result.cpp @@ -27,14 +27,14 @@ namespace YouCompleteMe namespace { -int NumWordBoundaryCharMatches( const std::string &text, +int NumWordBoundaryCharMatches( const std::string &query, const std::string &word_boundary_chars ) { int i = 0; int j = 0; - while ( j < text.size() && i < word_boundary_chars.size() ) + while ( j < query.size() && i < word_boundary_chars.size() ) { - if ( toupper( text[ j ] ) == toupper( word_boundary_chars[ i ] ) ) + if ( toupper( query[ j ] ) == toupper( word_boundary_chars[ i ] ) ) ++i; ++j; } @@ -69,7 +69,7 @@ Result::Result( bool is_subsequence, text_( text ) { if ( is_subsequence ) - SetResultFeaturesFromQuery( word_boundary_chars, query ); + SetResultFeaturesFromQuery( query, word_boundary_chars ); } @@ -157,8 +157,9 @@ void Result::SetResultFeaturesFromQuery( if ( query.empty() || text_->empty() ) return; - first_char_same_in_query_and_text_ = query[ 0 ] == (*text_)[ 0 ]; - int num_wb_matches = NumWordBoundaryCharMatches( *text_, + first_char_same_in_query_and_text_ = + toupper( query[ 0 ] ) == toupper( (*text_)[ 0 ] ); + int num_wb_matches = NumWordBoundaryCharMatches( query, word_boundary_chars ); ratio_of_word_boundary_chars_in_query_ = num_wb_matches / static_cast< double >( query.length() ); diff --git a/cpp/tests/Completer_test.cpp b/cpp/tests/Completer_test.cpp index 870af782..8173807c 100644 --- a/cpp/tests/Completer_test.cpp +++ b/cpp/tests/Completer_test.cpp @@ -42,6 +42,38 @@ std::vector ToStringVector( const boost::python::list &pylist ) return values; } +Pylist Candidates( const std::string &a, + const std::string &b = std::string(), + const std::string &c = std::string(), + const std::string &d = std::string(), + const std::string &e = std::string(), + const std::string &f = std::string(), + const std::string &g = std::string(), + const std::string &h = std::string(), + const std::string &i = std::string() ) +{ + Pylist candidates; + candidates.append( a ); + if ( !b.empty() ) + candidates.append( b ); + if ( !c.empty() ) + candidates.append( c ); + if ( !d.empty() ) + candidates.append( d ); + if ( !e.empty() ) + candidates.append( e ); + if ( !f.empty() ) + candidates.append( f ); + if ( !g.empty() ) + candidates.append( g ); + if ( !h.empty() ) + candidates.append( h ); + if ( !i.empty() ) + candidates.append( i ); + + return candidates; +} + } // unnamed namespace class CompleterTest : public ::testing::Test @@ -56,30 +88,19 @@ class CompleterTest : public ::testing::Test TEST_F( CompleterTest, OneCandidate ) { - Pylist candidates; - candidates.append( "foobar" ); - - Completer completer; - completer.AddCandidatesToDatabase( candidates ); - Pylist results; - completer.GetCandidatesForQuery( "fbr", results ); + Completer( Candidates( "foobar" ) ).GetCandidatesForQuery( "fbr", results ); EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar" ) ); } TEST_F( CompleterTest, ManyCandidateSimple ) { - Pylist candidates; - candidates.append( "foobar" ); - candidates.append( "foobartest" ); - candidates.append( "Foobartest" ); - - Completer completer; - completer.AddCandidatesToDatabase( candidates ); - Pylist results; - completer.GetCandidatesForQuery( "fbr", results ); + Completer( Candidates( + "foobar", + "foobartest", + "Foobartest" ) ).GetCandidatesForQuery( "fbr", results ); EXPECT_THAT( ToStringVector( results ), WhenSorted( ElementsAre( "Foobartest", @@ -89,15 +110,10 @@ TEST_F( CompleterTest, ManyCandidateSimple ) TEST_F( CompleterTest, FirstCharSameAsQueryWins ) { - Pylist candidates; - candidates.append( "foobar" ); - candidates.append( "afoobar" ); - - Completer completer; - completer.AddCandidatesToDatabase( candidates ); - Pylist results; - completer.GetCandidatesForQuery( "fbr", results ); + Completer( Candidates( + "foobar", + "afoobar" ) ).GetCandidatesForQuery( "fbr", results ); EXPECT_THAT( ToStringVector( results ), ElementsAre( "foobar", @@ -106,19 +122,67 @@ TEST_F( CompleterTest, FirstCharSameAsQueryWins ) TEST_F( CompleterTest, CompleteMatchForWordBoundaryCharsWins ) { - Pylist candidates; - candidates.append( "FooBarQux" ); - candidates.append( "FBaqux" ); - - Completer completer; - completer.AddCandidatesToDatabase( candidates ); - Pylist results; - completer.GetCandidatesForQuery( "fbq", results ); + Completer( Candidates( + "FooBarQux", + "FBaqux" ) ).GetCandidatesForQuery( "fbq", results ); EXPECT_THAT( ToStringVector( results ), ElementsAre( "FooBarQux", "FBaqux" ) ); + + Pylist results2; + Completer( Candidates( + "CompleterTest", + "CompleteMatchForWordBoundaryCharsWins" + ) ).GetCandidatesForQuery( "ct", results2 ); + + EXPECT_THAT( ToStringVector( results2 ), + ElementsAre( "CompleterTest", + "CompleteMatchForWordBoundaryCharsWins" ) ); + + Pylist results3; + Completer( Candidates( + "FooBar", + "FooBarRux" + ) ).GetCandidatesForQuery( "fbr", results3 ); + + EXPECT_THAT( ToStringVector( results3 ), + ElementsAre( "FooBarRux", + "FooBar" ) ); +} + +TEST_F( CompleterTest, RatioUtilizationTieBreak ) +{ + Pylist results; + Completer( Candidates( + "FooBarQux", + "FooBarQuxZaa" ) ).GetCandidatesForQuery( "fbq", results ); + + EXPECT_THAT( ToStringVector( results ), + ElementsAre( "FooBarQux", + "FooBarQuxZaa" ) ); +} + +TEST_F( CompleterTest, ShorterCandidateWins ) +{ + Pylist results; + Completer( Candidates( + "FooBarQux", + "FaBarQux" ) ).GetCandidatesForQuery( "fbq", results ); + + EXPECT_THAT( ToStringVector( results ), + ElementsAre( "FaBarQux", + "FooBarQux" ) ); + + Pylist results2; + Completer( Candidates( + "CompleterT", + "CompleterTest" ) ).GetCandidatesForQuery( "co", results2 ); + + EXPECT_THAT( ToStringVector( results2 ), + ElementsAre( "CompleterT", + "CompleterTest" ) ); } } // namespace YouCompleteMe