DECLARE_TEST( string, queries ) { { char teststr[] = ""; char teststr2[] = "test"; char teststr3[] = "testing long string with more than 16 characters"; char teststr4[] = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; char* str1 = string_substr( teststr, 0, 0 ); char* str2 = string_substr( teststr2, 0, 4 ); char* str3 = string_substr( teststr2, 0, 20 ); char* str4 = string_substr( teststr3, 0, 0 ); char* str5 = string_substr( teststr3, 0, 10 ); char* str6 = string_substr( teststr3, 0, STRING_NPOS ); char* str7 = string_substr( teststr4, 0, 0 ); char* str8 = string_substr( teststr4, 0, 20 ); char* str9 = string_substr( teststr4, 0, STRING_NPOS ); EXPECT_EQ( 0, str1[0] ); EXPECT_EQ( 0, str2[ string_length( str2 ) ] ); EXPECT_EQ( 0, str3[ string_length( str3 ) ] ); EXPECT_EQ( 0, str4[ string_length( str4 ) ] ); EXPECT_EQ( 0, str5[ string_length( str5 ) ] ); EXPECT_EQ( 0, str6[ string_length( str6 ) ] ); EXPECT_EQ( 0, str7[ string_length( str7 ) ] ); EXPECT_EQ( 0, str8[ string_length( str8 ) ] ); EXPECT_EQ( 0, str9[ string_length( str9 ) ] ); EXPECT_EQ( strlen( teststr ), string_length( str1 ) ); EXPECT_EQ( strlen( teststr2 ), string_length( str2 ) ); EXPECT_EQ( strlen( teststr2 ), string_length( str3 ) ); EXPECT_EQ( 0U, string_length( str4 ) ); EXPECT_EQ( 10U, string_length( str5 ) ); EXPECT_EQ( strlen( teststr3 ), string_length( str6 ) ); EXPECT_EQ( 0U, string_length( str7 ) ); EXPECT_EQ( 20U, string_length( str8 ) ); EXPECT_EQ( strlen( teststr4 ), string_length( str9 ) ); string_deallocate( str1 ); string_deallocate( str2 ); string_deallocate( str3 ); string_deallocate( str4 ); string_deallocate( str5 ); string_deallocate( str6 ); string_deallocate( str7 ); string_deallocate( str8 ); string_deallocate( str9 ); } { //Only ASCII characters, so wstring->string conversion should not introduce any extra UTF-8 sequences wchar_t teststr[] = L""; wchar_t teststr2[] = L"test"; wchar_t teststr3[] = L"testing long string with more than 16 characters"; wchar_t teststr4[] = L"01234567890123456789012345678901234567890123456789012345678901234567890123456789"; char* str1 = string_allocate_from_wstring( teststr, 0 ); char* str2 = string_allocate_from_wstring( teststr2, 0 ); char* str3 = string_allocate_from_wstring( teststr2, 20 ); char* str4 = string_allocate_from_wstring( teststr3, 0 ); char* str5 = string_allocate_from_wstring( teststr3, 10 ); char* str6 = string_allocate_from_wstring( teststr3, STRING_NPOS ); char* str7 = string_allocate_from_wstring( teststr4, 0 ); char* str8 = string_allocate_from_wstring( teststr4, 20 ); char* str9 = string_allocate_from_wstring( teststr4, STRING_NPOS ); EXPECT_EQ( 0, str1[0] ); EXPECT_EQ( 0, str2[ string_length( str2 ) ] ); EXPECT_EQ( 0, str3[ string_length( str3 ) ] ); EXPECT_EQ( 0, str4[ string_length( str4 ) ] ); EXPECT_EQ( 0, str5[ string_length( str5 ) ] ); EXPECT_EQ( 0, str6[ string_length( str6 ) ] ); EXPECT_EQ( 0, str7[ string_length( str7 ) ] ); EXPECT_EQ( 0, str8[ string_length( str8 ) ] ); EXPECT_EQ( 0, str9[ string_length( str9 ) ] ); EXPECT_EQ( wcslen( teststr ), string_length( str1 ) ); EXPECT_EQ( wcslen( teststr2 ), string_length( str2 ) ); EXPECT_EQ( wcslen( teststr2 ), string_length( str3 ) ); EXPECT_EQ( wcslen( teststr3 ), string_length( str4 ) ); EXPECT_EQ( 10U, string_length( str5 ) ); EXPECT_EQ( wcslen( teststr3 ), string_length( str6 ) ); EXPECT_EQ( wcslen( teststr4 ), string_length( str7 ) ); EXPECT_EQ( 20U, string_length( str8 ) ); EXPECT_EQ( wcslen( teststr4 ), string_length( str9 ) ); string_deallocate( str1 ); string_deallocate( str2 ); string_deallocate( str3 ); string_deallocate( str4 ); string_deallocate( str5 ); string_deallocate( str6 ); string_deallocate( str7 ); string_deallocate( str8 ); string_deallocate( str9 ); } { char* emptystr = string_allocate( 0 ); char* shortstr = string_clone( "short string" ); char* longstr = string_clone( "testing utility string methods like finds, split, merge, explode and similar." ); { unsigned int find = string_find( longstr, ' ', 0 ); unsigned int find2 = string_find( longstr, 12, 0 ); unsigned int find3 = string_find( emptystr, ' ', 0 ); unsigned int find4 = string_find( shortstr, ' ', 0 ); unsigned int find5 = string_find( shortstr, 'z', 0 ); unsigned int findofs = string_find( longstr, ' ', find ); unsigned int findofs2 = string_find( longstr, ' ', find + 1 ); unsigned int findofs3 = string_find( longstr, 'z', 10 ); unsigned int findofs4 = string_find( emptystr, 'z', STRING_NPOS ); unsigned int findofs5 = string_find( shortstr, 's', 10 ); unsigned int findofs6 = string_find( shortstr, 's', findofs5 ); unsigned int findofs7 = string_find( shortstr, 't', 0 ); unsigned int findofs8 = string_find( shortstr, ' ', 5 ); unsigned int findofs9 = string_find( longstr, ' ', STRING_NPOS ); EXPECT_EQ( find, 7U ); EXPECT_EQ( find2, STRING_NPOS ); EXPECT_EQ( find3, STRING_NPOS ); EXPECT_EQ( find4, 5U ); EXPECT_EQ( find5, STRING_NPOS ); EXPECT_EQ( findofs, find ); EXPECT_EQ( findofs2, 15U ); EXPECT_EQ( findofs3, STRING_NPOS ); EXPECT_EQ( findofs4, STRING_NPOS ); EXPECT_EQ( findofs5, STRING_NPOS ); EXPECT_EQ( findofs6, STRING_NPOS ); EXPECT_EQ( findofs7, 4U ); EXPECT_EQ( findofs8, 5U ); EXPECT_EQ( findofs9, STRING_NPOS ); } { unsigned int rfind = string_rfind( longstr, ' ', STRING_NPOS ); unsigned int rfind2 = string_rfind( longstr, ';', STRING_NPOS ); unsigned int rfind3 = string_rfind( emptystr, ';', STRING_NPOS ); unsigned int rfind4 = string_rfind( shortstr, 's', STRING_NPOS ); unsigned int rfind5 = string_rfind( shortstr, 'z', STRING_NPOS ); unsigned int rfindofs = string_rfind( longstr, ' ', rfind ); unsigned int rfindofs2 = string_rfind( longstr, ' ', rfind - 1 ); unsigned int rfindofs3 = string_rfind( longstr, ' ', string_length( longstr ) - 1 ); unsigned int rfindofs4 = string_rfind( emptystr, ' ', 0 ); unsigned int rfindofs5 = string_rfind( longstr, ' ', 0 ); unsigned int rfindofs6 = string_rfind( shortstr, 's', 5 ); unsigned int rfindofs7 = string_rfind( shortstr, 's', 0 ); EXPECT_EQ( rfind, 68U ); EXPECT_EQ( rfind2, STRING_NPOS ); EXPECT_EQ( rfind3, STRING_NPOS ); EXPECT_EQ( rfind4, 6U ); EXPECT_EQ( rfind5, STRING_NPOS ); EXPECT_EQ( rfindofs, rfind ); EXPECT_EQ( rfindofs2, 64U ); EXPECT_EQ( rfindofs3, rfind ); EXPECT_EQ( rfindofs4, STRING_NPOS ); EXPECT_EQ( rfindofs5, STRING_NPOS ); EXPECT_EQ( rfindofs6, 0U ); EXPECT_EQ( rfindofs7, 0U ); } { unsigned int findstr = string_find_string( longstr, "st", 0 ); unsigned int findstr2 = string_find_string( longstr, "xwqz", 0 ); unsigned int findstr3 = string_find_string( emptystr, "xwqz", 0 ); unsigned int findstr4 = string_find_string( longstr, "", 0 ); unsigned int findstr5 = string_find_string( longstr, "dslike", 0 ); unsigned int findstr6 = string_find_string( shortstr, "rt", 0 ); unsigned int findstr7 = string_find_string( shortstr, "long key that does not exist", 0 ); unsigned int findstr8 = string_find_string( shortstr, "so", 0 ); unsigned int findstrofs = string_find_string( longstr, "st", findstr ); unsigned int findstrofs2 = string_find_string( longstr, "st", findstr + 1 ); unsigned int findstrofs3 = string_find_string( longstr, "xwqz", string_length( longstr ) ); unsigned int findstrofs4 = string_find_string( emptystr, "xwqz", string_length( emptystr ) ); unsigned int findstrofs5 = string_find_string( shortstr, "", 5 ); unsigned int findstrofs6 = string_find_string( shortstr, "string", 0 ); unsigned int findstrofs7 = string_find_string( shortstr, "string", 7 ); unsigned int findstrofs8 = string_find_string( longstr, "utility", 14 ); unsigned int findstrofs9 = string_find_string( longstr, "", string_length( longstr ) ); unsigned int findstrofs10 = string_find_string( longstr, "", STRING_NPOS ); unsigned int findstrofs11 = string_find_string( longstr, "string", STRING_NPOS ); EXPECT_EQ( findstr, 2U ); EXPECT_EQ( findstr2, STRING_NPOS ); EXPECT_EQ( findstr3, STRING_NPOS ); EXPECT_EQ( findstr4, 0U ); EXPECT_EQ( findstr5, STRING_NPOS ); EXPECT_EQ( findstr6, 3U ); EXPECT_EQ( findstr7, STRING_NPOS ); EXPECT_EQ( findstr8, STRING_NPOS ); EXPECT_EQ( findstrofs, findstr ); EXPECT_EQ( findstrofs2, 16U ); EXPECT_EQ( findstrofs3, STRING_NPOS ); EXPECT_EQ( findstrofs4, STRING_NPOS ); EXPECT_EQ( findstrofs5, 5U ); EXPECT_EQ( findstrofs6, 6U ); EXPECT_EQ( findstrofs7, STRING_NPOS ); EXPECT_EQ( findstrofs8, STRING_NPOS ); EXPECT_EQ( findstrofs9, strlen( longstr ) ); EXPECT_EQ( findstrofs10, STRING_NPOS ); EXPECT_EQ( findstrofs11, STRING_NPOS ); } { unsigned int rfindstr = string_rfind_string( longstr, ", ", STRING_NPOS ); unsigned int rfindstr2 = string_rfind_string( longstr, ":;", STRING_NPOS ); unsigned int rfindstr3 = string_rfind_string( emptystr, ":;", STRING_NPOS ); unsigned int rfindstr4 = string_rfind_string( longstr, "", STRING_NPOS ); unsigned int rfindstr5 = string_rfind_string( shortstr, "string", STRING_NPOS ); unsigned int rfindstr6 = string_rfind_string( shortstr, " tring", STRING_NPOS ); unsigned int rfindstrofs = string_rfind_string( longstr, ", ", rfindstr ); unsigned int rfindstrofs2 = string_rfind_string( longstr, ", ", rfindstr - 1 ); unsigned int rfindstrofs3 = string_rfind_string( longstr, ":;", 0 ); unsigned int rfindstrofs4 = string_rfind_string( emptystr, ":;", 0 ); unsigned int rfindstrofs5 = string_rfind_string( longstr, "", 5 ); unsigned int rfindstrofs6 = string_rfind_string( shortstr, "ort str", 6 ); unsigned int rfindstrofs7 = string_rfind_string( shortstr, "ort str", 1 ); EXPECT_EQ( rfindstr, 55U ); EXPECT_EQ( rfindstr2, STRING_NPOS ); EXPECT_EQ( rfindstr3, STRING_NPOS ); EXPECT_EQ( rfindstr4, strlen( longstr ) ); EXPECT_EQ( rfindstr5, 6U ); EXPECT_EQ( rfindstr6, STRING_NPOS ); EXPECT_EQ( rfindstrofs, rfindstr ); EXPECT_EQ( rfindstrofs2, 48U ); EXPECT_EQ( rfindstrofs3, STRING_NPOS ); EXPECT_EQ( rfindstrofs4, STRING_NPOS ); EXPECT_EQ( rfindstrofs5, 5U ); EXPECT_EQ( rfindstrofs6, 2U ); EXPECT_EQ( rfindstrofs7, STRING_NPOS ); } { unsigned int findof = string_find_first_of( longstr, "ui", 0 ); unsigned int findof2 = string_find_first_of( longstr, ";:", 0 ); unsigned int findof3 = string_find_first_of( emptystr, "", 0 ); unsigned int findof4 = string_find_first_of( emptystr, " ", 0 ); unsigned int findof5 = string_find_first_of( shortstr, "", 0 ); unsigned int findofofs = string_find_first_of( longstr, "ui", findof ); unsigned int findofofs2 = string_find_first_of( longstr, "ui", findof - 1 ); unsigned int findofofs3 = string_find_first_of( longstr, "ui", findof + 1 ); unsigned int findofofs4 = string_find_first_of( longstr, "ui", string_length( longstr ) ); unsigned int findofofs5 = string_find_first_of( emptystr, "", string_length( emptystr ) ); unsigned int findofofs6 = string_find_first_of( shortstr, "string", 6 ); unsigned int findofofs7 = string_find_first_of( shortstr, "", 6 ); unsigned int findofofs8 = string_find_first_of( longstr, "", 10 ); unsigned int findofofs9 = string_find_first_of( longstr, "", string_length( longstr ) ); EXPECT_EQ( findof, 4U ); EXPECT_EQ( findof2, STRING_NPOS ); EXPECT_EQ( findof3, STRING_NPOS ); EXPECT_EQ( findof4, STRING_NPOS ); EXPECT_EQ( findof5, STRING_NPOS ); EXPECT_EQ( findofofs, 4U ); EXPECT_EQ( findofofs2, 4U ); EXPECT_EQ( findofofs3, 8U ); EXPECT_EQ( findofofs4, STRING_NPOS ); EXPECT_EQ( findofofs5, STRING_NPOS ); EXPECT_EQ( findofofs6, 6U ); EXPECT_EQ( findofofs7, STRING_NPOS ); EXPECT_EQ( findofofs8, STRING_NPOS ); EXPECT_EQ( findofofs9, STRING_NPOS ); } { unsigned int findnotof = string_find_first_not_of( longstr, "testing ", 0 ); unsigned int findnotof2 = string_find_first_not_of( longstr, longstr, 0 ); unsigned int findnotof3 = string_find_first_not_of( shortstr, "", 0 ); unsigned int findnotofofs = string_find_first_not_of( longstr, "testing ", findnotof ); unsigned int findnotofofs2 = string_find_first_not_of( longstr, "testing ", findnotof + 1 ); unsigned int findnotofofs3 = string_find_first_not_of( longstr, "testing ", string_length( longstr ) ); unsigned int findnotofofs4 = string_find_first_not_of( shortstr, "", string_length( shortstr ) ); EXPECT_EQ( findnotof, 8U ); EXPECT_EQ( findnotof2, STRING_NPOS ); EXPECT_EQ( findnotof3, 0U ); EXPECT_EQ( findnotofofs, 8U ); EXPECT_EQ( findnotofofs2, 11U ); EXPECT_EQ( findnotofofs3, STRING_NPOS ); EXPECT_EQ( findnotofofs4, STRING_NPOS ); } { unsigned int findlastof = string_find_last_of( longstr, "xp", STRING_NPOS ); unsigned int findlastof2 = string_find_last_of( longstr, ";:", STRING_NPOS ); unsigned int findlastof3 = string_find_last_of( emptystr, "", STRING_NPOS ); unsigned int findlastof4 = string_find_last_of( shortstr, "", STRING_NPOS ); unsigned int findlastofofs = string_find_last_of( longstr, "xp", findlastof ); unsigned int findlastofofs2 = string_find_last_of( longstr, "xp", findlastof - 2 ); unsigned int findlastofofs3 = string_find_last_of( longstr, "xp", 0 ); unsigned int findlastofofs4 = string_find_last_of( emptystr, "", 0 ); unsigned int findlastofofs5 = string_find_last_of( shortstr, "", 5 ); unsigned int findlastofofs6 = string_find_last_of( shortstr, "", string_length( shortstr ) ); unsigned int findlastofofs7 = string_find_last_of( shortstr, "short", 5 ); EXPECT_EQ( findlastof, 59U ); EXPECT_EQ( findlastof2, STRING_NPOS ); EXPECT_EQ( findlastof3, STRING_NPOS ); EXPECT_EQ( findlastof4, STRING_NPOS ); EXPECT_EQ( findlastofofs, 59U ); EXPECT_EQ( findlastofofs2, 44U ); EXPECT_EQ( findlastofofs3, STRING_NPOS ); EXPECT_EQ( findlastofofs4, STRING_NPOS ); EXPECT_EQ( findlastofofs5, STRING_NPOS ); EXPECT_EQ( findlastofofs6, STRING_NPOS ); EXPECT_EQ( findlastofofs7, 4U ); } { unsigned int findlastnotof = string_find_last_not_of( longstr, " similar.", STRING_NPOS ); unsigned int findlastnotof2 = string_find_last_not_of( longstr, longstr, STRING_NPOS ); unsigned int findlastnotof3 = string_find_last_not_of( emptystr, "", STRING_NPOS ); unsigned int findlastnotof4 = string_find_last_not_of( shortstr, "", STRING_NPOS ); unsigned int findlastnotof5 = string_find_last_not_of( longstr, " similar", STRING_NPOS ); unsigned int findlastnotofofs = string_find_last_not_of( longstr, " similar.", findlastnotof ); unsigned int findlastnotofofs2 = string_find_last_not_of( longstr, " and similar.", findlastnotof - 1 ); unsigned int findlastnotofofs3 = string_find_last_not_of( longstr, longstr, 0 ); unsigned int findlastnotofofs4 = string_find_last_not_of( emptystr, "", 0 ); unsigned int findlastnotofofs5 = string_find_last_not_of( shortstr, "string", 5 ); unsigned int findlastnotofofs6 = string_find_last_not_of( shortstr, "string ", 5 ); unsigned int findlastnotofofs7 = string_find_last_not_of( shortstr, "", 5 ); unsigned int findlastnotofofs8 = string_find_last_not_of( longstr, "", string_length( longstr ) ); EXPECT_EQ( findlastnotof, 67U ); EXPECT_EQ( findlastnotof2, STRING_NPOS ); EXPECT_EQ( findlastnotof3, STRING_NPOS ); EXPECT_EQ( findlastnotof4, 11U ); EXPECT_EQ( findlastnotof5, 76U ); EXPECT_EQ( findlastnotofofs, 67U ); EXPECT_EQ( findlastnotofofs2, 63U ); EXPECT_EQ( findlastnotofofs3, STRING_NPOS ); EXPECT_EQ( findlastnotofofs4, STRING_NPOS ); EXPECT_EQ( findlastnotofofs5, 5U ); EXPECT_EQ( findlastnotofofs6, 2U ); EXPECT_EQ( findlastnotofofs7, 5U ); EXPECT_EQ( findlastnotofofs8, strlen( longstr ) - 1 ); } string_deallocate( emptystr ); string_deallocate( shortstr ); string_deallocate( longstr ); } return 0; }
int main(int argc, char* argv[]) { string_t* pstr_str1a = create_string(); string_t* pstr_str1b = create_string(); string_t* pstr_str1c = create_string(); string_t* pstr_str2a = create_string(); string_t* pstr_str3a = create_string(); string_t* pstr_str4a = create_string(); size_t t_pos = 0; if(pstr_str1a == NULL || pstr_str1b == NULL || pstr_str1c == NULL || pstr_str2a == NULL || pstr_str3a == NULL || pstr_str4a == NULL) { return -1; } /* Searching a string for a substring as specified by a string */ string_init_cstr(pstr_str1a, "12-ab-12-ab"); string_init_cstr(pstr_str1b, "b-a"); string_init_cstr(pstr_str1c, "12"); printf("The original string str1a is \"%s\".\n", string_c_str(pstr_str1a)); t_pos = string_find_last_not_of(pstr_str1a, pstr_str1b, 5); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'b-a' in str1a before the 5th position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'b-a' were not found in the string str1a.\n"); } t_pos = string_find_last_not_of(pstr_str1a, pstr_str1c, NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '12' in str1a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring '12' were not found in the string str1a.\n"); } string_destroy(pstr_str1a); string_destroy(pstr_str1b); string_destroy(pstr_str1c); /* Searching a string for a substring as specified by a c-string */ string_init_cstr(pstr_str2a, "BBB-111"); printf("The original string str2a is \"%s\".\n", string_c_str(pstr_str2a)); t_pos = string_find_last_not_of_cstr(pstr_str2a, "B1", 6); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'B1' in str2a before the 6th position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'B1' were not found in the string str2a.\n"); } t_pos = string_find_last_not_of_cstr(pstr_str2a, "B-1", NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'B-1' in str2a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'B-1' were not found in the string str2a.\n"); } string_destroy(pstr_str2a); /* Searching a string for a substring as specified by part of a c-string */ string_init_cstr(pstr_str3a, "444-555-GGG"); printf("The original string str3a is \"%s\".\n", string_c_str(pstr_str3a)); t_pos = string_find_last_not_of_cstr(pstr_str3a, "45G", NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '45G' in str3a is %u.\n", t_pos); } else { printf("Elements other than those in the substring '45G' were not found in the string str3a.\n"); } t_pos = string_find_last_not_of_subcstr(pstr_str3a, "45G", 6, t_pos - 1); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of '45G' in str3a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring '45G' were not found in the string str3a.\n"); } string_destroy(pstr_str3a); /* Searching a string for a substring as specified by a single character */ string_init_cstr(pstr_str4a, "dddd-1dd4-abdd"); printf("The original string str4a is \"%s\".\n", string_c_str(pstr_str4a)); t_pos = string_find_last_not_of_char(pstr_str4a, 'd', 7); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'd' in str4a is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'd' were not found in the string str4a.\n"); } t_pos = string_find_last_not_of_char(pstr_str4a, 'x', NPOS); if(t_pos != NPOS) { printf("The index of the last non occurrence of an element of 'x' in str4a position is %u.\n", t_pos); } else { printf("Elements other than those in the substring 'x' were not found in the string str4a.\n"); } string_destroy(pstr_str4a); return 0; }