File: block_range.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 (162 lines) | stat: -rw-r--r-- 5,871 bytes parent folder | download | duplicates (2)
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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2015  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/>.
 *
 *  Justus Calvin
 *  Department of Chemistry, Virginia Tech
 *
 *  block_range.cpp
 *  May 29, 2015
 *
 */

#include "TiledArray/block_range.h"
#include "unit_test_config.h"
#include "range_fixture.h"

struct BlockRangeFixture {

  BlockRangeFixture() { }

  ~BlockRangeFixture() { }


  static const Range r0;
  static const Range r;
}; // BlockRangeFixture


const Range BlockRangeFixture::r0{std::array<int, 3>{{5,11,8}}};
const Range BlockRangeFixture::r{std::array<int, 3>{{0,1,2}}, std::array<int, 3>{{5,11,8}}};

BOOST_FIXTURE_TEST_SUITE( block_range_suite, BlockRangeFixture )


BOOST_AUTO_TEST_CASE( block_zero_lower_bound )
{
  BlockRange block_range;

  for(auto lower_it = r0.begin(); lower_it != r0.end(); ++lower_it) {
    const auto lower = *lower_it;
    for(auto upper_it = r0.begin(); upper_it != r0.end(); ++upper_it) {
      std::vector<std::size_t> upper = *upper_it;
      for(unsigned int i = 0u; i < upper.size(); ++i)
        ++(upper[i]);

      if(std::equal(lower.begin(), lower.end(), upper.begin(),
          [] (std::size_t l, std::size_t r0) { return l < r0; })) {

        // Check that the sub-block is constructed without exceptions
        BOOST_CHECK_NO_THROW(block_range = BlockRange(r0, lower,upper));

        // Check that the data of the block range is correct
        std::size_t volume = 1ul;
        for(unsigned int i = 0u; i < r0.rank(); ++i) {
          // Check that the range data is correct
          BOOST_CHECK_EQUAL(block_range.lobound_data()[i], lower[i]);
          BOOST_CHECK_EQUAL(block_range.upbound_data()[i], upper[i]);
          BOOST_CHECK_EQUAL(block_range.extent_data()[i], upper[i] - lower[i]);
          BOOST_CHECK_EQUAL(block_range.stride_data()[i], r0.stride_data()[i]);
          volume *= upper[i] - lower[i];
        }
        // Check for the correct volume
        BOOST_CHECK_EQUAL(block_range.volume(), volume);

        // Check that the subrange ordinal calculation returns the same offset
        // as the original range.
        Range::size_type index = 0ul;
        for(auto it = block_range.begin(); it != block_range.end(); ++it, ++index) {
          // Check that the ordinal offset returned for an ordianl offset and a
          // coordinate index agree.
          BOOST_CHECK_EQUAL(block_range.ordinal(*it), r0.ordinal(*it));

          // Check that the ordinal function returns the correct offset in the
          // parent index space.
          BOOST_CHECK_EQUAL(block_range.ordinal(index), r0.ordinal(*it));

          // Check that the index returned by idx is correct
          BOOST_CHECK_EQUAL(block_range.idx(index), *it);
        }
      }
#ifdef TA_EXCEPTION_ERROR
      else {
        // Check for excpetion with invalid input
        BOOST_CHECK_THROW(BlockRange(r0, lower,upper), TiledArray::Exception);
      }
#endif // TA_EXCEPTION_ERROR

    }
  }
}


BOOST_AUTO_TEST_CASE( block )
{
  BlockRange block_range;

  for(auto lower_it = r.begin(); lower_it != r.end(); ++lower_it) {
    const auto lower = *lower_it;
    for(auto upper_it = r.begin(); upper_it != r.end(); ++upper_it) {
      std::vector<std::size_t> upper = *upper_it;
      for(unsigned int i = 0u; i < r.rank(); ++i)
        ++(upper[i]);

      if(std::equal(lower.begin(), lower.end(), upper.begin(),
          [] (std::size_t l, std::size_t r) { return l < r; })) {

        // Check that the sub-block is constructed without exceptions
        BOOST_CHECK_NO_THROW(block_range = BlockRange(r, lower,upper));

        // Check that the data of the block range is correct
        std::size_t volume = 1ul;
        for(unsigned int i = 0u; i < r.rank(); ++i) {
          // Check that the range data is correct
          BOOST_CHECK_EQUAL(block_range.lobound_data()[i], lower[i]);
          BOOST_CHECK_EQUAL(block_range.upbound_data()[i], upper[i]);
          BOOST_CHECK_EQUAL(block_range.extent_data()[i], upper[i] - lower[i]);
          BOOST_CHECK_EQUAL(block_range.stride_data()[i], r.stride_data()[i]);
          volume *= upper[i] - lower[i];
        }
        // Check for the correct volume
        BOOST_CHECK_EQUAL(block_range.volume(), volume);

        // Check that the subrange ordinal calculation returns the same
        // offset as the original range.
        Range::size_type index = 0ul;
        for(auto it = block_range.begin(); it != block_range.end(); ++it, ++index) {
          // Check that the ordinal offset returned for an ordianl offset and a
          // coordinate index agree.
          BOOST_CHECK_EQUAL(block_range.ordinal(*it), r.ordinal(*it));

          // Check that the ordinal function returns the correct offset in the
          // parent index space.
          BOOST_CHECK_EQUAL(block_range.ordinal(index), r.ordinal(*it));
          // Check that the index returned by idx is correct
          BOOST_CHECK_EQUAL(block_range.idx(index), *it);
        }
      }
#ifdef TA_EXCEPTION_ERROR
      else {
        // Check for excpetion with invalid input
        BOOST_CHECK_THROW(BlockRange(r, lower,upper), TiledArray::Exception);
      }
#endif // TA_EXCEPTION_ERROR

    }
  }
}

BOOST_AUTO_TEST_SUITE_END()