Line data Source code
1 :
2 : /**
3 : * @file DetID_test.cxx DetID class Unit Tests
4 : *
5 : * This is part of the DUNE DAQ Application Framework, copyright 2022.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "detdataformats/DetID.hpp"
11 :
12 : #define BOOST_TEST_MODULE DetID_test // NOLINT
13 :
14 : #include "boost/test/unit_test.hpp"
15 :
16 : #include <string>
17 : #include <vector>
18 :
19 : using namespace dunedaq::detdataformats;
20 :
21 : BOOST_AUTO_TEST_SUITE(DetID_test)
22 :
23 2 : BOOST_AUTO_TEST_CASE(DefaultConstruction)
24 : {
25 1 : DetID detid;
26 1 : BOOST_REQUIRE_EQUAL(detid.subdetector, DetID::Subdetector::kUnknown);
27 1 : }
28 :
29 2 : BOOST_AUTO_TEST_CASE(ExplicitConstruction)
30 : {
31 1 : DetID detid(DetID::Subdetector::kND_GAr);
32 1 : BOOST_REQUIRE_EQUAL(detid.subdetector, DetID::Subdetector::kND_GAr);
33 1 : }
34 :
35 2 : BOOST_AUTO_TEST_CASE(ImplicitConstruction)
36 : {
37 : // Test implicit conversion from Subdetector to DetID
38 1 : DetID detid = DetID::Subdetector::kHD_TPC;
39 1 : BOOST_REQUIRE_EQUAL(detid.subdetector, DetID::Subdetector::kHD_TPC);
40 1 : }
41 :
42 2 : BOOST_AUTO_TEST_CASE(AllSubdetectorValues)
43 : {
44 : // Test all defined subdetector enum values
45 1 : const std::vector<DetID::Subdetector> all_subdetectors = {
46 : DetID::Subdetector::kUnknown, DetID::Subdetector::kDAQ,
47 : DetID::Subdetector::kHD_PDS, DetID::Subdetector::kHD_TPC,
48 : DetID::Subdetector::kHD_CRT, DetID::Subdetector::kVD_CathodePDS,
49 : DetID::Subdetector::kVD_MembranePDS, DetID::Subdetector::kVD_BottomTPC,
50 : DetID::Subdetector::kVD_TopTPC, DetID::Subdetector::kVD_BernCRT,
51 : DetID::Subdetector::kVD_GrenobleCRT, DetID::Subdetector::kNDLAr_TPC,
52 : DetID::Subdetector::kNDLAr_PDS, DetID::Subdetector::kND_GAr
53 1 : };
54 :
55 15 : for (const auto& subdet : all_subdetectors) {
56 14 : DetID detid(subdet);
57 14 : BOOST_REQUIRE_EQUAL(detid.subdetector, subdet);
58 : }
59 1 : }
60 :
61 2 : BOOST_AUTO_TEST_CASE(SubdetectorToString)
62 : {
63 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kUnknown), "Unknown");
64 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kDAQ), "DAQ");
65 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kHD_PDS), "HD_PDS");
66 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kHD_TPC), "HD_TPC");
67 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kHD_CRT), "HD_CRT");
68 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_CathodePDS), "VD_CathodePDS");
69 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_MembranePDS), "VD_MembranePDS");
70 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_BottomTPC), "VD_BottomTPC");
71 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_TopTPC), "VD_TopTPC");
72 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_BernCRT), "VD_BernCRT");
73 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kVD_GrenobleCRT), "VD_GrenobleCRT");
74 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kNDLAr_TPC), "NDLAr_TPC");
75 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kNDLAr_PDS), "NDLAr_PDS");
76 1 : BOOST_REQUIRE_EQUAL(DetID::subdetector_to_string(DetID::Subdetector::kND_GAr), "ND_GAr");
77 1 : }
78 :
79 2 : BOOST_AUTO_TEST_CASE(StringToSubdetector)
80 : {
81 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("Unknown"), DetID::Subdetector::kUnknown);
82 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("DAQ"), DetID::Subdetector::kDAQ);
83 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("HD_PDS"), DetID::Subdetector::kHD_PDS);
84 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("HD_TPC"), DetID::Subdetector::kHD_TPC);
85 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("HD_CRT"), DetID::Subdetector::kHD_CRT);
86 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_CathodePDS"), DetID::Subdetector::kVD_CathodePDS);
87 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_MembranePDS"), DetID::Subdetector::kVD_MembranePDS);
88 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_BottomTPC"), DetID::Subdetector::kVD_BottomTPC);
89 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_TopTPC"), DetID::Subdetector::kVD_TopTPC);
90 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_BernCRT"), DetID::Subdetector::kVD_BernCRT);
91 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("VD_GrenobleCRT"), DetID::Subdetector::kVD_GrenobleCRT);
92 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("NDLAr_TPC"), DetID::Subdetector::kNDLAr_TPC);
93 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("NDLAr_PDS"), DetID::Subdetector::kNDLAr_PDS);
94 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("ND_GAr"), DetID::Subdetector::kND_GAr);
95 1 : }
96 :
97 2 : BOOST_AUTO_TEST_CASE(StringToSubdetectorInvalid)
98 : {
99 : // Invalid strings should fall back to kUnknown
100 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("InvalidName"), DetID::Subdetector::kUnknown);
101 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector(""), DetID::Subdetector::kUnknown);
102 1 : BOOST_REQUIRE_EQUAL(DetID::string_to_subdetector("hd_tpc"), DetID::Subdetector::kUnknown); // case sensitive
103 1 : }
104 :
105 2 : BOOST_AUTO_TEST_CASE(ConversionRoundTrip)
106 : {
107 : // Test that conversion to/from string is reversible for all values
108 1 : const std::vector<DetID::Subdetector> all_subdetectors = {
109 : DetID::Subdetector::kUnknown, DetID::Subdetector::kDAQ,
110 : DetID::Subdetector::kHD_PDS, DetID::Subdetector::kHD_TPC,
111 : DetID::Subdetector::kHD_CRT, DetID::Subdetector::kVD_CathodePDS,
112 : DetID::Subdetector::kVD_MembranePDS, DetID::Subdetector::kVD_BottomTPC,
113 : DetID::Subdetector::kVD_TopTPC, DetID::Subdetector::kVD_BernCRT,
114 : DetID::Subdetector::kVD_GrenobleCRT, DetID::Subdetector::kNDLAr_TPC,
115 : DetID::Subdetector::kNDLAr_PDS, DetID::Subdetector::kND_GAr
116 1 : };
117 :
118 15 : for (const auto& subdet : all_subdetectors) {
119 14 : std::string str = DetID::subdetector_to_string(subdet);
120 14 : DetID::Subdetector recovered = DetID::string_to_subdetector(str);
121 14 : BOOST_REQUIRE_EQUAL(recovered, subdet);
122 14 : }
123 1 : }
124 :
125 2 : BOOST_AUTO_TEST_CASE(StreamOperatorOutput)
126 : {
127 1 : DetID detid(DetID::Subdetector::kND_GAr);
128 1 : std::ostringstream ostr;
129 1 : ostr << detid;
130 1 : std::string output = ostr.str();
131 1 : BOOST_TEST_MESSAGE("Stream output: " << output);
132 :
133 1 : BOOST_REQUIRE(!output.empty());
134 1 : BOOST_REQUIRE(output.find("subdetector: ND_GAr") != std::string::npos);
135 1 : }
136 :
137 2 : BOOST_AUTO_TEST_CASE(StreamOperatorRoundTrip)
138 : {
139 1 : DetID detid(DetID::Subdetector::kHD_TPC);
140 1 : std::ostringstream ostr;
141 1 : ostr << detid;
142 :
143 1 : std::istringstream iss(ostr.str());
144 1 : DetID detid_from_stream;
145 1 : iss >> detid_from_stream;
146 :
147 1 : BOOST_REQUIRE_EQUAL(detid_from_stream.subdetector, detid.subdetector);
148 1 : }
149 :
150 2 : BOOST_AUTO_TEST_CASE(StreamOperatorRoundTripAll)
151 : {
152 : // Test round-trip for all subdetector values:
153 : // DetID -> (<<) -> string -> (>>) -> DetID', verify equal
154 1 : const std::vector<DetID::Subdetector> all_subdetectors = {
155 : DetID::Subdetector::kUnknown, DetID::Subdetector::kDAQ,
156 : DetID::Subdetector::kHD_PDS, DetID::Subdetector::kHD_TPC,
157 : DetID::Subdetector::kHD_CRT, DetID::Subdetector::kVD_CathodePDS,
158 : DetID::Subdetector::kVD_MembranePDS, DetID::Subdetector::kVD_BottomTPC,
159 : DetID::Subdetector::kVD_TopTPC, DetID::Subdetector::kVD_BernCRT,
160 : DetID::Subdetector::kVD_GrenobleCRT, DetID::Subdetector::kNDLAr_TPC,
161 : DetID::Subdetector::kNDLAr_PDS, DetID::Subdetector::kND_GAr
162 1 : };
163 :
164 15 : for (const auto& subdet : all_subdetectors) {
165 14 : DetID original(subdet);
166 :
167 : // Serialize to string using <<
168 14 : std::ostringstream oss;
169 14 : oss << original;
170 14 : std::string serialized = oss.str();
171 :
172 : // Deserialize from string using >>
173 14 : std::istringstream iss(serialized);
174 14 : DetID recovered;
175 14 : iss >> recovered;
176 :
177 : // Verify they're equal
178 14 : BOOST_REQUIRE_EQUAL(recovered.subdetector, original.subdetector);
179 14 : }
180 1 : }
181 :
182 2 : BOOST_AUTO_TEST_CASE(SubdetectorEnumOperator)
183 : {
184 : // Test stream operator on Subdetector enum directly
185 1 : DetID::Subdetector subdet = DetID::Subdetector::kHD_TPC;
186 1 : std::ostringstream ostr;
187 1 : ostr << subdet;
188 1 : std::string output = ostr.str();
189 1 : BOOST_REQUIRE_EQUAL(output, "HD_TPC");
190 1 : }
191 :
192 : BOOST_AUTO_TEST_SUITE_END()
|