Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 09:15:46

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Surfaces/CylinderBounds.hpp"
0013 #include "Acts/Surfaces/RectangleBounds.hpp"
0014 #include "ActsPlugins/Json/SurfaceBoundsJsonConverter.hpp"
0015 
0016 #include <algorithm>
0017 #include <fstream>
0018 #include <memory>
0019 #include <numbers>
0020 #include <stdexcept>
0021 #include <string>
0022 #include <vector>
0023 
0024 #include <nlohmann/json.hpp>
0025 
0026 using namespace Acts;
0027 
0028 namespace ActsTests {
0029 
0030 BOOST_AUTO_TEST_SUITE(JsonSuite)
0031 
0032 BOOST_AUTO_TEST_CASE(SurfaceBoundsRoundTripTests) {
0033   std::ofstream out;
0034 
0035   // As all SurfaceBounds have the same streaming API only a one is
0036   // tested here, all others are tests are identical
0037 
0038   auto rectangeRef = std::make_shared<const RectangleBounds>(4., 6.);
0039   nlohmann::json rectangleOut =
0040       SurfaceBoundsJsonConverter::toJson(*rectangeRef);
0041   out.open("RectangleBounds.json");
0042   out << rectangleOut.dump(2);
0043   out.close();
0044 
0045   auto in = std::ifstream("RectangleBounds.json",
0046                           std::ifstream::in | std::ifstream::binary);
0047   BOOST_CHECK(in.good());
0048   nlohmann::json rectangleIn;
0049   in >> rectangleIn;
0050   in.close();
0051 
0052   auto rectangleTest =
0053       SurfaceBoundsJsonConverter::fromJson<RectangleBounds>(rectangleIn);
0054 
0055   BOOST_CHECK(rectangeRef->values() == rectangleTest->values());
0056 }
0057 
0058 BOOST_AUTO_TEST_CASE(SurfaceBoundsValueCountTests) {
0059   nlohmann::json jShort;
0060   jShort["type"] = "CylinderBounds";
0061   jShort["values"] = std::vector<double>{30., 200., std::numbers::pi};
0062   BOOST_CHECK_THROW(
0063       SurfaceBoundsJsonConverter::fromJson<CylinderBounds>(jShort),
0064       std::invalid_argument);
0065 
0066   // CylinderBounds used to carry two bevel angles after the four values.
0067   nlohmann::json jLegacy;
0068   jLegacy["type"] = "CylinderBounds";
0069   jLegacy["values"] =
0070       std::vector<double>{30., 200., std::numbers::pi, 0., 0., 0.};
0071   auto legacy = SurfaceBoundsJsonConverter::fromJson<CylinderBounds>(jLegacy);
0072   BOOST_CHECK_EQUAL(legacy->values().size(), 4u);
0073   BOOST_CHECK_EQUAL(legacy->get(CylinderBounds::eR), 30.);
0074 
0075   nlohmann::json jBeveled;
0076   jBeveled["type"] = "CylinderBounds";
0077   jBeveled["values"] =
0078       std::vector<double>{30., 200., std::numbers::pi, 0., 0.1, 0.};
0079   BOOST_CHECK_THROW(
0080       SurfaceBoundsJsonConverter::fromJson<CylinderBounds>(jBeveled),
0081       std::invalid_argument);
0082 
0083   nlohmann::json jLong;
0084   jLong["type"] = "RectangleBounds";
0085   jLong["values"] = std::vector<double>{-4., -6., 4., 6., 0.};
0086   BOOST_CHECK_THROW(
0087       SurfaceBoundsJsonConverter::fromJson<RectangleBounds>(jLong),
0088       std::invalid_argument);
0089 }
0090 
0091 BOOST_AUTO_TEST_SUITE_END()
0092 
0093 }  // namespace ActsTests