forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexStringsTest.h
141 lines (124 loc) · 5.67 KB
/
RegexStringsTest.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <algorithm>
#include <cmath>
#include <cxxtest/TestSuite.h>
#include <sstream>
#include <vector>
#include "MantidKernel/Logger.h"
#include "MantidKernel/System.h"
#include "MantidKernel/RegexStrings.h"
#include <boost/regex.hpp>
using namespace Mantid;
using namespace Mantid::Kernel::Strings;
class RegexStringsTest : public CxxTest::TestSuite {
private:
std::stringstream testStream;
public:
static RegexStringsTest *createSuite() { return new RegexStringsTest(); }
static void destroySuite(RegexStringsTest *suite) { delete suite; }
RegexStringsTest() {
testStream << "2007-11-16T13:25:48 END\n"
<< "2007-11-16T13:29:36 CHANGE RUNTABLE\n"
<< "2007-11-16T13:29:49 CHANGE RUNTABLE\n"
<< "2007-11-16T13:30:21 CHANGE RUNTABLE\n"
<< "2007-11-16T13:32:38 BEGIN\n"
<< "2007-11-16T13:43:40 ABORT\n";
}
void testStrComp() {
double result;
// By Default perl regex because using boost directly
TS_ASSERT_EQUALS(
StrComp(std::string("100.01 101.02 103.04 105.06 "), boost::regex("(([0-9]*.[0-9]*) )?"), result, 0), 1);
TS_ASSERT_EQUALS(result, 100.01);
TS_ASSERT_EQUALS(
StrComp(std::string("100.01 101.02 103.04 105.06 "), boost::regex("(([0-9]*.[0-9]*) )?"), result, 1), 1);
TS_ASSERT_EQUALS(result, 101.02);
TS_ASSERT_EQUALS(
StrComp(std::string("100.01 101.02 103.04 105.06 "), boost::regex("(([0-9]*.[0-9]*) )?"), result, 2), 1);
TS_ASSERT_EQUALS(result, 103.04);
TS_ASSERT_EQUALS(StrComp("100.01 101.02 103.04 105.06 ", boost::regex("(([0-9]*.[0-9]*) )?"), result, 3), 1);
TS_ASSERT_EQUALS(result, 105.06);
}
void testStrLook() {
// By Default perl regex because using boost directly
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"), boost::regex("xp")), 1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"), boost::regex("met")), 1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"), boost::regex(" ")), 1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"), boost::regex("rE")), 0);
TS_ASSERT_EQUALS(
StrLook(std::string("1234-5678-1234-456"), boost::regex("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}")), 1);
TS_ASSERT_EQUALS(
StrLook(std::string("OX11 0QX"), boost::regex("^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$")),
1);
}
void testStrParts() {
std::vector<std::string> tokens = StrParts("Mantid Geometry Regular Expression", boost::regex(" "));
TS_ASSERT_EQUALS(tokens[0], "Mantid");
TS_ASSERT_EQUALS(tokens[1], "Geometry");
TS_ASSERT_EQUALS(tokens[2], "Regular");
TS_ASSERT_EQUALS(tokens[3], "Expression");
}
void testStrFullSplit() {
std::vector<double> dblresult;
TS_ASSERT_EQUALS(
StrFullSplit(std::string("100.01 101.02 103.04 105.06 "), boost::regex("([0-9]*.[0-9]* )?"), dblresult), 4);
TS_ASSERT_EQUALS(dblresult[0], 100.01);
TS_ASSERT_EQUALS(dblresult[1], 101.02);
TS_ASSERT_EQUALS(dblresult[2], 103.04);
TS_ASSERT_EQUALS(dblresult[3], 105.06);
std::vector<int> intresult;
TS_ASSERT_EQUALS(StrFullSplit(std::string("100 101 103 105 "), boost::regex("([0-9]* )?"), intresult), 4);
TS_ASSERT_EQUALS(intresult[0], 100);
TS_ASSERT_EQUALS(intresult[1], 101);
TS_ASSERT_EQUALS(intresult[2], 103);
TS_ASSERT_EQUALS(intresult[3], 105);
std::vector<std::string> strresult;
TS_ASSERT_EQUALS(
StrFullSplit(std::string("100.01 101.02 103.04 105.06 "), boost::regex("([0-9]*.[0-9]* )?"), strresult), 4);
TS_ASSERT_EQUALS(strresult[0], "100.01");
TS_ASSERT_EQUALS(strresult[1], "101.02");
TS_ASSERT_EQUALS(strresult[2], "103.04");
TS_ASSERT_EQUALS(strresult[3], "105.06");
}
void testStrSingleSplit() {
std::vector<double> dblresult;
TS_ASSERT_EQUALS(
StrSingleSplit(std::string("100.01 101.02 103.04 105.06 "), boost::regex("([0-9]*.[0-9]* )?"), dblresult), 1);
TS_ASSERT_EQUALS(dblresult[0], 100.01);
dblresult.clear();
TS_ASSERT_EQUALS(StrSingleSplit(std::string("101.02 103.04 105.06 "), boost::regex("([0-9]*.[0-9]* )?"), dblresult),
1);
TS_ASSERT_EQUALS(dblresult[0], 101.02);
std::vector<int> intresult;
TS_ASSERT_EQUALS(StrSingleSplit(std::string("100 101 103 105 "), boost::regex("([0-9]* )?"), intresult), 1);
TS_ASSERT_EQUALS(intresult[0], 100);
}
void testStrFullCut() {
double sgldblResult;
std::string input("100.01 101.02 103.04 105.06 Remainder of string");
TS_ASSERT_EQUALS(StrFullCut(input, boost::regex("([0-9]*.[0-9]* )?"), sgldblResult, 0), 1);
TS_ASSERT_EQUALS(sgldblResult, 100.01);
TS_ASSERT_EQUALS(input, "101.02 103.04 105.06 Remainder of string");
TS_ASSERT_EQUALS(StrFullCut(input, boost::regex("([0-9]*.[0-9]* )?"), sgldblResult, -1), 1);
TS_ASSERT_EQUALS(sgldblResult, 101.02);
TS_ASSERT_EQUALS(input, "103.04 105.06 Remainder of string");
}
void testFindComp() {
std::string output;
TS_ASSERT_EQUALS(findComp(testStream, boost::regex("BEGIN"), output), 5);
TS_ASSERT_EQUALS(output, "");
// Reset the stream pointer
testStream.seekg(0);
}
void testFindPattern() {
std::string output;
TS_ASSERT_EQUALS(findPattern(testStream, boost::regex("BEGIN"), output), 5);
TS_ASSERT_EQUALS(output, "2007-11-16T13:32:38 BEGIN");
}
};