Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-20 08:20:18

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/Utilities/AxisDefinitions.hpp"
0012 #include "Acts/Utilities/AxisSpec.hpp"
0013 #include "Acts/Utilities/MultiAxisSpec.hpp"
0014 #include "ActsPlugins/Json/ActsJson.hpp"
0015 #include "ActsPlugins/Json/AxisSpecJsonConverter.hpp"
0016 
0017 #include <stdexcept>
0018 
0019 using namespace Acts;
0020 
0021 namespace ActsTests {
0022 
0023 BOOST_AUTO_TEST_SUITE(JsonSuite)
0024 
0025 BOOST_AUTO_TEST_CASE(AxisSpecJsonRoundTrip) {
0026   using enum AxisBoundaryType;
0027   using enum AxisDirection;
0028 
0029   std::vector<AxisSpec> specs = {
0030       AxisSpec::Equidistant(5, 0., 10., Bound),
0031       AxisSpec::Equidistant(12, -3., 3., Closed, AxisPhi),
0032       AxisSpec::Variable({0., 1., 4., 10.}, Open),
0033       AxisSpec::Variable({-1., 0., 2.}, Bound, AxisZ),
0034       AxisSpec::DeferredEquidistant(20),
0035       AxisSpec::DeferredEquidistant(8, AxisRPhi),
0036       AxisSpec::DeferredVariable({0., 0.1, 0.5, 1.}),
0037       AxisSpec::DeferredVariable({0., 0.25, 1.}, std::nullopt, AxisR),
0038       // Partially specified: a range without a boundary type and the other
0039       // way round
0040       AxisSpec::Equidistant(6, 0., 1.),
0041       AxisSpec::Equidistant(6, std::nullopt, std::nullopt, Bound),
0042       AxisSpec::Variable({0., 1., 2.}),
0043       AxisSpec::DeferredVariable({0., 0.5, 1.}, Closed)};
0044 
0045   for (const AxisSpec& axisSpec : specs) {
0046     nlohmann::json j = AxisSpecJsonConverter::toJson(axisSpec);
0047     AxisSpec read = AxisSpecJsonConverter::fromJson(j);
0048     BOOST_CHECK(read == axisSpec);
0049     // The direction key is only written when a direction is set
0050     BOOST_CHECK_EQUAL(j.contains("direction"),
0051                       axisSpec.direction().has_value());
0052   }
0053 
0054   // Unknown type tag is rejected
0055   nlohmann::json jInvalid = {{"type", "unknown"}};
0056   BOOST_CHECK_THROW(AxisSpecJsonConverter::fromJson(jInvalid),
0057                     std::invalid_argument);
0058 }
0059 
0060 BOOST_AUTO_TEST_CASE(MultiAxisSpecJsonRoundTrip) {
0061   using enum AxisBoundaryType;
0062   using enum AxisDirection;
0063 
0064   MultiAxisSpec oneD({AxisSpec::DeferredEquidistant(8, AxisZ)});
0065   nlohmann::json j1 = MultiAxisSpecJsonConverter::toJson(oneD);
0066   BOOST_CHECK(MultiAxisSpecJsonConverter::fromJson(j1) == oneD);
0067 
0068   MultiAxisSpec twoD(
0069       {AxisSpec::DeferredEquidistant(4, AxisRPhi),
0070        AxisSpec::DeferredVariable({0., 0.5, 1.}, std::nullopt, AxisZ)});
0071   nlohmann::json j2 = MultiAxisSpecJsonConverter::toJson(twoD);
0072   BOOST_CHECK_EQUAL(j2.size(), 2u);
0073   BOOST_CHECK(MultiAxisSpecJsonConverter::fromJson(j2) == twoD);
0074 
0075   // An empty axis list is rejected
0076   BOOST_CHECK_THROW(
0077       MultiAxisSpecJsonConverter::fromJson(nlohmann::json::array()),
0078       std::invalid_argument);
0079 }
0080 
0081 BOOST_AUTO_TEST_SUITE_END()
0082 
0083 }  // namespace ActsTests