'Is there a way to print the final value of an increment, before incrementing it, in C++?

EDIT TITLE (I changed the title because it was wrongly referring to macros and compilation time, leading to confusion about my question)

In order to help with the output of my tests in c++ programs, I print the test number before each test. Something like this output :

[1/2] test :
// some test

[2/2] test :
// some test

the numeration of the test is in two parts :

[ <number of this test> / <total number of tests> ]

If I add a test or remove one, I don't have to hard-written the <number of this test>, because it's an int that increment itself each time. But, I have to count the <total number of test> and write it down manually (for example, in a macro, but that could be a variable as well).

here is how the code looks like :

#include <iostream>
#define N_TEST "2"

int     main() {
    int i = 0;

    std::cout << "\n[" << ++i << "/" N_TEST "] test something :\n";
    {
        // some tests here
    }

    std::cout << "\n[" << ++i << "/" N_TEST "] test another thing :\n";
    {
        // some different tests here
    }

    return 0;
}

Is there a way to fill the <total number of tests> automatically ?

c++


Solution 1:[1]

I post here a solution using only c++98

I'm not sure anyone will see it, since my question was really badly asked

And I'm not sure it's useful, outside a school context, to avoid using c++11

And finally, I'm not sure my solution is very good or solid, but it works ;)

anyway :

tests.h

#ifndef TESTS_H
#define TESTS_H

#include <vector>
#include <string>

# define TEST(s) \
    {\
        test_title = #s;\
        struct tester : public test_base {\
            void func()

# define TESTEND \
        };\
        test = new(tester);\
        test->title = test_title;\
        test_list.push_back(test);\
    }

struct test_base {
        std::string title;
        virtual void func() {}
};

std::vector<test_base *> test_list;
test_base *test;
std::string test_title;

#endif

main.cpp

#include <iostream>

int main()
{
    TEST(name of first test)
    {
        std::cout << "hello test.\n";
    }
    TESTEND

    TEST(name of second test)
    {
        std::cout << "hello another test.\n";
    }
    TESTEND

    int size = test_list.size();
    for (int i = 0, i < size, i++)
    {
        std::cout
            << "\n[" << i + 1 << "/" << size << "] "
            << test_list[i]->title << " :\n";
        test_list[i]->func();
    }
}

or, if I get rid of the macros, for clarity here :

#include <iostream>
#include <vector>
#include <string>

struct test_base {
        std::string title;
        virtual void func() {}
};

int main()
{
    std::vector<test_base *> test_list;
    test_base *test;

    {
        struct tester : public test_base {
            void func()
            {
                std::cout << "hello test.\n";
            }
        };
        test = new(tester);
        test->title = "name of first test";
        test_list.push_back(test);
    }

    {
        struct tester : public test_base {
            void func()
            {
                std::cout << "hello another test.\n";
            }
        };
        test = new(tester);
        test->title = "name of first test";
        test_list.push_back(test);
    }

    int size = test_list.size();
    for (int i = 0, i < size, i++)
    {
        std::cout
            << "\n[" << i + 1 << "/" << size << "] "
            << test_list[i]->title << " :\n";
        test_list[i]->func();
    }
}

I use a local scope for each test, so I can define the struct tester again

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hugogogo