Newer
Older
#ifndef MANTID_TESTREGEXSUPPORT__
#define MANTID_TESTREGEXSUPPORT__
#include <cxxtest/TestSuite.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include "MantidKernel/Logger.h"
#include "MantidKernel/System.h"
#include <boost/regex.hpp>
Janik Zikovsky
committed
#include "MantidKernel/RegexStrings.h"
using namespace Mantid;
Janik Zikovsky
committed
using namespace Mantid::Kernel::Strings;
class RegexStringsTest : public CxxTest::TestSuite {
private:
std::stringstream testStream;
public:
Russell Taylor
committed
static RegexStringsTest *createSuite() { return new RegexStringsTest(); }
static void destroySuite(RegexStringsTest *suite) { delete suite; }
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";
}
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
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("Mantid Geometry Regular Expression", boost::regex("xp")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex("met")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex(" ")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex("rE")), 0);
TS_ASSERT_EQUALS(
StrLook("1234-5678-1234-456",
boost::regex("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}")),
1);
TS_ASSERT_EQUALS(
StrLook(
"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() {
std::vector<double> dblresult;
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 testStrRemove() {
std::string input("100.01 101.02 103.04 105.06 Remainder of string");
std::string output;
TS_ASSERT_EQUALS(
StrRemove(input, output, boost::regex("([0-9]*.[0-9]* )*")), 1);
TS_ASSERT_EQUALS(input, "Remainder of string");
TS_ASSERT_EQUALS(output, "100.01 101.02 103.04 105.06 ");
}
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");
}