<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>claushoefele.com</title>
	<atom:link href="http://www.claushoefele.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.claushoefele.com</link>
	<description>A.P.P.s</description>
	<lastBuildDate>Sun, 05 Feb 2012 08:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Testing C++ Code with UnitTest++ on Android</title>
		<link>http://www.claushoefele.com/2010/09/testing-code-with-unittest-on-android/</link>
		<comments>http://www.claushoefele.com/2010/09/testing-code-with-unittest-on-android/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 11:13:08 +0000</pubDate>
		<dc:creator>Claus</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.claushoefele.com/?p=101</guid>
		<description><![CDATA[I&#8217;m sure you&#8217;ve come across a unit testing framework or two. At least with me, the concept of running automated tests to make sure that changes don&#8217;t break my code, caught on very quickly. My favourite unit testing framework for C++ code is UnitTest++, which I&#8217;ve been using for a while now. Because of recently [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you&#8217;ve come across a unit testing framework or two. At least with me, the concept of running automated tests to make sure that changes don&#8217;t break my code, caught on very quickly.</p>
<p>My favourite unit testing framework for C++ code is <a href="http://code.google.com/p/unittestpp/">UnitTest++</a>, which I&#8217;ve been using for a while now. Because of recently announced <a href="http://twitter.com/c_nich/status/24114599172">additional features</a> in UnitTest++ that make it easier to run tests on Android, I&#8217;m outlining here the steps to get your unit tests running on Google&#8217;s mobile operating system.<br />
 <span id="more-101"></span></p>
<h3>Why Use a C++ Unit Testing Framework on Android?</h3>
<p>Apps on Android are written in Java, but Google also offers the <a href="http://developer.android.com/sdk/ndk/index.html">Android Native Development Kit (NDK)</a>, which allows you to extend your Java app with code written in C/C++.</p>
<p>This makes sense when you develop a game: not only does C/C++ code execute faster than Java, but you can also use the same code on every platform that supports C/C++ and OpenGL. In the mobile space, this includes iPhone, Android, WebOS, and Windows Phone 6.5, but not BlackBerry (uses Java) and Windows Phone 7 (C#). Mac OS and Windows on desktop computers can be supported as well, of course.</p>
<p>UnitTest++ is being developed by <a href="http://www.gamesfromwithin.com/">Noel Llopis</a> and Charles Nicholson; themselves active game developers. There&#8217;s nothing game-specific in UnitTest++, but games are one of those application domains where C++ is still the most popular choice for programmers.</p>
<p>If you are like me and develop a cross-platform codebase in C++, running your unit tests on Android makes sure that no platform-specific bugs sneak into your code.</p>
<h3>Installing the Android SDK</h3>
<p>To start working with the NDK, you first have to install the latest <a href="http://developer.android.com/sdk/index.html">Android SDK</a>. The NDK contains the compilers and tools for building C/C++ code, but you&#8217;ll need the SDK to add a small Java app and package it with the native code so the bundle can be run on an Android emulator or device. </p>
<p>All of Google&#8217;s tools support Windows, Mac OS X, and Linux, so you shouldn&#8217;t have problems following my steps on either operating system.</p>
<p>To install the Android SDK, I suggest you follow <a href="http://developer.android.com/sdk/installing.html">Google&#8217;s installation guide</a>. I&#8217;m assuming here that you want to use Eclipse as your IDE, so follow the bits where it mentions the Android Development Tools (ADT) plugin for Eclipse in Google&#8217;s description. In the end you should have installed the SDK Starter Package, Eclipse, ADT, and also downloaded one or more Android platforms.</p>
<p>If you are new to Android development, it&#8217;s a good idea to follow the <a href="http://developer.android.com/resources/tutorials/hello-world.html">Hello World tutorial</a> to make sure everything is installed properly and you also have an Android Virtual Device configured. That&#8217;s the emulator that runs your apps before you install them on a real device.</p>
<h3>Installing the Android NDK</h3>
<p>The Android NDK is a <a href="http://developer.android.com/sdk/ndk/index.html">download</a> that doesn&#8217;t need installation and you can simply extract the contents of the zip file to a folder of your choice (I keep mine next to the SDK). Windows users will also need <a href="http://www.cygwin.com/">Cygwin</a> in order to get <code>gnumake</code> and <code>bash</code>; both of which are used by the NDK&#8217;s build environment. More information can be found <a href="http://developer.android.com/sdk/ndk/index.html#requirements">here</a>.</p>
<h3>Downloading the Example Project</h3>
<p>I combined all the files required for building and running UnitTest++ into one zip file so you can get started right away. This zip file includes a version of UnitTest++, a simple Android Java app to run your unit tests, and some bits of code to glue the two together.</p>
<p><a href='http://www.claushoefele.com/wp-content/uploads/2010/09/unittest++_android_10.zip'>Download Example Project</a></p>
<p>I suggest you download this file now and unzip it to a folder, and I&#8217;ll explain the contents in more detail.</p>
<h3>Using the Java Native Interface</h3>
<p>C/C+ code compiled with the NDK produces a shared library that can be loaded by your Android app at run-time. In order to call code in this shared library from Java, you have to use the <a href="http://java.sun.com/docs/books/jni/">Java Native Interface (JNI)</a>.</p>
<p>Here is how I declared the entry point that my example Java app will use to execute the unit tests (you can find this code in <code>src/com/example/unittestpp/UnitTestPP.java</code>):</p>
<pre>

package com.example.unittestpp;

public class UnitTestPP extends Activity
{
      public native String executeUnitTests();
}
</pre>
<p>Important here is that <code>executeUnitTests()</code> is tagged with the <code>native</code> keyword. JNI then maps <code>executeUnitTests()</code> to the following C function declaration:</p>
<pre>

jstring Java_com_example_unittestpp_UnitTestPP \
_executeUnitTests(JNIEnv* env, jobject thiz)
</pre>
<p>You can see how the package, class, and method name in Java are concatenated into a function name. This naming convention makes it possible for the Android system to find the right entry point in your shared library.</p>
<p>This is what the implementation of this function looks like (<code>jni/UnitTestPP.cpp</code>):</p>
<pre>

#ifdef __cplusplus
extern "C" {
#endif

jstring Java_com_example_unittestpp_UnitTestPP \
_stringFromJNI(JNIEnv* env, jobject thiz)
{
  // Run all tests (same as UnitTest::RunAllTests()).
  TestReporterStdout reporter;
  TestRunner runner(reporter);
  runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0);
  TestResults* testResults = runner.GetTestResults();

  // Pass results back to Java for display.
  char result[128];
  if (testResults->GetFailedTestCount() > 0)
  {
    sprintf(result, "Final results: failure\n" \
      "%d out of %d test(s) failed (%d failure(s)).",
      testResults->GetFailedTestCount(),
      testResults->GetTotalTestCount(),
      testResults->GetFailureCount());
  }
  else
  {
    sprintf(result, "Final results: success\n" \
      "%d test(s) passed.", testResults->GetTotalTestCount());
  }
  return env->NewStringUTF(result);
}

#ifdef __cplusplus
}
#endif
</pre>
<p>Because this function is located in a file ending with <code>.cpp</code>, the NDK will consider the contents C++ code. The JNI, however, expects C-style function entries so you have to surround the function with <code>extern "C" {}</code>. This makes sure the function can be called like a C function, even if it&#8217;s containing C++ code.</p>
<p>The code itself does the following: A <code>TestRunner</code> is created that executes all the unit tests the framework can find (I&#8217;ve included a few simple tests at the top of <code>jni/UnitTestPP.cpp</code>). After the unit tests end, <code>TestRunner</code> also provides statistics about the successful and failed tests. This information is appended to a character array and finally passed back to the Java calling code in the return statement. <code>NewStringUTF()</code> must be used here to convert the C character array to a Java <code>String</code> object.</p>
<h3>Building UnitTest++ for Android</h3>
<p>To build C++ code with the Android NDK, you&#8217;ll need to provide a file called Android.mk that lists the files to compile and their build options. This is what this file looks like for UnitTest++ (you can find it in the <code>jni</code> folder):</p>
<pre>

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := unittestpp
LOCAL_C_INCLUDES := \
  $(LOCAL_PATH)/../unittestpp-read-only \
  $(LOCAL_PATH)/../unittestpp-read-only/src \
  $(LOCAL_PATH)/../unittestpp-read-only/src/Posix
LOCAL_SRC_FILES := \
  UnitTestPP.cpp \
  ../unittestpp-read-only/src/AssertException.cpp \
  ../unittestpp-read-only/src/Checks.cpp \
  ../unittestpp-read-only/src/CurrentTest.cpp \
  ../unittestpp-read-only/src/DeferredTestReporter.cpp \
  ../unittestpp-read-only/src/DeferredTestResult.cpp \
  ../unittestpp-read-only/src/MemoryOutStream.cpp \
  ../unittestpp-read-only/src/ReportAssert.cpp \
  ../unittestpp-read-only/src/Test.cpp \
  ../unittestpp-read-only/src/TestDetails.cpp \
  ../unittestpp-read-only/src/TestList.cpp \
  ../unittestpp-read-only/src/TestReporter.cpp \
  ../unittestpp-read-only/src/TestReporterStdout.cpp \
  ../unittestpp-read-only/src/TestResults.cpp \
  ../unittestpp-read-only/src/TestRunner.cpp \
  ../unittestpp-read-only/src/TimeConstraint.cpp \
  ../unittestpp-read-only/src/XmlTestReporter.cpp \
  ../unittestpp-read-only/src/Posix/SignalTranslator.cpp \
  ../unittestpp-read-only/src/Posix/TimeHelpers.cpp
LOCAL_CFLAGS := \
  -DUNITTEST_POSIX -DNULL=0 \
  -DUNITTEST_NO_DEFERRED_REPORTER \
  -DUNITTEST_NO_EXCEPTIONS

include $(BUILD_SHARED_LIBRARY)
</pre>
<p><code>Android.mk</code> is executed as part of a generic makefile provided by the NDK, so you only need to set some makefile variables with project-specific settings. These settings include:</p>
<ul>
<li><code>LOCAL_MODULE</code>: The name of your project, which will also be used for the name of the resulting shared library (<code>libunittestpp.so</code>).</li>
<li><code>LOCAL_C_INCLUDES</code>: A list of directories with your project&#8217;s include files.</li>
<li><code>LOCAL_SRC_FILES</code>: This is the list of all the source files to compile for your project including the JNI code (<code>UnitTestPP.cpp</code>) and the UnitTest++ sources (located in <code>unittestpp-read-only/src</code>).</li>
<li><code>LOCAL_CFLAGS</code>: A list of additional compiler flags.</li>
</ul>
<p>The Android NDK only supports a small set of APIs; it doesn&#8217;t include the <a href="http://en.wikipedia.org/wiki/Standard_Template_Library">STL</a>, doesn&#8217;t support <a href="http://www.cplusplus.com/doc/tutorial/exceptions/">exceptions</a>, and only provides a a subset of the <a href="http://en.wikipedia.org/wiki/C_standard_library">C standard library</a>. </p>
<p>To get UnitTest++ to compile despite these restrictions, I had to define <code>UNITTEST_NO_DEFERRED_REPORTER</code> to disable all deferred test reporters, such as XmlTestReporter, because those classes use <code>stl::vector</code>. I also disabled the use of exceptions in UnitTest++ by defining <code>UNITTEST_NO_EXCEPTIONS</code> (UnitTest++ then uses <code>setjmp</code>/<code>longjmp</code> instead.</p>
<p>A rather annoying problem was the lack of C++-style standard library headers. In C++, you can replace header includes such as <code>#include &lt;stdio.h&gt;</code> with <code>#include &lt;cstdio&gt;</code>. UnitTest++ makes ample use of this include style and modifying all source files would have been annoying. Fortunately, Android&#8217;s x86 environment does have those headers, so I just copied the missing files from NDK&#8217;s <code>build/platforms/android-8/arch-x86/usr/include</code> folder into the <code>jni</code> folder of the example project where the compiler can find them. The contents of those files are very simple, so you can just use these until Google fixes <a href="http://code.google.com/p/android/issues/detail?id=7444">this issue</a>. </p>
<p>Another change I had to do was to disable signal processing. (It might be possible to make this work, but I didn&#8217;t need it.) If you have a look in <code>unittestpp-read-only/src/ExecuteTest.h</code>, I commented out the line that mentions <code>THROW_SIGNALS</code>.</p>
<pre>

    UT_TRY
    ({
//      UNITTEST_THROW_SIGNALS_POSIX_ONLY
      testObject.RunImpl();
    })
</pre>
<p>With all the configuring done, you can now compile UnitTest++:</p>
<ol>
<li>Open a terminal and change into the <code>unittest++</code> folder from the example project. (Windows user will need to start Cygwin&#8217;s version of <code>bash</code> in a DOS box and then do the same.)</li>
<li>Run <code>../ndk-build</code> and press Return to build the code (<code>ndk-build</code> is a shell program that&#8217;s located in the NDK&#8217;s top-level folder, but you have to ececute the script from within the <code>uinttest++</code> directory.</li>
</ol>
<p>The compiled library with the name <code>libunittestpp.so</code> should now be in the <code>libs/armeabi</code> folder.</p>
<h3>Creating an Android Java App</h3>
<p>The shared library that includes UnitTest++ needs to be included in a standard Android Java app. I provided a simple Java project with only one <a href="http://developer.android.com/reference/android/app/Activity.html"><code>Activity</code></a> (<code>src/com/example/unittestpp/UnitTestPP.java</code>). </p>
<pre>

public class UnitTestPP extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content. The text is
         * retrieved by calling a native function.
         */
        TextView  tv = new TextView(this);
        tv.setText(executeUnitTests());
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * native library, which is packaged with this application.
     */
    public native String executeUnitTests();

    /* This is used to load the native library on application
     * startup. The library has already been unpacked at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("unittestpp");
    }
}
</pre>
<p>This Android <code>Activity</code> has two important tasks: It loads the shared library with UnitTest++ and executes your unit tests. </p>
<p>The first task is achieved by calling <code>System.loadLibrary("unittestpp")</code>, where <code>unittestpp</code> is used to find the shared library with the name <code>libunittestpp.so</code>. Because this code is contained in a <code>static</code> block, it&#8217;s executed before any other method in this class is called.</p>
<p>The second task uses the JNI mapping that I have discussed previously. Calling <code>executeUnitTests()</code>, which is tagged with the keyword <code>static</code>, will call into the entry point that I defined in <code>UnitTestPP.cpp</code>. The string with the unit test results is then displayed on the screen with a <code>TextView</code>.</p>
<p>To import the Java project from the example:</p>
<ol>
<li>Open Eclipse and select File > New > Project&#8230;</li>
<li>Select Android Project and click Next</li>
<li>In the New Android Project dialog, enter &#8220;unittestpp&#8221; as project name</li>
<li>Select Create project from existing source</li>
<li>Find the unittest++ folder on your computer and enter it in the Location textfield</li>
<li>Select Android 1.5 as the Build Target (minimum for NDK projects is API level 3)</li>
<li>Click Finish</li>
</ol>
<p>The project should now turn up in your project navigator. Make sure that <code>libunittestpp.so</code> turns up under <code>libs/armeabi</code> in the project navigation window. If it doesn&#8217;t, right click on the <code>armeabi</code> folder and click Refresh. </p>
<p><img src="http://www.claushoefele.com/wp-content/uploads/2010/09/unittest++_eclipse.png" alt="UnitTest++ Eclipse Project" title="UnitTest++ Eclipse Project" width="791" height="751" class="aligncenter size-full wp-image-119" /></p>
<div class="clear" />
<p>Eclipse will build the Android project for you automatically and also include <code>libunittestpp.so</code> in the installation file. To check if that has worked, copy <code>bin/unittestpp.apk</code>, give it the file ending .zip and have a look inside. The shared library should be in the <code>lib/armeabi</code> folder. Android&#8217;s application manager will automatically install the library on the emulator or device when you install the app.</p>
<p><strong>Note</strong>: when you recompile the shared library, Eclipse doesn&#8217;t automatically realise that the project has changed. The easiest way to repackage the app is to make a small change to a comment in <code>UnitTestPP.java</code> and save the file. Eclipse will then rebuild the APK file and include the latest library.</p>
<h3>Running the Unit Tests</h3>
<p>To run the Java application with your unit tests:</p>
<ol>
<li>Select Run > Run Configurations&#8230;</li>
<li>Select Android Application in the upcoming Run Configurations dialog</li>
<li>Click the little icon on the left where it says New launch configuration when you mouse over the icon</li>
<li>Enter a name for the configuration or accept the default one</li>
<li>Select the unittestpp project with the Browse&#8230; button</li>
<li>Click Apply and then Run</li>
</ol>
<p>This should start the Android emulator with your app. If everything goes according to plan, you should eventually see the result of running your unit tests:</p>
<p><img src="http://www.claushoefele.com/wp-content/uploads/2010/09/unittest++_emulator.png" alt="UnitTest++ Running inside the Android Emulator" title="UnitTest++ Running inside the Android Emulator" width="791" height="556" class="aligncenter size-full wp-image-118" /></p>
<div class="clear" />
<h3>Updating to the Latest UnitTest++ Version</h3>
<p>UnitTest++ hasn&#8217;t provided an official release for a while. Instead, you are supposed to download the latest code from its SVN <a href="http://code.google.com/p/unittestpp/source/checkout">code repository</a>. If you want to update the UnitTest++ version that comes with the example project, simply run <code>svn update</code> in the <code>unittestpp-read-only</code> directory. Don&#8217;t forget to check if my modification of <code>TestExecute.h</code> has merged correctly.</p>
<h3>Final Words</h3>
<p>That&#8217;s it! Much of the complexity comes from using the Android NDK rather than from UnitTest++. Once you have set everything up, adding more tests should be much easier. Add some comments to this post if you get stuck.</p>
<p><a href='http://www.claushoefele.com/wp-content/uploads/2010/09/unittest++_android_10.zip'>Download Example Project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.claushoefele.com/2010/09/testing-code-with-unittest-on-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Freeplay 2010</title>
		<link>http://www.claushoefele.com/2010/08/freeplay-2010/</link>
		<comments>http://www.claushoefele.com/2010/08/freeplay-2010/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 09:46:58 +0000</pubDate>
		<dc:creator>Claus</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.claushoefele.com/?p=70</guid>
		<description><![CDATA[Questions about game development? I&#8217;ll be chairing Freeplay&#8217;s Ask a Game Developer panel on Sunday, August 15, in Melbourne.]]></description>
			<content:encoded><![CDATA[<p>Questions about game development? I&#8217;ll be chairing <a href="http://www.freeplay.net.au/2010-session-details/#AskaGameDeveloper">Freeplay&#8217;s Ask a Game Developer panel</a> on Sunday, August 15, in Melbourne.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.claushoefele.com/2010/08/freeplay-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Book Contributions</title>
		<link>http://www.claushoefele.com/2010/03/new-book-contributions/</link>
		<comments>http://www.claushoefele.com/2010/03/new-book-contributions/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 08:42:32 +0000</pubDate>
		<dc:creator>Claus</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.claushoefele.com/?p=68</guid>
		<description><![CDATA[The books Game Programming Gems 8 and More iPhone Cool Projects &#8211; with chapter contributions written by me &#8211; are now available for pre-order.]]></description>
			<content:encoded><![CDATA[<p>The books <a href="http://www.amazon.com/gp/product/1584507020">Game Programming Gems 8</a> and <a href="http://www.amazon.com/gp/product/1430229225">More iPhone Cool Projects</a> &#8211; with chapter contributions written by me &#8211; are now available for pre-order.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.claushoefele.com/2010/03/new-book-contributions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>linux.conf.au 2009 in Hobart</title>
		<link>http://www.claushoefele.com/2009/01/linux-conf-au-2009-in-hobart/</link>
		<comments>http://www.claushoefele.com/2009/01/linux-conf-au-2009-in-hobart/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 08:40:33 +0000</pubDate>
		<dc:creator>Claus</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.claushoefele.com/?p=65</guid>
		<description><![CDATA[Slides of my talk at the 2009 linux.conf.au conference in Hobart are now available on the Gaming Miniconf web site.]]></description>
			<content:encoded><![CDATA[<p>Slides of my talk at the 2009 <a href="http://linux.conf.au/">linux.conf.au</a> conference in Hobart are now available on the <a href="http://games.sericyb.com.au/LCA2009-talks.html#ShareYourExperienceYouTubeIntegrationInGames">Gaming Miniconf web site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.claushoefele.com/2009/01/linux-conf-au-2009-in-hobart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

