HistoryViewTest.h 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef HISTORYVIEWTEST_H_
#define HISTORYVIEWTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/HistoryView.h"

using namespace Mantid::API;
using namespace Mantid::Kernel;
10
using Mantid::Kernel::DateAndTime;
11

12
class HistoryViewTest : public CxxTest::TestSuite {
13

14
15
16
17
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
  static HistoryViewTest *createSuite() { return new HistoryViewTest(); }
18
  static void destroySuite(HistoryViewTest *suite) { delete suite; }
19
20

private:
Samuel Jackson's avatar
Samuel Jackson committed
21
  // 'Empty' algorithm class for tests
22
  class testalg : public Algorithm {
Samuel Jackson's avatar
Samuel Jackson committed
23
24
25
  public:
    testalg() : Algorithm() {}
    virtual ~testalg() {}
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    const std::string summary() const {
      return "testalg";
    } ///< Algorithm's documentation summary
    const std::string name() const {
      return "testalg";
    } ///< Algorithm's name for identification
    int version() const {
      return 1;
    } ///< Algorithm's version for identification
    const std::string category() const {
      return "Cat";
    } ///< Algorithm's category for identification

    void init() { declareProperty("name", "", Direction::Input); }
40

Samuel Jackson's avatar
Samuel Jackson committed
41
42
    void exec() {}
  };
43
44

private:
45
46
47
  AlgorithmHistory_sptr
  createFromTestAlg(const std::string &name,
                    DateAndTime execTime = DateAndTime::defaultTime()) {
48
49
50
51
52
    testalg alg;
    alg.initialize();
    alg.setPropertyValue("name", name);
    alg.execute();

53
    AlgorithmHistory history(&alg, execTime, 14.0, m_execCount++);
54
55
    return boost::make_shared<AlgorithmHistory>(history);
  }
56

57
public:
58
59
  HistoryViewTest() : m_wsHist(), m_execCount(0) {
    // create dummy history structure
60
61
    auto alg1 = createFromTestAlg("alg1", DateAndTime(100, 0));
    auto child1 = createFromTestAlg("child1", DateAndTime(110, 0));
62
    alg1->addChildHistory(child1);
63
64
65
66
67
68
69

    auto alg2 = createFromTestAlg("alg2", DateAndTime(200, 0));
    auto child2 = createFromTestAlg("child2", DateAndTime(210, 0));

    auto subChild21 = createFromTestAlg("subChild21", DateAndTime(211, 0));
    auto subChild22 = createFromTestAlg("subChild22", DateAndTime(212, 0));

70
71
72
73
    child2->addChildHistory(subChild21);
    child2->addChildHistory(subChild22);

    alg2->addChildHistory(child2);
74
75

    auto alg3 = createFromTestAlg("alg3", DateAndTime(300, 0));
76
77
78
79
80
81

    m_wsHist.addHistory(alg1);
    m_wsHist.addHistory(alg2);
    m_wsHist.addHistory(alg3);
  }

82
  void test_Empty() {
83
84
    WorkspaceHistory wsHist;
    HistoryView view(wsHist);
85
86
87
    TS_ASSERT_EQUALS(view.size(), 0);
    TS_ASSERT_THROWS_ANYTHING(view.unroll(0));
    TS_ASSERT_THROWS_ANYTHING(view.roll(0));
88
89
  }

90
  void test_Build() {
91
    HistoryView view(m_wsHist);
92
    TS_ASSERT_EQUALS(view.size(), 3);
93
94
95

    int i = 0;
    auto items = view.getAlgorithmsList();
96
    for (auto it = items.begin(); it != items.end(); ++it, ++i) {
97
98
      auto history = it->getAlgorithmHistory();
      auto props = history->getProperties();
99
100
      TS_ASSERT_EQUALS(props[0]->value(),
                       "alg" + boost::lexical_cast<std::string>(i + 1));
101
102
103
    }
  }

104
  void test_Unroll_History() {
105
    HistoryView view(m_wsHist);
106
107
    // unroll alg 2
    TS_ASSERT_THROWS_NOTHING(view.unroll(0));
108
    TS_ASSERT_EQUALS(view.size(), 4);
109
110
111
112

    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 4);

113
    std::vector<std::string> propNames(items.size());
114
115
116
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
117
    }
