Lumiera  0.pre.03
»edit your freedom«
removefromsettest.cpp
Go to the documentation of this file.
1 /*
2  RemoveFromSet(Test) - algorithm removing predicated elements from set
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/util-foreach.hpp"
30 #include "lib/format-cout.hpp"
31 #include "lib/format-util.hpp"
32 
33 #include <boost/lexical_cast.hpp>
34 #include <functional>
35 #include <string>
36 #include <set>
37 
38 using util::join;
39 using std::function;
40 using std::string;
41 
42 
43 
44 namespace util {
45 namespace test {
46 
47  using util::for_each;
48  using boost::lexical_cast;
49 
50 
51  using IntSet = std::set<uint>;
52 
53  void
54  show (IntSet const& coll)
55  {
56  cout << "[ "
57  << join (coll)
58  << " ]" <<endl;
59  }
60 
61  function<bool(uint)>
62  select_match (string description)
63  {
64  return [&](uint candidate)
65  {
66  return string::npos != description.find( lexical_cast<string> (candidate));
67  };
68  }
69 
70 
71 
72  class RemoveFromSet_test : public Test
73  {
74  virtual void
75  run (Arg)
76  {
77  test_remove (" nothing ");
78  test_remove ("0");
79  test_remove ("9");
80  test_remove ("5");
81  test_remove ("0 2 4 6 8 ");
82  test_remove (" 1 3 5 7 9");
83  test_remove ("0 1 2 3 4 5 6 7 8 9");
84  test_remove ("0 1 2 3 4 5 6 7 8 ");
85  test_remove (" 1 2 3 4 5 6 7 8 9");
86  test_remove ("0 1 2 3 4 6 7 8 9");
87  }
88 
89 
93  void
94  test_remove (string elems_to_remove)
95  {
96  IntSet theSet;
97  for (int i=0; i<10; ++i)
98  theSet.insert (i);
99 
100  util::remove_if (theSet, select_match(elems_to_remove));
101 
102  cout << "removed " << elems_to_remove << " ---> ";
103  show (theSet);
104  }
105 
106  };
107 
108 
109  LAUNCHER (RemoveFromSet_test, "unit common");
110 
111 
112 }} // namespace util::test
113 
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
Simple test class runner.
void test_remove(string elems_to_remove)
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
bool remove_if(SET &set, FUN test)
remove all elements fulfilling a given predicate from a (sorted) set.
Definition: util.hpp:319
string join(CON &&coll, string const &delim=", ")
enumerate a collection&#39;s contents, separated by delimiter.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Perform operations "for each element" of a collection.