OpenMC/tests/cpp_unit_tests/test_region.cpp
2026-01-15 22:23:35 -06:00

101 lines
2.5 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "openmc/cell.h"
#include "openmc/surface.h"
#include <pugixml.hpp>
namespace {
// Helper class to set up and tear down test surfaces
class SurfaceFixture {
public:
SurfaceFixture()
{
pugi::xml_document doc;
pugi::xml_node surf_node = doc.append_child("surface");
surf_node.set_name("surface");
surf_node.append_attribute("id") = "0";
surf_node.append_attribute("type") = "x-plane";
surf_node.append_attribute("coeffs") = "1";
for (int i = 1; i < 10; ++i) {
surf_node.attribute("id") = i;
openmc::model::surfaces.push_back(
std::make_unique<openmc::SurfaceXPlane>(surf_node));
openmc::model::surface_map[i] = i - 1;
}
}
~SurfaceFixture()
{
openmc::model::surfaces.clear();
openmc::model::surface_map.clear();
}
};
} // anonymous namespace
TEST_CASE("Test region simplification")
{
SurfaceFixture fixture;
SECTION("Original bug case from issue #3685")
{
// Input: "-1 2 (-3 4) | (-5 6)" was being incorrectly interpreted
auto region = openmc::Region("(-1 2 (-3 4) | (-5 6))", 0);
REQUIRE(region.str() == " ( ( -1 2 ( -3 4 ) ) | ( -5 6 ) )");
}
SECTION("Simple union - no extra parentheses needed")
{
auto region = openmc::Region("1 | 2", 0);
REQUIRE(region.str() == " 1 | 2");
}
SECTION("Intersection then union")
{
// Intersection should have higher precedence, so (1 2) grouped
auto region = openmc::Region("1 2 | 3", 0);
REQUIRE(region.str() == " ( 1 2 ) | 3");
}
SECTION("Union then intersection")
{
// The (2 3) intersection should be grouped
auto region = openmc::Region("1 | 2 3", 0);
REQUIRE(region.str() == " 1 | ( 2 3 )");
}
SECTION("Nested parentheses preserved")
{
// These parentheses are meaningful and should be preserved
auto region = openmc::Region("(1 | 2) (3 | 4)", 0);
REQUIRE(region.str() == " ( 1 | 2 ) ( 3 | 4 )");
}
SECTION("Deep nesting")
{
auto region = openmc::Region("((1 2) | (3 4)) 5", 0);
REQUIRE(region.str() == " ( ( 1 2 ) | ( 3 4 ) ) 5");
}
SECTION("Multiple unions")
{
auto region = openmc::Region("1 | 2 | 3", 0);
REQUIRE(region.str() == " 1 | 2 | 3");
}
SECTION("Multiple intersections")
{
auto region = openmc::Region("1 2 3", 0);
// Simple cell - no operators in output
REQUIRE(region.str() == " 1 2 3");
}
SECTION("Complex mixed expression")
{
auto region = openmc::Region("1 2 | 3 4 | 5 6", 0);
REQUIRE(region.str() == " ( 1 2 ) | ( 3 4 ) | ( 5 6 )");
}
}