Boost unittest

S

saneman

I am using boost for unittesting. But I would like to make a global
container with some content that can be used in all the boost functions:


#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>
// Boost Test declaration and Checking macros
#include <boost/test/unit_test_suite.hpp>
#include <boost/test/test_tools.hpp>

BOOST_AUTO_TEST_SUITE(my_module);
BOOST_AUTO_TEST_SUITE();

// Types
std::vector<int> v;
v.push_back(22); //This gives an error!

BOOST_AUTO_TEST_CASE(test_one)
{
int t = *v.begin()
BOOST_CHECK_EQUAL(t,22);
}


BOOST_AUTO_TEST_CASE(test_two)
{
// do som other test
BOOST_CHECK_EQUAL(i,4);
}

BOOST_AUTO_TEST_SUITE_END();
BOOST_AUTO_TEST_CASE(my_always_fail_test_case)
{
BOOST_CHECK(false);
}
BOOST_AUTO_TEST_SUITE_END();



But when I do v.push_back(22); and compile I get:

syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

Is it not possible to use global data?
 
A

acehreli

// Types
std::vector<int> v;
v.push_back(22);   //This gives an error!

You can't use the global namespace for everything. You must use a
function:

std::vector<int> init_v()
{
std::vector<int> v;
v.push_back(22);
return v;
}

std::vector<int> v = init_v();

There won't be any performance implication.

Ali
 
S

saneman

<[email protected]> skrev i en meddelelse
// Types
std::vector<int> v;
v.push_back(22); //This gives an error!

You can't use the global namespace for everything. You must use a
function:

std::vector<int> init_v()
{
std::vector<int> v;
v.push_back(22);
return v;
}

std::vector<int> v = init_v();





Another thing I have tried to make the function work on a reference instead:

void init_v(std::vector<int> & v_)
{
v_.push_back(22);
}
std::vector<int> v;
init_v(v);

But the I get:

missing type specifier - int assumed. Note: C++ does not support default-int

What is wrong with using references?
 
M

Michael DOUBEZ

saneman a écrit :
<[email protected]> skrev i en meddelelse


You can't use the global namespace for everything.


Out of curiosity why not? Is it a special boost thing?

It is something specific to many compiled languages: you have only one
entry point of execution (i.e. main() in C++) and code get executed from
there only. The only exception in C++ being the construction of globals
for initialisation. In all cases code is executed from within a function.

Concerning your question:
1. if you need only one value in your global, you can use:
std::vector<int> v(1,22);//vector of size one filled with 22
2. For unit test, usual practice is to set up the environement before
the test such that tests are independant.

Example:

std::vector<int> v;
void init_v(std::vector<int> & vec)
{
vec.clear();
vec.push_back(22);
}


BOOST_AUTO_TEST_CASE(test_one)
{
//init
init_v(v);

//test
int t = *v.begin()
BOOST_CHECK_EQUAL(t,22);
}

Make v a local variable is even better.
 
J

James Kanze

saneman a écrit :
It is something specific to many compiled languages: you have
only one entry point of execution (i.e. main() in C++) and
code get executed from there only. The only exception in C++
being the construction of globals for initialisation. In all
cases code is executed from within a function.

Actually, as you point out later, this isn't true of C++. You
can add additional entry points anywhere you like, just by
defining a variable with static lifetime, e.g.:

static bool dummyForInitialization = (startUpFunction(), true) ;

(Generally speaking, I'd prefer having the startUpFunction
return a bool, and drop the comma operator. But I did it this
way to show that it can easily be done with any function.)
Concerning your question:
1. if you need only one value in your global, you can use:
std::vector<int> v(1,22);//vector of size one filled with 22

In this particular case, that's obviously the solution. More
generally, however, he could do something like:

std::vector< int > v ;
bool dummyForInitialization = (v.push_back( 22 ), true) ;

(I'm not saying that he should, of course:). Only that the
possibility exists.)
2. For unit test, usual practice is to set up the environement before
the test such that tests are independant.

I'd say that that is almost a requirement, no?
 
M

Michael DOUBEZ

James Kanze a écrit :
I'd say that that is almost a requirement, no?

Yes.

I have seen some test suite that rely on the outcome of previous test
case within a suite; such as: previous test did insertion and current
test is deletion. I don't find it good practice but I am not the
maintainer (and quite happy to stay that way).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top