Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8992619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:59:52+00:00 2026-06-15T22:59:52+00:00

I have a program written in C++ and to make sure we don’t break

  • 0

I have a program written in C++ and to make sure we don’t break anything when making a change I would like to add unit tests.

In the program we used macros to create certain objects that are frequently used. This would look like:

#define PROPERTY_SWITCHPOINT(var) \
private: \
   comp::SwitchPoint* m##var; \
public: \
   void set##var(bool val, unsigned short switchIdx = 0) \
   {\
      if(m##var)  m##var->setValue(val,switchIdx); \
   }\
   bool get##var() \
   {\
      return (NULL == m##var ? false : m##var->getValue()); \
   }\
   comp::SwitchPoint* get##var##Ptr() \
   {\
      return m##var; \
   }

In the header of the class that contains the switchpoints we call the macro

class Classname
{
    private:
        PROPERTY_SWITCHPOINT(SwitchpointObject)
}

In the constructor of the class that contains the switchpoints we then do:

Classname::Classname()
{
    mSwitchpointObject = CreateSwitchpoint("Switchpoint name", 2);
}

comp::SwitchPoint* Classname::CreateSwitchpoint(const std::string& name, unsigned short numberOfSwitches = 1)
{
    comp::SwitchPoint* sp = new comp::SwitchPoint(name, true, numberOfSwitches);
    return sp;
}

Now we can use mSwitchpointObject->getValue() to obtain this object’s value. All of this works, but I can’t manage to create unit tests for it, where I’m using the unittest++ framework.
I tried it with this test:

#include "UnitTest++.h"
#include "Classname.h"

namespace
{
    TEST(SwitchpointTest1)
    {
        PROPERTY_SWITCHPOINT(SwitchpointObject)
        mSwitchpointTestVariabele           = CreateSwitchpoint("test switchpoint", 2);
        CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
        //mSwitchpointTestVariabele->setValue(false, 0);
        //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
        //mSwitchpointTestVariabele->setValue(false, 1);
        //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
        //mSwitchpointTestVariabele->setValue(true, 0);
        //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
        //mSwitchpointTestVariabele->setValue(true, 1);
        //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
    }
}

But that gives me compiler errors:

|  |In member function 'virtual void<unnamed>::TestSwitchpointTest1::RunImpl() const':|
| 9|error: expected primary-expression before 'private'|
| 9|error: expected ';' before 'private'|
| 9|error: expected primary-expression before 'public'|
| 9|error: expected ';' before 'public'|
| 9|error: a function-definition is not allowed here before '{' token|
| 9|error: a function-definition is not allowed here before '{' token|
|10|error: 'mSwitchpointTestVariabele' was not declared in this scope|
|10|error: 'CreateSwitchpoint' was not declared in this scope|
|  |=== Build finished: 8 errors, 0 warnings ===|

I guessed the problem is that the macro creates a part of the code that needs to be tested and the unit test is performed before this part is created, but from Aleguna’s reply I understand this can’t be the problem.

How should I write a test for code like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:59:53+00:00Added an answer on June 15, 2026 at 10:59 pm

    I would suggest taking a look at what code is actually generated, since you have a compilation problem, it sounds like the macro is not generating the code you are expecting it to. If you are using windows/VS you can use “cl.exe /E” or in g++/gcc you can use “gcc -E source.cpp” to run the preprocessor and generate the c++ file that will be compiled.

    Unit tests can only be run once the code is successfully compiled (and can be tested like you would unit test any other code).

    Your unit test code is expanding to this:

     #include "UnitTest++.h"
        #include "Classname.h"
    
        namespace
        {
            TEST(SwitchpointTest1)
            {
                    private:
           comp::SwitchPoint* mSwitchpointObject;
        public:
           void setSwitchpointObject(bool val, unsigned short switchIdx = 0)
               {
                  if(mSwitchpointObject)  mSwitchpointObject->setValue(val,switchIdx);
               }
           bool getSwitchpointObject()
           {
              return (NULL == mSwitchpointObject ? false : mSwitchpointObject->getValue()); 
           }
           comp::SwitchPoint* getSwitchpointObject##Ptr() 
           {
              return mSwitchpointObject; 
           }
    
    
     mSwitchpointTestVariabele           = CreateSwitchpoint("test switchpoint", 2);
            CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
            //mSwitchpointTestVariabele->setValue(false, 0);
            //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
            //mSwitchpointTestVariabele->setValue(false, 1);
            //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
            //mSwitchpointTestVariabele->setValue(true, 0);
            //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), false);
            //mSwitchpointTestVariabele->setValue(true, 1);
            //CHECK_EQUAL(mSwitchpointTestVariabele->getValue(), true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program written in python, and I would like to make it
I would like to see a 'Hello world' program written in C/C++ but made
I have a program that needs a class file written to make it run
I am attempting to make alarm program. So far I have written an activity
I have program written in C. It takes 2 arguments username/password and try to
I Have a program written in VB6 that reads a long text file and
I have my program written in C++ and it is can be successfully compiled
I have a program written in C++, on Linux, compiled with -g. When I
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I have a program written in C using ncurses. It let user input and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.