LCOV - code coverage report
Current view: top level - utilities/test - sequence_test.cpp (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 31 31 100.0 %
Date: 2024-04-26 03:18:02 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Wieger Wesselink
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file sequence_test.cpp
      10             : /// \brief Add your file description here.
      11             : 
      12             : #define BOOST_TEST_MODULE sequence_test
      13             : 
      14             : #include "mcrl2/utilities/sequence.h"
      15             : 
      16             : #include <boost/test/included/unit_test.hpp>
      17             : #include <numeric>
      18             : 
      19             : using namespace mcrl2;
      20             : 
      21             : struct f
      22             : {
      23             :   std::vector<int>& v_;
      24             :   int& sum_;
      25             : 
      26           1 :   f(std::vector<int>& v, int& sum)
      27           1 :     : v_(v), sum_(sum)
      28           1 :   {}
      29             : 
      30             :   // Adds the sum of the elements of v_ to sum_.
      31           4 :   void operator()() const
      32             :   {
      33          16 :     for (std::vector<int>::const_iterator i = v_.begin(); i != v_.end(); ++i)
      34             :     {
      35          12 :       std::cout << *i << " ";
      36             :     }
      37           4 :     std::cout << std::endl;
      38           4 :     sum_ += std::accumulate(v_.begin(), v_.end(), 0);
      39           4 :   }
      40             : };
      41             : 
      42           1 : void test_sequence()
      43             : {
      44           1 :   std::vector<int> a;
      45           1 :   std::vector<int> b;
      46           1 :   std::vector<int> c;
      47             : 
      48           1 :   a.push_back(1);
      49           1 :   a.push_back(2);
      50           1 :   b.push_back(3);
      51           1 :   c.push_back(2);
      52           1 :   c.push_back(5);
      53             : 
      54           1 :   std::vector<std::vector<int> > v;
      55           1 :   v.push_back(a);
      56           1 :   v.push_back(b);
      57           1 :   v.push_back(c);
      58             : 
      59           1 :   int sum = 0;
      60             : 
      61             :   // w will hold a sequence
      62           1 :   std::vector<int> w(v.size());
      63             : 
      64           1 :   utilities::foreach_sequence(v, w.begin(), f(w, sum));
      65           1 :   std::cout << "sum == " << sum << std::endl;
      66           1 :   BOOST_CHECK(sum == 32);
      67           1 : }
      68             : 
      69           2 : BOOST_AUTO_TEST_CASE(test_main)
      70             : {
      71           1 :   test_sequence();
      72           1 : }

Generated by: LCOV version 1.14