118

119
120
121
122
    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "child1")
    TS_ASSERT_EQUALS(propNames[2], "alg2")
    TS_ASSERT_EQUALS(propNames[3], "alg3")
123
124
  }

125
126
  void test_Simple_Roll_History() {
    // tests the case where we have a single layer of history unrolled
127
128
    HistoryView view(m_wsHist);

129
130
    // unroll alg 2
    TS_ASSERT_THROWS_NOTHING(view.unroll(0));
131

132
133
134
135
    TS_ASSERT_EQUALS(view.size(), 4);
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 4);

136
    std::vector<std::string> propNames(items.size());
137
138
139
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
140
    }
141

142
    // check it unrolled properly
143
144
145
146
    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "child1")
    TS_ASSERT_EQUALS(propNames[2], "alg2")
    TS_ASSERT_EQUALS(propNames[3], "alg3")
147

148
149
    // roll it back up
    TS_ASSERT_THROWS_NOTHING(view.roll(0));
150
151
152
153
154

    TS_ASSERT_EQUALS(view.size(), 3);
    items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 3);

155
156
    propNames.clear();
    propNames.resize(items.size());
157
158
159
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
160
161
    }

162
    // check it rolled back up properly
163
164
165
    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "alg2")
    TS_ASSERT_EQUALS(propNames[2], "alg3")
166
167
  }

168
169
  void test_Complex_Roll_History() {
    // tests the case where we have multiple layers of history unrolled
170
    HistoryView view(m_wsHist);
171

172
173
    // unroll alg2
    TS_ASSERT_THROWS_NOTHING(view.unroll(1));
174
175
176
177
178

    TS_ASSERT_EQUALS(view.size(), 4);
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 4);

179
    std::vector<std::string> propNames(items.size());
180
181
182
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
183
184
185
186
187
188
    }

    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "alg2")
    TS_ASSERT_EQUALS(propNames[2], "child2")
    TS_ASSERT_EQUALS(propNames[3], "alg3")
189

190
191
    // unroll another level of history
    TS_ASSERT_THROWS_NOTHING(view.unroll(2))
192
193
194
195

    TS_ASSERT_EQUALS(view.size(), 6);
    items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 6);
196
197
198

    propNames.clear();
    propNames.resize(items.size());
199
200
201
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
202
203
204
205
206
207
208
209
    }

    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "alg2")
    TS_ASSERT_EQUALS(propNames[2], "child2")
    TS_ASSERT_EQUALS(propNames[3], "subChild21")
    TS_ASSERT_EQUALS(propNames[4], "subChild22")
    TS_ASSERT_EQUALS(propNames[5], "alg3")
210

211
212
    // now roll everything back up to the top level
    TS_ASSERT_THROWS_NOTHING(view.roll(1))
213

214
215
216
217
    TS_ASSERT_EQUALS(view.size(), 3);
    items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 3);

218
219
    propNames.clear();
    propNames.resize(items.size());
220
221
222
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
223
224
225
226
227
228
229
    }

    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "alg2")
    TS_ASSERT_EQUALS(propNames[2], "alg3")
  }

230
  void test_Unroll_All() {
231
    HistoryView view(m_wsHist);
232

233
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
234
235
236
237
238
239

    TS_ASSERT_EQUALS(view.size(), 7);
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 7);

    std::vector<std::string> propNames(items.size());
