Project structure issue - c++

My goal is to achieve a test project on VisualStudio 2019.
I think that my main problem is with How to structure my project. Because, at the end, I find many files and don't know how to manage them.
These are the test .cpp files :
TIStream.cpp :
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>
#include "../ReemasCore/serialization.h"
#include <string.h>
BOOST_AUTO_TEST_SUITE(Serialization_InMemStream)
BOOST_AUTO_TEST_CASE(InMemStreamCtor_SizePositif_CorrectPointer)
{
auto in = std::make_unique<InMemStream>(10);
BOOST_TEST(in->ppStart != nullptr);
BOOST_TEST(in->ppCurrent == in->ppStart);
BOOST_TEST(in->ppEnd == in->ppStart + 10);
}
BOOST_AUTO_TEST_SUITE_END()
TTCPServer :
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(TCP_SERVER)
BOOST_AUTO_TEST_CASE(Server_Case1)
{
BOOST_TEST(1 == 1);
BOOST_TEST(true);
}
BOOST_AUTO_TEST_SUITE_END()

Based on this mailing the dynamic version of Boost Test doesn't contain the main function from version 1.34.1 and up. So if you want to use the shared version of Boost Test you need to follow the instructions from Boost Test doc.
You have to put #define BOOST_TEST_MODULE test module name before the #include <boost/test/unit_test.hpp>. As the documentation says it must be defined exactly for one compilation unit of your test module. As you create different executables for every test, then it practically means you have to put it into every test source file.

Related

Eclipse C++ unable to compile, undefined reference to 'xTaskCreate'

I am trying to move my project to Eclipse (from platformio). I want to pull in some libraries, but I am failing to setup the environment properly to get things compiled.
in my main.cpp I have the following includes:
extern "C"
{
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MIMXRT1021.h"
#include "FreeRTOS.h"
#include "task.h" // <== contains the macro BaseType_t xTaskCreate(....)
}
And then the following code
void Test(void *vParameters)
{
}
int main(void) {
/* Init board hardware. */
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
xTaskCreate(
Test,
"bla",
100,
nullptr,
1,
nullptr);
while(1) {}
return 0 ;
}
This gives the following compile error:
C:\Users\baprins\Documents\MCUXpressoIDE_11.3.1_5262\workspace\iobox\Debug/../source/iobox.cpp:64:
undefined reference to `xTaskCreate'
I did add the include paths :
What am I missing?
As always... once you know what to do, it's really simple ;-)
Go to
project properties
C/C+ General
Paths and Symbols
In the source location tab, add a location where you want to store your libraries. E.g.
I added a "link" and just typed in the name "libraries". Which felt a bit counter intuitive, maybe it can be done more straight forward. The "add folder" button didn't seem to allow to create a new folder.
Anyway, next step is to simply add all include paths in the includes tab.
I am not sure if / when it's necessary, but I added the paths for all configs, all languages. That seems to work fine.

Compilation error after including jsoncpp

When I try to build test sources I get an error like after this.
stl_tree.h:542:14: error: ‘__node’ does not name a type
::new(__node) _Rb_tree_node<_Val>;
Executor's content.
#include <CppUTest/TestHarness.h>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/UtestMacros.h>
#include <CppUTestExt/MockSupport.h>
int main(int argc, char** argv) {
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
return CommandLineTestRunner::RunAllTests(argc, argv);
}
My test' s source code starts with below includes.
#include <CppUTest/TestHarness.h>
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/UtestMacros.h>
#include <CppUTestExt/MockSupport.h>
#include <iostream>
#include "common/data_util_astro_cfg.h"
TEST_GROUP(ASTRO_UTIL_TEST) {
void setup() { }
void teardown() { }
};
And the "common/data_util_astro_cfg.h" file has following includes.
#include "../data/data_type_file.h"
#include <json/json.h>
static AstroConfigs toAstroConfigs(std::string content)
My problem is I get compilation error with these includes, when I remove line json.h include everything is fine I can get binary output.
I think the problem is about new operator' s conflict. The solution is offered by Cpputest side and it is located on http://cpputest.github.io/manual.html#memory_leak_detection. But it isn't clear somehow. :(
The question has been already defined on Compilation error after including <map>. It is so similar to mine but the problem has solved with creating new project. In that case I have no option for that. I'm using Yocto project and the project has created with auto generation tools as well.
Can you help me on this? (Thank you for your time.)
Unfortunately, I solved the problem after a while. It seems something wrong with CPPUTest. At the top of the test file, I have several includes. Some of them belong to CPPUTest library, some of them are mine. My includes are following CPPUTest that's why I’m getting an error. If I changed their places, it would be working correctly. It seems meaningless, but it is the correct solution. ”PROBLEM SHOULD BE RESOLVED WITH DEFINE CPPUTEST LIBRARIES AT THE BOTTOM OF YOUR INCLUDE LIST.”

How to get Google Test to execute multiple test files in QtCreator?

I used Google Test in Qt Creator and wrote two test files, one is tst_a.h(generated by default).The other file is tst_b.h, which I created automatically, but only tst_a.h is executed during unit test.How can I get Google Test to execute tst_b.h?
Please find contents of files mentioned above.
This is tst_a.h
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "Decimal.hpp"
using namespace testing;
TEST(contractTestCase, contractTestSet)
{
EXPECT_EQ(1, 1);
ASSERT_THAT(0, Eq(0));
}
this is tst_b.h
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
using namespace testing;
TEST(testNormal, contractNormalTestSet)
{
EXPECT_EQ(1, 1);
}
When you write your tests in header files these header files need to be included in a cpp for them to be seen by the compiler. The default test setup starts out with a main.cpp in which you can add the include for your new .h file.

Using Catch2 in Visual C++ 2015

I'm trying to make a Unit Testing project using the Catch framework, but am faced with Link errors.
I've set up the project as follows:
Create a Native Unit Test project
Add Catch to the include directories
Add #include <catch.hpp> to stdafx.h
Write the following simple source file
unittest.cpp:
#include "stdafx.h"
namespace Catch2_Test
{
TEST_CASE("Y U no work")
{
REQUIRE(1);
}
}
For integrating Catch into Visual Studio
refer the ACCU article Integrating the Catch Test Framework into Visual Studio, by Malcolm Noyes
For using Catch2 in pre-compiled headers,
refer Catch2 issue 1061, where horenmar gave an example. The changes have been released as part of v2.1.0
In summary, the solution given is:
// stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#define CATCH_CONFIG_ALL_PARTS
#include "catch.hpp"
// PCH-test.cpp:
#include "stdafx.h"
#undef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED
#define CATCH_CONFIG_IMPL_ONLY
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// tests1.cpp:
#include "stdafx.h"
TEST_CASE("FooBarBaz") {
REQUIRE(1 == 2);
}
The problem was that I had cloned Catch2 from the master branch, while the VS integration worked on a branch off Catch.

Run Boost unit test with my own main function for debugging

I have a library inside which there are some testing programs written using Boost.Test. The test files don't have #define BOOST_TEST_DYN_LINK or #include <boost/test/included/unit_test.hpp>. They only have #include <boost/test/unit_test.hpp>. So the main() function isn't there implicitly.
Now I have to debug some library functions which have been used in the test cases. Given that I cannot add or change anything in the test programs, how can I invoke the test programs under a debugger ?
Create a test runner (e.g. main_test.cpp) and link your library against that.
# main_test.cpp
// --- Boost Includes ---
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
Invoking the resulting executable should run your tests. You can then debug individual tests by invoking the runner with ./myrunner --run_test='some_testsuite'/../'some_testname'.