Skip to content

Commit

Permalink
refactor to use inline static array
Browse files Browse the repository at this point in the history
  • Loading branch information
sbird committed Dec 13, 2024
1 parent d2201fb commit a1b5f95
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions include/constants/generational_changes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
enum GenConfigTag
{
GEN_CONFIG_GALE_WINGS,
GEN_CONFIG_COUNT
};

#endif // GUARD_CONSTANTS_GENERATIONAL_CHANGES_H
36 changes: 34 additions & 2 deletions include/generational_changes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,40 @@
#define GUARD_GENERATIONAL_CHANGES_H

#include "constants/generational_changes.h"
#include "config/battle.h"

u32 GetGenConfig(enum GenConfigTag configTag);
void SetGenConfig(enum GenConfigTag configTag, u32 value);
static const u8 sGenerationalChanges[GEN_CONFIG_COUNT] =
{
[GEN_CONFIG_GALE_WINGS] = B_GALE_WINGS,
};

#if TESTING
extern u8 *gGenerationalChangesTestOverride;
#endif

static inline u32 GetGenConfig(enum GenConfigTag configTag)
{
if (configTag >= GEN_CONFIG_COUNT) return GEN_LATEST;
#if TESTING
if (gGenerationalChangesTestOverride == NULL) return sGenerationalChanges[configTag];
return gGenerationalChangesTestOverride[configTag];
#else
return sGenerationalChanges[configTag];
#endif
}

static inline void SetGenConfig(enum GenConfigTag configTag, u32 value)
{
#if TESTING
if (configTag >= GEN_CONFIG_COUNT) return;
if (gGenerationalChangesTestOverride == NULL) return;
gGenerationalChangesTestOverride[configTag] = value;
#endif
}

#if TESTING
void TestInitConfigData(void);
void TestFreeConfigData(void);
#endif

#endif // GUARD_GENERATIONAL_CHANGES_H
28 changes: 7 additions & 21 deletions src/generational_changes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,16 @@
#include "constants/generational_changes.h"

#if TESTING
EWRAM_INIT struct GenerationalChanges gGenerationalChanges =
#else
const struct GenerationalChanges gGenerationalChanges =
#endif
{
.galeWingsFullHealth = B_GALE_WINGS >= GEN_7,
};
EWRAM_DATA u8 *gGenerationalChangesTestOverride = NULL;

u32 GetGenConfig(enum GenConfigTag configTag)
void TestInitConfigData(void)
{
switch (configTag)
{
case GEN_CONFIG_GALE_WINGS: return gGenerationalChanges.galeWingsFullHealth ? GEN_7 : GEN_6;
default: return GEN_LATEST;
}
gGenerationalChangesTestOverride = Alloc(sizeof(sGenerationalChanges));
memcpy(gGenerationalChangesTestOverride, sGenerationalChanges, sizeof(sGenerationalChanges));
}

void SetGenConfig(enum GenConfigTag configTag, u32 value)
void TestFreeConfigData(void)
{
#if TESTING
switch (configTag)
{
case GEN_CONFIG_GALE_WINGS: gGenerationalChanges.galeWingsFullHealth = (value >= GEN_7);
}
#else
#endif
TRY_FREE_AND_SET_NULL(gGenerationalChangesTestOverride)
}
#endif
2 changes: 2 additions & 0 deletions test/test_runner_battle.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ static void BattleTest_SetUp(void *data)
{
const struct BattleTest *test = data;
memset(STATE, 0, sizeof(*STATE));
TestInitConfigData();
InvokeTestFunction(test);
STATE->parameters = STATE->parametersCount;
if (STATE->parametersCount == 0 && test->resultsSize > 0)
Expand Down Expand Up @@ -1417,6 +1418,7 @@ static void BattleTest_TearDown(void *data)
// Free resources that aren't cleaned up when the battle was
// aborted unexpectedly.
ClearFlagAfterTest();
TestFreeConfigData();
if (STATE->tearDownBattle)
TearDownBattle();
}
Expand Down

0 comments on commit a1b5f95

Please sign in to comment.