The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
iterator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "config_cache.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/grid.hpp"
23 
24 #include <iostream>
25 #include <sstream>
26 #include <typeinfo>
27 
28 /*
29  * In the unit tests we use a widget tree that looks like:
30  *
31  * [0]
32  * \
33  * [1|2|3|4]
34  * \
35  * [5|6|7|8]
36  *
37  * Where widgets 0 and 1 are a grid and the rest of the widgets a label.
38  * The unit tests traverse the tree.
39  */
40 
42 {
43  static const std::string result =
44  "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
45  "Iterate child: proceed. Down and visit '1'.\n"
46  "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
47  "Iterate child: proceed. Down and visit '5'.\n"
48  "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
49  "Iterate child: reached the end. Up widget '5'. "
50  "Iterate: reached '6'. Down and visit '6'.\n"
51  "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
52  "Iterate child: reached the end. Up widget '6'. "
53  "Iterate: reached '7'. Down and visit '7'.\n"
54  "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
55  "Iterate child: reached the end. Up widget '7'. "
56  "Iterate: reached '8'. Down and visit '8'.\n"
57  "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
58  "Iterate child: reached the end. Up widget '8'. "
59  "Iterate: failed. Up widget '1'. "
60  "Iterate: reached '2'. Down and visit '2'.\n"
61  "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
62  "Iterate child: reached the end. Up widget '2'. "
63  "Iterate: reached '3'. Down and visit '3'.\n"
64  "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
65  "Iterate child: reached the end. Up widget '3'. "
66  "Iterate: reached '4'. Down and visit '4'.\n"
67  "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
68  "Iterate child: reached the end. Up widget '4'. "
69  "Iterate: failed. Finished iteration.\n";
70 
71  return result;
72 }
73 
75 {
76  static const std::string result =
77  "Constructor: Down widget '1'. Down widget '5'. Finished at '5'.\n"
78  "At '5'. Iterate widget: reached the end. Iterate grid: failed. "
79  "Iterate child: Up '1'. Iterate child: visit '1'. "
80  "Down widget '6'. Visit '6'.\n"
81  "At '6'. Iterate widget: reached the end. Iterate grid: failed. "
82  "Iterate child: Up '1'. Iterate child: visit '1'. "
83  "Down widget '7'. Visit '7'.\n"
84  "At '7'. Iterate widget: reached the end. Iterate grid: failed. "
85  "Iterate child: Up '1'. Iterate child: visit '1'. "
86  "Down widget '8'. Visit '8'.\n"
87  "At '8'. Iterate widget: reached the end. Iterate grid: failed. "
88  "Iterate child: Up '1'. Iterate child: reached the end. Visit '1'.\n"
89  "At '1'. Iterate widget: reached the end. Iterate grid: failed. "
90  "Iterate child: Up '0'. Iterate child: visit '0'. "
91  "Down widget '2'. Visit '2'.\n"
92  "At '2'. Iterate widget: reached the end. Iterate grid: failed. "
93  "Iterate child: Up '0'. Iterate child: visit '0'. "
94  "Down widget '3'. Visit '3'.\n"
95  "At '3'. Iterate widget: reached the end. Iterate grid: failed. "
96  "Iterate child: Up '0'. Iterate child: visit '0'. "
97  "Down widget '4'. Visit '4'.\n"
98  "At '4'. Iterate widget: reached the end. Iterate grid: failed. "
99  "Iterate child: Up '0'. Iterate child: reached the end. Visit '0'.\n"
100  "At '0'. Iterate widget: reached the end. Iterate grid: failed. "
101  "Iterate child: Finished iteration.\n";
102 
103  return result;
104 }
105 
107  , gui2::twidget* widget
108  , const std::string& id
109  , const unsigned row
110  , const unsigned column)
111 {
112  BOOST_REQUIRE_NE(widget, static_cast<gui2::twidget*>(nullptr));
113 
114  widget->set_id(id);
115  grid.set_child(widget
116  , row
117  , column
120  , 0);
121 }
122 
123 template<class T>
124 static void test_control()
125 {
126  T control;
127 
128  {
130  true
131  , true
132  , true> >
133  iterator(control);
134 
135  /***** INITIAL STATE *****/
136 
137  BOOST_CHECK_EQUAL(iterator.at_end(), false);
138 
139  BOOST_CHECK_EQUAL(&*iterator, &control);
140 
141  /***** POST END *****/
142 
143  BOOST_CHECK_EQUAL(iterator.next(), false);
144 
145  BOOST_CHECK_EQUAL(iterator.at_end(), true);
146 
147  }
148  {
150  false
151  , true
152  , true> >
153  iterator(control);
154 
155  /***** INITIAL STATE *****/
156 
157  BOOST_CHECK_EQUAL(iterator.at_end(), true);
158  }
159 
160  {
162  BOOST_CHECK_EQUAL(iterator.at_end(), false);
163  }
164  {
166  BOOST_CHECK_EQUAL(iterator.at_end(), true);
167  }
168 }
169 
170 static void test_control()
171 {
172  /* Could add more widgets to the list. */
173  test_control<gui2::tlabel>();
174 
175 }
176 
177 static void test_grid()
178 {
179  /* An empty grid behaves the same as a control so test here. */
180  test_control<gui2::tgrid>();
181 
182  /* Test the child part here. */
183  gui2::tgrid grid(2 ,2);
184  grid.set_id("0");
185 
186  gui2::tgrid* g = new gui2::tgrid(2, 2);
187  add_widget(grid, g, "1", 0, 0);
188  add_widget(grid, new gui2::tlabel(), "2", 1, 0);
189  add_widget(grid, new gui2::tlabel(), "3", 0, 1);
190  add_widget(grid, new gui2::tlabel(), "4", 1, 1);
191 
192  add_widget(*g, new gui2::tlabel(), "5", 0, 0);
193  add_widget(*g, new gui2::tlabel(), "6", 1, 0);
194  add_widget(*g, new gui2::tlabel(), "7", 0, 1);
195  add_widget(*g, new gui2::tlabel(), "8", 1, 1);
196 
197  {
198  std::stringstream sstr;
199  lg::tredirect_output_setter redirect_output(sstr);
200 
202  true
203  , true
204  , true> >
205  iterator(grid);
206 
207  while(iterator.next()) {
208  /* DO NOTHING */
209  }
210 
211  BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
212  }
213  {
214  std::stringstream sstr;
215  lg::tredirect_output_setter redirect_output(sstr);
216 
218  true
219  , true
220  , true> >
221  iterator(grid);
222 
223  for( ; !iterator.at_end(); ++iterator) {
224  /* DO NOTHING */
225  }
226 
227  BOOST_CHECK_EQUAL(top_down_t_t_t_result(), sstr.str());
228  }
229  {
230  std::stringstream sstr;
231  lg::tredirect_output_setter redirect_output(sstr);
232 
234  true
235  , true
236  , true> >
237  iterator(grid);
238 
239  while(iterator.next()) {
240  /* DO NOTHING */
241  }
242 
243  BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
244  }
245  {
246  std::stringstream sstr;
247  lg::tredirect_output_setter redirect_output(sstr);
248 
250  true
251  , true
252  , true> >
253  iterator(grid);
254 
255  for( ; !iterator.at_end(); ++iterator) {
256  /* DO NOTHING */
257  }
258 
259  BOOST_CHECK_EQUAL(bottom_up_t_t_t_result(), sstr.str());
260  }
261 }
262 
263 BOOST_AUTO_TEST_CASE(test_gui2_iterator)
264 {
265  /**** Initialize the environment. *****/
267 
268  cache.clear_defines();
269  cache.add_define("EDITOR");
270  cache.add_define("MULTIPLAYER");
271 
272  lg::set_log_domain_severity("gui/iterator", lg::debug());
273  lg::timestamps(false);
274 
275  std::stringstream sstr;
276  lg::tredirect_output_setter redirect_output(sstr);
277 
278  test_control();
279  test_grid();
280 }
281 
static config_cache & instance()
Get reference to the singleton object.
Base container class.
Definition: grid.hpp:29
void timestamps(bool t)
Definition: log.cpp:76
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:41
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:48
GLboolean GLboolean g
Definition: glew.h:7319
Label showing a text.
Definition: label.hpp:29
The iterator class.
Definition: iterator.hpp:39
static void test_control()
Definition: iterator.cpp:124
bool set_log_domain_severity(std::string const &name, int severity)
Definition: log.cpp:117
GLuint64EXT * result
Definition: glew.h:10727
static void add_widget(gui2::tgrid &grid, gui2::twidget *widget, const std::string &id, const unsigned row, const unsigned column)
Definition: iterator.cpp:106
void set_id(const std::string &id)
Definition: widget.cpp:97
logger & debug()
Definition: log.cpp:97
Contains the base iterator class for the gui2 widgets.
static std::string top_down_t_t_t_result()
Definition: iterator.cpp:41
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void clear_defines()
Clear stored defines map to default values.
static tcache cache
Definition: minimap.cpp:139
GLenum GLenum GLvoid * row
Definition: glew.h:3805
static std::string bottom_up_t_t_t_result()
Definition: iterator.cpp:74
BOOST_AUTO_TEST_CASE(test_gui2_iterator)
Definition: iterator.cpp:263
Base class for all widgets.
Definition: widget.hpp:49
void set_child(twidget *widget, const unsigned row, const unsigned col, const unsigned flags, const unsigned border_size)
Sets a child in the grid.
Definition: grid.cpp:69
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
Helper class to redirect the output of the logger in a certain scope.
Definition: log.hpp:71
static void test_grid()
Definition: iterator.cpp:177
void add_define(const std::string &define)
Add a entry to preproc defines map.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool at_end() const
Has the iterator reached the end?
Definition: iterator.hpp:58
Singleton class to manage game config file caching.