Re: Boost unittest
saneman a ?crit :
<acehreli@gmail.com> skrev i en meddelelse
news:012bcdc5-8af2-403a-ace7-12ffa1a3b588@1g2000pre.googlegroups.com...
On Aug 6, 12:34 pm, "saneman" <as...@asd.com> wrote:
// Types
std::vector<int> v;
v.push_back(22); //This gives an error!
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.
--
Michael