There will be maintenance performed on git.ligo.org, chat.ligo.org, containers.lig.org, and docs.ligo.org starting at 9am PDT on Tuesday 18th August 2020. There will be an extremely small period of downtime at the start of the maintenance window as various services are restarted. Please address any comments, questions, or concerns to computing-help@igwn.org.

Commit c50fe3df authored by Jonathan Hanks's avatar Jonathan Hanks

Correct the comparision used so that lower/upper bounds work

The comparision function did not work properly.  Give it the length
of the expected key, and do not require the a specific input to the
comparision to be the search key.

This had caused issues where the key was not in the expected argument,
allowing strings that were shorter than the key length to be selected
as output.
parent 7afc4e8e
Pipeline #46145 passed with stage
in 11 minutes and 18 seconds
......@@ -64,16 +64,28 @@ namespace completion
inline bool
simple_comparison( const channel& a, const channel& b )
{
return std::strcmp( a.name.c_str( ), b.name.c_str( ) ) < 0;
return a.name < b.name;
}
inline bool
key_comparison( const std::string& key, const channel& element )
class key_comparison
{
int result =
std::strncmp( key.c_str( ), element.name.c_str( ), key.size( ) );
return result < 0;
}
public:
explicit key_comparison( std::string::size_type key_len )
: key_len_( key_len )
{
}
bool
operator( )( const std::string& str1, const std::string& str2 ) const
{
std::string::size_type n =
std::min( key_len_, std::max( str1.size( ), str2.size( ) ) );
int result = std::strncmp( str1.c_str( ), str2.c_str( ), n );
return result < 0;
}
private:
std::string::size_type key_len_;
};
inline Database
load_database( const std::string& path )
......@@ -105,9 +117,12 @@ namespace completion
boost::make_transform_iterator( begin_it_, detail::project_name );
auto end_it =
boost::make_transform_iterator( end_it_, detail::project_name );
auto lower = std::lower_bound( begin_it, end_it, key, key_comparison );
auto upper = std::upper_bound( lower, end_it, key, key_comparison );
auto key_size = key.size( );
auto key_size = key.size( );
key_comparison comp( key_size );
auto lower = std::lower_bound( begin_it, end_it, key, comp );
auto upper = std::upper_bound( lower, end_it, key, comp );
std::for_each(
lower, upper, [key_size, &results]( const std::string& entry ) {
static const std::string delimiters( "-_" );
......@@ -148,4 +163,4 @@ namespace completion
}
}
#endif // LIGO_CHANNEL_COMPLETION_HH
\ No newline at end of file
#endif // LIGO_CHANNEL_COMPLETION_HH
......@@ -220,6 +220,27 @@ TEST_CASE( "Basic completion behavior" )
}
}
TEST_CASE( "Channel completion should not remove user input" )
{
completion::Database db;
db.channels = {
{ "H0:VAC-SYSTEM" }, { "H0:VAC-SYSTEM_1_a" },
{ "H0:VAC-SYSTEM_1_b" }, { "H0:VAC-SYSTEM_1_c" },
{ "H0:VAC-SYSTEM_1_d" }, { "H0:VAC-SYSTEM_1_e" },
{ "H0:VAC-SYSTEM_1_f" },
};
{
std::vector< std::string > expected = {
"H0:VAC-SYSTEM_1_a", "H0:VAC-SYSTEM_1_b", "H0:VAC-SYSTEM_1_c",
"H0:VAC-SYSTEM_1_d", "H0:VAC-SYSTEM_1_e", "H0:VAC-SYSTEM_1_f",
};
completion::string_list results = completion::search(
db.channels.begin( ), db.channels.end( ), "H0:VAC-SYSTEM_1" );
REQUIRE( results == expected );
}
}
TEST_CASE( "Basic DB sorting" )
{
completion::Database input;
......
......@@ -253,6 +253,20 @@ TEST_CASE( "Basic completion behavior with an specified database with spaces "
run_search_test( input, "H0:VAC-SYSTEM_1", expected );
}
TEST_CASE( "Completions should never remove user input" )
{
std::vector< std::string > input = {
"H0:VAC-SYSTEM", "H0:VAC-SYSTEM_1_a", "H0:VAC-SYSTEM_1_b",
"H0:VAC-SYSTEM_1_c", "H0:VAC-SYSTEM_1_d", "H0:VAC-SYSTEM_1_e",
"H0:VAC-SYSTEM_1_f",
};
std::vector< std::string > expected = {
"H0:VAC-SYSTEM_1_a", "H0:VAC-SYSTEM_1_b", "H0:VAC-SYSTEM_1_c",
"H0:VAC-SYSTEM_1_d", "H0:VAC-SYSTEM_1_e", "H0:VAC-SYSTEM_1_f",
};
run_search_test( input, "H0:VAC-SYSTEM_1", expected );
}
TEST_CASE( "Basic sorting with a specified input database" )
{
std::vector< std::string > input = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment