TDD 2 – Search a Null-Terminated String

이게 원래 순서상으로는 앞이지만, 단순히 string search 관련 함수들의 사용법에 대한것

#include    <cstring>
#include    <gtest/gtest.h>

using namespace std;

int main( int argc, char *argv[] ){
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

bool is_top_level_domain( const char *url ){
    const char *tld[] = { “.com”, “.net”, “.org” };
    const char *p;

    for( int i = 0; i < 3; i++ ){
        p = strstr( url, tld[i] );
        if( p && p != url ){
            return true;
        }
    }

    return false;
}

const char *get_domain_name_from_emailaddress( const char *emailaddress ){
    const char *p = strchr( emailaddress, ‘@’ );
    return (p+1);
}

const char *get_found_emailchar_info( const char *emailaddress ){
    return strpbrk( emailaddress, “@.” );
}

TEST(unittest, is_top_level_domain_true){
    ASSERT_TRUE( is_top_level_domain(“HerbSchildt.com”) );
    ASSERT_TRUE( is_top_level_domain(“Apache.org”) );
}

TEST(unittest, is_top_level_domain_false){
    ASSERT_FALSE( is_top_level_domain(“HerbSchildt.co.kr”) );
    ASSERT_FALSE( is_top_level_domain(“.com”) );
}

TEST(unittest, get_domain_name_true){
    const char *p = get_domain_name_from_emailaddress( “Herb@herbSchildt.com” );
    ASSERT_STRCASEEQ( “herbSchildt.com”, (p) );
}

TEST(unittest, get_domain_name_false){
    const char *p = get_domain_name_from_emailaddress( “Herb@” );
    ASSERT_TRUE( 0 == *p );
}

TEST(unittest, get_found_info_ok){
    const char *p = get_found_emailchar_info( “Herb@herbSchildt.com”);
    ASSERT_TRUE( p != NULL );
}

TEST(unittest, get_found_info_ok2){
    const char *p = get_found_emailchar_info( “HerbherbSchildt.com”);
    ASSERT_TRUE( p != NULL );
}

TEST(unittest, get_found_info_fail){
    const char *p = get_found_emailchar_info( “HerbherbSchildtcom”);
    ASSERT_FALSE( p != NULL );
}