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.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 atermpp; 20 : 21 : // function object to test if it is an aterm_appl with function symbol "f" 22 : struct is_f 23 : { 24 26 : bool operator()(const aterm_appl& t) const 25 : { 26 26 : return t.function().name() == "f"; 27 : } 28 : }; 29 : 30 2 : BOOST_AUTO_TEST_CASE(test_algorithm) 31 : { 32 2 : aterm_appl a (read_appl_from_string("h(g(x),f(y),p(a(x,y),q(f(z))))")); 33 2 : aterm_appl b (read_appl_from_string("h(g(x),p(a(x,y),q(g(z))))")); 34 : 35 1 : aterm_appl t = find_if(a, is_f()); 36 1 : BOOST_CHECK(t == read_term_from_string("f(y)")); 37 1 : aterm_appl t1 = find_if(b, is_f()); 38 1 : BOOST_CHECK(t1 == aterm_appl()); 39 : 40 1 : std::vector<aterm_appl> v; 41 1 : find_all_if(a, is_f(), back_inserter(v)); 42 1 : BOOST_CHECK(v.front() == read_term_from_string("f(y)")); 43 1 : BOOST_CHECK(v.back() == read_term_from_string("f(z)")); 44 1 : } 45 : 46 : struct for_each_proc 47 : { 48 : std::set<std::string>& m_names; 49 : 50 1 : for_each_proc(std::set<std::string>& names) 51 1 : : m_names(names) 52 1 : {} 53 : 54 5 : bool operator()(const aterm& t) 55 : { 56 5 : if(t.type_is_appl()) 57 : { 58 5 : return (*this)(down_cast<aterm_appl>(t)); 59 : } 60 : else 61 : { 62 0 : return false; 63 : } 64 : } 65 : 66 5 : bool operator()(const aterm_appl& t) 67 : { 68 5 : m_names.insert(t.function().name()); 69 5 : return true; 70 : } 71 : }; 72 : 73 2 : BOOST_AUTO_TEST_CASE(test_for_each) 74 : { 75 2 : aterm_appl t (read_appl_from_string("h(g(x),f(y))")); 76 1 : std::set<std::string> names; 77 1 : atermpp::for_each(t, for_each_proc(names)); 78 6 : for (std::set<std::string>::iterator i = names.begin(); i != names.end(); ++i) 79 : { 80 5 : std::cout << *i << " "; 81 : } 82 1 : BOOST_CHECK(names.find("h") != names.end()); 83 1 : BOOST_CHECK(names.find("g") != names.end()); 84 1 : BOOST_CHECK(names.find("x") != names.end()); 85 1 : BOOST_CHECK(names.find("f") != names.end()); 86 1 : BOOST_CHECK(names.find("y") != names.end()); 87 1 : } 88 : 89 2 : BOOST_AUTO_TEST_CASE(test_operators) 90 : { 91 : { 92 2 : aterm_appl a1 (read_appl_from_string("a1")); 93 2 : aterm_appl a2 (read_appl_from_string("a2")); 94 1 : bool b = (a1 < a2); 95 1 : std::clog << "b = " << (b?"true":"false") << std::endl; 96 1 : } 97 : 98 : { 99 2 : atermpp::aterm_string a1("a1"); 100 2 : atermpp::aterm_string a2("a2"); 101 1 : bool b = (a1 < a2); 102 1 : std::clog << "b = " << (b?"true":"false") << std::endl; 103 1 : } 104 : 105 : { 106 2 : aterm_int a1 = read_int_from_string("1"); 107 2 : aterm_int a2 = read_int_from_string("2"); 108 1 : bool b = (a1 < a2); 109 1 : std::clog << "b = " << (b?"true":"false") << std::endl; 110 1 : } 111 : 112 : { 113 2 : aterm_list a1 = read_list_from_string("[1,2]"); 114 2 : aterm_list a2 = read_list_from_string("[3,2]"); 115 1 : bool b = (a1 < a2); 116 1 : std::clog << "b = " << (b?"true":"false") << std::endl; 117 1 : } 118 1 : }