From 4daddebaca6b4b1861778bc2b09d265302c05ed1 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 21 Apr 2022 08:07:22 -0400 Subject: [PATCH] Add ASSERT_DECLARE() macro. Declare variables that will be used by later assertions with the goal of making them easier to read and maintain. This is particularly useful for variables that are used more than once and require a lot of syntax to extract. --- src/common/assert.h | 5 ++++ src/config/config.c | 64 +++++++++++++++++++-------------------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/common/assert.h b/src/common/assert.h index 03c2e1326..f5e141e8a 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -35,10 +35,15 @@ Asserts are used in test code to ensure that certain conditions are true. They // Used when execution reaches an invalid location rather than an invalid condition #define ASSERT_MSG(message) \ THROW_FMT(AssertError, message); + + // Declare variables that will be used by later assertions with the goal of making them easier to read and maintain + #define ASSERT_DECLARE(declaration) \ + declaration #else #define ASSERT(condition) #define ASSERT_INLINE(condition) #define ASSERT_MSG(message) + #define ASSERT_DECLARE(declaration) #endif /*********************************************************************************************************************************** diff --git a/src/config/config.c b/src/config/config.c index 26b7bbd91..e938e3beb 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -512,10 +512,9 @@ cfgOptionIdxDisplay(const ConfigOption optionId, const unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); // Check that the option is valid for the current command if (!cfgOptionValid(optionId)) @@ -573,10 +572,9 @@ cfgOptionIdxName(ConfigOption optionId, unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); // If an indexed option ConfigOptionData *const option = &configLocal->option[optionId]; @@ -629,10 +627,9 @@ cfgOptionIdxNegate(ConfigOption optionId, unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); FUNCTION_TEST_RETURN(configLocal->option[optionId].index[optionIdx].negate); } @@ -658,10 +655,9 @@ cfgOptionIdxReset(ConfigOption optionId, unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); FUNCTION_TEST_RETURN(configLocal->option[optionId].index[optionIdx].reset); } @@ -681,10 +677,9 @@ cfgOptionIdxInternal( ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); // Check that the option is valid for the current command if (!cfgOptionValid(optionId)) @@ -719,10 +714,9 @@ cfgOptionIdxVar(const ConfigOption optionId, const unsigned int optionIdx) FUNCTION_TEST_END(); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); const ConfigOptionData *const option = &configLocal->option[optionId]; @@ -928,10 +922,9 @@ cfgOptionIdxSet(ConfigOption optionId, unsigned int optionIdx, ConfigSource sour ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); // Set the source ConfigOptionData *const option = &configLocal->option[optionId]; @@ -1024,10 +1017,9 @@ cfgOptionIdxSource(ConfigOption optionId, unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - (!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal)); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal)); FUNCTION_TEST_RETURN(configLocal->option[optionId].index[optionIdx].source); } @@ -1053,11 +1045,9 @@ cfgOptionIdxTest(ConfigOption optionId, unsigned int optionIdx) ASSERT(optionId < CFG_OPTION_TOTAL); ASSERT(configLocal != NULL); - ASSERT( - !cfgOptionValid(optionId) || - ((!configLocal->option[optionId].group && optionIdx == 0) || - (configLocal->option[optionId].group && optionIdx < - configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal))); + ASSERT_DECLARE(const bool group = configLocal->option[optionId].group); + ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal); + ASSERT(!cfgOptionValid(optionId) || ((!group && optionIdx == 0) || (group && optionIdx < indexTotal))); FUNCTION_TEST_RETURN(cfgOptionValid(optionId) && configLocal->option[optionId].index[optionIdx].set); }