240
241
242
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
243
244
245
246
247
248
249
250
251
252
253
    }

    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "child1")
    TS_ASSERT_EQUALS(propNames[2], "alg2")
    TS_ASSERT_EQUALS(propNames[3], "child2")
    TS_ASSERT_EQUALS(propNames[4], "subChild21")
    TS_ASSERT_EQUALS(propNames[5], "subChild22")
    TS_ASSERT_EQUALS(propNames[6], "alg3")
  }

254
  void test_Roll_All() {
255
    HistoryView view(m_wsHist);
256

257
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
258
259
260
261

    TS_ASSERT_EQUALS(view.size(), 7);
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 7);
262

263
    std::vector<std::string> propNames(items.size());
264
265
266
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
267
268
269
270
271
272
273
274
275
276
    }

    TS_ASSERT_EQUALS(propNames[0], "alg1")
    TS_ASSERT_EQUALS(propNames[1], "child1")
    TS_ASSERT_EQUALS(propNames[2], "alg2")
    TS_ASSERT_EQUALS(propNames[3], "child2")
    TS_ASSERT_EQUALS(propNames[4], "subChild21")
    TS_ASSERT_EQUALS(propNames[5], "subChild22")
    TS_ASSERT_EQUALS(propNames[6], "alg3")

277
    TS_ASSERT_THROWS_NOTHING(view.rollAll());
278
279
280

    items = view.getAlgorithmsList();
    int i = 0;
281
    for (auto it = items.begin(); it != items.end(); ++it, ++i) {
282
283
      auto history = it->getAlgorithmHistory();
      auto props = history->getProperties();
284
285
      TS_ASSERT_EQUALS(props[0]->value(),
                       "alg" + boost::lexical_cast<std::string>(i + 1));
286
    }
287
288
  }

289
  void test_Index_To_Large() {
290
    HistoryView view(m_wsHist);
291
292
    TS_ASSERT_THROWS_ANYTHING(view.unroll(3));
    TS_ASSERT_THROWS_ANYTHING(view.roll(3));
293
294
  }

295
  void test_Filter_By_Exec_Time_Full_Range() {
296
297
298
    HistoryView view(m_wsHist);

    // Unroll to get all algorithms
299
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
300
301
302
    TS_ASSERT_EQUALS(view.size(), 7);

    // Filter by time with a start and end time
303
304
    TS_ASSERT_THROWS_NOTHING(
        view.filterBetweenExecDate(DateAndTime(200, 0), DateAndTime(211, 0)));
305
306
307
308
309
310
311
    TS_ASSERT_EQUALS(view.size(), 3);

    // Get algorithm list and compare results
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 3);

    std::vector<std::string> propNames(items.size());
312
313
314
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
315
316
317
318
319
320
321
    }

    TS_ASSERT_EQUALS(propNames[0], "alg2")
    TS_ASSERT_EQUALS(propNames[1], "child2")
    TS_ASSERT_EQUALS(propNames[2], "subChild21")
  }

322
  void test_Filter_By_Exec_Time_Start_Only() {
323
324
325
    HistoryView view(m_wsHist);

    // Unroll to get all algorithms
326
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
327
328
329
    TS_ASSERT_EQUALS(view.size(), 7);

    // Filter by time with a start time only
330
    TS_ASSERT_THROWS_NOTHING(view.filterBetweenExecDate(DateAndTime(200, 0)));
331
332
333
334
335
336
337
    TS_ASSERT_EQUALS(view.size(), 5);

    // Get algorithm list and compare results
    auto items = view.getAlgorithmsList();
    TS_ASSERT_EQUALS(items.size(), 5);

    std::vector<std::string> propNames(items.size());
338
339
340
    for (size_t i = 0; i < items.size(); ++i) {
      propNames[i] =
          items[i].getAlgorithmHistory()->getProperties()[0]->value();
341
342
343
344
345
346
347
348
349
    }

    TS_ASSERT_EQUALS(propNames[0], "alg2")
    TS_ASSERT_EQUALS(propNames[1], "child2")
    TS_ASSERT_EQUALS(propNames[2], "subChild21")
    TS_ASSERT_EQUALS(propNames[3], "subChild22")
    TS_ASSERT_EQUALS(propNames[4], "alg3")
  }

