-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTestLiteralEvalCpp.cpp
More file actions
307 lines (260 loc) · 10.6 KB
/
TestLiteralEvalCpp.cpp
File metadata and controls
307 lines (260 loc) · 10.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* TestLiteralEvalCpp.cpp
*
* Test suite for the C++ Python-literal-compatible parser in concore_base.hpp.
* Validates Issue #389 fix: C++ parser must accept all valid concore payloads
* that Python's ast.literal_eval() accepts.
*
* Compile: g++ -std=c++11 -o TestLiteralEvalCpp TestLiteralEvalCpp.cpp
* Run: ./TestLiteralEvalCpp (Linux/macOS)
* TestLiteralEvalCpp.exe (Windows)
*/
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include "concore_base.hpp"
using namespace concore_base;
static int passed = 0;
static int failed = 0;
// ------------- helpers -------------------------------------------------
static void check(const std::string& testName, bool condition) {
if (condition) {
std::cout << "PASS: " << testName << std::endl;
++passed;
} else {
std::cout << "FAIL: " << testName << std::endl;
++failed;
}
}
static bool approx(double a, double b, double eps = 1e-9) {
return std::fabs(a - b) < eps;
}
// ------------- backward-compatibility tests ----------------------------
static void test_flat_numeric_list() {
std::vector<double> v = parselist_double("[10.0, 0.5, 2.3]");
check("flat_numeric size==3", v.size() == 3);
check("flat_numeric[0]==10.0", approx(v[0], 10.0));
check("flat_numeric[1]==0.5", approx(v[1], 0.5));
check("flat_numeric[2]==2.3", approx(v[2], 2.3));
}
static void test_empty_list() {
std::vector<double> v = parselist_double("[]");
check("empty_list size==0", v.size() == 0);
}
static void test_single_element() {
std::vector<double> v = parselist_double("[42.0]");
check("single_element size==1", v.size() == 1);
check("single_element[0]==42", approx(v[0], 42.0));
}
static void test_negative_numbers() {
std::vector<double> v = parselist_double("[-1.5, -3.0, 2.0]");
check("negative size==3", v.size() == 3);
check("negative[0]==-1.5", approx(v[0], -1.5));
check("negative[1]==-3.0", approx(v[1], -3.0));
}
static void test_scientific_notation() {
std::vector<double> v = parselist_double("[1e3, 2.5E-2, -1.0e+1]");
check("sci size==3", v.size() == 3);
check("sci[0]==1000", approx(v[0], 1000.0));
check("sci[1]==0.025", approx(v[1], 0.025));
check("sci[2]==-10", approx(v[2], -10.0));
}
static void test_integer_values() {
std::vector<double> v = parselist_double("[1, 2, 3]");
check("int size==3", v.size() == 3);
check("int[0]==1", approx(v[0], 1.0));
check("int[2]==3", approx(v[2], 3.0));
}
// ------------- mixed-type payload tests (Issue #389 core) --------------
static void test_string_element() {
// [10.0, "start", 0.5] – string should be skipped in numeric flatten
std::vector<double> v = parselist_double("[10.0, \"start\", 0.5]");
check("string_elem size==2", v.size() == 2);
check("string_elem[0]==10.0", approx(v[0], 10.0));
check("string_elem[1]==0.5", approx(v[1], 0.5));
}
static void test_boolean_element() {
// [10.0, True, 0.5]
std::vector<double> v = parselist_double("[10.0, True, 0.5]");
check("bool_elem size==3", v.size() == 3);
check("bool_elem[0]==10.0", approx(v[0], 10.0));
check("bool_elem[1]==1.0 (True)", approx(v[1], 1.0));
check("bool_elem[2]==0.5", approx(v[2], 0.5));
}
static void test_bool_false() {
std::vector<double> v = parselist_double("[False, 5.0]");
check("bool_false size==2", v.size() == 2);
check("bool_false[0]==0.0", approx(v[0], 0.0));
}
static void test_nested_list() {
// [10.0, [0.5, 0.3], 0.1] – nested list flattened to [10.0, 0.5, 0.3, 0.1]
std::vector<double> v = parselist_double("[10.0, [0.5, 0.3], 0.1]");
check("nested size==4", v.size() == 4);
check("nested[0]==10.0", approx(v[0], 10.0));
check("nested[1]==0.5", approx(v[1], 0.5));
check("nested[2]==0.3", approx(v[2], 0.3));
check("nested[3]==0.1", approx(v[3], 0.1));
}
static void test_tuple_payload() {
// (10.0, 0.3) – tuple treated as array
std::vector<double> v = parselist_double("(10.0, 0.3)");
check("tuple size==2", v.size() == 2);
check("tuple[0]==10.0", approx(v[0], 10.0));
check("tuple[1]==0.3", approx(v[1], 0.3));
}
static void test_nested_tuple() {
// [10.0, (0.5, 0.3)]
std::vector<double> v = parselist_double("[10.0, (0.5, 0.3)]");
check("nested_tuple size==3", v.size() == 3);
check("nested_tuple[0]==10.0", approx(v[0], 10.0));
check("nested_tuple[1]==0.5", approx(v[1], 0.5));
check("nested_tuple[2]==0.3", approx(v[2], 0.3));
}
static void test_mixed_types() {
// [10.0, "label", True, [1, 2], (3,), False, "end"]
std::vector<double> v = parselist_double("[10.0, \"label\", True, [1, 2], (3,), False, \"end\"]");
// numeric values: 10.0, 1.0(True), 1, 2, 3, 0.0(False) = 6 values
check("mixed size==6", v.size() == 6);
check("mixed[0]==10.0", approx(v[0], 10.0));
check("mixed[1]==1.0", approx(v[1], 1.0)); // True
check("mixed[2]==1.0", approx(v[2], 1.0)); // nested [1,...]
check("mixed[3]==2.0", approx(v[3], 2.0)); // nested [...,2]
check("mixed[4]==3.0", approx(v[4], 3.0)); // tuple (3,)
check("mixed[5]==0.0", approx(v[5], 0.0)); // False
}
// ------------- full ConcoreValue parse tests ---------------------------
static void test_parse_literal_string() {
ConcoreValue v = parse_literal("[10.0, \"start\", 0.5]");
check("literal_string is ARRAY", v.type == ConcoreValueType::ARRAY);
check("literal_string len==3", v.array.size() == 3);
check("literal_string[0] NUMBER", v.array[0].type == ConcoreValueType::NUMBER);
check("literal_string[1] STRING", v.array[1].type == ConcoreValueType::STRING);
check("literal_string[1]==\"start\"", v.array[1].str == "start");
check("literal_string[2] NUMBER", v.array[2].type == ConcoreValueType::NUMBER);
}
static void test_parse_literal_bool() {
ConcoreValue v = parse_literal("[True, False]");
check("literal_bool is ARRAY", v.type == ConcoreValueType::ARRAY);
check("literal_bool[0] BOOL", v.array[0].type == ConcoreValueType::BOOL);
check("literal_bool[0]==true", v.array[0].boolean == true);
check("literal_bool[1]==false", v.array[1].boolean == false);
}
static void test_parse_literal_nested() {
ConcoreValue v = parse_literal("[1, [2, [3]]]");
check("literal_nested outer ARRAY", v.type == ConcoreValueType::ARRAY);
check("literal_nested[1] ARRAY", v.array[1].type == ConcoreValueType::ARRAY);
check("literal_nested[1][1] ARRAY", v.array[1].array[1].type == ConcoreValueType::ARRAY);
check("literal_nested[1][1][0]==3", approx(v.array[1].array[1].array[0].number, 3.0));
}
static void test_parse_single_quoted_string() {
ConcoreValue v = parse_literal("['hello']");
check("single_quote ARRAY", v.type == ConcoreValueType::ARRAY);
check("single_quote[0] STRING", v.array[0].type == ConcoreValueType::STRING);
check("single_quote[0]=='hello'", v.array[0].str == "hello");
}
static void test_parse_escape_sequences() {
ConcoreValue v = parse_literal("[\"line\\none\"]");
check("escape STRING", v.array[0].type == ConcoreValueType::STRING);
check("escape has newline", v.array[0].str == "line\none");
}
static void test_parse_none() {
ConcoreValue v = parse_literal("[None, 1]");
check("none[0] STRING", v.array[0].type == ConcoreValueType::STRING);
check("none[0]==\"None\"", v.array[0].str == "None");
}
static void test_trailing_comma() {
// Python allows trailing comma: [1, 2,]
std::vector<double> v = parselist_double("[1, 2,]");
check("trailing_comma size==2", v.size() == 2);
check("trailing_comma[1]==2", approx(v[1], 2.0));
}
// ------------- error / failure case tests ------------------------------
static void test_malformed_bracket() {
bool caught = false;
try {
parse_literal("[1, 2");
} catch (const std::runtime_error&) {
caught = true;
}
check("malformed_bracket throws", caught);
}
static void test_malformed_string() {
bool caught = false;
try {
parse_literal("[\"unterminated]");
} catch (const std::runtime_error&) {
caught = true;
}
check("malformed_string throws", caught);
}
static void test_unsupported_object() {
bool caught = false;
try {
parse_literal("{1: 2}");
} catch (const std::runtime_error&) {
caught = true;
}
check("unsupported_object throws", caught);
}
static void test_empty_string_input() {
std::vector<double> v = parselist_double("");
check("empty_input size==0", v.size() == 0);
}
// ------------- cross-language round-trip tests -------------------------
static void test_python_write_cpp_read_flat() {
// Simulate Python write: "[5.0, 1.0, 2.0]"
std::vector<double> v = parselist_double("[5.0, 1.0, 2.0]");
check("py2cpp_flat size==3", v.size() == 3);
check("py2cpp_flat[0]==5.0", approx(v[0], 5.0));
}
static void test_python_write_cpp_read_mixed() {
// Simulate Python write: "[5.0, 'sensor_a', True, [0.1, 0.2]]"
std::vector<double> v = parselist_double("[5.0, 'sensor_a', True, [0.1, 0.2]]");
// numeric: 5.0, 1.0(True), 0.1, 0.2 = 4
check("py2cpp_mixed size==4", v.size() == 4);
check("py2cpp_mixed[0]==5.0", approx(v[0], 5.0));
check("py2cpp_mixed[1]==1.0", approx(v[1], 1.0));
check("py2cpp_mixed[2]==0.1", approx(v[2], 0.1));
check("py2cpp_mixed[3]==0.2", approx(v[3], 0.2));
}
// ------------- main ----------------------------------------------------
int main() {
std::cout << "===== C++ Literal Parser Tests (Issue #389) =====\n\n";
// Backward compatibility
test_flat_numeric_list();
test_empty_list();
test_single_element();
test_negative_numbers();
test_scientific_notation();
test_integer_values();
// Mixed-type payloads (core of Issue #389)
test_string_element();
test_boolean_element();
test_bool_false();
test_nested_list();
test_tuple_payload();
test_nested_tuple();
test_mixed_types();
// Full ConcoreValue structure tests
test_parse_literal_string();
test_parse_literal_bool();
test_parse_literal_nested();
test_parse_single_quoted_string();
test_parse_escape_sequences();
test_parse_none();
test_trailing_comma();
// Error / failure cases
test_malformed_bracket();
test_malformed_string();
test_unsupported_object();
test_empty_string_input();
// Cross-language round-trip
test_python_write_cpp_read_flat();
test_python_write_cpp_read_mixed();
std::cout << "\n=== Results: " << passed << " passed, " << failed
<< " failed out of " << (passed + failed) << " tests ===\n";
return (failed > 0) ? 1 : 0;
}