Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
removefromsettest.cpp
Go to the documentation of this file.
1/*
2 RemoveFromSet(Test) - algorithm removing predicated elements from set
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
20#include "lib/util-foreach.hpp"
21#include "lib/format-cout.hpp"
22#include "lib/format-util.hpp"
23
24#include <boost/lexical_cast.hpp>
25#include <functional>
26#include <string>
27#include <set>
28
29using util::join;
30using std::function;
31using std::string;
32
33
34
35namespace util {
36namespace test {
37
38 using util::for_each;
39 using boost::lexical_cast;
40
41
42 using IntSet = std::set<uint>;
43
44 void
45 show (IntSet const& coll)
46 {
47 cout << "[ "
48 << join (coll)
49 << " ]" <<endl;
50 }
51
52 function<bool(uint)>
53 select_match (string description)
54 {
55 return [&](uint candidate)
56 {
57 return string::npos != description.find( lexical_cast<string> (candidate));
58 };
59 }
60
61
62
63 class RemoveFromSet_test : public Test
64 {
65 virtual void
66 run (Arg)
67 {
68 test_remove (" nothing ");
69 test_remove ("0");
70 test_remove ("9");
71 test_remove ("5");
72 test_remove ("0 2 4 6 8 ");
73 test_remove (" 1 3 5 7 9");
74 test_remove ("0 1 2 3 4 5 6 7 8 9");
75 test_remove ("0 1 2 3 4 5 6 7 8 ");
76 test_remove (" 1 2 3 4 5 6 7 8 9");
77 test_remove ("0 1 2 3 4 6 7 8 9");
78 }
79
80
84 void
85 test_remove (string elems_to_remove)
86 {
87 IntSet theSet;
88 for (int i=0; i<10; ++i)
89 theSet.insert (i);
90
91 util::remove_if (theSet, select_match(elems_to_remove));
92
93 cout << "removed " << elems_to_remove << " ---> ";
94 show (theSet);
95 }
96
97 };
98
99
100 LAUNCHER (RemoveFromSet_test, "unit common");
101
102
103}} // namespace util::test
104
void test_remove(string elems_to_remove)
Automatically use custom string conversion in C++ stream output.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
unsigned int uint
Definition integral.hpp:29
Test runner and basic definitions for tests.
function< bool(uint)> select_match(string description)
void show(IntSet const &coll)
std::set< uint > IntSet
bool remove_if(SET &set, FUN test)
remove all elements fulfilling a given predicate from a (sorted) set.
Definition util.hpp:319
string join(COLL &&coll, string const &delim=", ")
enumerate a collection's contents, separated by delimiter.
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Perform operations "for each element" of a collection.