Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "Pokemon/Navigation/Pokemon_RouteLog_Tests.h"
#include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h"
#include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h"
#include "Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h"
#include "Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h"
#include "VideoGames/PokemonHome/PokemonHome_Tests.h"
#include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h"
Expand All @@ -60,7 +61,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 64 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=45 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -86,6 +87,7 @@
Pokemon::add_tests_MapData(ret);
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Pokemon::add_tests_SimFaults(ret);
Pokemon::add_tests_SimGolden(ret);
Pokemon::add_tests_AutoStory(ret);
NintendoSwitch::add_tests_CheckOnlineDetector(ret);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Sim Faults
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <utility>
#include "Pokemon_SimFaults.h"

namespace PokemonAutomation{
namespace Pokemon{


const std::vector<FaultClass>& fault_classes(){
static const std::vector<FaultClass> classes{
FaultClass::SCENE_END_POSE,
FaultClass::SCENE_EARLY,
FaultClass::BATTLE_OPENING,
FaultClass::WILD_BATTLES,
FaultClass::LATE_FADE,
FaultClass::SLOW_FADE_IN,
FaultClass::WARP_NO_OP,
FaultClass::DARK_CAVE,
FaultClass::LOST_TAP,
FaultClass::UNSEEN_STEP,
};
return classes;
}

std::string_view fault_class_name(FaultClass fault){
switch (fault){
case FaultClass::SCENE_END_POSE:
return "scene end pose";
case FaultClass::SCENE_EARLY:
return "scene early";
case FaultClass::BATTLE_OPENING:
return "battle opening";
case FaultClass::WILD_BATTLES:
return "wild battles";
case FaultClass::LATE_FADE:
return "late fade";
case FaultClass::SLOW_FADE_IN:
return "slow fade-in";
case FaultClass::WARP_NO_OP:
return "warp no-op";
case FaultClass::DARK_CAVE:
return "dark cave";
case FaultClass::LOST_TAP:
return "lost tap";
case FaultClass::UNSEEN_STEP:
return "unseen step";
}
return "?";
}


FaultOccurrences::FaultOccurrences(std::set<int> faulted)
: m_numbers(std::move(faulted))
{}

bool FaultOccurrences::next(){
m_chances++;
if (!m_numbers.contains(m_chances)){
return false;
}
m_faulted++;
return true;
}


SimFaults::SimFaults(std::map<FaultClass, std::set<int>> faulted){
for (auto& [fault, numbers] : faulted){
m_classes.emplace(fault, FaultOccurrences(std::move(numbers)));
}
}

bool SimFaults::next(FaultClass fault){
return m_classes[fault].next();
}

int SimFaults::chances(FaultClass fault) const{
const auto iter = m_classes.find(fault);
return iter == m_classes.end() ? 0 : iter->second.chances();
}

int SimFaults::faulted(FaultClass fault) const{
const auto iter = m_classes.find(fault);
return iter == m_classes.end() ? 0 : iter->second.faulted();
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* Sim Faults
*
* From: https://github.com/PokemonAutomation/
*
* The faults the games' overworld simulations inject, shared by BDSP's
* and FRLG's: the classes of failure the consoles showed, and a counter
* per class that says which of a fault's chances to fault. A chance is
* each time the sim reaches a point where the game could fail that way
* (a tap, a warp, a step's end); a test names the chances it faults by
* number, from 1 in the order the sim reaches them.
*
* How a class is injected is each game's (its sim's knobs and its fault
* sweep); a game may inject only some of the classes.
*
*/

#ifndef PokemonAutomation_Pokemon_SimFaults_H
#define PokemonAutomation_Pokemon_SimFaults_H

#include <map>
#include <set>
#include <string_view>
#include <vector>

namespace PokemonAutomation{
namespace Pokemon{


// A class of failure a console showed, which a sim can inject.
enum class FaultClass{
// A step whose end the game sets (a talk, a scene, a battle) leaves the
// player on another walkable tile next to the expected one.
SCENE_END_POSE,
// A scene starts on the last tiles of the walk before it.
SCENE_EARLY,
// A battle's opening is still playing when a step starts.
BATTLE_OPENING,
// Wild battles while walking, more than a clean run meets.
WILD_BATTLES,
// A warp's fade shows late.
LATE_FADE,
// Every warp's fade-in runs long: a move right after it goes nowhere.
SLOW_FADE_IN,
// A step onto a warp shows the fade's black but does not warp.
WARP_NO_OP,
// A dark cave walked without Flash.
DARK_CAVE,
// A tap the game drops.
LOST_TAP,
// A step tap that moved the player, read as not moved.
UNSEEN_STEP,
};

// Every fault class, in the enum's order.
const std::vector<FaultClass>& fault_classes();

// The class's name for logs: "scene end pose".
std::string_view fault_class_name(FaultClass fault);


// Which chances of one fault to fault: those numbered in `faulted`,
// counted from 1 as next() reaches them.
class FaultOccurrences{
public:
FaultOccurrences() = default;
explicit FaultOccurrences(std::set<int> faulted);

// Counts one more chance: whether it is one to fault.
bool next();

// The chances counted so far.
int chances() const{
return m_chances;
}
// How many of them were faulted.
int faulted() const{
return m_faulted;
}

private:
std::set<int> m_numbers;
int m_chances = 0;
int m_faulted = 0;
};


// A FaultOccurrences per fault class: a class not given counts its
// chances and faults none.
class SimFaults{
public:
SimFaults() = default;
explicit SimFaults(std::map<FaultClass, std::set<int>> faulted);

// Counts one more chance of `fault`: whether it is one to fault.
bool next(FaultClass fault);

// The chances of `fault` counted so far, and how many were faulted.
int chances(FaultClass fault) const;
int faulted(FaultClass fault) const;

private:
std::map<FaultClass, FaultOccurrences> m_classes;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* Sim Faults Tests
*
* From: https://github.com/PokemonAutomation/
*
* The fault counters on their own: which chances a counter faults, what
* it counts, that each class counts apart, and the classes' names.
*
*/

#include <format>
#include <map>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Pokemon_SimFaults.h"
#include "Pokemon_SimFaults_Tests.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

struct SimFaultsInput{
std::string name;
UnitTestResult (*body)();
};

struct SimFaultsExpectation{
std::string_view description;
};

struct SimFaultsTestCase{
SimFaultsInput input;
SimFaultsExpectation expectation;
};

class Test_SimFaults : public UnitTest{
public:
explicit Test_SimFaults(SimFaultsTestCase scenario)
: UnitTest("Pokemon::Navigation - Sim faults: " + scenario.input.name)

Check warning on line 44 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
, m_scenario(std::move(scenario))
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
return m_scenario.input.body();
}

private:
SimFaultsTestCase m_scenario;
};

// What `chances` chances of FaultClass::LOST_TAP did with `faulted`
// named, taken as a game's sim takes them, a chance of
// FaultClass::WARP_NO_OP (none named) after each.
struct Counted{
std::vector<int> faulted_at;
int chances = 0;
int faulted = 0;
int other_chances = 0;
int other_faulted = 0;
int never_reached = 0;
};

Counted count(const std::set<int>& faulted, int chances){
SimFaults faults({{FaultClass::LOST_TAP, faulted}});
Counted out;
for (int chance = 1; chance <= chances; chance++){
if (faults.next(FaultClass::LOST_TAP)){
out.faulted_at.push_back(chance);
}
faults.next(FaultClass::WARP_NO_OP);
}
out.chances = faults.chances(FaultClass::LOST_TAP);
out.faulted = faults.faulted(FaultClass::LOST_TAP);
out.other_chances = faults.chances(FaultClass::WARP_NO_OP);
out.other_faulted = faults.faulted(FaultClass::WARP_NO_OP);
out.never_reached = faults.chances(FaultClass::DARK_CAVE);
return out;
}

// "[1, 3]".
std::string numbers_text(const std::vector<int>& numbers){
std::string out;
for (int number : numbers){
out += out.empty() ? std::format("{}", number) : std::format(", {}", number);
}
return std::format("[{}]", out);
}

UnitTestResult expect_counted(const std::set<int>& faulted, int chances, const std::vector<int>& faulted_at){
const Counted counted = count(faulted, chances);
if (counted.faulted_at != faulted_at){
return std::format("Faulted chances {}, expected {}.", numbers_text(counted.faulted_at), numbers_text(faulted_at));
}
if (counted.chances != chances || counted.faulted != (int)faulted_at.size()){
return std::format("Counted {} chances, {} faulted; expected {}, {}.", counted.chances, counted.faulted, chances, faulted_at.size());
}
if (counted.other_chances != chances || counted.other_faulted != 0){
return std::format("The other class counted {} chances, {} faulted; expected {}, 0.", counted.other_chances, counted.other_faulted, chances);
}
if (counted.never_reached != 0){
return std::format("A class never reached counted {} chances.", counted.never_reached);
}
return true;
}

UnitTestResult test_none_named(){
return expect_counted({}, 3, {});
}

UnitTestResult test_named_from_one(){
return expect_counted({1, 3}, 4, {1, 3});
}

UnitTestResult test_never_reached(){
return expect_counted({2, 9}, 3, {2});
}

UnitTestResult test_no_chances(){
return expect_counted({1}, 0, {});
}

UnitTestResult test_class_names(){
std::map<std::string_view, FaultClass> seen;
for (FaultClass fault : fault_classes()){
const std::string_view name = fault_class_name(fault);
if (name == "?" || !seen.emplace(name, fault).second){
return std::format("Fault class {} has no name of its own: {}", static_cast<int>(fault), name);
}
}
return true;
}

const SimFaultsTestCase SIM_FAULTS_CASES[] = {
{.input = {.name = "none named faults none", .body = test_none_named}, .expectation = {.description = "3 chances, none faulted"}},
{.input = {.name = "the named chances are faulted, from 1", .body = test_named_from_one}, .expectation = {.description = "1 and 3 of 4 faulted"}},
{.input = {.name = "a chance never reached is not faulted", .body = test_never_reached}, .expectation = {.description = "2 of 3 faulted, 9 never reached"}},
{.input = {.name = "no chances, nothing counted", .body = test_no_chances}, .expectation = {.description = "0 chances, none faulted"}},
{.input = {.name = "each class has its own name", .body = test_class_names}, .expectation = {.description = "no two classes share a name"}},
};

} // namespace


void add_tests_SimFaults(UnitTestDatabase& database){
for (const SimFaultsTestCase& scenario : SIM_FAULTS_CASES){
database.add<Test_SimFaults>(scenario);
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
Loading
Loading