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 );
}

Advertisements

TDD – std::map 에 대한 사용법 TDD

 매주 수요일 마다 회사에서 하기로 한 TDD 스터디 -_-


아직은 TDD를 잘 모르겠다라는 T.T




#include    <gtest/gtest.h>
#include    <iostream>
#include    <string>
#include    <map>
#include    <utility>


using namespace std;


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


TEST(MapTest, IteratorTest){
    map<string, string> phonemap;
    phonemap[“Tom”] = “555-1234”;
    phonemap[“Jane”]= “314 555-6576”;
    phonemap[“Ken”] = “660 555-9843”;
    map<string, string>::iterator itr;
    itr = phonemap.begin();
    ASSERT_STREQ( (itr->first).c_str(), “Jane” );
    ASSERT_STREQ( (itr->second).c_str(), “314 555-6576” );
    ++itr;
    ASSERT_STREQ( (itr->first).c_str(), “Ken” );
    ASSERT_STREQ( (itr->second).c_str(), “660 555-9843” );
    ++itr;
    ASSERT_STREQ( (itr->first).c_str(), “Tom” );
    ASSERT_STREQ( (itr->second).c_str(), “555-1234” );
}
TEST(MapTest, ReverseIteratorTest){
    map<string, string> phonemap;
    phonemap[“Tom”] = “555-1234”;
    phonemap[“Jane”]= “314 555-6576”;
    phonemap[“Ken”] = “660 555-9843”;
    map<string, string>::reverse_iterator ritr;
    ritr = phonemap.rbegin();
    ASSERT_STREQ( (ritr->first).c_str(), “Tom” );
    ASSERT_STREQ( (ritr->second).c_str(), “555-1234” );
    ++ritr;
    ASSERT_STREQ( (ritr->first).c_str(), “Ken” );
    ASSERT_STREQ( (ritr->second).c_str(), “660 555-9843” );
    ++ritr;
    ASSERT_STREQ( (ritr->first).c_str(), “Jane” );
    ASSERT_STREQ( (ritr->second).c_str(), “314 555-6576” );
}


TEST(MapTest, FindTest){
    map<string, string> phonemap;
    phonemap[“Tom”] = “555-1234”;
    phonemap[“Jane”]= “314 555-6576”;
    phonemap[“Ken”] = “660 555-9843”;
    map<string, string>::iterator itr;
    itr = phonemap.find(“Jane”);
    ASSERT_STREQ( (itr->second).c_str(), “314 555-6576” );
}
TEST(MapTest, AddSizeTest){
    map<string, string> phonemap;
    phonemap[“Tom”] = “555-1234”;
    phonemap[“Jane”]= “314 555-6576”;
    phonemap[“Ken”] = “660 555-9843”;
    ASSERT_TRUE( phonemap.size() == 3 );
    pair<map<string, string>::iterator, bool> result;
    result = phonemap.insert( pair<string, string>(“Jay”, “555-9999”));
    ASSERT_TRUE( phonemap.size() == 4 );
}
TEST(MapTest, DuplicateAddSizeTest){
    map<string, string> phonemap;
    phonemap[“Tom”] = “555-1234”;
    pair<map<string, string>::iterator, bool> result;
    result = phonemap.insert( pair<string, string>(“Tom”, “555-9999”));
    ASSERT_TRUE( phonemap.size() == 1 );
}