F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
TestUtils.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CcsdsTestUtils.cpp
3 // \author bocchino
4 // \brief Test utilities for CCSDS
5 // ======================================================================
6 
8 #include "STest/Random/Random.hpp"
9 
10 namespace Svc {
11 
12 namespace CcsdsTestUtils {
13 
16 
18  const SerialType selectedIdx = static_cast<SerialType>(STest::Random::startLength(0, ComCfg::Apid::NUM_CONSTANTS));
19  // Choose from within the bounds provided by CCSDS and the SerialType.
20  // 1. We have to respect the CCSDS bound because we are testing CCSDS code
21  // 2. We have to respect the SerialType bound because F Prime may not be configured
22  // to use CCSDS, but we still want the test to be valid, if possible.
23  constexpr auto ccsdsBound = static_cast<SerialType>((1 << 11) - 1); // 11 bits
24  constexpr auto serialTypeBound = std::numeric_limits<SerialType>::max();
25  constexpr auto bound = std::min(ccsdsBound, serialTypeBound);
26  // Search through the interval [0, maxApid] until we find a valid APID at the
27  // selected index, or we run out of numbers
28  SerialType idx = 0;
29  SerialType apid = 0;
30  // Break the loop checking into two operations so that g++ can't report a Wtype-limits warning
31  // when bound is the type max (since neither individual check is always true, but <= would be)
32  for (SerialType candidateApid = 0; candidateApid < bound || candidateApid == bound; candidateApid++) {
33  if (ComCfg::Apid::isValid(candidateApid)) {
34  // Found a valid APID: store it
35  apid = candidateApid;
36  if (idx == selectedIdx) {
37  // We are at the selected index: done
38  break;
39  }
40  // Not yet at the selected index: keep going
41  // We'll either go onto the next valid APID or use the current one
42  // if we run off the end of the 11-bit range
43  idx++;
44  }
45  }
46  // If the APID we found is not valid, then return NONE
47  // This can happen if all of the configured APIDs are out of the 11-bit range
48  // required by CCSDS
49  const auto result = ComCfg::Apid::isValid(apid) ? ApidOption(static_cast<ComCfg::Apid::T>(apid)) : Fw::NONE;
50  return result;
51 }
52 
53 } // namespace CcsdsTestUtils
54 
55 } // namespace Svc
Fw::Optional< ComCfg::Apid::T > ApidOption
Definition: TestUtils.cpp:15
U16 SerialType
The serial representation type.
Definition: ApidEnumAc.hpp:29
ApidOption getRandomApid()
Definition: TestUtils.cpp:17
The number of enumerated constants.
Definition: ApidEnumAc.hpp:72
constexpr None_t NONE
Global constant representing an absent/empty optional value.
Definition: Optional.hpp:31
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
RateGroupDivider component implementation.
bool isValid() const
Check raw enum value for validity.
Definition: ApidEnumAc.cpp:69
ComCfg::Apid::SerialType SerialType
Definition: TestUtils.cpp:14
A type-safe container for an optional value.
Definition: Optional.hpp:43