Skip to content

Commit

Permalink
Test enums match between C and C++ with static asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
gunvirranu committed Nov 20, 2024
1 parent 295c797 commit a3827ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 2 additions & 4 deletions include/perturb/perturb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Copyright (c) 2022 Gunvir Singh Ranu
*/

//! @file Primary C++ header for the perturb library
//! @file Primary header for the C++ wrapper of the perturb library
//! @author Gunvir Singh Ranu
//! @version 1.0.0
//! @copyright Gunvir Singh Ranu, MIT License
Expand All @@ -35,7 +35,7 @@
#include "perturb/tle.h"
#include "perturb/sgp4.h"

/// Primary namespace for the perturb C++ library wrapper, everything is in here.
/// Primary namespace for the perturb library C++ wrapper, everything is in here.
///
/// See README for a brief intro to the main library types and basic usage.
/// The C library interface is hidden in the `perturb::c_internal` namespace.
Expand All @@ -54,8 +54,6 @@ using Vec3 = std::array<real_t, 3>;
/// purposes only and doesn't pertain to general usage.
constexpr std::size_t TLE_LINE_LEN = 69;

// TODO: Add a few static asserts for enums as a sanity check

enum class TLEParseError {
NONE, ///< If no issues when parsing
SHOULD_BE_SPACE, ///< If there is a lack of space in the TLE
Expand Down
23 changes: 23 additions & 0 deletions src/perturb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@

namespace perturb {

// Sanity check enums matches
#define CHECK_ENUM_MATCHES(A, B) static_assert(static_cast<int>(A) == c_internal::PERTURB_ ## B, "Enum mismatch!")

CHECK_ENUM_MATCHES(TLEParseError::NONE, TLE_PARSE_ERROR_NONE);
CHECK_ENUM_MATCHES(TLEParseError::SHOULD_BE_SPACE, TLE_PARSE_ERROR_SHOULD_BE_SPACE);
CHECK_ENUM_MATCHES(TLEParseError::INVALID_FORMAT, TLE_PARSE_ERROR_INVALID_FORMAT);
CHECK_ENUM_MATCHES(TLEParseError::INVALID_VALUE, TLE_PARSE_ERROR_INVALID_VALUE);
CHECK_ENUM_MATCHES(TLEParseError::CHECKSUM_MISMATCH, TLE_PARSE_ERROR_CHECKSUM_MISMATCH);

CHECK_ENUM_MATCHES(GravModel::WGS72_OLD, GRAVITY_MODEL_WGS72_OLD);
CHECK_ENUM_MATCHES(GravModel::WGS72, GRAVITY_MODEL_WGS72);
CHECK_ENUM_MATCHES(GravModel::WGS84, GRAVITY_MODEL_WGS84);

CHECK_ENUM_MATCHES(Sgp4Error::NONE, SGP4_ERROR_NONE);
CHECK_ENUM_MATCHES(Sgp4Error::MEAN_ELEMENTS, SGP4_ERROR_MEAN_ELEMENTS);
CHECK_ENUM_MATCHES(Sgp4Error::MEAN_MOTION, SGP4_ERROR_MEAN_MOTION);
CHECK_ENUM_MATCHES(Sgp4Error::PERT_ELEMENTS, SGP4_ERROR_PERT_ELEMENTS);
CHECK_ENUM_MATCHES(Sgp4Error::SEMI_LATUS_RECTUM, SGP4_ERROR_SEMI_LATUS_RECTUM);
CHECK_ENUM_MATCHES(Sgp4Error::EPOCH_ELEMENTS_SUB_ORBITAL, SGP4_ERROR_EPOCH_ELEMENTS_SUB_ORBITAL);
CHECK_ENUM_MATCHES(Sgp4Error::DECAYED, SGP4_ERROR_DECAYED);
CHECK_ENUM_MATCHES(Sgp4Error::INVALID_TLE, SGP4_ERROR_INVALID_TLE);
CHECK_ENUM_MATCHES(Sgp4Error::UNKNOWN, SGP4_ERROR_UNKNOWN);

static Sgp4Error convert_sgp4_error_code(const int error_code) {
if (error_code < 0 || error_code >= static_cast<int>(Sgp4Error::UNKNOWN)) {
return Sgp4Error::UNKNOWN;
Expand Down

0 comments on commit a3827ab

Please sign in to comment.