<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Tutorial</title><link>https://sourceforge.net/p/cipra/wiki/Tutorial/</link><description>Recent changes to Tutorial</description><atom:link href="https://sourceforge.net/p/cipra/wiki/Tutorial/feed" rel="self"/><language>en</language><lastBuildDate>Fri, 26 Jul 2013 20:00:57 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/cipra/wiki/Tutorial/feed" rel="self" type="application/rss+xml"/><item><title>Tutorial modified by Patrick M. Niedzielski</title><link>https://sourceforge.net/p/cipra/wiki/Tutorial/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v1
+++ v2
@@ -367,7 +367,7 @@

 However, if `w` failed to construct the first time, it will fail to construct again, and we'll have leaked an exception.

-`cipra` provides a function template specifically to test constructors.  To turn it on, be sure you define the preprocessor symbol `CIPRA_USE_VARIADIC_TEMPLATES` before including `cipra.h`.  The function template, called `new_ok`, has the following signature:
+`cipra` provides a function template specifically to test constructors.  The function template, called `new_ok`, has the following signature:

 ~~~~{.cpp}
 template T new_ok(argsT&amp;&amp;... args);
@@ -376,7 +376,6 @@
 `new_ok` constructs a object of type `T` using `T`'s constructor that takes the given `args`.  If the constructor of the object fails (that is, if the constructor throws an exception), `new_ok` will abort from the test program.  This function requires that `T` either be `Copyable` or `Movable`.  Since the `value` class in our example is `Copyable`, we can simplify our test snippet:

 ~~~~{.cpp}
