File: tiled_range1.cpp

package info (click to toggle)
tiledarray 0.6.0-5.2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 5,844 kB
  • sloc: cpp: 31,688; sh: 237; ansic: 227; makefile: 57; python: 12
file content (229 lines) | stat: -rw-r--r-- 8,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * This file is a part of TiledArray.
 * Copyright (C) 2013  Virginia Tech
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <utility>
#include "TiledArray/tiled_range1.h"
#include "unit_test_config.h"
#include "range_fixture.h"
#include <sstream>

using namespace TiledArray;

BOOST_FIXTURE_TEST_SUITE( range1_suite, Range1Fixture )

BOOST_AUTO_TEST_CASE( range_accessor )
{
  BOOST_CHECK_EQUAL(tr1.tiles_range().first, tiles.first);
  BOOST_CHECK_EQUAL(tr1.tiles_range().second, tiles.second);
  BOOST_CHECK_EQUAL(tr1.elements_range().first, elements.first);
  BOOST_CHECK_EQUAL(tr1.elements_range().second, elements.second);

  // Check individual tiles
  for(std::size_t i = 0; i < a.size() - 1; ++i) {
    BOOST_CHECK_EQUAL(tr1.tile(i).first, a[i]);
    BOOST_CHECK_EQUAL(tr1.tile(i).second, a[i + 1]);
  }
}

BOOST_AUTO_TEST_CASE( range_info )
{
  BOOST_CHECK_EQUAL(tr1.tiles_range().first, 0ul);
  BOOST_CHECK_EQUAL(tr1.tiles_range().second, a.size() - 1);
  BOOST_CHECK_EQUAL(tr1.elements_range().first, 0ul);
  BOOST_CHECK_EQUAL(tr1.elements_range().second, a.back());
  for(std::size_t i = 0; i < a.size() - 1; ++i) {
    BOOST_CHECK_EQUAL(tr1.tile(i).first, a[i]);
    BOOST_CHECK_EQUAL(tr1.tile(i).second, a[i + 1]);
  }
}

BOOST_AUTO_TEST_CASE( constructor )
{
  // check default construction and range info.
  {
    BOOST_REQUIRE_NO_THROW(TiledRange1 r);
    TiledRange1 r;
    BOOST_CHECK_EQUAL(r.tiles_range().first, 0ul);
    BOOST_CHECK_EQUAL(r.tiles_range().second, 0ul);
    BOOST_CHECK_EQUAL(r.elements_range().first, 0ul);
    BOOST_CHECK_EQUAL(r.elements_range().second, 0ul);
#ifdef TA_EXCEPTION_ERROR
    BOOST_CHECK_THROW(r.tile(0), Exception);
#endif // TA_EXCEPTION_ERROR
  }

  // check construction with a iterators and the range info.
  {
    BOOST_REQUIRE_NO_THROW(TiledRange1 r(a.begin(), a.end()));
    TiledRange1 r(a.begin(), a.end());
    BOOST_CHECK_EQUAL(r.tiles_range().first, tiles.first);
    BOOST_CHECK_EQUAL(r.tiles_range().second, tiles.second);
    BOOST_CHECK_EQUAL(r.elements_range().first, elements.first);
    BOOST_CHECK_EQUAL(r.elements_range().second, elements.second);
    for(std::size_t i = 0; i < a.size() - 1; ++i) {
      BOOST_CHECK_EQUAL(r.tile(i).first, a[i]);
      BOOST_CHECK_EQUAL(r.tile(i).second, a[i + 1]);
    }
  }

  // check variadic constructor using a's tile boundaries. NOTE: a is a runtime object, must manually specify sizes here
  {
    BOOST_REQUIRE_NO_THROW(TiledRange1 r(0,1,2,3,4,5));
    if (Range1Fixture::ntiles == 5) {
      TiledRange1 r(0,2,5,10,17,28);
      BOOST_CHECK_EQUAL(r.tiles_range().first, tiles.first);
      BOOST_CHECK_EQUAL(r.tiles_range().second, tiles.second);
      BOOST_CHECK_EQUAL(r.elements_range().first, elements.first);
      BOOST_CHECK_EQUAL(r.elements_range().second, elements.second);
      for(std::size_t i = 0; i < a.size() - 1; ++i) {
        BOOST_CHECK_EQUAL(r.tile(i).first, a[i]);
        BOOST_CHECK_EQUAL(r.tile(i).second, a[i + 1]);
      }
    }
  }

  // check initializer list constructor using a's tile boundaries. NOTE: a is a runtime object, must manually specify sizes here
  {
    if (Range1Fixture::ntiles == 5) {
      TiledRange1 r {0,2,5,10,17,28};
      BOOST_CHECK_EQUAL(r.tiles_range().first, tiles.first);
      BOOST_CHECK_EQUAL(r.tiles_range().second, tiles.second);
      BOOST_CHECK_EQUAL(r.elements_range().first, elements.first);
      BOOST_CHECK_EQUAL(r.elements_range().second, elements.second);
      for(std::size_t i = 0; i < a.size() - 1; ++i) {
        BOOST_CHECK_EQUAL(r.tile(i).first, a[i]);
        BOOST_CHECK_EQUAL(r.tile(i).second, a[i + 1]);
      }
    }
  }

  // check copy constructor
  {
    BOOST_REQUIRE_NO_THROW(TiledRange1 r(tr1));
    TiledRange1 r(tr1);
    BOOST_CHECK_EQUAL(r.tiles_range().first, tiles.first);
    BOOST_CHECK_EQUAL(r.tiles_range().second, tiles.second);
    BOOST_CHECK_EQUAL(r.elements_range().first, elements.first);
    BOOST_CHECK_EQUAL(r.elements_range().second, elements.second);
    for(std::size_t i = 0; i < a.size() - 1; ++i) {
      BOOST_CHECK_EQUAL(r.tile(i).first, a[i]);
      BOOST_CHECK_EQUAL(r.tile(i).second, a[i + 1]);
    }
  }

  // check construction with element range that does not start at 0.
  {
    BOOST_REQUIRE_NO_THROW(TiledRange1 r(a.begin() + 1, a.end()));
    TiledRange1 r(a.begin() + 1, a.end());
    BOOST_CHECK_EQUAL(r.tiles_range().first, 0ul);
    BOOST_CHECK_EQUAL(r.tiles_range().second, a.size() - 2);
    BOOST_CHECK_EQUAL(r.elements_range().first, a[1]);
    BOOST_CHECK_EQUAL(r.elements_range().second, a.back());
    for(std::size_t i = 1; i < a.size() - 1; ++i) {
      BOOST_CHECK_EQUAL(r.tile(i - 1).first, a[i]);
      BOOST_CHECK_EQUAL(r.tile(i - 1).second, a[i + 1]);
    }
  }

  // Check that invalid input throws an exception.
  {
#ifndef NDEBUG
    std::vector<std::size_t> boundaries;
    BOOST_CHECK_THROW(TiledRange1 r(boundaries.begin(), boundaries.end()), Exception);
    BOOST_CHECK_THROW(TiledRange1 r(a.begin(), a.begin()), Exception);
    BOOST_CHECK_THROW(TiledRange1 r(a.begin(), a.begin() + 1), Exception);
    boundaries.push_back(2);
    boundaries.push_back(0);
    BOOST_CHECK_THROW(TiledRange1 r(boundaries.begin(), boundaries.end()), Exception);
#endif // NDEBUG
  }
}