350
351
352
353
  WorkspaceHistory m_wsHist;
  size_t m_execCount;
};

354
class HistoryViewTestPerformance : public CxxTest::TestSuite {
Hahn, Steven's avatar
Hahn, Steven committed
355

356
357
358
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
Hahn, Steven's avatar
Hahn, Steven committed
359
360
361
  static HistoryViewTestPerformance *createSuite() {
    return new HistoryViewTestPerformance();
  }
362
  static void destroySuite(HistoryViewTestPerformance *suite) { delete suite; }
Hahn, Steven's avatar
Hahn, Steven committed
363

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
private:
  // 'Empty' algorithm class for tests
  class testalg : public Algorithm {
  public:
    testalg() : Algorithm() {}
    virtual ~testalg() {}
    const std::string summary() const {
      return "testalg";
    } ///< Algorithm's documentation summary
    const std::string name() const {
      return "testalg";
    } ///< Algorithm's name for identification
    int version() const {
      return 1;
    } ///< Algorithm's version for identification
    const std::string category() const {
      return "Cat";
    } ///< Algorithm's category for identification
Hahn, Steven's avatar
Hahn, Steven committed
382

383
    void init() { declareProperty("name", "", Direction::Input); }
Hahn, Steven's avatar
Hahn, Steven committed
384

385
386
    void exec() {}
  };
Hahn, Steven's avatar
Hahn, Steven committed
387

388
389
390
391
392
393
394
395
private:
  AlgorithmHistory_sptr
  createFromTestAlg(const std::string &name,
                    DateAndTime execTime = DateAndTime::defaultTime()) {
    testalg alg;
    alg.initialize();
    alg.setPropertyValue("name", name);
    alg.execute();
Hahn, Steven's avatar
Hahn, Steven committed
396

397
398
399
    AlgorithmHistory history(&alg, execTime, 14.0, m_execCount++);
    return boost::make_shared<AlgorithmHistory>(history);
  }
Hahn, Steven's avatar
Hahn, Steven committed
400

401
402
403
public:
  HistoryViewTestPerformance() : m_wsHist(), m_execCount(0) {
    // create dummy history structure
Hahn, Steven's avatar
Hahn, Steven committed
404
405
406
407
408
409
410
411
412
413
414
415
416

    for (int i = 0; i < 20; ++i) {
      auto alg = createFromTestAlg("alg" + boost::lexical_cast<std::string>(i),
                                   DateAndTime(100 + i, 0));
      for (int j = 0; j < 20; ++j) {
        auto childAlg =
            createFromTestAlg("child" + boost::lexical_cast<std::string>(j),
                              DateAndTime(200 + j, 0));
        for (int k = 0; k < 20; ++k) {
          auto subChildAlg = createFromTestAlg(
              "subChild" + boost::lexical_cast<std::string>(j) +
                  boost::lexical_cast<std::string>(k),
              DateAndTime(300 + k, 0));
417
418
419
420
421
422
423
          childAlg->addChildHistory(subChildAlg);
        }
        alg->addChildHistory(childAlg);
      }
      m_wsHist.addHistory(alg);
    }
  }
Hahn, Steven's avatar
Hahn, Steven committed
424

425
426
427
428
  void test_UnrollAll() {
    HistoryView view(m_wsHist);
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
  }
Hahn, Steven's avatar
Hahn, Steven committed
429

430
431
432
433
434
  void test_UnrollAllRollAll() {
    HistoryView view(m_wsHist);
    TS_ASSERT_THROWS_NOTHING(view.unrollAll());
    TS_ASSERT_THROWS_NOTHING(view.rollAll());
  }
Hahn, Steven's avatar
Hahn, Steven committed
435

436
437
438
439
  WorkspaceHistory m_wsHist;
  size_t m_execCount;
};

440
#endif