forked from metricq/metricq-combinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(tests): test cases for displayExpression
- Loading branch information
Showing
2 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
add_executable(metricq-combinator.test_single_derivation test_single_derivation.cpp) | ||
add_test(metricq-combinator.test_single_derivation metricq-combinator.test_single_derivation) | ||
macro(add_test_case test_name) | ||
add_executable(${test_name} ${test_name}.cpp) | ||
add_test(${test_name} ${test_name}) | ||
target_link_libraries(${test_name} PRIVATE metricq-combinator-lib) | ||
endmacro() | ||
|
||
target_link_libraries( | ||
metricq-combinator.test_single_derivation | ||
PRIVATE | ||
metricq-combinator-lib | ||
) | ||
add_test_case(test_single_derivation) | ||
add_test_case(test_display_expression) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "../src/combinator.hpp" | ||
#include <iostream> | ||
#include <metricq/json.hpp> | ||
|
||
int main() | ||
{ | ||
try | ||
{ | ||
// Input test data | ||
nlohmann::json data = { | ||
{ "testCases", | ||
{ { { "expression", | ||
{ { "operation", "*" }, | ||
{ "left", 5 }, | ||
{ "right", { { "operation", "-" }, { "left", 45 }, { "right", 3 } } } } } }, | ||
{ { "expression", | ||
{ { "operation", "*" }, | ||
{ "left", { { "operation", "+" }, { "left", 1 }, { "right", 2 } } }, | ||
{ "right", | ||
{ { "operation", "-" }, | ||
{ "left", 10 }, | ||
{ "right", "dummy.source" } } } } } }, | ||
{ { "expression", | ||
{ { "operation", "-" }, | ||
{ "left", | ||
{ { "operation", "+" }, | ||
{ "left", 15.3 }, | ||
{ "right", | ||
{ { "operation", "min" }, { "inputs", { 42, 24, 8, 12 } } } } } }, | ||
{ "right", | ||
{ { "operation", "throttle" }, | ||
{ "cooldown_period", "42" }, | ||
{ "input", 8 } } } } } } } } | ||
}; | ||
|
||
// Expected outputs | ||
std::vector<std::string> expected = { "(5 * (45 - 3))", "((1 + 2) * (10 - dummy.source))", | ||
"((15.300000 + min[42, 24, 8, 12]) - 8)" }; | ||
|
||
// Test the cases | ||
const auto& testCases = data["testCases"]; | ||
for (size_t i = 0; i < testCases.size(); ++i) | ||
{ | ||
const auto& testCase = testCases[i]; | ||
const auto& expression = testCase["expression"]; | ||
|
||
// Call the function to display the expression | ||
std::string result = Combinator::displayExpression(expression); | ||
|
||
// Compare with expected result | ||
if (result != expected[i]) | ||
{ | ||
std::cerr << "Test case " << i + 1 << " failed:\n"; | ||
std::cerr << "Expected: " << expected[i] << "\n"; | ||
std::cerr << "Got: " << result << "\n"; | ||
return 1; | ||
} | ||
} | ||
|
||
std::cout << "All test cases passed successfully!" << std::endl; | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |