|
2 | 2 | #include <iostream> |
3 | 3 | #include <metricq/json.hpp> |
4 | 4 |
|
| 5 | +struct TestDisplayExpression |
| 6 | +{ |
| 7 | + nlohmann::json input; |
| 8 | + std::string expected; |
| 9 | +}; |
| 10 | + |
5 | 11 | int main() |
6 | 12 | { |
7 | 13 | try |
8 | 14 | { |
9 | | - // Input test data |
10 | | - nlohmann::json data = { |
11 | | - { "testCases", |
12 | | - { { { "expression", |
13 | | - { { "operation", "*" }, |
14 | | - { "left", 5 }, |
15 | | - { "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } }, |
16 | | - { { "expression", |
17 | | - { { "operation", "*" }, |
18 | | - { "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } }, |
19 | | - { "right", |
20 | | - { { "operation", "-" }, |
21 | | - { "left", 10 }, |
22 | | - { "right", "dummy.source" } } } } } }, |
23 | | - { { "expression", |
24 | | - { { "operation", "-" }, |
25 | | - { "left", |
26 | | - { { "operation", "+" }, |
27 | | - { "left", 15.3 }, |
28 | | - { "right", |
29 | | - { { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } }, |
30 | | - { "right", |
31 | | - { { "operation", "throttle" }, |
32 | | - { "cooldown_period", "42" }, |
33 | | - { "input", 8 } } } } } } } } |
34 | | - }; |
| 15 | + std::vector<TestDisplayExpression> testCases; |
| 16 | + |
| 17 | + testCases.emplace_back(TestDisplayExpression{ |
| 18 | + { { "expression", |
| 19 | + { { "operation", "*" }, |
| 20 | + { "left", 5 }, |
| 21 | + { "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } }, |
| 22 | + "(5 * (45 - 3))" }); |
| 23 | + |
| 24 | + testCases.emplace_back(TestDisplayExpression{ |
| 25 | + { { "expression", |
| 26 | + { { "operation", "*" }, |
| 27 | + { "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } }, |
| 28 | + { "right", |
| 29 | + { { "operation", "-" }, { "left", 10 }, { "right", "dummy.source" } } } } } }, |
| 30 | + "((1 + 2) * (10 - dummy.source))" }); |
35 | 31 |
|
36 | | - // Expected outputs |
37 | | - std::vector<std::string> expected = { "(5 * (45 - 3))", "((1 + 2) * (10 - dummy.source))", |
38 | | - "((15.300000 + min[42, 24, 8, 12]) - 8)" }; |
| 32 | + testCases.emplace_back(TestDisplayExpression{ |
| 33 | + { { "expression", |
| 34 | + { { "operation", "-" }, |
| 35 | + { "left", |
| 36 | + { { "operation", "+" }, |
| 37 | + { "left", 15.3 }, |
| 38 | + { "right", { { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } }, |
| 39 | + { "right", |
| 40 | + { { "operation", "throttle" }, |
| 41 | + { "cooldown_period", "42" }, |
| 42 | + { "input", 8 } } } } } }, |
| 43 | + "((15.300000 + min[42, 24, 8, 12]) - 8)" }); |
39 | 44 |
|
40 | 45 | // Test the cases |
41 | | - const auto& testCases = data["testCases"]; |
42 | | - for (size_t i = 0; i < testCases.size(); ++i) |
| 46 | + for (const auto& testCase : testCases) |
43 | 47 | { |
44 | | - const auto& testCase = testCases[i]; |
45 | | - const auto& expression = testCase["expression"]; |
46 | 48 |
|
47 | | - // Call the function to display the expression |
| 49 | + auto expression = testCase.input["expression"]; |
48 | 50 | std::string result = Combinator::displayExpression(expression); |
49 | 51 |
|
50 | 52 | // Compare with expected result |
51 | | - if (result != expected[i]) |
| 53 | + if (result != testCase.expected) // comparing with the expected output |
52 | 54 | { |
53 | | - std::cerr << "Test case " << i + 1 << " failed:\n"; |
54 | | - std::cerr << "Expected: " << expected[i] << "\n"; |
| 55 | + std::cerr << "Test case " << testCase.input << " failed:\n"; |
| 56 | + std::cerr << "Expected: " << testCase.expected << "\n"; |
55 | 57 | std::cerr << "Got: " << result << "\n"; |
56 | 58 | return 1; |
57 | 59 | } |
|
0 commit comments