{"id":2013,"date":"2016-10-07T19:09:59","date_gmt":"2016-10-08T02:09:59","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2013"},"modified":"2016-10-07T19:23:07","modified_gmt":"2016-10-08T02:23:07","slug":"developing-unit-tests-for-raspberry-pi-with-cpputest","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/tests\/cpputest\/","title":{"rendered":"Developing Unit Tests for Raspberry Pi with CppUTest"},"content":{"rendered":"<p>This tutorial shows how to use the CppUTest framework to create C++ unit tests with VisualGDB. We will show how to create a basic test project verifying the behavior of the <strong>std::atomic<\/strong> types and how to use the CppUTest framework to automatically check your code for memory leaks.<\/p>\n<p>Before you begin, install VisualGDB 5.2 or later and ensure that you are using a Custom edition or higher.<\/p>\n<ol>\n<li>Start Visual Studio, open the New Project window and select VisualGDB Linux Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2014\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname.png\" alt=\"01-prjname\" width=\"807\" height=\"478\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname.png 807w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/01-prjname-300x178.png 300w\" sizes=\"(max-width: 807px) 100vw, 807px\" \/><\/a><\/li>\n<li>Select Create a new project -&gt; Unit test project -&gt; Use MSBuild and select the CppUTest framework. As this tutorial will demonstrate the std::atomic types present only in C++11, select <strong>C++11<\/strong> as the language standard:\u00a0 <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-prjtype-std.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2016\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-prjtype-std.png\" alt=\"02-prjtype-std\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-prjtype-std.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/02-prjtype-std-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>On the next page select the machines to build and run your project. For Raspberry Pi we recommend building the code on Windows using a cross-toolchain provided by VisualGDB:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-target.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2017\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-target.png\" alt=\"03-target\" width=\"822\" height=\"662\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-target.png 822w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/03-target-300x242.png 300w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate a project and build it with Ctrl-Shift-B. VisualGDB will generate a few sample tests that will appear in the Test Explorer window:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2018\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-build.png\" alt=\"04-build\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-build.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-build-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/04-build-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Select Test-&gt;Run All to ensure that the <strong>FailingTest<\/strong> fails as suggested by the name and the rest of the tests pass: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-runtests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2019\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-runtests.png\" alt=\"05-runtests\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-runtests.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-runtests-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/05-runtests-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Now we will show how to make some real-world tests using the CppUTest framework. First of all, we will make a very basic test that checks the atomicity of the &#8220;++&#8221; operator. It will do the following:\n<ul>\n<li>Define a variable shared between several threads<\/li>\n<li>Run several threads, each one independently incrementing the variable<\/li>\n<li>Wait for all the threads to exit<\/li>\n<li>Report the final value of the variable<\/li>\n<\/ul>\n<pre class=\"\">#include &lt;CppUTest\/CommandLineTestRunner.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\n#include &lt;unistd.h&gt;\r\n#include &lt;atomic&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;pthread.h&gt;\r\n\r\ntemplate &lt;typename _Type&gt; class AtomicTest\r\n{\r\nprivate:\r\n\u00a0\u00a0\u00a0 struct Context\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int Iterations;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 volatile _Type Counter;\r\n\u00a0\u00a0\u00a0 };\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 static void *AtomicTestThread(void *pContext)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Context *pCtx = (Context*)pContext;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for (int i = 0; i &lt; pCtx-&gt;Iterations; i++)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pCtx-&gt;Counter++;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return nullptr;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\npublic:\r\n\u00a0\u00a0\u00a0 static int Run(int threadCount, int iterationCount)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 std::vector&lt;pthread_t&gt; threads;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Context ctx;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ctx.Counter = 0;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ctx.Iterations = iterationCount;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for (int i = 0; i &lt; threadCount; i++)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pthread_t id;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pthread_create(&amp;id, nullptr, AtomicTestThread, &amp;ctx);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 threads.push_back(id);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for (auto id : threads)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pthread_join(id, nullptr);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return ctx.Counter;\r\n\u00a0\u00a0\u00a0 }\r\n};<\/pre>\n<\/li>\n<li>Now we will define two tests that will use the <strong>AtomicTest<\/strong> class: one will check the regular &#8216;int&#8217; variable and will expect it to fail, the other one will use the C++11 <strong>std::atomic_int<\/strong> type that is a drop-in replacement for the regular <strong>int<\/strong> type that guarantees thread-safety:\n<pre class=\"\">TEST_GROUP(AtomicTests)\r\n{\r\n};\r\n\r\nTEST(AtomicTests, TestAtomicInteger)\r\n{\r\n\u00a0\u00a0\u00a0 const int IterationCount = 1000000;\r\n\u00a0\u00a0\u00a0 LONGS_EQUAL(IterationCount\u00a0 * 4, AtomicTest&lt;std::atomic_int&gt;::Run(4, IterationCount));\r\n}\r\n\r\nTEST(AtomicTests, TestRegularInteger)\r\n{\r\n\u00a0\u00a0\u00a0 const int IterationCount = 1000000;\r\n\u00a0\u00a0\u00a0 int finalCount = AtomicTest&lt;int&gt;::Run(4, IterationCount);\r\n\u00a0\u00a0\u00a0 if (finalCount == IterationCount * 4)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FAIL(\"The final counter unexpectedly matches for non-atomic type.\");\r\n}<\/pre>\n<p>Both tests will run 4 threads. The <strong>TestAtomicInteger<\/strong> test expects the counter value to match the expected one and the <strong>TestRegularInteger<\/strong> expects it to mismatch.<\/li>\n<li>Before you can build the code, open VisualGDB Project Properties and add &#8216;pthread&#8217; to the &#8220;Library names&#8221; field:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-pthread.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2020\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-pthread.png\" alt=\"06-pthread\" width=\"813\" height=\"621\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-pthread.png 813w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/06-pthread-300x229.png 300w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/a><\/li>\n<li>Now you can build your project and run both tests: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-success.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2021\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-success.png\" alt=\"07-success\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-success.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-success-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/07-success-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>If the TestRegularInteger fails because it does not reproduce the race condition, try running it on an optimized release build and increasing the number of iterations.<\/li>\n<li>CppUTest allows tests in the same group to share common constants and variables. You can declare those variables inside the TEST_GROUP() statement. To do some initialization before each tests in a group runs, define a <strong>setup()<\/strong> method inside the the group. In this example we will define an <strong>IterationCount<\/strong> constant and set the number of threads to the number of CPU cores active on the target machine:\n<pre class=\"\">TEST_GROUP(AtomicTests)\r\n{\r\n\u00a0\u00a0\u00a0 int NumberOfCores;\r\n\u00a0\u00a0\u00a0 const int IterationCount = 1000000;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 void setup()\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NumberOfCores = sysconf(_SC_NPROCESSORS_ONLN);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 void teardown()\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0 }\r\n};<\/pre>\n<p>Note that <strong>setup()<\/strong> will be called before each test in the group and each test will have its own instance of <strong>NumberOfCores<\/strong>.<\/li>\n<li>You can rewrite the <strong>TestAtomicInteger<\/strong> and <strong>TestRegularInteger<\/strong> tests to use the shared information instead of hardcoding it in each of the tests:\n<pre class=\"\">TEST(AtomicTests, TestAtomicInteger)\r\n{\r\n\u00a0\u00a0\u00a0 LONGS_EQUAL(IterationCount\u00a0 * NumberOfCores, AtomicTest&lt;std::atomic_int&gt;::Run(4, IterationCount));\r\n}\r\n\r\nTEST(AtomicTests, TestRegularInteger)\r\n{\r\n\u00a0\u00a0\u00a0 int finalCount = AtomicTest&lt;int&gt;::Run(NumberOfCores, IterationCount);\r\n\u00a0\u00a0\u00a0 if (finalCount == IterationCount * NumberOfCores)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FAIL(\"The final counter unexpectedly matches for non-atomic type.\");\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 printf(\"Regular integer confirmed to be non-atomic: final counter is %d instead of %d\\n\", finalCount, NumberOfCores * IterationCount);\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<\/li>\n<li>Finally we will add another test that will fail if we are not running on a 4-core Raspberry Pi 3:\n<pre class=\"\">TEST(AtomicTests, CheckCoreCount)\r\n{\r\n\u00a0\u00a0\u00a0 if (NumberOfCores &lt; 4)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 FAIL(\"Number of online cores too low. Please check your device.\");\r\n}<\/pre>\n<\/li>\n<li>Now we can build the project and run all the 3 tests: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-3tests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2023\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-3tests.png\" alt=\"09-3tests\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-3tests.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-3tests-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/09-3tests-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Now that both atomic and non-atomic version behaves as expected, let&#8217;s use the debugger to see the differences between them. Switch to the release mode, set a breakpoint in AtomicTestThread(), right-click on the <strong>TestRegularInteger<\/strong> test and select &#8220;Debug Selected Tests&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debugnormal.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2024\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debugnormal.png\" alt=\"10-debugnormal\" width=\"924\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debugnormal.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debugnormal-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/10-debugnormal-130x90.png 130w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Once the breakpoint hits, switch to the Disassembly view. You will see that the &#8216;++&#8217; operator got expanded into 3 operations:\n<ul>\n<li>Reading memory contents to register<\/li>\n<li>Incrementing the register value<\/li>\n<li>Writing it back<\/li>\n<\/ul>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-disasmnormal.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2025\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-disasmnormal.png\" alt=\"11-disasmnormal\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-disasmnormal.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/11-disasmnormal-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>This can easily cause a race condition and result in an invalid value, e.g.<\/p>\n<ol>\n<li>Thread A reads value = 0<\/li>\n<li>Thread B reads value = 0<\/li>\n<li>Thread A increments the value (0 =&gt; 1)<\/li>\n<li>Thread A writes the new value (1)<\/li>\n<li>Thread B increments the value (0 =&gt; 1)<\/li>\n<li>Thread B writes the new value (1)<\/li>\n<\/ol>\n<p>Instead of 2 (expected after 2 increments) the counter will have a value of 1.<\/li>\n<li>Repeat the same steps for the <strong>TestAtomicInteger<\/strong> test: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-disasmatomic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2026\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-disasmatomic.png\" alt=\"12-disasmatomic\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-disasmatomic.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/12-disasmatomic-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>The disassembly window shows that the &#8220;++&#8221; operator was expanded to a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Load-link\/store-conditional\">load-link\/store-conditional<\/a> instructions. These instructions check if any other core has modified the same value between the load and store operations. If a modification is detected, the store is aborted and the entire increment operation is repeated until it runs without any other core interfering.<\/li>\n<li>Now we will show how to use CppUTest to detect memory leaks. Add the following test to your source file:\n<pre class=\"\">#include &lt;memory&gt;\r\n\r\nTEST_GROUP(TestGroup2)\r\n{\r\n\r\n};\r\n\r\nclass Base\r\n{\r\npublic:\r\n};\r\n\r\nclass Child : public Base\r\n{\r\nprivate:\r\n\u00a0\u00a0\u00a0 std::string m_String = \"A considerably long string that will not it in the static buffer\";\u00a0\u00a0 \u00a0\r\n};\r\n\r\nTEST(TestGroup2, BasicTest)\r\n{\r\n\u00a0\u00a0\u00a0 std::unique_ptr&lt;Base&gt; ptr(new Child());\r\n}<\/pre>\n<p>This test creates an instance of the <strong>Child<\/strong> class and deletes it using the pointer to the <strong>Base<\/strong> class. As the Base class does not declare a virtual destructor, the code deleting it has no way of detecting that it&#8217;s actually deleting an instance of <strong>Child<\/strong> and that it should call the destructor of <strong>m_String<\/strong>.<\/li>\n<li>Run the new test. CppUTest will report a memory leak:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-leak.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2027\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-leak.png\" alt=\"13-leak\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-leak.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/13-leak-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Add a virtual destructor to the <strong>Base<\/strong> class, build the project and ensure that the test no longer fails:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/15-noleak.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2028\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/15-noleak.png\" alt=\"15-noleak\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/15-noleak.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/15-noleak-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>Finally we will show how to use CppUTest to detect leaks related to the <strong>malloc()<\/strong> and <strong>free()<\/strong> functions. Normally it won&#8217;t catch those leaks, so the following basic test will succeed despite causing a memory leak:\n<pre class=\"\">TEST(MemoryLeakTests, MallocTest)\r\n{\r\n\u00a0\u00a0 \u00a0malloc(123);\r\n}<\/pre>\n<p><a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/16-malloc1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2029\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/16-malloc1.png\" alt=\"16-malloc1\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/16-malloc1.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/16-malloc1-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>To fix that, insert the following line before any calls to malloc() and free():<\/li>\n<li>\n<pre class=\"\">#include &lt;CppUTest\/MemoryLeakDetectorMallocMacros.h&gt;<\/pre>\n<p>Now the leak will be detected properly:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/17-malloc2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2030\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/17-malloc2.png\" alt=\"17-malloc2\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/17-malloc2.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/17-malloc2-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a><\/li>\n<li>You can tell CppUTest to ignore the leak in this test by inserting the following line:\n<pre class=\"\">\u00a0\u00a0\u00a0 EXPECT_N_LEAKS(1);<\/pre>\n<p>The leak will be still reported, but it will not cause the test to fail:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/18-leakok.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2031\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/18-leakok.png\" alt=\"18-leakok\" width=\"924\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/18-leakok.png 924w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/10\/18-leakok-300x239.png 300w\" sizes=\"(max-width: 924px) 100vw, 924px\" \/><\/a>You can use VisualGDB to create unit tests for Linux, Android and Embedded projects. It supports CppUTest and GoogleTest frameworks out-of-the-box and provides a separate <a href=\"http:\/\/visualgdb.com\/tutorials\/tests\/arm\/\">TinyEmbeddedTest <\/a>framework optimized for small embedded devices.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the CppUTest framework to create C++ unit tests with VisualGDB. We will show how<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[33,43,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2013"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2013"}],"version-history":[{"count":6,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"predecessor-version":[{"id":2037,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2013\/revisions\/2037"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}