Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / MFC

Unit--, a simple and easy-to-use unit test aid for C++

Rate me:
Please Sign up or sign in to vote.
4.75/5 (12 votes)
14 Jun 20065 min read 32.1K   909   26  
An article describing a portable unit test framework for C++.
// unit--, a simple and easy-to-use unit test aid for C++
// Copyright (C) 2005~2006  Tsong Chong
// birdiez@126.com
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <string>
#include <sstream>
#include <stdexcept>
#include "../unit--.h"

testSuite(UtilitySuite)

namespace {

// test if AssertionInfo transfer info correctly
testCase(AssertionInfoCase, UtilitySuite)
{
    {
        unit_minus::AssertionInfo info(true);
        assertTrue(true  == info.successful());
        assertTrue(false == info.hasMessage());
    }

    {
        unit_minus::AssertionInfo info(false);
        assertTrue(false == info.successful());
        assertTrue(false == info.hasMessage());
        assertTrue("" == info.message());
    }

    {
        const std::string message = "arBitRarY sTriNg";
        unit_minus::AssertionInfo info(false, message);
        assertTrue(false == info.successful());
        assertTrue(true  == info.hasMessage());
        assertTrue(message == info.message());
    }
}

template <typename T>
inline T strTo(const std::string& str)
{
    std::istringstream ii(str);
    T result;
    if (ii >> result) return result;
    else throw std::invalid_argument("cannot convert (" + str + ")");
}

// find sub string enclosed by < and >, starting at "end"
void getNumber(const std::string& message, unsigned& begin, unsigned& end)
{
    using std::string;
    begin = static_cast<unsigned>(message.find("<", end));
    assertTrue(string::npos != begin);
    ++begin;
    assertTrue(message.length() > begin);
    end = static_cast<unsigned>(message.find(">", begin));
    assertTrue(string::npos != end);
}

// test if equalValueInfo give correct hint about failure
testCase(EqualValueCase, UtilitySuite)
{
    {
        unit_minus::AssertionInfo info(unit_minus::equalValue(1 + 1, 2));
        assertTrue(info.successful());
    }

    {
        const unsigned expected = 1;
        const unsigned actual = 2;
        unit_minus::AssertionInfo info(unit_minus::equalValueInfo(expected, actual));
        assertTrue(!info.successful());
        assertTrue( info.hasMessage());

        using std::string;
        string message = info.message();
        assertTrue(message.length() > 0);

        unsigned begin = 0;
        unsigned end = 0;
        getNumber(message, begin, end);
        assertTrue(expected == strTo<unsigned>(message.substr(begin, end - begin)));

        getNumber(message, begin, end);
        assertTrue(actual == strTo<unsigned>(message.substr(begin, end - begin)));
    }
}

// test if closeValueInfo give correct hint about failure
testCase(CloseValueCase, UtilitySuite)
{
    {
        unit_minus::AssertionInfo info(unit_minus::closeValue(2.0, 2.0, 0.01));
        assertTrue(info.successful());
    }

    {
        const float expected = 0.0f + 3.5f - 1.9f;
        const double actual = 2.0;
        const double prec = 0.01;
        unit_minus::AssertionInfo info(unit_minus::closeValueInfo(expected, actual, prec));
        assertTrue(!info.successful());
        assertTrue( info.hasMessage());

        using std::string;
        string message = info.message();
        assertTrue(message.length() > 0);

        unsigned begin = 0;
        unsigned end = 0;
        getNumber(message, begin, end);
        const float expectedFromMessage = strTo<float>(message.substr(begin, end - begin));
        assertTrue(unit_minus::closeValueInfo(expected, expectedFromMessage, 0.001));

        getNumber(message, begin, end);
        const double actualFromMessage = strTo<double>(message.substr(begin, end - begin));
        assertTrue(unit_minus::closeValueInfo(actual, actualFromMessage, 0.001));

        getNumber(message, begin, end);
        const double precFromMessage = strTo<double>(message.substr(begin, end - begin));
        assertTrue(unit_minus::closeValueInfo(prec, precFromMessage, 0.001));
    }
}


} // namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions