{"id":8502,"date":"2023-10-05T08:53:19","date_gmt":"2023-10-05T15:53:19","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=8502"},"modified":"2023-10-05T09:00:36","modified_gmt":"2023-10-05T16:00:36","slug":"tracing-embedded-unit-test-projects","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/tracing\/embedded\/tests\/","title":{"rendered":"Tracing Embedded Unit Test Projects"},"content":{"rendered":"<p>This tutorial shows how to use Live Tracing to record the execution of embedded unit test projects. We will create a basic test project using its own number class with &#8220;+&#8221; and &#8220;*&#8221; operators, that will verify the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Binomial_theorem\">Binomial Theorem<\/a> by verifying that the <strong>(x + y)<sup>2<\/sup> = x<sup>2<\/sup> + 2xy + y<sup>2<\/sup> <\/strong>equation holds.<\/p>\n<p>The addition and multiplication will use the custom operators defined in our number class, and the tracing logic will record every invocation of these operators, so if the tests start failing in the future, you could easily compare the recording against a reference one without re-running the old version on the actual hardware.<\/p>\n<ol>\n<li>Start Visual Studio and launch the VisualGDB Embedded Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/01-emb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8504\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/01-emb.png\" alt=\"\" width=\"890\" height=\"625\" \/><\/a><\/li>\n<li>Enter the name and location for the project you would be creating: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/02-path.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8505\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/02-path.png\" alt=\"\" width=\"890\" height=\"625\" \/><\/a><\/li>\n<li>Select &#8220;<strong>Create a new project -&gt; Unit Test -&gt;Advanced CMake<\/strong>&#8221; and pick the <strong>TinyEmbeddedTest<\/strong> framework: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/03-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8506\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/03-test.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select the latest ARM toolchain and pick the device you would like to target: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/04-dev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8507\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/04-dev.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the default test project template: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/05-sample-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8508\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/05-sample-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select the debugging settings that work with your device and make sure the software tracing is enabled at the bottom of the page: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/06-debug-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8509\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/06-debug-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Once it is created, replace the contents of the <strong>tests.cpp<\/strong> file with the following:\n<pre class=\"\">class MyNumber\r\n{\r\nprivate:\r\n    int Value;\r\n\r\npublic:\r\n    MyNumber(int x)\r\n        : Value(x)\r\n    {\r\n    }\r\n\r\n    MyNumber operator+(const MyNumber &amp;right) const\r\n    {\r\n        return MyNumber(Value + right.Value);\r\n    }\r\n\r\n    MyNumber operator*(const MyNumber &amp;right) const\r\n    {\r\n        return MyNumber(Value * right.Value);\r\n    }  \r\n  \r\n    bool operator==(const MyNumber &amp;right) const\r\n    {\r\n        return Value == right.Value;\r\n    }\r\n};\r\n\r\nvoid TestBinomial(MyNumber x, MyNumber y)\r\n{\r\n    CHECK_EQUAL((x + y) * (x + y), (x * x) + MyNumber(2) * x * y + (y * y));\r\n}\r\n\r\nTEST(DemoTestGroup, MathTest)\r\n{\r\n    TestBinomial(1, 2);\r\n    TestBinomial(3, 4);\r\n}<\/pre>\n<p>This code verifies the <strong>(x + y)<sup>2<\/sup> = x<sup>2<\/sup> + 2xy + y<sup>2<\/sup> <\/strong>equation for 2 sets of numbers: <strong>(x=1, y=2)<\/strong> and <strong>(x=3, y=4)<\/strong>. You can find a working version of the sample project in our <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/tracing\/TestTracingDemo\/TestTracingDemoTests.cpp\">GitHub repository<\/a>.<\/li>\n<li>Build the solution and ensure it does not trigger any errors:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/07-code.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8510\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/07-code.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Open the Test Explorer window and run MathTest. Make sure it passes: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/08-pass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8511\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/08-pass.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Now we will show how to record the function calls using Live Tracing. Open the <strong>Debug-&gt;Windows-&gt;Tracepoints<\/strong> window, click &#8220;create a new function tracepoint&#8221; and check all 3 operators defined for <strong>MyNumber<\/strong>. Select to trace <strong>this-&gt;Value<\/strong> and <strong>right.Value<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/09-value.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8512\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/09-value.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Also trace <strong>TestBinomial()<\/strong> with the values of <strong>x.Value <\/strong>and <strong>y.Value<\/strong>: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/10-binomial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8513\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/10-binomial.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Start debugging the unit test: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/11-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8514\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/11-debug.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>VisualGDB will run the test and open the test report file showing which function calls were recorded: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/12-passed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8515\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/12-passed.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Click &#8220;Replay trace&#8221; and go to the Tracepoints view. You will see all the recorded function calls together with their arguments:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13-data.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8516\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/13-data.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a>You can also place tracepoints anywhere in the code and record local variables, global variables, hardware registers (e.g. UART status registers) or the ARM Cortex cycle counter.<\/li>\n<li>You can get a better overview of recorded events by creating a table view from the Trace Data window:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/14-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8517\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/14-table.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Simply drag the values from different events into the lower part of the view, and VisualGDB will create a table showing the order of the calls with the arguments:<br \/>\n<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/15-view.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8518\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/15-view.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a>The table makes it very clear how the <strong>(x + y)<sup>2<\/sup> = x<sup>2<\/sup> + 2xy + y<sup>2<\/sup><\/strong> equation got checked:<\/p>\n<ol style=\"list-style-type: lower-alpha;\">\n<li>Computed (3 + 4) = 7 twice.<\/li>\n<li>Multiplied 7 * 7 to get the left part of the equation.<\/li>\n<li>Computed <strong>x<sup>2<\/sup><\/strong> for the right part (3 * 3 = 9).<\/li>\n<li>Calculated <strong>2xy<\/strong> by multiplying 2 * 3 = 6 and 6 * 4 = 24.<\/li>\n<li>Added <strong>x<sup>2<\/sup><\/strong> to <strong>2xy<\/strong> (9 + 24 = 33).<\/li>\n<li>Squared 4 to compute <strong>y<sup>2<\/sup><\/strong> = 16.<\/li>\n<li>Added it to the previous sum, also arriving at 49.<\/li>\n<li>Verified that both sides of the equation evaluated to 49.<\/li>\n<\/ol>\n<\/li>\n<li>You can create the tracing reports via command line as well. Select all tracepoints and export them into an XML file: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/16-export-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8519\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/16-export-1.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>Then, run the following command line:\n<pre class=\"\">\"%VISUALGDB_DIR%\\VisualGDB.exe\" \/runtests TestTracingDemo.vgdbcmake \/output:TestTracingDemo.xml \/traceconfig:TracepointSets\\MathFunctions.xml \/tracereport:MathFunctions.vgdbtrace \/xmltracereport:MathFunctions.xml \/targetpath:build\\VisualGDB\\Debug\\TestTracingDemo<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/17-run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8520\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/17-run.png\" alt=\"\" width=\"979\" height=\"512\" \/><\/a><\/li>\n<li>VisualGDB will produce a browseable report (MathFunctions.vgdbtrace) and a machine-readable report (<a href=\"https:\/\/github.com\/sysprogs\/tutorials\/blob\/master\/visualgdb\/tracing\/TestTracingDemo\/MathFunctions.xml\">MathFunctions.xml<\/a>) . You can open the regular report via <strong>File-&gt;Open<\/strong> command in Visual Studio:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/18-open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8521\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/18-open.png\" alt=\"\" width=\"1024\" height=\"705\" \/><\/a><\/li>\n<li>It will look the same as the report that was generated from the VS itself. You can replay it as needed, create table\/graph views, or export data from it to CSV files:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/19-replay.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8522\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/19-replay.png\" alt=\"\" width=\"1258\" height=\"837\" \/><\/a><\/li>\n<li>If the test produces more tracing data than VisualGDB can read over the debugging interface, it would normally discard further data to avoid slowing down the program.\u00a0 You can override this behavior via <strong>VisualGDB Project Properties -&gt; Software Tracing -&gt; When trace buffer gets full -&gt; Wait until events are processed<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/wait.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-8526\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2023\/10\/wait.png\" alt=\"\" width=\"1054\" height=\"573\" \/><\/a>This way the recording won&#8217;t miss a single function call, even if the target has to wait several times for the debugger to read the generated data.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use Live Tracing to record the execution of embedded unit test projects. We will create<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[247],"tags":[248,204,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8502"}],"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=8502"}],"version-history":[{"count":4,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8502\/revisions"}],"predecessor-version":[{"id":8562,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/8502\/revisions\/8562"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=8502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=8502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=8502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}