How to set Timeout in CppUnitTestFramework (C++) in Visual Studio? - c++

How to Add Timeout for a test method in C++ in Microsoft unit testing using CppUnitTestFramework ? Most of the Solutions I found online are for CSharp projects where I can add lines like [TEST_METHOD,TIME_OUT(80)] or such ,but those are not working while testing C++ (VC++) code
I have tried the below code
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../src/factorial_dp.cpp"
#include "stdio.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace spec
{
TEST_CLASS(factorial_dpSpec)
{
public:
//Add Timout for these methods
TEST_METHOD(Smallnumber)
{
int result = fact(5);
Assert::AreEqual(120, result, L"5 fact should be 120", LINE_INFO());
}
};
}

Use managed test classes.
and you can keep Timeouts in that .
[TestMethod(), Timeout(3000)]
void functionName()
{
//
}

Related

How to include System in C++/WinRT console application

Using Visual Studio 2019 16.10.2 how do you include .NET components in a C++ / WinRT Console programme?
The indexOf method of IVector requires a UInt32 struct from System.
How is the System utilised in this context? Trying to use the namespace results in a
"System' : a namespace with this name does not exist"
This has been covered already on SO, but only for a C++/CLI application and not in the context of a C++/WinRT console app. The solutions provided in that post also do not work.
#include "pch.h"
#include <Windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
using winrt::Windows::Foundation::Collections::IVector;
using namespace System;
int main()
{
IVector<int> foo;
foo.Append(1);
UInt32 bar;
foo.indexOf(1, bar);
return EXIT_SUCCESS;
}
In this case pch.h is empty.
When you select the appropriate language from the documentation's language dropdown list in the top right corner (C++/WinRT) you'll see the C++/WinRT-specific signature:
bool IndexOf(T const& value, uint32_t & index);
You'll need to replace
UInt32 bar;
with
uint32_t bar{};

How to set TextBlock properties programmatically using C++/WinRT + WinUI 3

