all unit tests: replaced BOOST_AUTO_UNIT_TEST with new SENF_AUTO_UNIT_TEST macro
[senf.git] / senf / Utils / Console / STLSupport.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief STLSupport unit tests */
25
26 //#include "STLSupport.test.hh"
27 //#include "STLSupport.test.ih"
28
29 // Custom includes
30 #include <boost/assign/list_of.hpp>
31 #include "Console.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40
41     template <class Container>
42     struct Summer
43     {
44         static int test(Container const & data)
45         {
46             int sum (0);
47             for (typename Container::const_iterator i (data.begin()), i_end (data.end());
48                  i != i_end; ++i)
49                 sum += *i;
50             return sum;
51         }
52     };
53
54     std::pair<std::string, int> mapTest(std::map<std::string,int> const & data)
55     {
56         std::string keys;
57         int sum (0);
58         for (std::map<std::string,int>::const_iterator i (data.begin()), i_end (data.end());
59              i != i_end; ++i) {
60             keys += i->first;
61             sum += i->second;
62         }
63         return std::make_pair(keys,sum);
64     }
65                 
66 }
67
68 SENF_AUTO_UNIT_TEST(vectorSupport)
69 {
70     senf::console::Executor executor;
71     senf::console::CommandParser parser;
72     senf::console::ScopedDirectory<> dir;
73     senf::console::root().add("test", dir);
74
75     std::vector<int> defv (boost::assign::list_of(7)(2).to_container(defv));
76     dir.add("test", &Summer<std::vector<int> >::test)
77         .arg("data", "test data", senf::console::kw::default_value = defv);
78     std::stringstream ss;
79
80     SENF_CHECK_NO_THROW(
81         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
82                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
83     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
84
85     ss.str("");
86     SENF_CHECK_NO_THROW( 
87         parser.parse("help test/test",
88                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
89     BOOST_CHECK_EQUAL(
90         ss.str(), 
91         "Usage:\n"
92         "    test [data:vector<int>]\n"
93         "\n"
94         "With:\n"
95         "    data      test data\n"
96         "        default: (7 2)\n" );
97 }
98
99 SENF_AUTO_UNIT_TEST(listSupport)
100 {
101     senf::console::Executor executor;
102     senf::console::CommandParser parser;
103     senf::console::ScopedDirectory<> dir;
104     senf::console::root().add("test", dir);
105
106     std::list<int> defv (boost::assign::list_of(7)(2).to_container(defv));
107     dir.add("test", &Summer<std::list<int> >::test)
108         .arg("data", "test data", senf::console::kw::default_value = defv);
109     std::stringstream ss;
110
111     SENF_CHECK_NO_THROW(
112         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
113                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
114     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
115
116     ss.str("");
117     SENF_CHECK_NO_THROW( 
118         parser.parse("help test/test",
119                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
120     BOOST_CHECK_EQUAL(
121         ss.str(), 
122         "Usage:\n"
123         "    test [data:list<int>]\n"
124         "\n"
125         "With:\n"
126         "    data      test data\n"
127         "        default: (7 2)\n" );
128 }
129
130 SENF_AUTO_UNIT_TEST(setSupport)
131 {
132     senf::console::Executor executor;
133     senf::console::CommandParser parser;
134     senf::console::ScopedDirectory<> dir;
135     senf::console::root().add("test", dir);
136
137     std::set<int> defv (boost::assign::list_of(7)(2).to_container(defv));
138     dir.add("test", &Summer<std::set<int> >::test)
139         .arg("data", "test data", senf::console::kw::default_value = defv);
140     std::stringstream ss;
141
142     SENF_CHECK_NO_THROW(
143         parser.parse("test/test; test/test (); test/test 5; test/test (13); test/test (4 5 8)",
144                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
145     BOOST_CHECK_EQUAL( ss.str(), "9\n" "0\n" "5\n" "13\n" "17\n" );
146
147     ss.str("");
148     SENF_CHECK_NO_THROW( 
149         parser.parse("help test/test",
150                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
151     BOOST_CHECK_EQUAL(
152         ss.str(), 
153         "Usage:\n"
154         "    test [data:set<int>]\n"
155         "\n"
156         "With:\n"
157         "    data      test data\n"
158         "        default: (2 7)\n" );
159 }
160
161 SENF_AUTO_UNIT_TEST(mapSupport)
162 {
163     senf::console::Executor executor;
164     senf::console::CommandParser parser;
165     senf::console::ScopedDirectory<> dir;
166     senf::console::root().add("test", dir);
167
168     std::map<std::string, int> defv (
169         boost::assign::map_list_of("foo bar",7)("bar",2).to_container(defv));
170     dir.add("test", &mapTest)
171         .arg("data", "test data", senf::console::kw::default_value = defv);
172     std::stringstream ss;
173
174     SENF_CHECK_NO_THROW(
175         parser.parse("test/test; test/test (); "
176                      "test/test (vier=4 fuenf = 5 acht=8 )",
177                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
178     BOOST_CHECK_EQUAL( ss.str(), "(\"barfoo bar\" 9)\n" "(\"\" 0)\n" "(achtfuenfvier 17)\n" ); // 
179
180     ss.str("");
181     SENF_CHECK_NO_THROW( 
182         parser.parse("help test/test",
183                      boost::bind<void>( boost::ref(executor), boost::ref(ss), _1 )) );
184     BOOST_CHECK_EQUAL(
185         ss.str(), 
186         "Usage:\n"
187         "    test [data:map<string,int>]\n"
188         "\n"
189         "With:\n"
190         "    data      test data\n"
191         "        default: (bar=2 \"foo bar\"=7)\n" );
192 }
193
194 ///////////////////////////////cc.e////////////////////////////////////////
195 #undef prefix_
196
197 \f
198 // Local Variables:
199 // mode: c++
200 // fill-column: 100
201 // comment-column: 40
202 // c-file-style: "senf"
203 // indent-tabs-mode: nil
204 // ispell-local-dictionary: "american"
205 // compile-command: "scons -u test"
206 // End: