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