I've been hiding under the MFC rock for many years so I can stick to standard C++ but still write Windows Desktop apps. With C++/WinRT and WinUI 3.0, it appears that I may finally have an opportunity to modernize my code. The problem is that I know nothing about XAML or the Windows API. To fix this problem, I'm trying to work my way through Petzold's "Programming Windows, 6th ed.", replacing the C# code with C++/WinRT. When all I have to do is write XAML, all is copacetic. However, when I get to p. 24, I'm supposed to adjust TextBlock properties in code. Here's the C#:
TextBlock tb = new TextBlock();
tb.Text = "Hello, Windows 8!";
tb.FontFamily = new FontFamily("Times New Roman");
tb.FontSize = 96;
tb.FontStyle = FontStyle.Italic;
...
and here's my attempt at a replacement:
TextBlock tb;
tb.Text(L"Hello, Windows 8!");
tb.FontFamily(FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(FontStyle::Italic);
...
All goes well until the last line. "FontStyle::Italic" is not recognized. I have similar issues with the enums for Color and HorizontalAlignment. What is the correct way to access these enums? Have I forgotten an include or a "using"? Here's what I currently have:
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.ApplicationModel.Activation.h>
#include <winrt/Microsoft.UI.Composition.h>
#include <winrt/Microsoft.UI.Text.h>
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Microsoft.UI.Xaml.Controls.Primitives.h>
#include <winrt/Microsoft.UI.Xaml.Data.h>
#include <winrt/Microsoft.UI.Xaml.Interop.h>
#include <winrt/Microsoft.UI.Xaml.Markup.h>
#include <winrt/Microsoft.UI.Xaml.Media.h>
#include <winrt/Microsoft.UI.Xaml.Navigation.h>
#include <winrt/Microsoft.UI.Xaml.Shapes.h>
and
using namespace winrt;
using namespace Microsoft::UI::Text;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Controls::Primitives;
using namespace Microsoft::UI::Xaml::Media;
I hope there's a short answer to my long question.
To be sure, you'd need to post a little more detail (like what error message you're getting). But I'll take a guess that perhaps your code is missing a namespace qualifier. I'm just going off the UWP Windows namespace types, not the WinUI Microsoft namespace, but this code builds for me:
#include <winrt/Windows.UI.h>
#include <winrt/Windows.UI.Text.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
void f()
{
winrt::Windows::UI::Xaml::Controls::TextBlock tb;
tb.Text(L"Hello");
tb.FontFamily(winrt::Windows::UI::Xaml::Media::FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(winrt::Windows::UI::Text::FontStyle::Italic);
tb.SelectionHighlightColor(winrt::Windows::UI::Xaml::Media::SolidColorBrush(winrt::Windows::UI::Colors::Red()));
tb.HorizontalAlignment(winrt::Windows::UI::Xaml::HorizontalAlignment::Center);
}
As does this:
#include <winrt/Windows.UI.h>
#include <winrt/Windows.UI.Text.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Media.h>
using namespace winrt;
using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Text;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media;
void f()
{
TextBlock tb;
tb.Text(L"Hello");
tb.FontFamily(FontFamily(L"Times New Roman"));
tb.FontSize(96);
tb.FontStyle(FontStyle::Italic);
tb.SelectionHighlightColor(SolidColorBrush(Colors::Red()));
tb.HorizontalAlignment(HorizontalAlignment::Center);
}

Using a C++ Class in Visual Studio - undeclared identifier error [duplicate]

This question already has answers here:
Why stdfax.h should be the first include on MFC applications? [duplicate]
(3 answers)
Closed 5 years ago.
I expect this is incredibly simple but can't for the life of me figure out whats wrong. I'm new to C++ and I've created a C++ class in visual studio and am trying to use it in the main method of another file. I've stripped everything back to the bare minimum but still I can't get it to run. The first compile error I get is 'Test': undeclared identifier. If I remove 'Test test;' from App.cpp it all compiles fine.
Below is the code. Can anybody help?
App.cpp:
#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
Test test;
//cout << test.getNumber() << endl;
return 0;
}
Test.h:
#pragma once
class Test
{
private:
int number;
public:
int getNumber();
Test();
~Test();
};
Test.cpp:
#include "stdafx.h"
#include "Test.h"
int Test::getNumber()
{
return number;
}
Test::Test()
{
this->number = 1;
}
Test::~Test()
{
}
The problem is that "stdafx.h" is pre-compiled.
Visual c++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The solution is to move it to be the first include.
You can read more about it in the wiki page of Precompiled header.

Error: Class computer comp is already defined in computer.obj

I am currently playing with C++, and attempting to rebuild a Tic Tac Toe batch console game I made in C++, but have hit a wall, where I cannot figure out how to get rid of the error TicTacToe.obj : error LNK2005: "class computer comp" (?comp##3Vcomputer##A) already defined in computer.obj. I have tried removing the declaration of the function computer from the header, and the definition of the function in the C++, but that didn't fix the error. The only way I figured out how to remove this error was to remove the object name, which I kind of don't want to do. I used the example given on the website http://www.cplusplus.com/doc/tutorial/classes/ to set up the class computer. Any information you can provide on any errors that I currently have, or any functions I may not need are most definately welcome, as I am wanting to know much much more about C++.
CODE:
TicTacToe.cpp
// TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
comp.Select();
Sleep(1000);
}
computer.cpp
#include "stdafx.h"
#include "computer.h"
#include <iostream>
using namespace std;
computer::computer()
{
}
computer::~computer()
{
}
void computer::Select()
{
}
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
} comp;
EXTRA INFO:
I am using Microsoft Visual Studio Professional 2013 on a laptop running Windows 7.
As you included header "computer.h" in both modules computer.cpp and TicTacToe.cpp then the both modules contain the same definition of object comp
pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
} comp;
So the linker issues the error.
Define the object only in one cpp module. The header should contain only the class definition.
For example
computer.h
#pragma once
class computer
{
public:
computer();
~computer();
void Select(void);
};
TicTacToe.cpp
// TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include "computer.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
computer comp;
comp.Select();
Sleep(1000);
}
You have to remove comp from the header file. Create the object in a cpp file like this:
computer comp;
You said you don't want to do that. If that causes some other problem for you then post a new question about that problem.
You are defining comp in the header, and so in every .cpp that includes that header, so you are breaking the One Definition Rule.
Instead you can declare it in the header:
extern computer comp;
And then define it in exactly one .cpp:
computer comp;
Which will still allow you to access it from any .cpp that includes the header.

