UtilsTest.h 5.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef MANTID_KERNEL_UTILSTEST_H_
#define MANTID_KERNEL_UTILSTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidKernel/System.h"

#include "MantidKernel/Utils.h"

using namespace Mantid::Kernel;

12
class UtilsTest : public CxxTest::TestSuite {
13
public:
14
  /** Set up counter array */
15
  void test_nestedForLoopSetUp() {
16
17
    size_t counters[3];
    Utils::NestedForLoop::SetUp(3, counters, 123);
18
    for (size_t i = 0; i < 3; i++)
19
20
21
22
      TS_ASSERT_EQUALS(counters[i], 123);
  }

  /** Set up an index maker*/
23
  void test_nestedForLoopSetUpIndexMaker() {
24
25
    size_t index_max[4];
    Utils::NestedForLoop::SetUp(4, index_max);
26
27
28
29
30
    index_max[0] = 10;
    index_max[1] = 5;
    index_max[2] = 2;
    index_max[3] = 8;

31
32
    size_t index_maker[4];
    Utils::NestedForLoop::SetUpIndexMaker(4, index_maker, index_max);
33
34
35
36
37
38
39
    TS_ASSERT_EQUALS(index_maker[0], 1);
    TS_ASSERT_EQUALS(index_maker[1], 10);
    TS_ASSERT_EQUALS(index_maker[2], 50);
    TS_ASSERT_EQUALS(index_maker[3], 100);
  }

  /** Use the index_maker */
40
  void test_nestedForLoopGetLinearIndex() {
41
42
    size_t index_max[4];
    Utils::NestedForLoop::SetUp(4, index_max);
43
44
45
46
    index_max[0] = 10;
    index_max[1] = 5;
    index_max[2] = 2;
    index_max[3] = 8;
47
48
    size_t index_maker[4];
    Utils::NestedForLoop::SetUpIndexMaker(4, index_maker, index_max);
49

50
51
52
53
54
55
56
57
    size_t index[4] = {1, 1, 1, 1};
    TS_ASSERT_EQUALS(
        Utils::NestedForLoop::GetLinearIndex(4, index, index_maker),
        1 + 10 + 50 + 100);
    size_t index2[4] = {3, 2, 1, 0};
    TS_ASSERT_EQUALS(
        Utils::NestedForLoop::GetLinearIndex(4, index2, index_maker),
        3 + 20 + 50);
58
59
60
  }

  /** Back-conversion from linear index */
61
  void test_nestedForLoopGetIndicesFromLinearIndex() {
62
63
    size_t index_max[4];
    Utils::NestedForLoop::SetUp(4, index_max);
64
65
66
67
    index_max[0] = 10;
    index_max[1] = 5;
    index_max[2] = 2;
    index_max[3] = 8;
68
69
    size_t index_maker[4];
    Utils::NestedForLoop::SetUpIndexMaker(4, index_maker, index_max);
70

71
    size_t indices[4] = {0, 0, 0, 0};
72
73
    size_t out_indices[4];
    bool allDone = false;
74
    while (!allDone) {
75
      // Convert to linear index
76
77
      size_t linear_index =
          Utils::NestedForLoop::GetLinearIndex(4, indices, index_maker);
78
79

      // Back-convert
80
81
82
83
      Utils::NestedForLoop::GetIndicesFromLinearIndex(
          4, linear_index, index_maker, index_max, out_indices);
      for (size_t d = 0; d < 4; d++) {
        TS_ASSERT_EQUALS(out_indices[d], indices[d]);
84
85
86
      }

      // Keep going
87
      allDone = Utils::NestedForLoop::Increment(4, indices, index_max);
88
89
90
    }
  }

91
  /** Make a nested loop with each counter resetting at 0 */
92
  void test_nestedForLoopIncrement() {
93
94
95
96
    size_t counters[3];
    Utils::NestedForLoop::SetUp(3, counters);
    size_t counters_max[3];
    Utils::NestedForLoop::SetUp(3, counters_max, 10);
97
98
99
100
101
102

    // The data
    size_t data[10][10][10];
    memset(data, 0, sizeof(data));

    bool allDone = false;
103
104
105
    while (!allDone) {
      data[counters[0]][counters[1]][counters[2]] =
          counters[0] * 10000 + counters[1] * 100 + counters[2];
106
      allDone = Utils::NestedForLoop::Increment(3, counters, counters_max);
107
108
    }

109
110
111
112
    for (size_t x = 0; x < 10; x++)
      for (size_t y = 0; y < 10; y++)
        for (size_t z = 0; z < 10; z++) {
          TS_ASSERT_EQUALS(data[x][y][z], x * 10000 + y * 100 + z);
113
114
115
116
        }
  }

