GridContainers_test.cc
Go to the documentation of this file.
1 /**
2  * @file GridContainers_test.cc
3  * @brief Test for GridContainers classes
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date June 30, 2016
6  * @see GridContainers.h
7  *
8  * This test instantiates some GridContainer objects.
9  *
10  * The test is run with no arguments.
11  *
12  * Tests are run:
13  *
14  * * `GridContainer2DTest`: two-dimension container test
15  * * `GridContainer3DTest`: three-dimension container test
16  *
17  * See the documentation of the three functions for more information.
18  *
19  */
20 
21 // LArSoft libraries
23 
24 // Boost libraries
25 #define BOOST_TEST_MODULE ( PointIsolationAlg_test )
26 #include "boost/test/unit_test.hpp"
27 
28 //------------------------------------------------------------------------------
29 //--- tests
30 //
31 /**
32  * @brief Test for a GridContainer2D of integers
33  *
34  * The test instantiates a `GridContainer2D<int>` container with a hard-coded,
35  * known content, and verifies all the elements of the interface (except the
36  * move version of the insertion methods).
37  *
38  */
40 
41  //
42  // initialise
43  //
44  using Container_t = util::GridContainer2D<int>;
45  // BUG the double brace syntax is required to work around clang bug 21629
46  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
47  // not sure precisely why this is required a second time... it has to do
48  // with using the constructor of a base class
49  Container_t grid({{{ 2U, 3U }}});
50 
51  //
52  // container structure and indexing
53  //
54  BOOST_TEST(grid.dims() == 2U);
55 
56  BOOST_TEST(grid.size() == 6U);
57  BOOST_TEST(grid.sizeX() == 2U);
58  BOOST_TEST(grid.sizeY() == 3U);
59 
60  // BUG the double brace syntax is required to work around clang bug 21629
61  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
62  BOOST_TEST(grid.index({{ 0, 0 }}) == 0U);
63  BOOST_TEST(grid.index({{ 1, 2 }}) == 5U);
64  BOOST_CHECK_NO_THROW(grid.index({{ 2, 2 }})); // out-of-bound
65 
66  BOOST_TEST( grid.has(0));
67  BOOST_TEST( grid.has(grid.size() - 1));
68  BOOST_TEST(!grid.has(grid.size()));
69 
70  // BUG the double brace syntax is required to work around clang bug 21629
71  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
72  BOOST_TEST(grid.indexOffset({{ 0, 1 }}, {{ 1, 2 }}) == 4);
73  BOOST_TEST(grid.indexOffset({{ 1, 2 }}, {{ 0, 1 }}) == -4);
74 
75  //
76  // fill the container
77  //
78  Container_t::CellID_t cellID;
79  for (cellID[0] = 0; (size_t) cellID[0] < grid.sizeX(); ++cellID[0]) {
80  for (cellID[1] = 0; (size_t) cellID[1] < grid.sizeY(); ++cellID[1]) {
81 
82  auto cellIndex = grid.index(cellID);
83 
84  int count = cellID[0] + cellID[1];
85  while (count-- > 0) {
86  if (count & 1) grid.insert(cellID, count);
87  else grid.insert(cellIndex, count);
88  } // while
89 
90  } // for iy
91  } // for ix
92 
93  //
94  // read the container
95  //
96  for (cellID[0] = 0; (size_t) cellID[0] < grid.sizeX(); ++cellID[0]) {
97  for (cellID[1] = 0; (size_t) cellID[1] < grid.sizeY(); ++cellID[1]) {
98 
99  int count = cellID[0] + cellID[1];
100 
101  auto cellIndex = grid.index(cellID);
102  auto const& cell = (count & 1)? grid[cellIndex]: grid[cellID];
103 
104  BOOST_TEST_CHECKPOINT
105  ("[" << cellID[0] << "][" << cellID[1] << "]");
106  BOOST_TEST(cell.size() == (size_t) count);
107 
108  for (size_t k = 0; k < cell.size(); ++k) {
109  int val = cell[k];
110  BOOST_TEST_CHECKPOINT(" [" << k << "]");
111  BOOST_TEST(val == --count);
112  } // for
113 
114  } // for iy
115  } // for ix
116 
117 } // GridContainer2DTest()
118 
119 
120 //------------------------------------------------------------------------------
121 /**
122  * @brief Test for a GridContainer3D of integers
123  *
124  * The test instantiates a `GridContainer3D<int>` container with a hard-coded,
125  * known content, and verifies all the elements of the interface (except the
126  * move version of the insertion methods).
127  *
128  */
130 
131  //
132  // initialise
133  //
134  using Container_t = util::GridContainer3D<int>;
135  // BUG the double brace syntax is required to work around clang bug 21629
136  // (https://bugs.llvm.org/show_bug.cgi?id=21629);
137  // not sure precisely why this is required a second time... it has to do
138  // with using the constructor of a base class
139  Container_t grid({{{ 2U, 3U, 4U }}});
140 
141  //
142  // container structure and indexing
143  //
144  BOOST_TEST(grid.dims() == 3U);
145 
146  BOOST_TEST(grid.size() == 24U);
147  BOOST_TEST(grid.sizeX() == 2U);
148  BOOST_TEST(grid.sizeY() == 3U);
149  BOOST_TEST(grid.sizeZ() == 4U);
150 
151  // BUG the double brace syntax is required to work around clang bug 21629
152  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
153  BOOST_TEST(grid.index({{ 0, 0, 0 }}) == 0U);
154  BOOST_TEST(grid.index({{ 1, 2, 3 }}) == 23U);
155  BOOST_CHECK_NO_THROW(grid.index({{ 2, 2, 3 }})); // out-of-bound
156 
157  BOOST_TEST( grid.has(0));
158  BOOST_TEST( grid.has(grid.size() - 1));
159  BOOST_TEST(!grid.has(grid.size()));
160 
161  // BUG the double brace syntax is required to work around clang bug 21629
162  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
163  BOOST_TEST(grid.indexOffset({{ 0, 1, 2 }}, {{ 1, 2, 3 }}) == 17);
164  BOOST_TEST(grid.indexOffset({{ 1, 2, 3 }}, {{ 0, 1, 2 }}) == -17);
165 
166  //
167  // fill the container
168  //
169  Container_t::CellID_t cellID;
170  for (cellID[0] = 0; (size_t) cellID[0] < grid.sizeX(); ++cellID[0]) {
171  for (cellID[1] = 0; (size_t) cellID[1] < grid.sizeY(); ++cellID[1]) {
172  for (cellID[2] = 0; (size_t) cellID[2] < grid.sizeZ(); ++cellID[2]) {
173 
174  auto cellIndex = grid.index(cellID);
175 
176  int count = cellID[0] + cellID[1] + cellID[2];
177  while (count-- > 0) {
178  if (count & 1) grid.insert(cellID, count);
179  else grid.insert(cellIndex, count);
180  } // while
181 
182  } // for iz
183  } // for iy
184  } // for ix
185 
186  //
187  // read the container
188  //
189  for (cellID[0] = 0; (size_t) cellID[0] < grid.sizeX(); ++cellID[0]) {
190  for (cellID[1] = 0; (size_t) cellID[1] < grid.sizeY(); ++cellID[1]) {
191  for (cellID[2] = 0; (size_t) cellID[2] < grid.sizeZ(); ++cellID[2]) {
192 
193  int count = cellID[0] + cellID[1] + cellID[2];
194 
195  auto cellIndex = grid.index(cellID);
196  auto const& cell = (count & 1)? grid[cellIndex]: grid[cellID];
197 
198  BOOST_TEST_CHECKPOINT
199  ("[" << cellID[0] << "][" << cellID[1] << "][" << cellID[2] << "]");
200  BOOST_TEST(cell.size() == (size_t) count);
201 
202  for (size_t k = 0; k < cell.size(); ++k) {
203  int val = cell[k];
204  BOOST_TEST_CHECKPOINT(" [" << k << "]");
205  BOOST_TEST(val == --count);
206  } // for
207 
208  } // for iz
209  } // for iy
210  } // for ix
211 
212 } // GridContainer3DTest()
213 
214 
215 //------------------------------------------------------------------------------
216 //--- test cases
217 //
218 BOOST_AUTO_TEST_CASE(GridContainer2DTestCase) {
220 } // GridContainer2DTestCase
221 
222 BOOST_AUTO_TEST_CASE(GridContainer3DTestCase) {
224 } // GridContainer3DTestCase
BOOST_AUTO_TEST_CASE(GridContainer2DTestCase)
Base class for a container of data arranged on a 2D-grid.
long long int CellID_t
Definition: CaloRawDigit.h:24
void GridContainer3DTest()
Test for a GridContainer3D of integers.
void GridContainer2DTest()
Test for a GridContainer2D of integers.
Containers with indices in 1, 2 and 3 dimensions.
Base class for a container of data arranged on a 3D-grid.