Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:25:35

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/data/test_case.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Utilities/Axis.hpp"
0013 #include "Acts/Utilities/AxisDefinitions.hpp"
0014 #include "Acts/Utilities/ProtoAxis.hpp"
0015 #include "ActsPlugins/Json/ActsJson.hpp"
0016 #include "ActsPlugins/Json/ProtoAxisJsonConverter.hpp"
0017 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0018 
0019 using namespace Acts;
0020 
0021 namespace ActsTests {
0022 
0023 BOOST_AUTO_TEST_SUITE(JsonSuite)
0024 
0025 BOOST_AUTO_TEST_CASE(EquidistantProtoAxisJsonConversion) {
0026   using enum AxisBoundaryType;
0027   using enum AxisType;
0028 
0029   // Bound, equidistant axis
0030   ProtoAxis epab(Bound, 0.0, 1.0, 10);
0031 
0032   nlohmann::json jProtoAxis = ProtoAxisJsonConverter::toJson(epab);
0033 
0034   BOOST_CHECK(jProtoAxis.contains("axis"));
0035   BOOST_CHECK(jProtoAxis.contains("autorange"));
0036 
0037   ProtoAxis epabRead = ProtoAxisJsonConverter::fromJson(jProtoAxis);
0038 
0039   BOOST_CHECK_EQUAL(epabRead.getAxis(), epab.getAxis());
0040   BOOST_CHECK_EQUAL(epabRead.isAutorange(), epab.isAutorange());
0041   BOOST_CHECK_EQUAL(epabRead.toString(), epab.toString());
0042 }
0043 
0044 BOOST_AUTO_TEST_CASE(AutorangeProtoAxisJsonConversion) {
0045   using enum AxisBoundaryType;
0046   using enum AxisType;
0047 
0048   // Bound, equidistant axis, autorange
0049   ProtoAxis epa(Bound, 10);
0050 
0051   nlohmann::json jProtoAxis = ProtoAxisJsonConverter::toJson(epa);
0052 
0053   BOOST_CHECK(jProtoAxis.contains("axis"));
0054   BOOST_CHECK(jProtoAxis.contains("autorange"));
0055 
0056   ProtoAxis epaRead = ProtoAxisJsonConverter::fromJson(jProtoAxis);
0057 
0058   BOOST_CHECK_EQUAL(epaRead.getAxis(), epa.getAxis());
0059   BOOST_CHECK_EQUAL(epaRead.isAutorange(), epa.isAutorange());
0060   BOOST_CHECK_EQUAL(epaRead.toString(), epa.toString());
0061 }
0062 
0063 BOOST_AUTO_TEST_CASE(VariableProtoAxisJsonConversion) {
0064   using enum AxisBoundaryType;
0065   using enum AxisType;
0066 
0067   // Bound, variable axis
0068   ProtoAxis vpab(Bound, {0.0, 1.0, 10});
0069 
0070   nlohmann::json jProtoAxis = ProtoAxisJsonConverter::toJson(vpab);
0071   BOOST_CHECK(jProtoAxis.contains("axis"));
0072   BOOST_CHECK(jProtoAxis.contains("autorange"));
0073 
0074   ProtoAxis vpabRead = ProtoAxisJsonConverter::fromJson(jProtoAxis);
0075 
0076   BOOST_CHECK_EQUAL(vpabRead.getAxis(), vpab.getAxis());
0077   BOOST_CHECK_EQUAL(vpabRead.isAutorange(), vpab.isAutorange());
0078   BOOST_CHECK_EQUAL(vpabRead.toString(), vpab.toString());
0079 }
0080 
0081 BOOST_AUTO_TEST_CASE(InvalidAndValidInputJson) {
0082   // valid eq axis input
0083   nlohmann::json jValidEqAxis = {{"bins", 10},
0084                                  {"boundary_type", "Bound"},
0085                                  {"range", std::array<double, 2>{0.0, 1.0}},
0086                                  {"type", "Equidistant"}};
0087 
0088   // Valid input first
0089   nlohmann::json jValidEq = {{"axis", jValidEqAxis}, {"autorange", false}};
0090 
0091   BOOST_CHECK_NO_THROW(ProtoAxisJsonConverter::fromJson(jValidEq));
0092 
0093   // Invalid input - zero bins
0094   nlohmann::json jInvalidEqAxis = jValidEqAxis;
0095   jInvalidEqAxis["bins"] = 0;
0096 
0097   nlohmann::json jInvalidEq = {{"axis", jInvalidEqAxis}, {"autorange", false}};
0098 
0099   BOOST_CHECK_THROW(ProtoAxisJsonConverter::fromJson(jInvalidEq),
0100                     std::invalid_argument);
0101 
0102   // Invalid input - auto range without bins
0103   jInvalidEq = {{"axis", jInvalidEqAxis}, {"autorange", true}};
0104   BOOST_CHECK_THROW(ProtoAxisJsonConverter::fromJson(jInvalidEq),
0105                     std::invalid_argument);
0106 
0107   // Invalid input - min >= max
0108   jInvalidEqAxis = jValidEqAxis;
0109   jInvalidEqAxis["range"] = std::array<double, 2>{1.0, 0.0};
0110 
0111   jInvalidEq = {{"axis", jInvalidEqAxis}, {"autorange", false}};
0112 
0113   BOOST_CHECK_THROW(ProtoAxisJsonConverter::fromJson(jInvalidEq),
0114                     std::invalid_argument);
0115 
0116   nlohmann::json jValidVarAxis = {
0117       {"boundary_type", "Bound"},
0118       {"boundaries", std::vector<double>{0.0, 0.25, 0.75, 1.0}},
0119       {"type", "Variable"}};
0120 
0121   // Valid input first
0122   nlohmann::json jValidVar = {{"axis", jValidVarAxis}, {"autorange", false}};
0123   BOOST_CHECK_NO_THROW(ProtoAxisJsonConverter::fromJson(jValidVar));
0124 
0125   // Invalid input - less than two edges
0126   nlohmann::json jInvalidVarAxis = jValidVarAxis;
0127   jInvalidVarAxis["boundaries"] = std::vector<double>{0.0};
0128 
0129   nlohmann::json jInvalidVar = {{"axis", jInvalidVarAxis},
0130                                 {"autorange", false}};
0131   BOOST_CHECK_THROW(ProtoAxisJsonConverter::fromJson(jInvalidVar),
0132                     std::invalid_argument);
0133 
0134   // Invalid input - non-increasing edges
0135   jInvalidVarAxis = jValidVarAxis;
0136   jInvalidVarAxis["boundaries"] = std::vector<double>{0.0, 0.75, 0.25, 1.0};
0137 
0138   jInvalidVar = {{"axis", jInvalidVarAxis}, {"autorange", false}};
0139 
0140   BOOST_CHECK_THROW(ProtoAxisJsonConverter::fromJson(jInvalidVar),
0141                     std::invalid_argument);
0142 }
0143 
0144 BOOST_AUTO_TEST_SUITE_END()
0145 
0146 }  // namespace ActsTests