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.cpp 10 : /// \brief Add your file description here. 11 : 12 : #include "mcrl2/atermpp/algorithm.h" 13 : #include "mcrl2/atermpp/aterm_io.h" 14 : 15 : using namespace atermpp; 16 : 17 : // function object to test if it is an aterm_appl with function symbol "f" 18 : struct is_f 19 : { 20 0 : bool operator()(const atermpp::aterm& t) const 21 : { 22 0 : return (t.type_is_appl()) && down_cast<aterm_appl>(t).function().name() == "f"; 23 : } 24 : }; 25 : 26 : // function object to test if it is an aterm_appl with function symbol "a" or "b" 27 : struct is_a_or_b 28 : { 29 : bool operator()(const atermpp::aterm& t) const 30 : { 31 : return (t.type_is_appl()) && 32 : (down_cast<aterm_appl>(t).function().name() == "a" || down_cast<aterm_appl>(t).function().name() == "b"); 33 : } 34 : }; 35 : 36 0 : void test_find() 37 : { 38 0 : aterm_appl a(read_appl_from_string("h(g(x),f(y),p(a(x,y),q(f(z))))")); 39 : 40 0 : aterm_appl t = find_if(a, is_f()); 41 0 : assert(t == read_term_from_string("f(y)")); 42 : 43 0 : std::vector<atermpp::aterm> v; 44 0 : find_all_if(a, is_f(), back_inserter(v)); 45 0 : assert(v.front() == read_term_from_string("f(y)")); 46 0 : assert(v.back() == read_term_from_string("f(z)")); 47 0 : } 48 : 49 0 : void test_replace() 50 : { 51 0 : aterm_appl a (read_appl_from_string("f(f(x))")); 52 0 : aterm_appl b = replace(a, read_appl_from_string("f(x)"), read_appl_from_string("x")); 53 0 : assert(b == read_term_from_string("f(x)")); 54 0 : } 55 : 56 0 : int main(int /*argc*/, char** /*argv*/) 57 : { 58 0 : test_find(); 59 0 : test_replace(); 60 : 61 0 : return 0; 62 : }