  /** Make a nested loop but use a non-zero starting index for each counter */
117
  void test_nestedForLoopIncrement_nonZeroMinimum() {
118
119
120
121
122
123
    size_t counters[3];
    Utils::NestedForLoop::SetUp(3, counters, 4);
    size_t counters_min[3];
    Utils::NestedForLoop::SetUp(3, counters_min, 4);
    size_t counters_max[3];
    Utils::NestedForLoop::SetUp(3, counters_max, 8);
124
125
126
127
128
129

    // The data
    size_t data[10][10][10];
    memset(data, 0, sizeof(data));

    bool allDone = false;
130
131
132
133
134
    while (!allDone) {
      data[counters[0]][counters[1]][counters[2]] =
          counters[0] * 10000 + counters[1] * 100 + counters[2];
      allDone = Utils::NestedForLoop::Increment(3, counters, counters_max,
                                                counters_min);
135
136
    }

137
138
139
140
141
142
143
144
    for (size_t x = 0; x < 10; x++)
      for (size_t y = 0; y < 10; y++)
        for (size_t z = 0; z < 10; z++) {
          if ((x < 4 || y < 4 || z < 4) || (x >= 8 || y >= 8 || z >= 8)) {
            TS_ASSERT_EQUALS(data[x][y][z], 0);
          } else {
            TS_ASSERT_EQUALS(data[x][y][z], x * 10000 + y * 100 + z);
          }
145
146
147
        }
  }

148
149
150
151
  void test_nestedLinearIndexes() {
    std::vector<size_t> numBins(3, 10);
    numBins[1] = 20;
    numBins[2] = 5;
152
153
154

    size_t ic(0);

155
156
157
    for (size_t k = 0; k < numBins[2]; k++) {
      for (size_t j = 0; j < numBins[1]; j++) {
        for (size_t i = 0; i < numBins[0]; i++) {
158
          auto indexes = Utils::getIndicesFromLinearIndex(ic, numBins);
159
160
161
162
163
164
165
          ic++;

          TS_ASSERT_EQUALS(indexes[0], i);
          TS_ASSERT_EQUALS(indexes[1], j);
          TS_ASSERT_EQUALS(indexes[2], k);
        }
      }
166
167
168
    }
  }

169
170
171
172
  void test_nestedLinearIndexesWith0() {
    std::vector<size_t> numBins(3, 10);
    numBins[1] = 1; // there can not be 0, shluld be at least 1
    numBins[2] = 5;
173
174
175

    size_t ic(0);

176
177
178
    for (size_t k = 0; k < numBins[2]; k++) {
      for (size_t j = 0; j < numBins[1]; j++) {
        for (size_t i = 0; i < numBins[0]; i++) {
179
          auto indexes = Utils::getIndicesFromLinearIndex(ic, numBins);
180
181
182
183
184
185
186
          ic++;

          TS_ASSERT_EQUALS(indexes[0], i);
          TS_ASSERT_EQUALS(indexes[1], j);
          TS_ASSERT_EQUALS(indexes[2], k);
        }
      }
187
188
    }
  }
189
190
191
};

#endif /* MANTID_KERNEL_UTILSTEST_H_ */