Boost unittest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • saneman

    Boost unittest

    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_modul e);
    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_EQU AL(t,22);
    }


    BOOST_AUTO_TEST _CASE(test_two)
    {
    // do som other test
    BOOST_CHECK_EQU AL(i,4);
    }

    BOOST_AUTO_TEST _SUITE_END();
    BOOST_AUTO_TEST _CASE(my_always _fail_test_case )
    {
    BOOST_CHECK(fal se);
    }
    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?


  • acehreli@gmail.com

    #2
    Re: Boost unittest

    On Aug 6, 12:34 pm, "saneman" <as...@asd.comw rote:
    // 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

    Comment

    • saneman

      #3
      Re: Boost unittest


      <acehreli@gmail .comskrev i en meddelelse
      news:012bcdc5-8af2-403a-ace7-12ffa1a3b588@1g 2000pre.googleg roups.com...
      On Aug 6, 12:34 pm, "saneman" <as...@asd.comw rote:
      // 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?


      Comment

      • saneman

        #4
        Re: Boost unittest


        <acehreli@gmail .comskrev i en meddelelse
        news:012bcdc5-8af2-403a-ace7-12ffa1a3b588@1g 2000pre.googleg roups.com...
        On Aug 6, 12:34 pm, "saneman" <as...@asd.comw rote:
        // 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::vec tor<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?




        Comment

        • James Kanze

          #5
          Re: Boost unittest

          On Aug 7, 9:18 am, Michael DOUBEZ <michael.dou... @free.frwrote:
          saneman a écrit :
          <acehr...@gmail .comskrev i en meddelelse
          news:012bcdc5-8af2-403a-ace7-12ffa1a3b588@1g 2000pre.googleg roups.com...
          On Aug 6, 12:34 pm, "saneman" <as...@asd.comw rote:
          // 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.
          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 dummyForInitial ization = (startUpFunctio n(), 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 dummyForInitial ization = (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?

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...