잠토의 잠망경

TDD C++ 실제 예제 2 본문

공부/TDD

TDD C++ 실제 예제 2

잠수함토끼 2013. 1. 1. 19:34

 

이어서 좀더 진행해보겠습니다.

Main.cpp를 다음과 같이 바꿔보겠습니다.

 

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

 

 

 

 

[main.cpp]

  1. #include "student.h"  
  2.     
  3.     
  4. int main(void)  
  5. {  
  6.     // 1. Test 1  
  7.     Student* s = new Student();  
  8.     
  9.     assert(s);  
  10.     
  11.     delete s;  
  12.     
  13.     
  14.     
  15.     // 2. Test 2  
  16.     // 3. Test 3  
  17.         
  18.     s = new Student("이원재",19,100979571,"서울시 관악구 XXXX");  
  19.     
  20.     assert(!strcmp(s->getName()"이원재"));  
  21.     assert(s->getAge() == 19);  
  22.     assert(s->getStudentID() == 100979571);  
  23.     assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));  
  24.     
  25.     delete s;  
  26.     
  27.     return 0;  
  28. }  

 

그러면 아래와 같이 class의 멤버함수가 없으므로 error를 발생합니다.

 

 

그럼 test code가 실패하지 않도록 코드를 수정하겠습니다.

 

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

 

 

[student.h]

  1. #undef UNICODE  
  2.     
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <assert.h>  
  6.     
  7.     
  8. class Student  
  9. {  
  10.     
  11. private:  
  12.     
  13.     char Name[1024];  
  14.     int age;  
  15.     int studentID;  
  16.     char address[1024];  
  17.     
  18. public:  
  19.     
  20.     Student();  
  21.     Student(char* _Name, int _age, int _studentID, char* _address);  
  22.     char* getName();  
  23.     int getAge();  
  24.     int getStudentID();  
  25.     char* getAddress();  
  26. };  

 

[student.cpp]

  1. #include "student.h"  
  2.     
  3. Student::Student()  
  4. {  
  5.     ::memset(Name,      0,sizeof(char)*1024);  
  6.     ::memset(address,   0,sizeof(char)*1024);  
  7.     
  8.     age         = 0;  
  9.     studentID   = 0;  
  10. }  
  11.     
  12.     
  13. Student::Student(char* _Name, int _age, int _studentID, char* _address)  
  14. {  
  15.     ::memset(Name,      0,sizeof(char)*1024);  
  16.     ::memset(address,   0,sizeof(char)*1024);  
  17.     
  18.     memcpy(Name,_Name,strlen(_Name)*sizeof(char));  
  19.     age = _age;  
  20.     studentID = _studentID;  
  21.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  22.     
  23. }  
  24.     
  25. char* Student::getName()  
  26. {  
  27.     return this->Name;  
  28. }  
  29.     
  30. int Student::getAge()  
  31. {  
  32.     return this->age;  
  33. }  
  34.     
  35. int Student::getStudentID()  
  36. {  
  37.     return this->studentID;  
  38. }  
  39.     
  40. char* Student::getAddress()  
  41. {  
  42.     return this->address;  
  43. }  

 

F5를 누르니 잘된다. ㅎㅎ 이제 Refectoring이다. 고고

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

우선 호출하는 부위에서 해당 포인터를 가지고 변경하는 실수를 막기위해 const를 추가해봤다.

 

[student.h]

  1. #undef UNICODE  
  2.     
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <assert.h>  
  6.     
  7.     
  8. class Student  
  9. {  
  10.     
  11. private:  
  12.     
  13.     char Name[1024];  
  14.     int age;  
  15.     int studentID;  
  16.     char address[1024];  
  17.     
  18. public:  
  19.     
  20.     Student();  
  21.     Student(char* _Name, int _age, int _studentID, char* _address);  
  22.     const char* getName();  
  23.     int getAge();  
  24.     int getStudentID();  
  25.     const char* getAddress();  
  26. };  

 

