"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.
Re: I need some explanation about the following piece of code
Originally Posted by jenny_wui
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.
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?
Re: I need some explanation about the following piece of code
Originally Posted by jenny_wui
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?
Re: I need some explanation about the following piece of code
Originally Posted by jenny_wui
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.
Re: I need some explanation about the following piece of code
Originally Posted by Paul McKenzie
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:
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.
Re: I need some explanation about the following piece of code
Originally Posted by jenny_wui
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()
{ }
Re: I need some explanation about the following piece of code
Originally Posted by Paul McKenzie
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.
Re: I need some explanation about the following piece of code
Originally Posted by jenny_wui
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: I need some explanation about the following piece of code
Originally Posted by Paul McKenzie
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.