-// earlier we #define CIPRA_USE_VARIADIC_TEMPLATES
 throws([](){ value v{1.0, -0.5}; },
        "Can't have negative error.");
 auto w = new_ok&gt;(1.0, 0.5);
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Patrick M. Niedzielski</dc:creator><pubDate>Fri, 26 Jul 2013 20:00:57 -0000</pubDate><guid>https://sourceforge.net360bd2c4fe82433d83228c4144c88be832e967fc</guid></item><item><title>Tutorial modified by Patrick M. Niedzielski</title><link>https://sourceforge.net/p/cipra/wiki/Tutorial/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="tutorial"&gt;Tutorial&lt;/h1&gt;
&lt;p&gt;Modern software development knows the benefits of automated testing over (or as a complement to) dedicated QA staff.  Automated testing gives a quicker bug cycle for developers and allows them to catch bugs introduced in code changes by other modules or modifications to the same module.  Indeed, automated unit tests are &lt;a class="" href="http://www.extremeprogramming.org/rules/unittests.html"&gt;a corner stone of Extreme Programming&lt;/a&gt;, and many software projects don't allow new code to be checked in without accompanying automated tests.&lt;/p&gt;
&lt;p&gt;On the other hand, most C++ unit testing frameworks are heavy-weight and restricting.  They are filled with unnecessary macros which can present problems, and they are often not thread-safe, which makes testing concurrent code even more difficult.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cipra&lt;/code&gt; was designed as a response to this, with the following goals in mind:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Be simple.&lt;/em&gt;  The API of &lt;code&gt;cipra&lt;/code&gt; closely mirrors that of Perl's &lt;code&gt;Test::More&lt;/code&gt;, a widely-used testing module in the Perl community that elegantly works well in C++.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Be flexible.&lt;/em&gt;  We don't want to design the user's tests for them.  Instead of forcing the user's tests to follow a "Setup-Test-Teardown" structure, which can clash with popular C++ idioms like RAII and threading, we let the user of &lt;code&gt;cipra&lt;/code&gt; manage this themselves only if they need it.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Use Modern C++.&lt;/em&gt;  Although we are using Perl's &lt;code&gt;Test::More&lt;/code&gt; module as a guide, &lt;code&gt;cipra&lt;/code&gt; is thoroughly a C++ library.  Written in C++11, it eschews macros and is thread-safe, using modern C++ techniques that aren't intrusive on the code you want to test.  We don't require anything other than a C++11 compiler.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This tutorial should give you an introduction to using &lt;code&gt;cipra&lt;/code&gt;.  It expects C++11 experience, but doesn't require any knowledge of &lt;code&gt;Test::More&lt;/code&gt; or Perl testing.&lt;/p&gt;
&lt;h1 id="getting-started-with-cipra"&gt;Getting Started with &lt;code&gt;cipra&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;cipra&lt;/code&gt; is a header-only library, which means you don't have to compile anything to use it.  Simply install the header files to your system or package them in your project, and you can start using &lt;code&gt;cipra&lt;/code&gt; as a test framework.  Make sure you are using a C++11 compliant compiler.&lt;/p&gt;
&lt;p&gt;You will need a TAP13 test harness to track and pretty-print the output of &lt;code&gt;cipra&lt;/code&gt; test programs.  Right now, the best option is to use the &lt;code&gt;prove&lt;/code&gt; program that comes with Perl.  In a bind, the following UNIX command can also be used to check for failures, assuming your test program is named &lt;code&gt;test-program.t&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;$ &lt;span class="o"&gt;./&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;grep&lt;/span&gt; &amp;quot;^&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&amp;quot;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In the future, &lt;code&gt;cipra&lt;/code&gt; will provide a test harness written entirely in C++11, making &lt;code&gt;cipra&lt;/code&gt; tests dependency-free.  In this tutorial, we will be showing the output of &lt;code&gt;cipra&lt;/code&gt; itself, not run through any test harness.&lt;/p&gt;
&lt;p&gt;Let's start with a very simple test program using &lt;code&gt;cipra&lt;/code&gt;.  We'll assume that the header files are in your system include path (placed in &lt;code&gt;/usr/include&lt;/code&gt;, for instance, or compiled in with a &lt;code&gt;-I&lt;/code&gt; flag on GCC).  Start off by including &lt;code&gt;cipra.h&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#include &amp;lt;cipra.h&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In &lt;code&gt;cipra&lt;/code&gt;, all test programs need to have a class derived from &lt;code&gt;cipra::fixture&lt;/code&gt;, which contains all the test functions in &lt;code&gt;cipra&lt;/code&gt;.  The actual tests are contained in the overridden &lt;code&gt;test()&lt;/code&gt; function.  Let's make a class that will contain our tests and then make an instance of this class:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;my_fixture&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cipra&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fixture&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;/*&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="o"&gt;*/&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;By calling the &lt;code&gt;run()&lt;/code&gt; method of &lt;code&gt;test&lt;/code&gt;, we get the return value of the test, running all the test functions and then quitting.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Inside the &lt;code&gt;test()&lt;/code&gt; function, we need to declare how many tests we are planning to run, just in case we exit from the test prematurely.  For our first test program, we'll just have a single test, so we call the &lt;code&gt;plan()&lt;/code&gt; function at the beginning of the &lt;code&gt;test()&lt;/code&gt; function with the argument &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We're finally ready to actually run a test.  It will be a simple test, making sure that &lt;code&gt;5 == 5&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.  To do this, we use &lt;code&gt;cipra&lt;/code&gt;'s most basic test function template, &lt;code&gt;ok()&lt;/code&gt;, which is declared as follows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;funcT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;funcT&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here, &lt;code&gt;funcT&lt;/code&gt; is anything that has an &lt;code&gt;operator()&lt;/code&gt; whose return value is convertible to a &lt;code&gt;bool&lt;/code&gt;.  This could be a function pointer, a functor, the result of &lt;code&gt;std::bind()&lt;/code&gt;, or a C++11 lambda.  Instead of declaring our test code separately, we prefer to use lambdas and declare the test directly in the call to &lt;code&gt;ok()&lt;/code&gt;.  The second argument is a description of the test that will be printed with the test output.  When reading the test output at a glance, it's much easier to see which test is failing if you include these descriptions.  We recommend that you always do this.  Let's write a call to &lt;code&gt;ok()&lt;/code&gt; that checks whether &lt;code&gt;5 == 5&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 5 &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here is the full code of our first test file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#include &amp;lt;cipra.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;my_fixture&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cipra&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fixture&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 5 &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Five is equal to five.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we run the above code, we get the following output:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;1&lt;span class="p"&gt;..&lt;/span&gt;1
&lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This output is in a format called TAP13, short for the "Test Anything Protocol, version 13".  TAP is standard format used by many test harnesses, especially those within the Perl community.  &lt;code&gt;Test::More&lt;/code&gt;, the Perl module on which &lt;code&gt;cipra&lt;/code&gt; is based, uses TAP.  Because of the wide support for TAP, &lt;code&gt;cipra&lt;/code&gt; chooses it as a standard output format.&lt;/p&gt;
&lt;p&gt;For comparison, here is a Perl script using &lt;code&gt;Test::More&lt;/code&gt; that performs the same test:&lt;/p&gt;
&lt;table class="codehilitetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt;1
2
3
4&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;#!/usr/bin/env perl -w&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Test::&lt;/span&gt;&lt;span class="n"&gt;More&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;# or Test::Simple&lt;/span&gt;

&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Five is equal to five.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;The C++ code using &lt;code&gt;cipra&lt;/code&gt; does contain a lot of boilerplate code compared with the Perl code using &lt;code&gt;Test::More&lt;/code&gt;.  Ideally, this code should be reduced.  The goal for a future version of &lt;code&gt;cipra&lt;/code&gt; is to reduce the code to only the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#include &amp;lt;cipra.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 5 &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Five is equal to five.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In real testing environments, though, tests fail.  Let's add a failing test:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#include &amp;lt;cipra.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;my_fixture&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cipra&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fixture&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;2&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
            &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 5 &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Five is equal to five.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 5 &lt;span class="o"&gt;==&lt;/span&gt; 6&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Five is equal to six.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The expression &lt;code&gt;5 == 6&lt;/code&gt; is obviously false, so this test should fail.  If we run the test program, we'll get the following output:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;1&lt;span class="p"&gt;..&lt;/span&gt;2
&lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; 2 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;six&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is TAP's way of telling us that the test failed.&lt;/p&gt;
&lt;h1 id="more-complex-test-plans"&gt;More Complex Test Plans&lt;/h1&gt;
&lt;p&gt;So far, we've called the &lt;code&gt;plan()&lt;/code&gt; function using an integer argument, indicating the number of test functions that we are expecting to call in the test program.  This function is responsible for the &lt;code&gt;1..n&lt;/code&gt; line in the above output, where &lt;code&gt;n&lt;/code&gt; is the number you pass in.  That line is called the &lt;em&gt;test plan&lt;/em&gt; in TAP terms.  By including it, you declare to the test harness how many tests you are expecting to run, so it can tell whether the test was aborted prematurely.&lt;/p&gt;
&lt;p&gt;In our tests so far, the test plan has come at the very beginning of the output, before any of the test functions ran.  Sometimes, though, you don't know how many tests are going to run.  Consider the following test program snippet:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/*&lt;/span&gt; ??? &lt;span class="o"&gt;*/&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ifstream&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num_lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;num_lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;Read&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;know&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt;!
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([=](){&lt;/span&gt; &lt;span class="n"&gt;is_valid_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We don't know how many tests should have run until after the file has been read.  In this case, we can put the test plan at the end of the test.  &lt;em&gt;This does not mean that the test plan can go anywhere in the test program.&lt;/em&gt;  The test plan must come at either the beginning or the end of the test output.  We modify our code and get the right plan:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ifstream&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;txt&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num_lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;](){&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;num_lines&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;Read&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;know&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt;!
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([=](){&lt;/span&gt; &lt;span class="n"&gt;is_valid_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num_lines&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; 1&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;and the output&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Read&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; 2 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; 3 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; 4 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;ok&lt;/span&gt; 29 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;valid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
1&lt;span class="p"&gt;..&lt;/span&gt;29
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we want to skip all the tests in the test program (for example, if the test is testing behavior that isn't implemented yet), we can call the function with &lt;code&gt;skip_all&lt;/code&gt; as the argument.  It only makes sense to do this before all test functions.&lt;/p&gt;
&lt;h1 id="diagnostics"&gt;Diagnostics&lt;/h1&gt;
&lt;p&gt;TAP supports outputting information from the test program to the test harness that won't be interpreted, but will instead be considered comments to be displayed to the user.  &lt;code&gt;cipra&lt;/code&gt; implements these with the &lt;code&gt;diag()&lt;/code&gt; and &lt;code&gt;note()&lt;/code&gt; functions, as well as the &lt;code&gt;explain()&lt;/code&gt;&lt;br /&gt;
function template.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="nb"&gt;diag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The difference between &lt;code&gt;diag()&lt;/code&gt; and &lt;code&gt;note()&lt;/code&gt; is subtle.  While both output a TAP diagnostic, &lt;code&gt;diag()&lt;/code&gt; prints the message to &lt;code&gt;stderr&lt;/code&gt; and &lt;code&gt;note()&lt;/code&gt; prints the message to &lt;code&gt;stdout&lt;/code&gt;.  Consider the following code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;plan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;0&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;don&lt;/span&gt;&lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="n"&gt;functions&lt;/span&gt;!
&lt;span class="n"&gt;note&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;diagnostic&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;!&amp;quot;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The output of this code will be&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;1&lt;span class="p"&gt;..&lt;/span&gt;0
# &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;diagnostic&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;!
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you want to pretty-print an object using its streaming &lt;code&gt;operator&amp;lt;&amp;lt;()&lt;/code&gt;, use the function template &lt;code&gt;explain()&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id="convenience-test-functions"&gt;Convenience Test Functions&lt;/h1&gt;
&lt;p&gt;As we stated earlier, the &lt;code&gt;ok()&lt;/code&gt; function is the only test function you really need.  Every one of the other test functions in &lt;code&gt;cipra&lt;/code&gt; could be replaced by calls to &lt;code&gt;ok()&lt;/code&gt;.  These other test functions are simply conveniences that express &lt;em&gt;intent&lt;/em&gt;.  Because they express &lt;em&gt;intent&lt;/em&gt;, they can make the test source code easier to read and can provide better diagnostic information when the test fails.&lt;/p&gt;
&lt;p&gt;The two most simple test functions are &lt;code&gt;pass()&lt;/code&gt; and &lt;code&gt;fail()&lt;/code&gt;.  Their signatures are below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The the function call &lt;code&gt;pass(name)&lt;/code&gt; is equivalent to &lt;code&gt;ok(true, name)&lt;/code&gt;, while the function call &lt;code&gt;fail(name)&lt;/code&gt; is equivalent to &lt;code&gt;ok(false, name)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another test function template is &lt;code&gt;is()&lt;/code&gt;, which compares two values with their &lt;code&gt;operator==()&lt;/code&gt; function.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;U&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;U&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We could rewrite our first test using &lt;code&gt;is()&lt;/code&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// equivalent to ok(5 == 5, &amp;quot;Five is equal to five.&amp;quot;);&lt;/span&gt;
&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Five is equal to five.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Running this, we get the same output as before:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;1&lt;span class="p"&gt;..&lt;/span&gt;1
&lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;What about our failing test?&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;5&lt;span class="p"&gt;,&lt;/span&gt; 6&lt;span class="p"&gt;,&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;six&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Compared to the output we got with &lt;code&gt;ok()&lt;/code&gt;, a failing &lt;code&gt;is()&lt;/code&gt; gives much better diagnostics.  You need to provide an &lt;code&gt;operator&amp;lt;&amp;lt;()&lt;/code&gt; function for both types so it can be outputted.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;1&lt;span class="p"&gt;..&lt;/span&gt;2
&lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; 1 &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Five&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;six&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
#         &lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 5
#    &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 6
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Similarly, there is an &lt;code&gt;isnt()&lt;/code&gt; function template which checks whether the two values are not equal using their &lt;code&gt;operator!=()&lt;/code&gt; function.&lt;/p&gt;
&lt;h1 id="testing-exceptions"&gt;Testing Exceptions&lt;/h1&gt;
&lt;p&gt;In addition to simple truth values, &lt;code&gt;cipra&lt;/code&gt; can also test exceptions from a function.  Normally, exceptions cause the test functions we've seen to fail in the same way that a &lt;code&gt;false&lt;/code&gt; value does.  The exceptions are not propagated outside the test function, so we can't easily check whether the test function failed because of an exception or because of a &lt;code&gt;false&lt;/code&gt; return value.  If we need to, we can wrap our lambda code in a &lt;code&gt;try&lt;/code&gt;-&lt;code&gt;catch&lt;/code&gt; block.  Let's say the function &lt;code&gt;double div(double, double)&lt;/code&gt; throws an exception when its second argument is &lt;code&gt;0.0&lt;/code&gt;.  If we want to check that this correctly throws an exception, we might write the following test:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt;
       &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;);&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;never&lt;/span&gt; &lt;span class="n"&gt;execute&lt;/span&gt;!
       &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(...)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;},&lt;/span&gt; &amp;quot;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0 &lt;span class="n"&gt;denominator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Though this code does exactly what we want it to, the actual test is very verbose, because the &lt;code&gt;ok()&lt;/code&gt; function doesn't semantically encode the exception test.  Luckily, though, &lt;code&gt;cipra&lt;/code&gt; provides the function templates &lt;code&gt;throws()&lt;/code&gt; that do:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;funcT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;funcT&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;exceptionT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;funcT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;funcT&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&amp;quot;&amp;quot;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The first overload just checks that the &lt;code&gt;expr&lt;/code&gt; throws some exception, while the second overload checks that &lt;code&gt;expr&lt;/code&gt; throws an exception of the type &lt;code&gt;funcT&lt;/code&gt;.  In our case, we can simplify our code with the first overload:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &amp;quot;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0 &lt;span class="n"&gt;denominator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we want to test that it is of the type &lt;code&gt;std::runtime_error&lt;/code&gt; (or a derived class from this type), we can use the second overload:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &amp;quot;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0 &lt;span class="n"&gt;denominator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This overload succeeds &lt;em&gt;only&lt;/em&gt; when a &lt;code&gt;std::runtime_error&lt;/code&gt; is thrown.  If any other type of exception is thrown, it will catch it and fail, printing diagnostic information.  If no exception is thrown, the test function will also fail.&lt;/p&gt;
&lt;p&gt;On compilers that support the CXA ABI (for instance, GCC), you can define the preprocessor symbol &lt;code&gt;CIPRA_CXA_ABI&lt;/code&gt; before including &lt;code&gt;cipra.h&lt;/code&gt;.  &lt;code&gt;cipra&lt;/code&gt; will then use information available from the &lt;code&gt;abi::__cxa_current_exception_type()&lt;/code&gt; function to determine the name of the exception type and will output it with the diagnostics.  Otherwise, the name of the exception won't be available.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cipra&lt;/code&gt; also has two function templates &lt;code&gt;nothrows()&lt;/code&gt; which do the inverse of the &lt;code&gt;throws()&lt;/code&gt; templates.&lt;/p&gt;
&lt;h1 id="testing-constructors"&gt;Testing Constructors&lt;/h1&gt;
&lt;p&gt;Take the following class:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// Holds a floating point value with an associated experimental error.&lt;/span&gt;
&lt;span class="c1"&gt;// Invariant: The error is never negative.&lt;/span&gt;
&lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mh"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;runtime_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Cannot have negative error.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;constexpr&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;epsilon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0001&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;epsilon&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
               &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;epsilon&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cm"&gt;/* etc */&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we want to test the constructor, we could write the following code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &amp;quot;&lt;span class="n"&gt;Can&lt;/span&gt;&lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;negative&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;nothrows&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
         &amp;quot;&lt;span class="n"&gt;Positive&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;fine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;},&lt;/span&gt;
   &amp;quot;&lt;span class="n"&gt;Addition&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt; &lt;span class="n"&gt;works&lt;/span&gt; &lt;span class="n"&gt;fine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;code&gt;v&lt;/code&gt;'s constructor will fail and throw an exception, which causes the first test function to succeed.  &lt;code&gt;w&lt;/code&gt;'s constructor will succeed, and &lt;code&gt;w&lt;/code&gt; will be properly constructed, so the second test function will also succeed.  Because &lt;code&gt;w&lt;/code&gt; is inside a lambda, though, we can't use it in subsequent code.  The third test statement will fail to compile, because &lt;code&gt;w&lt;/code&gt; isn't in scope.  We could write the following code to reconstruct it in the outer scope:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &amp;quot;&lt;span class="n"&gt;Can&lt;/span&gt;&lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;negative&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;nothrows&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
         &amp;quot;&lt;span class="n"&gt;Positive&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;fine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;1&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;0&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;,&lt;/span&gt; 0&lt;span class="p"&gt;.&lt;/span&gt;5&lt;span class="p"&gt;},&lt;/span&gt;
   &amp;quot;&lt;span class="n"&gt;Addition&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt; &lt;span class="n"&gt;works&lt;/span&gt; &lt;span class="n"&gt;fine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&amp;quot;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;However, if &lt;code&gt;w&lt;/code&gt; failed to construct the first time, it will fail to construct again, and we'll have leaked an exception.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cipra&lt;/code&gt; provides a function template specifically to test constructors.  To turn it on, be sure you define the preprocessor symbol &lt;code&gt;CIPRA_USE_VARIADIC_TEMPLATES&lt;/code&gt; before including &lt;code&gt;cipra.h&lt;/code&gt;.  The function template, called &lt;code&gt;new_ok&lt;/code&gt;, has the following signature:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;typename&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;argsT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;new_ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argsT&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;code&gt;new_ok&lt;/code&gt; constructs a object of type &lt;code&gt;T&lt;/code&gt; using &lt;code&gt;T&lt;/code&gt;'s constructor that takes the given &lt;code&gt;args&lt;/code&gt;.  If the constructor of the object fails (that is, if the constructor throws an exception), &lt;code&gt;new_ok&lt;/code&gt; will abort from the test program.  This function requires that &lt;code&gt;T&lt;/code&gt; either be &lt;code&gt;Copyable&lt;/code&gt; or &lt;code&gt;Movable&lt;/code&gt;.  Since the &lt;code&gt;value&amp;lt;double&amp;gt;&lt;/code&gt; class in our example is &lt;code&gt;Copyable&lt;/code&gt;, we can simplify our test snippet:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// earlier we #define CIPRA_USE_VARIADIC_TEMPLATES&lt;/span&gt;
&lt;span class="n"&gt;throws&lt;/span&gt;&lt;span class="p"&gt;([](){&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
       &lt;span class="s"&gt;&amp;quot;Can't have negative error.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_ok&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;Addition operator works fine.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;code&gt;w&lt;/code&gt; is in the outer scope now, and it can be used in subsequent test calls, like the &lt;code&gt;is()&lt;/code&gt; call.  If the constructor were to fail, the &lt;code&gt;is()&lt;/code&gt; would never be called, and the test would exit.&lt;/p&gt;
&lt;h1 id="file-copyright-and-license"&gt;File Copyright and License&lt;/h1&gt;
&lt;p&gt;Copyright (C) 2013, &lt;a class="" href="mailto:PatrickNiedzielski@gmail.com"&gt;Patrick M. Niedzielski&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This work is licensed under a Creative Commons Attribution 3.0 United States License. You should have received a copy of the CC-BY 3.0 US license along with this file. If not, see &lt;a class="" href="http://creativecommons.org/licenses/by/3.0/us"&gt;the Creative Commons website&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Patrick M. Niedzielski</dc:creator><pubDate>Sat, 20 Apr 2013 15:47:14 -0000</pubDate><guid>https://sourceforge.netef297d98cc89efe180c580463453c06eb1e7a098</guid></item></channel></rss>