8#include <framework/utilities/GeneralCut.h>
9#include <framework/utilities/TestHelpers.h>
10#include <gtest/gtest.h>
15 struct MockObjectType {
25 class MockVariableType {
28 double function(
const MockObjectType*
object)
const
34 const std::string name =
"mocking_variable";
41 class MockVariableManager {
44 using Object = MockObjectType;
46 using Var = MockVariableType;
48 typedef std::variant<double, int, bool> VarVariant;
51 static MockVariableManager& Instance()
53 static MockVariableManager instance;
58 Var* getVariable(
const std::string& name)
60 if (name ==
"mocking_variable") {
61 return &m_mocking_variable;
67 Var* getVariable(
const std::string& functionName, __attribute__((unused))
const std::vector<std::string>& functionArguments)
69 if (functionName ==
"mocking_variable") {
70 return &m_mocking_variable;
78 Var m_mocking_variable;
85 TEST(GeneralCutTest, cutCheck)
87 MockObjectType testObject;
89 std::unique_ptr<MockGeneralCut> a = MockGeneralCut::compile(
"1.2 < 1.5 ");
90 EXPECT_TRUE(a->check(&testObject));
91 a = MockGeneralCut::compile(
" 1.5<1.2");
92 EXPECT_FALSE(a->check(&testObject));
96 a = MockGeneralCut::compile(
"-1 == 1");
97 EXPECT_FALSE(a->check(&testObject));
98 a = MockGeneralCut::compile(
"-1 != 1");
99 EXPECT_TRUE(a->check(&testObject));
101 a = MockGeneralCut::compile(
" 12.3 >1.5 ");
102 EXPECT_TRUE(a->check(&testObject));
103 a = MockGeneralCut::compile(
"12 > 15");
104 EXPECT_FALSE(a->check(&testObject));
106 a = MockGeneralCut::compile(
"1.2 == 1.2");
107 EXPECT_TRUE(a->check(&testObject));
108 a = MockGeneralCut::compile(
" 1.5!=1.2");
109 EXPECT_TRUE(a->check(&testObject));
111 a = MockGeneralCut::compile(
"1.2 == 1.2 == 1.2");
112 EXPECT_TRUE(a->check(&testObject));
113 a = MockGeneralCut::compile(
" 1.5 == 1.5!=1.2");
114 EXPECT_TRUE(a->check(&testObject));
115 a = MockGeneralCut::compile(
" 1.5 == 1.5!=1.5");
116 EXPECT_FALSE(a->check(&testObject));
118 a = MockGeneralCut::compile(
"1.0 < 1.2 == 1.2");
119 EXPECT_TRUE(a->check(&testObject));
120 a = MockGeneralCut::compile(
" 1.5 < 1.6 != 1.6");
121 EXPECT_FALSE(a->check(&testObject));
122 a = MockGeneralCut::compile(
" 1.5 < 1.6 != 1.7");
123 EXPECT_TRUE(a->check(&testObject));
125 a = MockGeneralCut::compile(
" [12 >= 12 ]");
126 EXPECT_TRUE(a->check(&testObject));
127 a = MockGeneralCut::compile(
"[ 15>= 16 ]");
128 EXPECT_FALSE(a->check(&testObject));
130 a = MockGeneralCut::compile(
" [12 <= 12 ]");
131 EXPECT_TRUE(a->check(&testObject));
132 a = MockGeneralCut::compile(
" [ 17<= 16.7 ]");
133 EXPECT_FALSE(a->check(&testObject));
135 a = MockGeneralCut::compile(
" [12 <= 12 < 13]");
136 EXPECT_TRUE(a->check(&testObject));
137 a = MockGeneralCut::compile(
" [ 17<= 16.7 < 18 ]");
138 EXPECT_FALSE(a->check(&testObject));
140 a = MockGeneralCut::compile(
" [12 >= 12 < 13]");
141 EXPECT_TRUE(a->check(&testObject));
142 a = MockGeneralCut::compile(
" [ 15> 16.7 <= 18 ]");
143 EXPECT_FALSE(a->check(&testObject));
145 a = MockGeneralCut::compile(
"mocking_variable > 1.0");
146 EXPECT_TRUE(a->check(&testObject));
147 a = MockGeneralCut::compile(
"1.0 < mocking_variable <= mocking_variable");
148 EXPECT_TRUE(a->check(&testObject));
150 a = MockGeneralCut::compile(
"mocking_variable < 100.0");
151 EXPECT_TRUE(a->check(&testObject));
152 a = MockGeneralCut::compile(
"mocking_variable <= mocking_variable <= mocking_variable");
153 EXPECT_TRUE(a->check(&testObject));
155 a = MockGeneralCut::compile(
"1 < 2 and 3 < 4");
156 EXPECT_TRUE(a->check(&testObject));
157 a = MockGeneralCut::compile(
"1 < 2 and 4 < 3");
158 EXPECT_FALSE(a->check(&testObject));
159 a = MockGeneralCut::compile(
"2 < 1 and 4 < 3");
160 EXPECT_FALSE(a->check(&testObject));
161 a = MockGeneralCut::compile(
"2 < 1 and 3 < 4");
162 EXPECT_FALSE(a->check(&testObject));
164 a = MockGeneralCut::compile(
"1 < 2 or 3 < 4");
165 EXPECT_TRUE(a->check(&testObject));
166 a = MockGeneralCut::compile(
"1 < 2 or 4 < 3");
167 EXPECT_TRUE(a->check(&testObject));
168 a = MockGeneralCut::compile(
"2 < 1 or 4 < 3");
169 EXPECT_FALSE(a->check(&testObject));
170 a = MockGeneralCut::compile(
"2 < 1 or 3 < 4");
171 EXPECT_TRUE(a->check(&testObject));
173 a = MockGeneralCut::compile(
"1 < 2 and 3 < 4 and [ 5 < 6 or 7 > 6 ]");
174 EXPECT_TRUE(a->check(&testObject));
175 a = MockGeneralCut::compile(
"[1 < 2 < 3 or 3 > 4 ] and [ 5 < 6 or 7 > 6 ]");
176 EXPECT_TRUE(a->check(&testObject));
177 a = MockGeneralCut::compile(
"[1 < 2 < 3 or 3 > 4 ] or [ 5 < 6 and 7 > 6 ]");
178 EXPECT_TRUE(a->check(&testObject));
180 a = MockGeneralCut::compile(
"1 < 2 and 3 < 4 or 5 > 6");
181 EXPECT_TRUE(a->check(&testObject));
182 a = MockGeneralCut::compile(
"1 < 2 or 3 < 4 and 5 > 6");
183 EXPECT_TRUE(a->check(&testObject));
184 a = MockGeneralCut::compile(
"1 < 2 and 4 < 3 or 6 > 5");
185 EXPECT_TRUE(a->check(&testObject));
186 a = MockGeneralCut::compile(
"1 < 2 or 4 < 3 and 6 > 5");
187 EXPECT_TRUE(a->check(&testObject));
189 a = MockGeneralCut::compile(
"1 != 2 and 3 < 4 or 5 > 6");
190 EXPECT_TRUE(a->check(&testObject));
191 a = MockGeneralCut::compile(
"1 < 2 or 3 != 4 and 5 > 6");
192 EXPECT_TRUE(a->check(&testObject));
194 a = MockGeneralCut::compile(
"1 != 2 and 3 == 4 or 5 > 6");
195 EXPECT_FALSE(a->check(&testObject));
196 a = MockGeneralCut::compile(
"1 < 2 or 3 == 4 and 5 > 6");
197 EXPECT_TRUE(a->check(&testObject));
199 a = MockGeneralCut::compile(
"15 != 0x15");
200 EXPECT_TRUE(a->check(&testObject));
202 a = MockGeneralCut::compile(
"15 == 0xF");
203 EXPECT_TRUE(a->check(&testObject));
206 EXPECT_B2FATAL(a = MockGeneralCut::compile(
"15 == 15.0 bla"));
208 EXPECT_THROW(a = MockGeneralCut::compile(
"15 == other_var"), std::runtime_error);
210 a = MockGeneralCut::compile(
"infinity == 15e1000");
211 EXPECT_TRUE(a->check(&testObject));
213 a = MockGeneralCut::compile(
"1e-3 < 1e3");
214 EXPECT_TRUE(a->check(&testObject));
216 a = MockGeneralCut::compile(
"1e-3 == 0.001");
217 EXPECT_TRUE(a->check(&testObject));
219 a = MockGeneralCut::compile(
"1000 < infinity");
220 EXPECT_TRUE(a->check(&testObject));
222 a = MockGeneralCut::compile(
"1000 > infinity");
223 EXPECT_FALSE(a->check(&testObject));
225 a = MockGeneralCut::compile(
"1000 < nan");
226 EXPECT_FALSE(a->check(&testObject));
228 a = MockGeneralCut::compile(
"1000 > nan");
229 EXPECT_FALSE(a->check(&testObject));
232 a = MockGeneralCut::compile(
"3.141592653589793 == 3.141592653589792");
233 EXPECT_FALSE(a->check(&testObject));
235 a = MockGeneralCut::compile(
"3.1415926535897931234567890 == 3.1415926535897931234567891");
236 EXPECT_TRUE(a->check(&testObject));
238 a = MockGeneralCut::compile(
"3141592653589 != 3141592653588");
239 EXPECT_TRUE(a->check(&testObject));
241 a = MockGeneralCut::compile(
"");
242 EXPECT_TRUE(a->check(&testObject));
244 a = MockGeneralCut::compile(
" ");
245 EXPECT_TRUE(a->check(&testObject));
247 EXPECT_B2FATAL(a = MockGeneralCut::compile(
"[ ]"));
249 EXPECT_B2FATAL(a = MockGeneralCut::compile(
"[ ] and []"));
253 TEST(GeneralCutTest, compileAndDecompile)
256 std::unique_ptr<MockGeneralCut> a = MockGeneralCut::compile(
"1 < 2");
257 EXPECT_EQ(a->decompile(),
"1 < 2");
259 a = MockGeneralCut::compile(
"[1 < 2]");
260 EXPECT_EQ(a->decompile(),
"[1 < 2]");
262 a = MockGeneralCut::compile(
"1 < 2 < 3");
263 EXPECT_EQ(a->decompile(),
"1 < 2 < 3");
265 a = MockGeneralCut::compile(
"( 1 + 3 ) * 2");
266 EXPECT_EQ(a->decompile(),
"( 1 + 3 ) * 2");
268 a = MockGeneralCut::compile(
"[1 < 2 < 3] or [[ 2 < 4] and [ mocking_variable < 4.4231 and [1 < 3 and 4 < mocking_variable]]]");
269 EXPECT_EQ(a->decompile(),
270 "[1 < 2 < 3] or [[2 < 4] and [mocking_variable < 4.4231 and [1 < 3 and 4 < mocking_variable]]]");
This class implements a common way to implement cut/selection functionality for arbitrary objects.
Abstract base class for different kinds of events.