BOOST_AUTO_TEST_CASE( ostream )
{
  std::stringstream stm;
  stm << "( tiles = [ " << 0 << ", " << a.size() - 1 <<
      " ), elements = [ " << a.front() << ", " << a.back() << " ) )";

  boost::test_tools::output_test_stream output;
  output << tr1;
  BOOST_CHECK( !output.is_empty( false ) );
  BOOST_CHECK( output.check_length( stm.str().size(), false ) );
  BOOST_CHECK( output.is_equal( stm.str().c_str() ) );
}

BOOST_AUTO_TEST_CASE( element2tile )
{
  // construct a map that should match the element to tile map for tr1.
  std::vector<std::size_t> e;
  for(std::size_t t = tr1.tiles_range().first; t < tr1.tiles_range().second; ++t)
    for(std::size_t i = tr1.tile(t).first; i < tr1.tile(t).second; ++i)
      e.push_back(t);

  // Construct a map that matches the internal element to tile map for tr1.
  std::vector<std::size_t> c;
  for(std::size_t i = tr1.elements_range().first; i < tr1.elements_range().second; ++i)
    c.push_back(tr1.element2tile(i));

  // Check that the expected and internal element to tile maps match.
  BOOST_CHECK_EQUAL_COLLECTIONS(c.begin(), c.end(), e.begin(), e.end());
}

BOOST_AUTO_TEST_CASE( comparison )
{
  TiledRange1 r1{ 1, 2, 4, 6, 8, 10 };
  TiledRange1 r2{ 1, 2, 4, 6, 8, 10 };
  TiledRange1 r3{ 1, 3, 6, 9, 12, 15 };
  BOOST_CHECK(r1 == r2);     // check equality operator
  BOOST_CHECK(! (r1 != r2)); // check not-equal operator
  BOOST_CHECK(! (r1 == r3)); // check for inequality with different number of tiles.
  BOOST_CHECK(r1 != r3);
}

BOOST_AUTO_TEST_CASE( iteration )
{
  // check for proper iteration functionality.
  std::size_t count = 0;
  for(TiledRange1::const_iterator it = tr1.begin(); it != tr1.end(); ++it, ++count) {
    BOOST_CHECK_EQUAL(it->first, a[count]);
    BOOST_CHECK_EQUAL(it->second, a[count + 1]);
  }
  BOOST_CHECK_EQUAL(count, tr1.tiles_range().second - tr1.tiles_range().first);
}

BOOST_AUTO_TEST_CASE( find )
{
  // check that find returns an iterator to the correct tile.
  BOOST_CHECK_EQUAL( tr1.find(tr1.tile(3).first + 1)->first, a[3]);
  BOOST_CHECK_EQUAL( tr1.find(tr1.tile(3).first + 1)->second, a[4]);

  // check that the iterator points to the end() iterator if the element is out of range.
  BOOST_CHECK( tr1.find(a.back() + 10) == tr1.end());

}

BOOST_AUTO_TEST_CASE( assignment )
{
  TiledRange1 r1;
  BOOST_CHECK_NE( r1, tr1);
  BOOST_CHECK_EQUAL((r1 = tr1), tr1); // check operator=
  BOOST_CHECK_EQUAL(r1, tr1);
}

BOOST_AUTO_TEST_SUITE_END()