Boost &
googletest


  Will Shen
2011/10/17
Outline
Boost
    •   Smart pointer
    •   Assign
    •   oreach
    •   bind
googletest
    • Simple tests
    • Assertions




2
Hundred of boost libraries




              hpp
               hpp
                                   hpp + lib
                                   hpp + lib
           (.h+.cpp)
            (.h+.cpp)

    up to 90% without linking     date_time
                                  filesystem
                                  regex
                                  program_otpions
                                  test
                                  thread
                                  python
3
Memory Leaking
    Cause of Evil - new/delete




4
boost – Smart pointer
    Cause of Evil - new/delete
    Managing memory in a smart way
    • std::auto_ptr  Ownership
    • boost::scoped_ptr  Non-copyable
    • boost:: shared_ptr  Reference counting




5
Stack and Heap
    Using Stack variable to manage Heap
    memory
               p1
       Proxy   fun()
               …

                 stack   heap




6
Different smart pointers
    Ownership, Non-copyable, Reference counting




      ap1                    ap1

      ap2                    ap2


      shp1                   shp1

      shp2                   shp2


7
Used in STL Container
    Both auto_ptr and scoped_ptr can not
    be used in STL container.




       vec[0         vec[0
       ]                         e
                     ]
       vec[1         vec[1
       ]             ]
       vec[2         vec[2
       ]             ]
8
scoped_array and shared_array
    operator[]




9
boost::assign – filling containers with data
 operator+=(), operator,()




 operator()()




10
boost::assign
 list_of - to initialize a container




 repeat, repeat_fun, range




11
boost::foreach
 BOOST_FOREACH, BOOST_REVERSE_FOREACH
 Supported Sequence Type
     •   STL containers
     •   arrays
     •   Null-terminated strings (char and wchar_t)
     •   std::pair of iterators




12
boost::foreach + typeof
 BOOST_TYPEOF




13
boost::bind
 A generalization of the standard functions std::bind1st and
 std::bind2nd.
 Supports: function objects, functions, function pointers, and
 member function pointers
 Able to bind any argument to a specific value or route input
 arguments into arbitrary positions.




                                          Function object




14
boost::bind - place holders
 _1, _2, …, _9




15
Use boost::bind() in STL algorithm
                         binding member function




16
googletest - Unit Testing Framework
                                     Execute test cases through the GUI
 Google test framework
 (gtest)
  • Easy to execute
  • Easy to implement test cases




“Unicode Debug-test” configuration   Execute test cases in console




17
gtest is more lightweight than cppunit
Cppunit                              gtest
1. Write a test class derived from
   TestFixture                       1. One TEST macro
2. Write setup
3. Write teardown
4. Write test method
5. Define test suite
6. Context-switch between .h/.cpp
   files




18
Basic concepts
 Start by writing ASSERTIONS
     • statements that check whether a condition is true.
     • success, nonfatal failure, or fatal failure.
 A test case contains one or many tests.
 A test program can contain multiple test cases.




19
Assertions
 Basic Assertions, Binary Comparison, String Comparison




20
To Write Simple Tests



               Subject to be tested




21
Test Fixture - Using the Same Data
             Configuration for Multiple Tests

  SUT



Test
Fixture




                                         Tests




 22
gtest folder in GMapShop project




                      Tests




23
Recommended Books




24
Recommended Books




25
Recommended Books




26
Recommended Books




27
More to talk
 Code Smell
 Refactoring method
 Principles of Object-oriented Design
     • SRP, OCP, LSP, DSP, DIP




28

20111018 boost and gtest

  • 1.
    Boost & googletest Will Shen 2011/10/17
  • 2.
    Outline Boost • Smart pointer • Assign • oreach • bind googletest • Simple tests • Assertions 2
  • 3.
    Hundred of boostlibraries hpp hpp hpp + lib hpp + lib (.h+.cpp) (.h+.cpp) up to 90% without linking date_time filesystem regex program_otpions test thread python 3
  • 4.
    Memory Leaking Cause of Evil - new/delete 4
  • 5.
    boost – Smartpointer Cause of Evil - new/delete Managing memory in a smart way • std::auto_ptr  Ownership • boost::scoped_ptr  Non-copyable • boost:: shared_ptr  Reference counting 5
  • 6.
    Stack and Heap Using Stack variable to manage Heap memory p1 Proxy fun() … stack heap 6
  • 7.
    Different smart pointers Ownership, Non-copyable, Reference counting ap1 ap1 ap2 ap2 shp1 shp1 shp2 shp2 7
  • 8.
    Used in STLContainer Both auto_ptr and scoped_ptr can not be used in STL container. vec[0 vec[0 ] e ] vec[1 vec[1 ] ] vec[2 vec[2 ] ] 8
  • 9.
  • 10.
    boost::assign – fillingcontainers with data operator+=(), operator,() operator()() 10
  • 11.
    boost::assign list_of -to initialize a container repeat, repeat_fun, range 11
  • 12.
    boost::foreach BOOST_FOREACH, BOOST_REVERSE_FOREACH Supported Sequence Type • STL containers • arrays • Null-terminated strings (char and wchar_t) • std::pair of iterators 12
  • 13.
    boost::foreach + typeof BOOST_TYPEOF 13
  • 14.
    boost::bind A generalizationof the standard functions std::bind1st and std::bind2nd. Supports: function objects, functions, function pointers, and member function pointers Able to bind any argument to a specific value or route input arguments into arbitrary positions. Function object 14
  • 15.
    boost::bind - placeholders _1, _2, …, _9 15
  • 16.
    Use boost::bind() inSTL algorithm binding member function 16
  • 17.
    googletest - UnitTesting Framework Execute test cases through the GUI Google test framework (gtest) • Easy to execute • Easy to implement test cases “Unicode Debug-test” configuration Execute test cases in console 17
  • 18.
    gtest is morelightweight than cppunit Cppunit gtest 1. Write a test class derived from TestFixture 1. One TEST macro 2. Write setup 3. Write teardown 4. Write test method 5. Define test suite 6. Context-switch between .h/.cpp files 18
  • 19.
    Basic concepts Startby writing ASSERTIONS • statements that check whether a condition is true. • success, nonfatal failure, or fatal failure. A test case contains one or many tests. A test program can contain multiple test cases. 19
  • 20.
    Assertions Basic Assertions,Binary Comparison, String Comparison 20
  • 21.
    To Write SimpleTests Subject to be tested 21
  • 22.
    Test Fixture -Using the Same Data Configuration for Multiple Tests SUT Test Fixture Tests 22
  • 23.
    gtest folder inGMapShop project Tests 23
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
    More to talk Code Smell Refactoring method Principles of Object-oriented Design • SRP, OCP, LSP, DSP, DIP 28