Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: installer/src/installer-lib/test/process_test.cpp

Issue 4790070691233792: Case-insensitive string class (Closed)
Patch Set: Created March 30, 2014, 7:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: installer/src/installer-lib/test/process_test.cpp
===================================================================
--- a/installer/src/installer-lib/test/process_test.cpp
+++ b/installer/src/installer-lib/test/process_test.cpp
@@ -10,8 +10,14 @@
//-------------------------------------------------------
const wchar_t exact_exe_name[] = L"installer-ca-tests.exe" ;
+const std::wstring exact_exe_string( exact_exe_name ) ;
+const wstring_ci exact_exe_string_ci( exact_exe_name ) ;
+
const wchar_t mixedcase_exe_name[] = L"Installer-CA-Tests.exe" ;
-const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, L"non-matching-name" } ;
+const wstring_ci mixedcase_exe_string_ci( mixedcase_exe_name ) ;
+
+const wchar_t unknown_name[] = L"non-matching-name" ;
+const wchar_t * multiple_exe_names[] = { mixedcase_exe_name, unknown_name } ;
/**
* Compare to our own process name, case-sensitive, no length limit
@@ -21,7 +27,7 @@
{
bool operator()( const PROCESSENTRY32W & process )
{
- return 0 == wcscmp( process.szExeFile, exact_exe_name ) ;
+ return std::wstring( process.szExeFile ) == exact_exe_string ;
} ;
};
@@ -33,22 +39,34 @@
{
bool operator()( const PROCESSENTRY32W & process )
{
- return 0 == wcscmpi( process.szExeFile, mixedcase_exe_name ) ;
+ return wstring_ci( process.szExeFile ) == mixedcase_exe_string_ci ;
} ;
} ;
-/**
- * Compare to our own process name, case-insensitive, length-limited
- */
-struct our_process_by_name_CI_N
- : std::unary_function< PROCESSENTRY32W, bool >
+
+struct our_process_by_name_subclassed
+ : public process_by_any_exe_name_CI
{
- bool operator()( const PROCESSENTRY32W & process )
- {
- return 0 == wcsncmpi( process.szExeFile, mixedcase_exe_name, sizeof( mixedcase_exe_name ) / sizeof( wchar_t ) ) ;
- } ;
+ our_process_by_name_subclassed( file_name_set( multiple_exe_names ) ) ;
} ;
+/**
+ * Filter by process name. Comparison is case-insensitive.
+ */
+class process_by_name_CI
+ : public std::unary_function< PROCESSENTRY32W, bool >
+{
+ const wstring_ci _name ;
+public:
+ bool operator()( const PROCESSENTRY32W & process )
+ {
+ return _name == wstring_ci( process.szExeFile ) ;
+ }
+
+ process_by_name_CI( const wchar_t * name )
+ : _name( name )
+ {}
+} ;
//-------------------------------------------------------
// TESTS, no snapshots
@@ -66,22 +84,21 @@
PROCESSENTRY32 process_explorer = process_with_name( L"explorer.exe" ) ;
PROCESSENTRY32 process_absent = process_with_name( L"no_such_name" ) ;
-exe_name_set multiple_name_set( multiple_exe_names, 2 ) ;
+file_name_set multiple_name_set( multiple_exe_names ) ;
process_by_any_exe_name_CI find_in_set( multiple_name_set ) ;
TEST( exe_name_set, validate_setup )
{
ASSERT_EQ( 2u, multiple_name_set.size() ) ;
- ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_name ) != multiple_name_set.end() ) ;
- ASSERT_TRUE( multiple_name_set.find( exact_exe_name ) != multiple_name_set.end() ) ;
+ ASSERT_TRUE( multiple_name_set.find( exact_exe_string_ci ) != multiple_name_set.end() ) ;
+ ASSERT_TRUE( multiple_name_set.find( mixedcase_exe_string_ci ) != multiple_name_set.end() ) ;
ASSERT_TRUE( multiple_name_set.find( L"" ) == multiple_name_set.end() ) ;
ASSERT_TRUE( multiple_name_set.find( L"not-in-list" ) == multiple_name_set.end() ) ;
}
TEST( process_by_any_exe_name_CI, empty )
{
- const wchar_t * elements[ 1 ] = { 0 } ; // cheating a bit
- exe_name_set s( elements, 0 ) ;
+ file_name_set s ;
process_by_any_exe_name_CI x( s ) ;
ASSERT_FALSE( x( process_empty ) ) ;
@@ -91,10 +108,10 @@
ASSERT_FALSE( x( process_absent ) ) ;
}
-TEST( process_by_any_exe_name_CI, single_element )
+TEST( process_by_any_exe_name_CI, single_element_known )
{
const wchar_t * elements[ 1 ] = { exact_exe_name } ;
- exe_name_set s( elements, 1 ) ;
+ file_name_set s( elements ) ;
process_by_any_exe_name_CI x( s ) ;
ASSERT_FALSE( x( process_empty ) ) ;
@@ -104,9 +121,22 @@
ASSERT_FALSE( x( process_absent ) ) ;
}
+TEST( process_by_any_exe_name_CI, single_element_unknown )
+{
+ const wchar_t * elements[ 1 ] = { unknown_name } ;
+ file_name_set s( elements ) ;
+ process_by_any_exe_name_CI x( s ) ;
+
+ ASSERT_FALSE( x( process_empty ) ) ;
+ ASSERT_FALSE( x( process_exact ) ) ;
+ ASSERT_FALSE( x( process_mixedcase ) ) ;
+ ASSERT_FALSE( x( process_explorer ) ) ;
+ ASSERT_FALSE( x( process_absent ) ) ;
+}
+
TEST( process_by_any_exe_name_CI, two_elements )
{
- exe_name_set s( multiple_exe_names, 2 ) ;
+ file_name_set s( multiple_exe_names ) ;
process_by_any_exe_name_CI x( s ) ;
ASSERT_FALSE( find_in_set( process_empty ) ) ;
@@ -164,12 +194,12 @@
/**
* The only process we are really guaranteed to have is this test process itself.
- * This test uses a filter function class with a special, fixed name comparison.
+ * This test uses the generic filter function.
*/
-TEST( Process_List_Test, find_our_process_CI_N_special )
+TEST( Process_List_Test, find_our_process_CI_generic )
{
std::vector< PROCESSENTRY32W > v ;
- initialize_process_list( v, our_process_by_name_CI_N(), copy_all() ) ;
+ initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all() ) ;
unsigned int size( v.size() );
EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simultaneously
ASSERT_GE( 1u, size );
@@ -177,12 +207,12 @@
/**
* The only process we are really guaranteed to have is this test process itself.
- * This test uses the generic filter function.
+ * This test uses same one used in Process_Closer
*/
-TEST( Process_List_Test, find_our_process_CI_N_generic )
+TEST( Process_List_Test, find_our_process_CI_as_used )
{
std::vector< PROCESSENTRY32W > v ;
- initialize_process_list( v, process_by_name_CI( mixedcase_exe_name ), copy_all() ) ;
+ initialize_process_list( v, process_by_any_exe_name_CI( file_name_set( multiple_exe_names ) ), copy_all() ) ;
unsigned int size( v.size() );
EXPECT_EQ( 1u, size ); // Please, don't run multiple test executables simultaneously
ASSERT_GE( 1u, size );

Powered by Google App Engine
This is Rietveld