Skip to content

Commit

Permalink
add(tests): test cases for displayExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
floork committed Dec 12, 2024
1 parent beffc34 commit 2150da0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tests/CMakeLists.txt
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)
69 changes: 69 additions & 0 deletions tests/test_display_expression.cpp
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;
}

0 comments on commit 2150da0

Please sign in to comment.