Line data Source code
1 :
2 : /**
3 : * @file WIBEthFrame_test.cxx WIBEthFrame 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 "fddetdataformats/WIBEthFrame.hpp"
11 :
12 : #define BOOST_TEST_MODULE WIBEthFrame_test // NOLINT
13 :
14 : #include "boost/test/unit_test.hpp"
15 : #include "logging/Logging.hpp" // For TLOG_DEBUG
16 :
17 : #include <random>
18 : #include <string>
19 : #include <vector>
20 :
21 : // NOLINTBEGIN(build/unsigned)
22 :
23 : BOOST_AUTO_TEST_SUITE(WIBEthFrame_test)
24 :
25 2 : BOOST_AUTO_TEST_CASE(WIBEthFrame_ADCDataMutators)
26 : {
27 : // RNG with max ADC-width values
28 1 : std::random_device dev;
29 1 : std::mt19937 rng(dev());
30 1 : int max_adc_value = (static_cast<dunedaq::fddetdataformats::WIBEthFrame::word_t>(1)
31 : << dunedaq::fddetdataformats::WIBEthFrame::s_bits_per_adc) -
32 : 1;
33 1 : std::uniform_int_distribution<std::mt19937::result_type> dist(1, max_adc_value);
34 :
35 : // Prepare source vector with ADC samples
36 1 : std::vector<std::vector<uint16_t>> v;
37 65 : for (int i = 0; i < 64; ++i) {
38 64 : v.emplace_back(64); // i.e., emplace back a 64-element 1-d vector
39 4160 : for (int j = 0; j < 64; ++j) {
40 4096 : auto rand_val = dist(rng);
41 4096 : v[i][j] = static_cast<uint16_t>(rand_val);
42 : }
43 : }
44 :
45 : // Set ADCs from ADC samples
46 1 : dunedaq::fddetdataformats::WIBEthFrame wibethframe{};
47 65 : for (std::size_t i = 0; i < v.size(); ++i) {
48 4160 : for (std::size_t j = 0; j < v[i].size(); ++j) {
49 4096 : wibethframe.set_adc(i, j, v[i][j]);
50 : }
51 : }
52 1 : wibethframe.set_adc(
53 : 0,
54 : 0,
55 1 : v[0][0]); // Set the first ADC again to check that we can overwrite existing values without affecting other values
56 :
57 1 : auto original_level = boost::unit_test::unit_test_log.set_threshold_level(boost::unit_test::log_warnings);
58 :
59 : // Get ADCs and compare
60 65 : for (std::size_t i = 0; i < v.size(); ++i) {
61 4160 : for (std::size_t j = 0; j < v[i].size(); ++j) {
62 8192 : TLOG_DEBUG(1) << "Comparing ADC value for channel " << i << ", sample " << j << ": " << wibethframe.get_adc(i, j)
63 4096 : << " vs " << v[i][j];
64 4096 : BOOST_REQUIRE_EQUAL(wibethframe.get_adc(i, j), v[i][j]);
65 : }
66 : }
67 1 : boost::unit_test::unit_test_log.set_threshold_level(original_level);
68 1 : }
69 :
70 2 : BOOST_AUTO_TEST_CASE(WIBEthFrame_IndexAndValueBounds)
71 : {
72 1 : using dunedaq::fddetdataformats::WIBEthFrame;
73 :
74 1 : WIBEthFrame wibethframe{};
75 :
76 1 : BOOST_CHECK_THROW(wibethframe.get_adc(-1, 0), std::out_of_range);
77 1 : BOOST_CHECK_THROW(wibethframe.get_adc(WIBEthFrame::s_num_channels, 0), std::out_of_range);
78 1 : BOOST_CHECK_THROW(wibethframe.get_adc(0, -1), std::out_of_range);
79 1 : BOOST_CHECK_THROW(wibethframe.get_adc(0, WIBEthFrame::s_time_samples_per_frame), std::out_of_range);
80 :
81 1 : BOOST_CHECK_THROW(wibethframe.set_adc(-1, 0, 123), std::out_of_range);
82 1 : BOOST_CHECK_THROW(wibethframe.set_adc(WIBEthFrame::s_num_channels, 0, 123), std::out_of_range);
83 1 : BOOST_CHECK_THROW(wibethframe.set_adc(0, -1, 123), std::out_of_range);
84 1 : BOOST_CHECK_THROW(wibethframe.set_adc(0, WIBEthFrame::s_time_samples_per_frame, 123), std::out_of_range);
85 1 : BOOST_CHECK_THROW(wibethframe.set_adc(0, 0, 1 << WIBEthFrame::s_bits_per_adc), std::out_of_range);
86 :
87 1 : BOOST_CHECK_NO_THROW(wibethframe.set_adc(0, 0, (1 << WIBEthFrame::s_bits_per_adc) - 1));
88 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(0, 0), (1 << WIBEthFrame::s_bits_per_adc) - 1);
89 1 : }
90 :
91 2 : BOOST_AUTO_TEST_CASE(WIBEthFrame_NeighborIsolationAcrossWordBoundary)
92 : {
93 1 : using dunedaq::fddetdataformats::WIBEthFrame;
94 :
95 1 : WIBEthFrame wibethframe{};
96 1 : constexpr auto max_adc = static_cast<uint16_t>((1u << WIBEthFrame::s_bits_per_adc) - 1u);
97 1 : constexpr int sample = 5;
98 1 : constexpr int boundary_channel = 4;
99 :
100 1 : wibethframe.set_adc(boundary_channel - 1, sample, 0x0000u);
101 1 : wibethframe.set_adc(boundary_channel, sample, 0x0000u);
102 1 : wibethframe.set_adc(boundary_channel + 1, sample, 0x0000u);
103 :
104 1 : wibethframe.set_adc(boundary_channel, sample, 0x2AAAu);
105 :
106 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel - 1, sample), 0x0000u);
107 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel, sample), 0x2AAAu);
108 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel + 1, sample), 0x0000u);
109 :
110 1 : wibethframe.set_adc(boundary_channel - 1, sample, max_adc);
111 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel - 1, sample), max_adc);
112 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel, sample), 0x2AAAu);
113 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel + 1, sample), 0x0000u);
114 :
115 1 : wibethframe.set_adc(boundary_channel + 1, sample, 0x1555u);
116 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel - 1, sample), max_adc);
117 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel, sample), 0x2AAAu);
118 1 : BOOST_CHECK_EQUAL(wibethframe.get_adc(boundary_channel + 1, sample), 0x1555u);
119 1 : }
120 :
121 2 : BOOST_AUTO_TEST_CASE(WIBEthFrame_MetadataMutators)
122 : {
123 1 : using dunedaq::fddetdataformats::WIBEthFrame;
124 :
125 1 : WIBEthFrame wibethframe{};
126 :
127 1 : wibethframe.set_timestamp(0x0123456789ABCDEFuLL);
128 1 : BOOST_CHECK_EQUAL(wibethframe.get_timestamp(), 0x0123456789ABCDEFuLL);
129 :
130 1 : wibethframe.set_channel(0);
131 1 : BOOST_CHECK_EQUAL(wibethframe.get_channel(), 0);
132 :
133 1 : wibethframe.set_channel(255);
134 1 : BOOST_CHECK_EQUAL(wibethframe.get_channel(), 255);
135 1 : }
136 :
137 : BOOST_AUTO_TEST_SUITE_END()
138 :
139 : // NOLINTEND(build/unsigned)
|