-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestControlFileParser.cc
More file actions
241 lines (195 loc) · 8.41 KB
/
Copy pathTestControlFileParser.cc
File metadata and controls
241 lines (195 loc) · 8.41 KB
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
#include "MHO_ControlFileParser.hh"
#include "MHO_Message.hh"
#include "MHO_TestAssertions.hh"
using namespace hops;
static int temp_file_counter = 0;
static std::string make_temp_file(const std::string& content)
{
std::string path = "/tmp/test_cf_" + std::to_string(::getpid()) + "_" + std::to_string(temp_file_counter++) + ".ctrl";
std::ofstream(path) << content;
return path;
}
int main(int /*argc*/, char** /*argv*/)
{
MHO_Message::GetInstance().AcceptAllKeys();
MHO_Message::GetInstance().SetMessageLevel(eDebug); //eFatal);
// CF1 fixture
const std::string CF1 = "* full line comment, should be removed entirely\n"
"if station G\n"
"pc_delay_l 1.5 * trailing comment trimmed here\n"
"sb_win -10.0 10.0\n"
"if station X\n"
"sb_win -5.0 5.0\n";
std::string path1 = make_temp_file(CF1);
REQUIRE(!path1.empty());
// CASE 1: Top-level shape / implicit leading "if true" block
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
mho_json J = parser.ParseControl();
REQUIRE(J.contains("conditions") || J.find("conditions") != J.end());
REQUIRE(J["conditions"].is_array());
REQUIRE(J["conditions"].size() == 3); // implicit-true + 2 "if" blocks
REQUIRE(J["conditions"][0]["statement_type"] == "conditional");
REQUIRE(J["conditions"][0]["value"] == mho_json({"true"}));
REQUIRE(J["conditions"][0]["line_number"] == 0);
}
// CASE 2: Statements grouped under correct conditional block
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
mho_json J = parser.ParseControl();
// Block 0: no statements (everything is under "if")
REQUIRE(J["conditions"][0]["statements"].size() == 0);
// Block 1: "if station G" has 2 statements
REQUIRE(J["conditions"][1]["value"] == mho_json({"station", "G"}));
REQUIRE(J["conditions"][1]["statements"].size() == 2);
// Block 2: "if station X" has 1 statement
REQUIRE(J["conditions"][2]["value"] == mho_json({"station", "X"}));
REQUIRE(J["conditions"][2]["statements"].size() == 1);
}
// CASE 3: Element value typing (real, list_real)
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
mho_json J = parser.ParseControl();
auto& stmts = J["conditions"][1]["statements"];
// pc_delay_l
REQUIRE(stmts[0]["name"] == "pc_delay_l");
REQUIRE(stmts[0]["statement_type"] == "operator");
REQUIRE(stmts[0]["value"].is_number());
CHECK_CLOSE(stmts[0]["value"].get< double >(), 1.5, 1e-12);
// sb_win
REQUIRE(stmts[1]["name"] == "sb_win");
REQUIRE(stmts[1]["value"].is_array());
REQUIRE(stmts[1]["value"].size() == 2);
CHECK_CLOSE(stmts[1]["value"][0].get< double >(), -10.0, 1e-12);
CHECK_CLOSE(stmts[1]["value"][1].get< double >(), 10.0, 1e-12);
}
// CASE 4: RemoveComments: full-line and trailing
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
parser.ParseControl();
std::string s = parser.GetProcessedControlFileText();
REQUIRE(s.find("full line comment") == std::string::npos);
REQUIRE(s.find("trailing comment") == std::string::npos);
REQUIRE(s.find("pc_delay_l 1.5") != std::string::npos);
}
// CASE 5: test FixSymbols padding (parentheses)
{
std::string CF2 = "if (station G or station X)\npc_delay_l 1.0\n";
std::string path2 = make_temp_file(CF2);
REQUIRE(!path2.empty());
MHO_ControlFileParser parser;
parser.SetControlFile(path2);
mho_json J = parser.ParseControl();
// Check the value array
// Regression test for the FixSymbols fix: parentheses are now padded with
// spaces (the replace pattern was previously the regex-escaped "\\(" / "\\)",
// which never matched under the literal-string replacement fallback), so
// they tokenize as standalone "(" and ")".
auto& block1 = J["conditions"][1]["value"];
REQUIRE(block1 == mho_json({"(", "station", "G", "or", "station", "X", ")"}));
::remove(path2.c_str());
}
// CASE 6: Legacy processed text excludes set-string lines
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
parser.PassSetString("pc_delay_r 2.0");
parser.ParseControl();
std::string full = parser.GetProcessedControlFileText();
std::string legacy = parser.GetLegacyProcessedControlFileText();
REQUIRE(full.find("pc_delay_r 2.0") != std::string::npos);
REQUIRE(legacy.find("pc_delay_r 2.0") == std::string::npos);
REQUIRE(legacy.find("pc_delay_l 1.5") != std::string::npos);
}
// CASE 7: Set-string injection adds an applicable block
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
parser.PassSetString("pc_delay_r 2.0");
mho_json J = parser.ParseControl();
REQUIRE(J["conditions"].size() == 4);
auto& lastBlock = J["conditions"][3];
REQUIRE(lastBlock["value"] == mho_json({"true"}));
REQUIRE(lastBlock["statements"].size() >= 1);
auto& stmt = lastBlock["statements"][0];
REQUIRE(stmt["name"] == "pc_delay_r");
CHECK_CLOSE(stmt["value"].get< double >(), 2.0, 1e-12);
}
// CASE 8: Empty / missing file behavior
{
MHO_ControlFileParser parser;
parser.SetControlFile("");
mho_json J = parser.ParseControl();
// Empty filename returns null/empty
REQUIRE(J.is_null() || J.empty());
}
// CASE 9: check parser object reuse / idempotency
{
MHO_ControlFileParser parser;
parser.SetControlFile(path1);
mho_json J1 = parser.ParseControl();
mho_json J2 = parser.ParseControl();
// Regression test for the ReadFile fLines.clear() fix: re-parsing the same
// file on the same object now yields an identical result (previously the
// second parse appended a second copy of the lines and doubled them).
REQUIRE(J2 == J1);
}
// CASE 10: statements before any "if" land in the implicit-true block 0
{
std::string CF3 = "pc_delay_l 1.0\nif station G\nsb_win -1.0 1.0\n";
std::string path3 = make_temp_file(CF3);
REQUIRE(!path3.empty());
MHO_ControlFileParser parser;
parser.SetControlFile(path3);
mho_json J = parser.ParseControl();
REQUIRE(J["conditions"].size() == 2); // implicit-true + 1 "if"
REQUIRE(J["conditions"][0]["value"] == mho_json({"true"}));
REQUIRE(J["conditions"][0]["statements"].size() == 1);
REQUIRE(J["conditions"][0]["statements"][0]["name"] == "pc_delay_l");
REQUIRE(J["conditions"][1]["value"] == mho_json({"station", "G"}));
REQUIRE(J["conditions"][1]["statements"].size() == 1);
::remove(path3.c_str());
}
// CASE 11: FixSymbols pads "<" / ">" so they tokenize standalone
{
std::string CF4 = "if scan < 288-210300\npc_delay_l 1.0\n";
std::string path4 = make_temp_file(CF4);
REQUIRE(!path4.empty());
MHO_ControlFileParser parser;
parser.SetControlFile(path4);
mho_json J = parser.ParseControl();
REQUIRE(J["conditions"][1]["value"] == mho_json({"scan", "<", "288-210300"}));
::remove(path4.c_str());
}
// CASE 12: comments-only file yields just the implicit-true block, no keywords
{
std::string CF5 = "* just a comment\n* another comment\n";
std::string path5 = make_temp_file(CF5);
REQUIRE(!path5.empty());
MHO_ControlFileParser parser;
parser.SetControlFile(path5);
mho_json J = parser.ParseControl();
REQUIRE(J["conditions"].size() == 1);
REQUIRE(J["conditions"][0]["value"] == mho_json({"true"}));
::remove(path5.c_str());
}
// CASE 13: a non-empty but unopenable filename throws (formerly std::exit(1))
{
MHO_ControlFileParser parser;
parser.SetControlFile("/no/such/dir/definitely_missing_control_file.ctrl");
REQUIRE_THROWS(parser.ParseControl());
}
// Cleanup
::remove(path1.c_str());
return 0;
}