[student.cpp]

  1. #include "student.h"  
  2.     
  3. Student::Student()  
  4. {  
  5.     ::memset(Name,      0,sizeof(char)*1024);  
  6.     ::memset(address,   0,sizeof(char)*1024);  
  7.     
  8.     age         = 0;  
  9.     studentID   = 0;  
  10. }  
  11.     
  12.     
  13. Student::Student(char* _Name, int _age, int _studentID, char* _address)  
  14. {  
  15.     ::memset(Name,      0,sizeof(char)*1024);  
  16.     ::memset(address,   0,sizeof(char)*1024);  
  17.     
  18.     memcpy(Name,_Name,strlen(_Name)*sizeof(char));  
  19.     age = _age;  
  20.     studentID = _studentID;  
  21.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  22.     
  23. }  
  24.     
  25. const char* Student::getName()  
  26. {  
  27.     return this->Name;  
  28. }  
  29.     
  30. int Student::getAge()  
  31. {  
  32.     return this->age;  
  33. }  
  34.     
  35. int Student::getStudentID()  
  36. {  
  37.     return this->studentID;  
  38. }  
  39.     
  40. const char* Student::getAddress()  
  41. {  
  42.     return this->address;  
  43. }  

 

F5를 눌러보면 success 여기까지도 잘 되었다.

 

이제 get를 했는니 set되 해볼까? ㅋㅋ

 

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

 

아래와 같이 class 변수 접근 가능한 set 변수를 만들었다. ㅋㅋ 그럼 초기화 될까. Run Run Run

[main.cpp]

  1. #include "student.h"  
  2.     
  3.     
  4. int main(void)  
  5. {  
  6.     // 1. Test 1  
  7.     Student* s = new Student();  
  8.     
  9.     assert(s);  
  10.     
  11.     delete s;  
  12.     
  13.     
  14.     
  15.     // 2. Test 2  
  16.     // 3. Test 3  
  17.         
  18.     s = new Student("이원재",19,100979571,"서울시 관악구 XXXX");  
  19.     
  20.     assert(!strcmp(s->getName(), "이원재"));  
  21.     assert(s->getAge() == 19);  
  22.     assert(s->getStudentID() == 100979571);  
  23.     assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));  
  24.     
  25.     delete s;  
  26.     
  27.     // 4. Test 4  
  28.     s = new Student();  
  29.     
  30.     s->setName("이원재");  
  31.     s->setAge(19);  
  32.     s->setStudentID("100979571");  
  33.     s->setAddress("서울시 관악구 XXX");  
  34.     
  35.     assert(!strcmp(s->getName(), "이원재"));  
  36.     assert(s->getAge() == 19);  
  37.     assert(s->getStudentID() == 100979571);  
  38.     assert(!strcmp(s->getAddress(),"서울시 관악구 XXXX"));  
  39.     
  40.     delete s;  
  41.     
  42.     return 0;  
  43. }  

 

당연히 Fail이다. 계속 이런식이다. ㅎㅎ 우리는 수정하면서 고치면된다. 고고

 

 

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

 

[student.h]

  1. #undef UNICODE  
  2.     
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <assert.h>  
  6.     
  7.     
  8. class Student  
  9. {  
  10.     
  11. private:  
  12.     
  13.     char Name[1024];  
  14.     int age;  
  15.     int studentID;  
  16.     char address[1024];  
  17.     
  18. public:  
  19.     
  20.     Student();  
  21.     Student(char* _Name, int _age, int _studentID, char* _address);  
  22.         
  23.         
  24.     const char* getName();  
  25.     int getAge();  
  26.     int getStudentID();  
  27.     const char* getAddress();  
  28.     
  29.     
  30.     void setName( char* );  
  31.     void setAge( int _age );  
  32.     void setStudentID(int _studentID );  
  33.     void setAddress( char* _address );  
  34. };  

 

 

