Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use static array #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ $(C_BUILDDIR)/pokedex_plus_hgss.o: CFLAGS := -mthumb -mthumb-interwork -O2 -mabi
# Annoyingly we can't turn this on just for src/data/trainers.h
$(C_BUILDDIR)/data.o: CFLAGS += -fno-show-column -fno-diagnostics-show-caret

$(TEST_BUILDDIR)/%.o: CFLAGS := -mthumb -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -Wno-pointer-to-int-cast -Werror -Wall -Wno-strict-aliasing -Wno-attribute-alias -Woverride-init

# Dependency rules (for the *.c & *.s sources to .o files)
# Have to be explicit or else missing files won't be reported.

Expand Down
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
12 changes: 12 additions & 0 deletions include/test/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,16 @@
* GIVEN {
* FLAG_SET(FLAG_SYS_EXAMPLE_FLAG);
*
* WITH_CONFIG(configTag, value)
* Runs the test with a specified config override. `configTag` must be
* of `enum GenConfigTag`
* Example:
* GIVEN {
* WITH_CONFIG(GEN_CONFIG_GALE_WINGS, GEN_6);
* }
* The `value` may be inferred from a local variable, e.g. set by
* PARAMETRIZE.
*
* PLAYER(species) and OPPONENT(species)
* Adds the species to the player's or opponent's party respectively.
* The Pokémon can be further customized with the following functions:
Expand Down Expand Up @@ -823,6 +833,7 @@ struct moveWithPP {
#define AI_LOG AILogScores(__LINE__)

#define FLAG_SET(flagId) SetFlagForTest(__LINE__, flagId)
#define WITH_CONFIG(configTag, value) TestSetConfig(__LINE__, configTag, value)

#define PLAYER(species) for (OpenPokemon(__LINE__, B_SIDE_PLAYER, species); gBattleTestRunnerState->data.currentMon; ClosePokemon(__LINE__))
#define OPPONENT(species) for (OpenPokemon(__LINE__, B_SIDE_OPPONENT, species); gBattleTestRunnerState->data.currentMon; ClosePokemon(__LINE__))
Expand Down Expand Up @@ -856,6 +867,7 @@ struct moveWithPP {
#define Shadow(isShadow) Shadow_(__LINE__, shadow)

void SetFlagForTest(u32 sourceLine, u16 flagId);
void TestSetConfig(u32 sourceLine, enum GenConfigTag configTag, u32 value);
void ClearFlagAfterTest(void);
void OpenPokemon(u32 sourceLine, u32 side, u32 species);
void ClosePokemon(u32 sourceLine);
Expand Down
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
3 changes: 1 addition & 2 deletions test/battle/ability/gale_wings.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ SINGLE_BATTLE_TEST("Gale Wings only grants priority at full HP (Gen 7+)")
PARAMETRIZE { hp = 100; config = GEN_6; }
PARAMETRIZE { hp = 99; config = GEN_6; }
GIVEN {
SetGenConfig(GEN_CONFIG_GALE_WINGS, config);
ASSUME(B_GALE_WINGS >= GEN_7);
WITH_CONFIG(GEN_CONFIG_GALE_WINGS, config);
ASSUME(gMovesInfo[MOVE_AERIAL_ACE].type == TYPE_FLYING);
PLAYER(SPECIES_TALONFLAME) { Ability(ABILITY_GALE_WINGS); HP(hp); MaxHP(100); Speed(1);}
OPPONENT(SPECIES_WOBBUFFET) { Speed(100);};
Expand Down
8 changes: 8 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 Expand Up @@ -1513,6 +1515,12 @@ void SetFlagForTest(u32 sourceLine, u16 flagId)
FlagSet(flagId);
}

void TestSetConfig(u32 sourceLine, enum GenConfigTag configTag, u32 value)
{
INVALID_IF(!STATE->runGiven, "WITH_CONFIG outside of GIVEN");
SetGenConfig(configTag, value);
}

void ClearFlagAfterTest(void)
{
if (DATA.flagId != 0)
Expand Down
Loading