In Boost.Test, how to obtain the name of current test? - boost

In Boost.Test, how can I obtain the name of the current auto test case?
Example:
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(MyTest)
{
std::cerr << "Starting " << test_name << std::endl;
// lots of code here
std::cerr << "Ending " << test_name << std::endl;
}
In the example, I want the variable test_name to contain "MyTest".

There is an undocumented* function that may be called for that purpose. The following line will flush the name of the current test to cerr:
#include <boost/test/framework.hpp>
...
std::cerr << boost::unit_test::framework::current_test_case().p_name
<< std::endl;
Note however that using this API does not flush the parameters in case of parametrized tests.
You might also be interested in the test checkpoints** (which seems to be what you want to do.)
#include <boost/test/included/unit_test.hpp>
...
BOOST_AUTO_TEST_CASE(MyTest)
{
BOOST_TEST_CHECKPOINT("Starting");
// lots of code here
BOOST_TEST_CHECKPOINT("Ending");
}
EDIT
* The current_test_case() function is now documented, see the official Boost documentation.
** BOOST_TEST_CHECKPOINT was previously called BOOST_CHECKPOINT. See the Boost changelog (1.35.0).

A different question about suite names gives a way to extract the name rather than just printing it:
auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)

Related

Visual studio: How to create hyperlink in comments section in c++ code to go to different lines within the code

I need to create a hyper link in my comments section of my c++ code so that , when I click on the link, it should take me to a specific line of the same code base.
That specific line could be in same file or different, but of course the same project.
Is this possible in Visual studio when writing c/c++ code.
For example,
int main()
{
Marks m1(10,20);
Marks m2(30,40);
Marks m3,m4;
//Line 7
std::cout <<m1.get_int_marks()<<std::endl;
std::cout << m1.get_ext_marks() << std::endl;
m3 = m1 + m2;
//Line 14
std::cout << m3.get_int_marks() << std::endl;
std::cout << m3.get_ext_marks() << std::endl;
m4 = m1 - m2;
int x = 10 + 20;
std::cout << m4.get_int_marks() << std::endl;
std::cout << m4.get_ext_marks() << std::endl;
std::cout << x << std::endl;
return 0;
}
In the above code , can I create a hyperlink in the comments, at line 7 to go to line 14.
Used case can be to go to any line of any file in the project.
Thanks in advance.
In case you want a link to method, you can use empty function macro to prevent compiler warnings, privates, etc.
Put pseudo code inside and follow by F12 (Visual Studio) or similar key elsewhere.
Includes are often mandatory for jump to wanted method directly.
#define CALLERS(...)
#include "myClass.h"
#include "myFunctions.h"
CALLERS(myClass::myMethod; function();)

Long path name in SetCurrentDirectoryW

Using this msdn reference I created a small use case.
#include <iostream>
#include <windows.h>
int main()
{
const std::wstring dir_path = L"\\\\?\\c:\\temp\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory";
if (!CreateDirectoryW(dir_path.c_str(), NULL))
{
std::cout << " Error occurred while creation of directory!!! " << GetLastError() << std::endl;
}
else
{
std::cout << "Lenth is " << dir_path.length() << std::endl;
if (dir_path.length() > 255)
{
if (!SetCurrentDirectoryW(buf.c_str()))
{
std::cout << " Error occurred!!! " << GetLastError() << std::endl;
}
else
{
std::cout << " Successful!!!" << std::endl;
}
}
}
return 0;
}
However, I get the output as
Error occurred while creation of directory!!! 3
Error code 3 points me to
ERROR_PATH_NOT_FOUND
3 (0x3)
The system cannot find the path specified.
Are there any other way to use long path names in CreateDirectoryW or SetCurrentDirectoryW APIs that I am unaware of ?
PS:I am using VS 2015 Update 3 compiler on Windows 7.
You have to create each directory on the path one by one. So the parent directory to the directory you pass must exist before you call CreateDirectory.
This information can be found by reading the documentation. It specifically calls out that error code and says:
One or more intermediate directories do not exist; this function will only create the final directory in the path.

Can't get move constructor to run

C++11
I'm having trouble using the move constructor. I have a simple container class, called Number, whose only data member is an integer. I have the following code:
//Number.h
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
class Number
{
public:
Number();
Number(int ipar);
Number(const Number& src);
Number(Number&& src);
private:
int num;
};
#endif
and
//Number.cpp
#include "Number.h"
Number::Number()
{
std::cout << "default ctor" << std::endl;
}
Number::Number(int ipar) : num(ipar)
{
std::cout << "integer argument ctor" << std::endl;
}
Number::Number(const Number& src) : num(src.num)
{
std::cout << "copy ctor" << std::endl;
}
Number::Number(Number&& src) : num(src.num)
{
std::cout << "move ctor" << std::endl;
}
and
//main.cpp
#include "Number.h"
using namespace std;
int main()
{
cout << "Part A:" << endl;
Number n1(1);
cout << "Part B:" << endl;
Number n2(n1);
cout << "Part C:" << endl;
Number n3{Number{n1}};
cout << "Part D:" << endl;
Number n4(Number(n1));
return 0;
}
This outputs:
Part A:
integer argument ctor
Part B:
copy ctor
Part C:
copy ctor
Part D:
Notice there is no output for Part D. The output for Parts A and B are what I expected, but the output for the others aren’t.
I expected this for Parts C and D:
Part C:
copy ctor
move ctor
Part D:
copy ctor
move ctor
Expectation for Part C:
I expected the Number{n1} part of Number n3{Number{n1}} to make a temporary nameless Number object, because there is no name between Number and the opening curly brace, by calling the copy constructor with n1. Then I expected Number n3 to be constructed by calling the move constructor with the temporary object.
Expectation for Part D:
Since this is like Part C, except with parentheses instead of curly braces, I expected this part to behave and output in the same way I expected Part C to.
Question:
Why does the actual output differ from my expectations and what is the correct way to get my desired output?
Note: If you want to compile this in Visual Studio, you need the Visual C++ Compiler November 2012 CTP or later for Visual Studio 2012 in order to support the uniform initialization syntax.
n4 is a function declaration. n3 is caused by copy elision.
Check that here I've enabled -fno-elide-constructors to avoid copy elision. n3 then shows a sequence of copy and move constructors.
There's a commented out line that tries to use n4 as an object. If you uncomment it, you'll see the compiler error telling it's a function instead.
For n4 do not be interpreted as a function declaration you could put extra parentheses around the temporary to prevent it from being viewed as a function parameter: Number n4((Number(n1))). With this and -fno-elide-constructors all you've expected happens.
Note that -fno-elide-constructors is not present as an option in MSVC.

UpdateDriverForPlugAndPlayDevices error is telling me I'm *not* doing something that I am

I'm working on a means of installing a driver. Because of the multiple platforms on which this must work, I'm shelling-out to both devcon and dpinst to do the work of driver install/update/removal when needed. While testing, I'm having problems with the shelling out to devcon. To isolate, I wrote a small app to do what devcon does in update see here, using the devcon source from the WinDDK for reference. I'm having some problems with UpdateDriverForPlugAndPlayDevices() from Setup API (actually part of Newdev.dll) see here. The source code is here:
#include <iostream>
#include <Windows.h>
#include <newdev.h>
int main(int argc, char** argv) {
// Go through the same steps as does dev con for this update crap
char infFile[MAX_PATH];
if(3 > argc) {
std::cerr << "an INF and HW ID must be specified" << std::endl;
return 1;
}
DWORD result(GetFullPathName(argv[1], MAX_PATH, infFile, NULL));
if((result >= MAX_PATH) || (0 == result)) {
std::cerr << "path is too long for buffer" << std::endl;
return 1;
}
if(GetFileAttributes(infFile) == -1) {
std::cerr << "file doesn't exist" << std::endl;
return 1;
}
BOOL reboot(FALSE);
if(!UpdateDriverForPlugAndPlayDevices(NULL, argv[2], infFile, INSTALLFLAG_FORCE, &reboot)) {
std::cerr << "Failed to install the driver. Code: "
<< GetLastError()
<< std::endl;
return 2;
}
if(reboot) {
std::cout << "A reboot is needed to complete driver install"
<< std::endl;
}
return 0;
}
The program fails when UpdateDriverForPlugAndPlayDevices() returns false. This then prints the error code, returned by GetLastError(), so I'd know what went wrong. The error code returned: 259. According to this resource says this is ERROR_NO_MORE_ITEMS. According to the link for UpdateDriverForPlugAndPlayDevices(), this function returns this error code when, "The function found a match for the HardwareId value, but the specified driver was not a better match than the current driver and the caller did not specify the INSTALLFLAG_FORCE flag." You'll notice from my code that I did specify this flag.
I do not know where to go from here. Can someone please identify from this code what it is I'm missing? This just has the "feel" of something simple, but I'm totally missing it.
Thank you,
Andy
The problem appeared to be not with the code but with the INF file. Interesting that the documentation for the function said that using that flag will force the install but didn't when the INF file didn't "list" any device classes in the models section. This is how I was able to install eventually. I added the correct device class to the models section in the INF.
EDIT Sep. 17, 2020
It was requested by someone just today (of the edit) to add an example from the INF. It's been 8 years since I had this issue and I no longer work for this team. However, as best as I can recall, and drawing heavily upon the docs for INF Models Section and INF Manufacturers Section, I hope this helps.
Essentially, the class is specified by the Models Section and the model is specified by the Manufacturer Section.
[Manufacturer]
%MfgName%=Standard,NTamd64
[Standard.NTamd64]
%DeviceString%=<class path or GUID>\<device>
[Strings]
MfgName=ACME
DeviceString="Device Type"

How to display a #defined constant during build, in Visual Studio 2010?

I've seen this, but none of the answers worked for VS2010. The constant's (or should I call it variable?) numerical value didn't get displayed
This line of code #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX is turning out to be true when I'm actually programming in windows. I need to see the value of OGRE_PLATFORM_WIN32 and OGRE_PLATFORM_LINUX during the build process itself. Could you help with how to go about it?
You can check the preprocessor output using:
/E - preprocess to stdout or
/P - preprocess to file or
/EP - preprocess to stdout without #line directives
options in visual studio
First, check the preprocessor defines in project options - active configuration and all configurations, and make sure the right things are defined.
If you are still having problems, try substituting this for your main method:
#include <iostream>
int main()
{
#ifdef OGRE_PLATFORM_LINUX
std::cout << "OGRE_PLATFORM_LINUX = " << OGRE_PLATFORM_LINUX << "\n";
#else
std::cout << "OGRE_PLATFORM_LINUX not defined.\n";
#endif
#ifdef OGRE_PLATFORM_WIN32
std::cout << "OGRE_PLATFORM_WIN32 = " << OGRE_PLATFORM_WIN32 << "\n";
#else
std::cout << "OGRE_PLATFORM_WIN32 not defined.\n";
#endif
#ifdef OGRE_PLATFORM
std::cout << "OGRE_PLATFORM = " << OGRE_PLATFORM << "\n";
#else
std::cout << "OGRE_PLATFORM not defined.\n";
#endif
return 0;
}
Also, did you create the project, was it created by some type of pre-make (CMake, automake, etc) system, did you download it from somewhere? If you didn't create it, somebody could have ported over some Linux code without checking their preprocessor options.

Resources