[student.cpp]

  1. #include "student.h"  
  2.     
  3. Student::Student()  
  4. {  
  5.     ::memset(Name,      0,sizeof(char)*1024);  
  6.     ::memset(address,   0,sizeof(char)*1024);  
  7.     
  8.     age         = 0;  
  9.     studentID   = 0;  
  10. }  
  11.     
  12.     
  13. Student::Student(char* _Name, int _age, int _studentID, char* _address)  
  14. {  
  15.     ::memset(Name,      0,sizeof(char)*1024);  
  16.     ::memset(address,   0,sizeof(char)*1024);  
  17.     
  18.     memcpy(Name,_Name,strlen(_Name)*sizeof(char));  
  19.     age = _age;  
  20.     studentID = _studentID;  
  21.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  22.     
  23. }  
  24.     
  25. const char* Student::getName()  
  26. {  
  27.     return this->Name;  
  28. }  
  29.     
  30. int Student::getAge()  
  31. {  
  32.     return this->age;  
  33. }  
  34.     
  35. int Student::getStudentID()  
  36. {  
  37.     return this->studentID;  
  38. }  
  39.     
  40. const char* Student::getAddress()  
  41. {  
  42.     return this->address;  
  43. }  
  44.     
  45. void Student::setName( char* _Name)  
  46. {  
  47.     memcpy(this->Name,_Name,strlen(_Name)*sizeof(char));  
  48. }  
  49.     
  50. void Student::setAge( int _age )  
  51. {  
  52.     age = _age;  
  53. }  
  54.     
  55. void Student::setStudentID(int _studentID )  
  56. {  
  57.     studentID = _studentID;  
  58. }  
  59.     
  60. void Student::setAddress( char* _address )  
  61. {  
  62.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  63. }  

 

F5를 눌러보자 아마 잘 돌꺼다. ㅋㅋㅋ

 

다음을 refactoring이다. 고고

 

  1. Write a test that fails
  2. Make the code work
  3. Eliminate redundancy

[student.h]

  1. #undef UNICODE  
  2.     
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <assert.h>  
  6.     
  7.     
  8. class Student  
  9. {  
  10.     
  11. private:  
  12.     
  13.     char Name[1024];  
  14.     int age;  
  15.     int studentID;  
  16.     char address[1024];  
  17.     
  18. public:  
  19.     
  20.     Student();  
  21.     Student(char* _Name, int _age, int _studentID, char* _address);  
  22.         
  23.         
  24.     const char* getName();  
  25.     int getAge();  
  26.     int getStudentID();  
  27.     const char* getAddress();  
  28.     
  29.     
  30.     void setName(const char* );  
  31.     void setAge( int _age );  
  32.     void setStudentID(int _studentID );  
  33.     void setAddress(const char* _address );  
  34. };  

 

[student.cpp]

  1. #include "student.h"  
  2.     
  3. Student::Student()  
  4. {  
  5.     ::memset(Name,      0,sizeof(char)*1024);  
  6.     ::memset(address,   0,sizeof(char)*1024);  
  7.     
  8.     age         = 0;  
  9.     studentID   = 0;  
  10. }  
  11.     
  12.     
  13. Student::Student(char* _Name, int _age, int _studentID, char* _address)  
  14. {  
  15.     ::memset(Name,      0,sizeof(char)*1024);  
  16.     ::memset(address,   0,sizeof(char)*1024);  
  17.     
  18.     memcpy(Name,_Name,strlen(_Name)*sizeof(char));  
  19.     age = _age;  
  20.     studentID = _studentID;  
  21.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  22.     
  23. }  
  24.     
  25. const char* Student::getName()  
  26. {  
  27.     return this->Name;  
  28. }  
  29.     
  30. int Student::getAge()  
  31. {  
  32.     return this->age;  
  33. }  
  34.     
  35. int Student::getStudentID()  
  36. {  
  37.     return this->studentID;  
  38. }  
  39.     
  40. const char* Student::getAddress()  
  41. {  
  42.     return this->address;  
  43. }  
  44.     
  45. void Student::setName(const char* _Name)  
  46. {  
  47.     memcpy(this->Name,_Name,strlen(_Name)*sizeof(char));  
  48. }  
  49.     
  50. void Student::setAge( int _age )  
  51. {  
  52.     age = _age;  
  53. }  
  54.     
  55. void Student::setStudentID(int _studentID )  
  56. {  
  57.     studentID = _studentID;  
  58. }  
  59.     
  60. void Student::setAddress( const char* _address )  
  61. {  
  62.     memcpy(address,_address,strlen(_address)*sizeof(char));  
  63. }  

 

고고 이렇게 하면 refactoring 완료완료

 

 

 

 

 

 

Comments