c++ TDD quest

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Part III: First tests

Post by bbguimaraes »

cxxtest

Code

Code: Select all

#include <cxxtest/TestSuite.h>
using CxxTest::TestSuite;

int sum(int n1, int n2) {
    return 3;
}

class Test : public CxxTest::TestSuite {
    public:
        void test1plus2() {
            TS_ASSERT_EQUALS(sum(1, 2), 3);
        }

        void test2plus2() {
            TS_ASSERT_EQUALS(sum(2, 2), 4);
        }
};
Output

Code: Select all

Running 2 tests.
In Test::test2plus2:
Test.h:15: Error: Expected (sum(2, 2) == 4), found (3 != 4)
Failed 1 of 2 tests
Success rate: 50%
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Part IV: Fixtures

Post by bbguimaraes »

The last fundamental element we need to test before starting some useful testing is fixtures. They allow tests to share data, making it much easier to write many tests that depend on the same set of values. Some important things to look for is the ability to create objects on the stack, the ability to assign specific fixtures to specific tests and the amount of overhead required, both in code and on execution.

Index:
Last edited by bbguimaraes on Sun Oct 28, 2012 11:14 pm, edited 3 times in total.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Part IV: Fixtures

Post by bbguimaraes »

boost::test

Code

Code: Select all

#define BOOST_TEST_MODULE Test
#include <boost/test/unit_test.hpp>

struct SumFixture {
    SumFixture() :
        N(5),
        VALUES_1{ 0, 2, 4,  6,  8 },
        VALUES_2{ 1, 3, 5,  7,  9 },
        RESULTS{ 1, 5, 9, 13, 17 }
    {}

    const unsigned int N;
    const int VALUES_1[5];
    const int VALUES_2[5];
    const int RESULTS[5];
};

int sum(int n1, int n2) {
    return n1 + n2;
}

BOOST_FIXTURE_TEST_CASE(TestSum, SumFixture) {
    for(unsigned int i = 0; i < N; ++i)
        BOOST_CHECK_EQUAL(sum(VALUES_1[i], VALUES_2[i]), RESULTS[i]);
}
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Part IV: Fixtures

Post by bbguimaraes »

QtTest

Code

Code: Select all

#include <QtTest/QtTest>

int sum(int n1, int n2) {
    return n1 + n2;
}

class TestSum : public QObject {
    Q_OBJECT

    private slots:
        void testSum_data() {
            QTest::addColumn<int>("value1");
            QTest::addColumn<int>("value2");
            QTest::addColumn<int>("result");

            QTest::newRow("0") << 0 << 1 << 1;
            QTest::newRow("1") << 2 << 3 << 5;
            QTest::newRow("2") << 4 << 5 << 9;
            QTest::newRow("3") << 6 << 7 << 13;
            QTest::newRow("4") << 8 << 9 << 17;
        }

        void testSum() {
            QFETCH(int, value1);
            QFETCH(int, value2);
            QFETCH(int, result);

            QCOMPARE(sum(value1, value2), result);
        }
};

QTEST_MAIN(TestSum)
#include <main.moc>
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Part IV: Fixtures

Post by bbguimaraes »

googletest

Code

Code: Select all

#include <gtest/gtest.h>

class SumFixture : public testing::Test {
    public:
        SumFixture() :
            N(5),
            VALUES_1{0, 2, 4, 6, 8},
            VALUES_2{1, 3, 5, 7, 9},
            RESULTS{1, 5, 9, 13, 17}
        {}

    protected:
        const unsigned int N;
        const int VALUES_1[5];
        const int VALUES_2[5];
        const int RESULTS[5];
};

int sum(int n1, int n2) {
    return n1 + n2;
}

TEST_F(SumFixture, TestSum) {
    for(unsigned int i = 0; i < N; ++i)
        ASSERT_EQ(RESULTS[i], sum(VALUES_1[i], VALUES_2[i]));
}
Post Reply