How to call managed C++ methods from Un-managed C++

PLEASE SEE UPDATE BELOW
(RESOLVED) Also I have extended this into a second question here Implement a C# DLL COM File In Unmanaged C++ Program
I have researched this to the end of the internet without finding a real, understandable, human example of how to do this.
I have a C# DLL that encrypts and decrypts text.
I don't want to / don't have the intellectual capability to rewrite this in C++ un-managed code. So instead I created a C++/CLR class that interfaces with the C# dll.
NOW I need to know how to call the managed C++ from my unmanaged code.
Here is my managed code and it is verified that it works
// clrTest.cpp : main project file.
#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
using namespace cSharpRiJHarn;
using namespace System;
String^ Encrypt(String ^s)
{
return RijndaelLink::encrypt(s);
}
String^ Decrypt(String ^s)
{
return RijndaelLink::decrpyt(s);
}
int main()
{
//Console::WriteLine(Encrypt("It Works"));
//Console::WriteLine(Decrypt(Encrypt("It Works")));
//Console::ReadLine();
return 0;
}
Now ONCE AGAIN I HAVE researched this.
I have seen allllllll the bad/overly complicated explanations
I know I need to use something called COM or Interop
I don't know how this works and I am just looking for a very simple explanation.
Thanks for the help.
UPDATE
I have turned the C# DLL into a COM File
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cSharpRiJHarn
{
[Guid("GuiD CODE REMOVED")]
public interface DBCOM_Interface
{
[DispId(1)]
String encrypt(string s);
[DispId(2)]
String decrpyt(string s);
}
[Guid("GuiD CODE REMOVED"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface DBCOM_Events
{
}
[Guid("GuiD CODE REMOVED"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(DBCOM_Events))]
public class RijndaelLink : DBCOM_Interface
{
public String encrypt(String s)
{
return Rijndael.EncryptString(s);
}
public String decrpyt(String s)
{
return Rijndael.DecryptString(s);
}
}
}
Now I am just need to know how to implement this in unmanaged C++...
I have tried both adding just the files to the C++ project and also adding the entire cSharpRiJHarn Project to this solution. Neither work.
#import "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <iostream>
//using namespace cSharpRiJHarn;
int main(){
cSharpRiJHarn::RijndaelLink::encrypt("It works");
char ch;
std::cin>>ch;
return 0;
}
This is one of the errors I am getting.
Error 6 error C2653: 'cSharpRiJHarn' : is not a class or namespace
name
and
Error 8 IntelliSense: cannot open source file
"C:/.../.../Documents/Visual Studio
2010/Projects/unmannagedCPPExample/unmannagedCPPExample/Debug/cSharpRiJHarn.tlh" c:......\documents\visual
studio
2010\projects\unmannagedcppexample\unmannagedcppexample\unmannagedcppexample.cpp
You could use the cool C++ Marshaling library provided by Microsoft, something like this:
#include "cSharpRiJHarn"
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "msclr\marshal_cppstd.h" // marshaling library
using namespace cSharpRiJHarn;
using namespace System;
using namespace msclr::interop; // marshaling library
std::wstring Encrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::encrypt(marshal_as<String^>(s)));
}
std::wstring Decrypt(std::wstring s)
{
return marshal_as<std::wstring>(RijndaelLink::decrypt(marshal_as<String^>(s)));
}
First, your methods receive and return a String^ which is a managed object. Unmanaged code does not know this type, and cannot create such object. So, you will need to wrap the function call such that the function marshal the managed type to something that the unmanaged code can understand.
After that, you can add the DllExport attribute to the managed method, as discussed here.