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 aterm_list_test.cpp
10 : /// \brief Add your file description here.
11 :
12 : #define BOOST_TEST_MODULE aterm_list_test
13 : #include <boost/test/included/unit_test.hpp>
14 :
15 : #include "mcrl2/atermpp/aterm_io.h"
16 : #include "mcrl2/atermpp/set_operations.h"
17 :
18 : using namespace atermpp;
19 :
20 : struct counter
21 : {
22 : int& m_sum;
23 :
24 1 : counter(int& sum)
25 1 : : m_sum(sum)
26 1 : {}
27 :
28 4 : void operator()(const atermpp::aterm& t)
29 : {
30 4 : m_sum += down_cast<aterm_int>(t).value();
31 4 : }
32 : };
33 :
34 : struct func
35 : {
36 : func()
37 : {}
38 :
39 : func(int)
40 : {}
41 :
42 : atermpp::aterm operator()(const atermpp::aterm& x) const
43 : {
44 : return read_term_from_string("f(" + pp(x) + ")");
45 : }
46 : };
47 :
48 2 : BOOST_AUTO_TEST_CASE(test_aterm_list)
49 : {
50 2 : aterm_list q = read_list_from_string("[1,2,3,4]");
51 1 : aterm_list r = reverse(q); // r == [4,3,2,1]
52 :
53 1 : BOOST_CHECK(r == read_term_from_string("[4,3,2,1]"));
54 :
55 1 : aterm_list r1 = q;
56 :
57 1 : r1=push_back<aterm>(r1,aterm_int(5));
58 1 : BOOST_CHECK(r1 == read_term_from_string("[1,2,3,4,5]"));
59 :
60 1 : atermpp::aterm f = q.front(); // f == 1
61 1 : BOOST_CHECK(f == aterm_int(1));
62 :
63 1 : q.push_front(read_term_from_string("[5,6]")); // q == [[5,6],1,2,3,4]
64 :
65 1 : std::stringstream os;
66 6 : for (aterm_list::iterator i = q.begin(); i != q.end(); ++i)
67 : {
68 5 : os << *i;
69 : }
70 1 : BOOST_CHECK(os.str() == "[5,6]1234");
71 :
72 1 : int sum = 0;
73 1 : std::for_each(r.begin(), r.end(), counter(sum));
74 1 : BOOST_CHECK(sum == 10);
75 :
76 2 : aterm_list v = read_list_from_string("[1,2,3,4]");
77 2 : aterm_list w = read_list_from_string("[0,1,2,3,4]");
78 1 : BOOST_CHECK(w.tail() == v);
79 1 : w.pop_front();
80 1 : BOOST_CHECK(w == v);
81 :
82 : // test concatenation
83 : {
84 2 : aterm_list a = read_list_from_string("[1,2,3]");
85 1 : BOOST_CHECK(a + a == read_term_from_string("[1,2,3,1,2,3]"));
86 1 : }
87 1 : }
88 :
89 2 : BOOST_AUTO_TEST_CASE(test_set_operations)
90 : {
91 2 : atermpp::aterm x = read_term_from_string("x");
92 2 : atermpp::aterm y = read_term_from_string("y");
93 2 : atermpp::aterm z = read_term_from_string("z");
94 :
95 1 : aterm_list l;
96 1 : l.push_front(x);
97 1 : l.push_front(y);
98 :
99 1 : aterm_list m;
100 1 : m.push_front(z);
101 1 : m.push_front(x);
102 :
103 1 : aterm_list lm_union = term_list_union(l, m);
104 1 : BOOST_CHECK(lm_union.size() == 3);
105 :
106 1 : aterm_list lm_difference = term_list_difference(l, m);
107 1 : BOOST_CHECK(lm_difference.size() == 1);
108 1 : }
109 :
110 2 : BOOST_AUTO_TEST_CASE(test_initializer_list)
111 : {
112 2 : atermpp::aterm x = read_term_from_string("x");
113 2 : atermpp::aterm y = read_term_from_string("y");
114 3 : aterm_list l = { x, y };
115 1 : }
116 :
117 2 : BOOST_AUTO_TEST_CASE(test_list_with_apply_filter)
118 : {
119 1 : std::vector<aterm_list> v;
120 2 : aterm_list l1= read_list_from_string("[1,2,3,4]");
121 1 : v.push_back(l1);
122 2 : aterm_list l2= read_list_from_string("[1,3,7]");
123 1 : v.push_back(l2);
124 2 : aterm_list l3= read_list_from_string("[1,2,3,7,4]");
125 1 : v.push_back(l3);
126 2 : aterm_list l4= read_list_from_string("[1,7]");
127 1 : v.push_back(l4);
128 2 : aterm_list l5= read_list_from_string("[1,7,9,10,12,13,15,15,16]");
129 1 : v.push_back(l5);
130 :
131 : // Remove the first element of each list. Only add resulting lists with a length larger than 2.
132 : // As a vector can be traversed backward, this tests the backward construction.
133 : term_list<aterm_list> result1(v.begin(),
134 : v.end(),
135 5 : [](aterm_list l)->aterm_list{l.pop_front(); return l;},
136 6 : [](const aterm_list& l)->bool{return l.size()>2;});
137 1 : BOOST_CHECK(result1.size()==3);
138 :
139 : // As a list can be traversed in a forward way, this tests the forward construction.
140 1 : term_list<aterm_list> result2(result1.begin(),
141 1 : result1.end(),
142 3 : [](aterm_list l)->aterm_list{l.pop_front(); return l;},
143 4 : [](const aterm_list& l)->bool{return l.size()>2;});
144 1 : BOOST_CHECK(result2.size()==2);
145 1 : }
146 :
147 2 : BOOST_AUTO_TEST_CASE(test_concatenation)
148 : {
149 1 : term_list<aterm_int> l1;
150 1 : term_list<aterm> l2;
151 1 : BOOST_CHECK(l2+l1 == l1+l2);
152 1 : }
|