Skip to content

Commit

Permalink
Add ASSERT_DECLARE() macro.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dwsteele committed Apr 21, 2022
1 parent e18b70b commit 4daddeb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
5 changes: 5 additions & 0 deletions src/common/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/***********************************************************************************************************************************
Expand Down
64 changes: 27 additions & 37 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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))
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 4daddeb

Please sign in to comment.