LCOV - code coverage report
Current view: top level - atermpp/test - algorithm_test.cpp (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 53 54 98.1 %
Date: 2020-02-13 00:44:47 Functions: 12 12 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 algorithm_test.cpp
      10             : /// \brief Add your file description here.
      11             : 
      12             : #define BOOST_TEST_MODULE algorithm_test
      13             : #include <boost/test/included/unit_test_framework.hpp>
      14             : 
      15             : #include "mcrl2/atermpp/aterm_io.h"
      16             : #include "mcrl2/atermpp/aterm_string.h"
      17             : #include "mcrl2/atermpp/algorithm.h"
      18             : 
      19             : using namespace std;
      20             : using namespace atermpp;
      21             : 
      22             : // function object to test if it is an aterm_appl with function symbol "f"
      23             : struct is_f
      24             : {
      25          26 :   bool operator()(const aterm_appl& t) const
      26             :   {
      27          26 :     return t.function().name() == "f";
      28             :   }
      29             : };
      30             : 
      31           3 : BOOST_AUTO_TEST_CASE(test_algorithm)
      32             : {
      33           2 :   aterm_appl a (read_appl_from_string("h(g(x),f(y),p(a(x,y),q(f(z))))"));
      34           2 :   aterm_appl b (read_appl_from_string("h(g(x),p(a(x,y),q(g(z))))"));
      35             : 
      36           2 :   aterm_appl t = find_if(a, is_f());
      37           1 :   BOOST_CHECK(t == read_term_from_string("f(y)"));
      38           2 :   aterm_appl t1 = find_if(b, is_f());
      39           1 :   BOOST_CHECK(t1 == aterm_appl());
      40             : 
      41           2 :   std::vector<aterm_appl> v;
      42           1 :   find_all_if(a, is_f(), back_inserter(v));
      43           1 :   BOOST_CHECK(v.front() == read_term_from_string("f(y)"));
      44           1 :   BOOST_CHECK(v.back() == read_term_from_string("f(z)"));
      45           1 : }
      46             : 
      47             : struct for_each_proc
      48             : {
      49             :   std::set<std::string>& m_names;
      50             : 
      51           1 :   for_each_proc(std::set<std::string>& names)
      52           1 :     : m_names(names)
      53           1 :   {}
      54             : 
      55           5 :   bool operator()(const aterm& t)
      56             :   {
      57           5 :     if(t.type_is_appl())
      58             :     {
      59           5 :       return (*this)(down_cast<aterm_appl>(t));
      60             :     }
      61             :     else
      62             :     {
      63           0 :       return false;
      64             :     }
      65             :   }
      66             : 
      67           5 :   bool operator()(const aterm_appl& t)
      68             :   {
      69           5 :     m_names.insert(t.function().name());
      70           5 :     return true;
      71             :   }
      72             : };
      73             : 
      74           3 : BOOST_AUTO_TEST_CASE(test_for_each)
      75             : {
      76           2 :   aterm_appl t (read_appl_from_string("h(g(x),f(y))"));
      77           2 :   std::set<std::string> names;
      78           1 :   atermpp::for_each(t, for_each_proc(names));
      79           6 :   for (std::set<std::string>::iterator i = names.begin(); i != names.end(); ++i)
      80             :   {
      81           5 :     std::cout << *i << " ";
      82             :   }
      83           1 :   BOOST_CHECK(names.find("h") != names.end());
      84           1 :   BOOST_CHECK(names.find("g") != names.end());
      85           1 :   BOOST_CHECK(names.find("x") != names.end());
      86           1 :   BOOST_CHECK(names.find("f") != names.end());
      87           1 :   BOOST_CHECK(names.find("y") != names.end());
      88           1 : }
      89             : 
      90           3 : BOOST_AUTO_TEST_CASE(test_operators)
      91             : {
      92             :   {
      93           2 :     aterm_appl a1 (read_appl_from_string("a1"));
      94           2 :     aterm_appl a2 (read_appl_from_string("a2"));
      95           1 :     bool b = (a1 < a2);
      96           1 :     std::clog << "b = " << (b?"true":"false") << std::endl;
      97             :   }
      98             : 
      99             :   {
     100           2 :     atermpp::aterm_string a1("a1");
     101           2 :     atermpp::aterm_string a2("a2");
     102           1 :     bool b = (a1 < a2);
     103           1 :     std::clog << "b = " << (b?"true":"false") << std::endl;
     104             :   }
     105             : 
     106             :   {
     107           2 :     aterm_int a1 = read_int_from_string("1");
     108           2 :     aterm_int a2 = read_int_from_string("2");
     109           1 :     bool b = (a1 < a2);
     110           1 :     std::clog << "b = " << (b?"true":"false") << std::endl;
     111             :   }
     112             : 
     113             :   {
     114           2 :     aterm_list a1 = read_list_from_string("[1,2]");
     115           2 :     aterm_list a2 = read_list_from_string("[3,2]");
     116           1 :     bool b = (a1 < a2);
     117           1 :     std::clog << "b = " << (b?"true":"false") << std::endl;
     118             :   }
     119           4 : }

Generated by: LCOV version 1.13