|
| 1 | +#include "../src/combinator.hpp" |
| 2 | +#include <iostream> |
| 3 | +#include <metricq/json.hpp> |
| 4 | + |
| 5 | +int main() |
| 6 | +{ |
| 7 | + try |
| 8 | + { |
| 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 | + }; |
| 35 | + |
| 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)" }; |
| 39 | + |
| 40 | + // Test the cases |
| 41 | + const auto& testCases = data["testCases"]; |
| 42 | + for (size_t i = 0; i < testCases.size(); ++i) |
| 43 | + { |
| 44 | + const auto& testCase = testCases[i]; |
| 45 | + const auto& expression = testCase["expression"]; |
| 46 | + |
| 47 | + // Call the function to display the expression |
| 48 | + std::string result = Combinator::displayExpression(expression); |
| 49 | + |
| 50 | + // Compare with expected result |
| 51 | + if (result != expected[i]) |
| 52 | + { |
| 53 | + std::cerr << "Test case " << i + 1 << " failed:\n"; |
| 54 | + std::cerr << "Expected: " << expected[i] << "\n"; |
| 55 | + std::cerr << "Got: " << result << "\n"; |
| 56 | + return 1; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + std::cout << "All test cases passed successfully!" << std::endl; |
| 61 | + } |
| 62 | + catch (const std::exception& e) |
| 63 | + { |
| 64 | + std::cerr << "Error: " << e.what() << std::endl; |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
0 commit comments