I need some explanation about the following piece of code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: I need some explanation about the following piece of code

  1. #1
    Join Date
    Mar 2013
    Posts
    31

    I need some explanation about the following piece of code

    Hello,
    I am trying to understand the following code written in unix platform about unitTest:

    /****************************************/
    #define TEST(name,classUnderTest)
    class classUnderTest##name##Test : public Test
    {
    public:
    classUnderTest##name##Test () : Test (#name "Test") {}
    void setup() {};
    void teardown() {};
    void runTest (TestResult& result_);
    } classUnderTest##name##Instance;
    void classUnderTest##name##Test::runTest (TestResult& result_)

    /*****************************************************/

    Could anyone explain what does ##name## stand for? I would like to use in visual studio 2010.

    Please provide some suggestion as I am a newbie in writing unitTest.


    Thanks,

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Hello,
    I am trying to understand the following code written in unix platform about unitTest:
    Look up how to use the C++ preprocessor and token pasting.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by Paul McKenzie View Post
    Look up how to use the C++ preprocessor and token pasting.

    Regards,

    Paul McKenzie
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    Code:
    #define TESTWITHSETUP(name,classUnderTest)
    	class classUnderTest##name##Test : public Test, name##Setup
    	{ 
    		public: 
    			classUnderTest##name##Test () : Test (#name "Test") {} 
                void setup() {name##Setup::setup();} 
                void teardown() {name##Setup::teardown();} 
    			void runTest (TestResult& result_); 
    	} classUnderTest##name##Instance; 
    	void classUnderTest##name##Test::runTest (TestResult& result_)

    In the following piece of code,

    "class classUnderTest##name##Test : public Test, name##Setup" , where name##Setup has not been define yet, how can it be used. Please give some clarification regarding this kind of syntax. I would like to run and compile some of this kind of code in visual studio 2010. It would be good, if I am referred to some tutorial website.

    Thanks.
    {

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    ... the following piece of code when I tried to paste in visual studio 2010, it showed error.
    Code:
    #define TESTWITHSETUP(name,classUnderTest)
    	...
    What is the error code and error message?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    That is not a full program. You don't even show usage of the macro.
    Please give some clarification regarding this kind of syntax.
    Do you understand what "token pasting" is and what the C++ preprocessor handles token pasting? That is all the information you need to look up to figure out what ## is supposed to do.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
    From MSDN:
    Use line concatenation — place a backslash (\) immediately before the newline character — for long directives on multiple source lines.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:

    1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
    2. What does the encircled line 2 (i.e. #name “Test”) mean?
    3. What does the encircled line 3 imply? (name##Setup::setup()) as name##Setup class has notyet been defined?
    4. At marking 4, “classUnderTest##name##Instance”, is it an instance of “classclassUnderTest##name##Test”, usually this kind of definition is used while writing stuct.
    5. In 5, the function “runTest” has again been defined, why?

    Thanks in advance.
    Attached Images Attached Images  

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:
    You didn't circle the most important part of the whole thing, and that is the first line.

    Do you know what that #define does? What those parameters are? If you do, then all you need to do is see what happens if you invoked the macro with a sample line:
    Code:
    TESTWITHSETUP(name1, name2)
    Take that line, apply what you learned about token pasting, and expand that macro with those parameters. What do you ultimately end up with once the macro is expanded?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
    A macro need not have the class predefined. When the macro is expanded (something I asked you to do in the previous post, so that you see what it does), then the compiler will expect that the class is already defined, but not before. That macro is perfectly valid.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by Paul McKenzie View Post
    A macro need not have the class predefined. When the macro is expanded (something I asked you to do in the previous post, so that you see what it does), then the compiler will expect that the class is already defined, but not before. That macro is perfectly valid.

    Regards,

    Paul McKenzie
    Hi, thanks for the reply. Actually I am trying to run a simple unit test project (that I have downloaded from the net) to test how it works as I need to write some code for unit test in C++. I have attached the zipped folder. I can compile the project in visual studio 2010, but while running, i get the following error:

    /******************************************************************************************************/
    fixture_test.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Test1MyFixtureTest::Test1MyFixtureTest(void)" (??0Test1MyFixtureTest@@QAE@XZ)
    1>simplest_test.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
    1>fixture_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
    1>simplest_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
    1>C:\Users\MYG741\Documents\Visual Studio 2010\Projects\Unit_test\Debug\Unit_test.exe : fatal error LNK1120: 2 unresolved externals


    /******************************************************************************************************/

    I don't understand what the problem is. It would be very kind, it some help me figure out what's going wrong. My goal is to learn to write unit test in C++. I don't want to use any external libraries. Please provide me some help regarding this.

    Thanks in advance.
    Attached Files Attached Files

  11. #11
    Join Date
    Apr 1999
    Posts
    27,427

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    I can compile the project in visual studio 2010, but while running, i get the following error:
    Those are linker errors, not runtime errors.

    Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?

    Also, the following compiles:
    Code:
    #include <string>
    
    #define TESTWITHSETUP(name,classUnderTest)\
        class classUnderTest##name##Test : public Test, name##Setup\
        { \
             public: \
                 classUnderTest##name##Test () : Test (#name "Test") {} \
                 void setup() {name##Setup::setup();} \
                 void teardown() {name##Setup::teardown();} \
                 void runTest (TestResult& result_); \
        };\
        void classUnderTest##name##Test::runTest (TestResult& result_) {}
    
        class Test
        {
            public:
                Test(const std::string& s) {}
        };
    
        class Name1Setup 
        {
            public:
                void setup() {}
                void teardown() {}
        };
        
        typedef int TestResult;
        
        TESTWITHSETUP(Name1,Name2)
        
        int main()
        { }
    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by Paul McKenzie View Post
    Those are linker errors, not runtime errors.

    Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?

    Also, the following compiles:
    Code:
    #include <string>
    
    #define TESTWITHSETUP(name,classUnderTest)\
        class classUnderTest##name##Test : public Test, name##Setup\
        { \
             public: \
                 classUnderTest##name##Test () : Test (#name "Test") {} \
                 void setup() {name##Setup::setup();} \
                 void teardown() {name##Setup::teardown();} \
                 void runTest (TestResult& result_); \
        };\
        void classUnderTest##name##Test::runTest (TestResult& result_) {}
    
        class Test
        {
            public:
                Test(const std::string& s) {}
        };
    
        class Name1Setup 
        {
            public:
                void setup() {}
                void teardown() {}
        };
        
        typedef int TestResult;
        
        TESTWITHSETUP(Name1,Name2)
        
        int main()
        { }
    Regards,

    Paul McKenzie
    Thank you very much for the reply. Actually I would like to know how unit test works. I don't understand how unit test. I need some full program in unit test which i can run and understand. Could you provide me with some "HelloWorld" type example for unit_test? Thanks in advance.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,382

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by jenny_wui View Post
    Thank you very much for the reply. Actually I would like to know how unit test works. I don't understand how unit test. I need some full program in unit test which i can run and understand. Could you provide me with some "HelloWorld" type example for unit_test? Thanks in advance.
    If you need further information about undertaking 'unit testing' I suggest you do a web search for info. There's a lot out there. A couple of sites that you may find of interest are

    http://stackoverflow.com/questions/3...isual-studio-c
    http://en.wikipedia.org/wiki/List_of...ing_frameworks
    http://msdn.microsoft.com/en-us/library/dd264975.aspx
    http://visualstudiomagazine.com/arti...plus-plus.aspx
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Mar 2013
    Posts
    31

    Re: I need some explanation about the following piece of code

    Quote Originally Posted by Paul McKenzie View Post
    Those are linker errors, not runtime errors.

    Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?

    Also, the following compiles:
    Code:
    #include <string>
    
    #define TESTWITHSETUP(name,classUnderTest)\
        class classUnderTest##name##Test : public Test, name##Setup\
        { \
             public: \
                 classUnderTest##name##Test () : Test (#name "Test") {} \
                 void setup() {name##Setup::setup();} \
                 void teardown() {name##Setup::teardown();} \
                 void runTest (TestResult& result_); \
        };\
        void classUnderTest##name##Test::runTest (TestResult& result_) {}
    
        class Test
        {
            public:
                Test(const std::string& s) {}
        };
    
        class Name1Setup 
        {
            public:
                void setup() {}
                void teardown() {}
        };
        
        typedef int TestResult;
        
        TESTWITHSETUP(Name1,Name2)
        
        int main()
        { }
    Regards,

    Paul McKenzie
    Hello, I am still a bit in the dark about unit test. Could you explain in your code, how tests are added and run. Also why TEST and TESTWITHSETUP these two macros are defined? What is the difference between these two macros.

    #define TEST(name,classUnderTest)\
    class classUnderTest##name##Test : public Test\
    { \
    public: \
    classUnderTest##name##Test () : Test (#name "Test") {} \
    void setup() {}; \
    void teardown() {}; \
    void runTest (TestResult& result_); \
    } classUnderTest##name##Instance; \
    void classUnderTest##name##Test::runTest (TestResult& result_) \


    #define TESTWITHSETUP(name,classUnderTest)\
    class classUnderTest##name##Test : public Test, name##Setup\
    { \
    public: \
    classUnderTest##name##Test () : Test (#name "Test") {} \
    void setup() {name##Setup::setup();} \
    void teardown() {name##Setup::teardown();} \
    void runTest (TestResult& result_); \
    } classUnderTest##name##Instance; \
    void classUnderTest##name##Test::runTest (TestResult& result_) \

    Also at the last line of the two macros why only runTest function has been defined? Please explain a bit more.

    Thanks in advance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center