Skip to content
Draft
33 changes: 33 additions & 0 deletions .github/workflows/logic-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Logic Tests
# Builds and runs SerialProgramsLogicTests (SerialPrograms/cmake/LogicTests.cmake): the unit
# tests that need no Qt, OpenCV, ONNX Runtime, Tesseract or test images. It configures with
# -DPA_CORE_ONLY=ON, so it downloads nothing and keeps testing refactors when the full
# build's third-party downloads fail. Runs on every PR push, drafts included.
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
ubuntu:
runs-on: ubuntu-26.04
steps:
- uses: actions/checkout@v7

- name: Install Ninja
run: |
sudo apt update
sudo apt install ninja-build

- name: Configure
run: cmake -S SerialPrograms -B build-logic -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DPA_CORE_ONLY=ON -DCOMMAND_LINE_EXE=OFF -DPA_LOGIC_TESTS=ON

- name: Build
run: cmake --build build-logic --target SerialProgramsLogicTests --parallel 4

- name: Run the logic tests
run: build-logic/SerialProgramsLogicTests
7 changes: 7 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ option(PA_CORE_ONLY "Build only the GUI-free core (basic microcontroller functio
# listing, so every file would be recompiled on every run.
option(PA_ASSEMBLY_LISTINGS "Write an assembly listing of every source file to Assembly/ (MSVC and clang-cl only)" ON)

# With PA_CORE_ONLY, also build SerialProgramsLogicTests: the unit tests that need no
# Qt, OpenCV, ONNX Runtime, Tesseract or test images. See cmake/LogicTests.cmake.
option(PA_LOGIC_TESTS "With PA_CORE_ONLY, build the logic-only unit tests (SerialProgramsLogicTests)" OFF)

# Enable SDL by giving -DPA_SDL=ON on the CMake command line
option(PA_SDL "Enable SDL library" OFF)

Expand All @@ -111,6 +115,9 @@ if(PA_CORE_ONLY)
set(CMAKE_AUTOUIC OFF)
include(cmake/SourceFiles.cmake)
include(cmake/CoreLib.cmake)
if(PA_LOGIC_TESTS)
include(cmake/LogicTests.cmake)
endif()
if(COMMAND_LINE_EXE)
include(Source/CommandLine/CommandLineExecutable.cmake)
endif()
Expand Down
133 changes: 133 additions & 0 deletions SerialPrograms/Source/Tests/LogicTests/LogicTests_Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* Logic Tests Main
*
* From: https://github.com/PokemonAutomation/
*
* SerialProgramsLogicTests: runs the unit tests of code that needs no Qt, OpenCV,
* ONNX Runtime or Tesseract, and no test images. It is built only with
* -DPA_CORE_ONLY=ON -DPA_LOGIC_TESTS=ON (see cmake/LogicTests.cmake), so it can
* test refactors even when the full build's third-party downloads are broken.
*
* This executable opens no serial port, camera or console: it only links the
* GUI-free CoreLib and the pure sources listed in LOGIC_LIBRARY_SOURCES. It is
* safe to run anywhere, unlike the SerialPrograms app.
*
* Usage: SerialProgramsLogicTests [--filter <substring>]
* Runs every registered test (or those whose name contains <substring>) one at a
* time, prints one line per test and a summary, and returns 1 if any test failed.
*
* A suite joins this runner when every file it includes is pure: add its sources to
* LOGIC_LIBRARY_SOURCES / LOGIC_TEST_SOURCES in cmake/SourceFiles.cmake and its
* add_tests_<Name>() call to make_logic_tests() below. The same suites also stay
* registered in the full test runner (ComputerPrograms_UnitTestRunner.cpp).
*/

#include <exception>
#include <format>
#include <iostream>
#include <string>
#include <string_view>
#include "Common/Cpp/CancellableScope.h"
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "CommonTools/ScreenEvents/ScreenEvents_Tests.h"
#include "CommonTools/ScreenMachine/ScreenMachine_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h"

namespace PokemonAutomation{
namespace{


UnitTestDatabase make_logic_tests(){
UnitTestDatabase database;
Pokemon::add_tests_AutoStory(database);
add_tests_ScreenEvents(database);
add_tests_ScreenMachine(database);
return database;
}


class StdoutLogger : public Logger{
public:
using Logger::log;
virtual void log(const std::string& msg, Color) override{
std::cout << msg << std::endl;
}
};


struct RunCounts{
size_t passed = 0;
size_t failed = 0;
size_t skipped = 0;
};

UnitTestResult run_one(const UnitTest& test, Logger& logger){
CancellableHolder<CancellableScope> scope;
try{
return test.run(logger, scope);
}catch (const Exception& e){
return UnitTestResult(UnitTestResult::FAILED, std::format("Exception: {}", e.to_str()));
}catch (const std::exception& e){
return UnitTestResult(UnitTestResult::FAILED, std::format("std::exception: {}", e.what()));
}
}

void record(const UnitTest& test, const UnitTestResult& result, RunCounts& counts){
switch (result.result){
case UnitTestResult::PASSED:
std::cout << "PASSED: " << test.name() << std::endl;
counts.passed++;
return;
case UnitTestResult::NOT_RUN:
case UnitTestResult::SKIPPED:
std::cout << "SKIPPED: " << test.name() << std::endl;
counts.skipped++;
return;
case UnitTestResult::FAILED:
case UnitTestResult::OOM:
std::cout << "FAILED: " << test.name() << ": " << result.message << std::endl;
counts.failed++;
return;
}
}

int run_logic_tests(std::string_view filter){
StdoutLogger logger;
RunCounts counts;
for (const auto& item : make_logic_tests()){
if (!filter.empty() && !item.first.contains(filter)){
continue;
}
record(*item.second, run_one(*item.second, logger), counts);
}
std::cout << "Tests Finished:"
<< "\n Passed: " << counts.passed
<< "\n Failed: " << counts.failed
<< "\n Skipped: " << counts.skipped << std::endl;
if (counts.passed + counts.failed == 0){
std::cout << "No test ran." << std::endl;
return 1;
}
return counts.failed > 0 ? 1 : 0;
}


} // namespace
} // namespace PokemonAutomation


int main(int argc, char* argv[]){
std::string_view filter;
for (int c = 1; c < argc; c++){
std::string_view arg = argv[c];
if (arg == "--filter" && c + 1 < argc){
filter = argv[++c];
}else{
std::cerr << "Usage: " << argv[0] << " [--filter <substring>]" << std::endl;
return 2;
}
}
return PokemonAutomation::run_logic_tests(filter);
}
30 changes: 30 additions & 0 deletions SerialPrograms/cmake/LogicTests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SerialProgramsLogicTests: the unit tests that need no Qt, OpenCV, ONNX Runtime,
# Tesseract or test images.
#
# Built only with -DPA_CORE_ONLY=ON -DPA_LOGIC_TESTS=ON, so it configures and builds
# even when the full build's third-party downloads fail, e.g.:
# cmake -S SerialPrograms -B build-logic -G Ninja -DPA_CORE_ONLY=ON -DCOMMAND_LINE_EXE=OFF -DPA_LOGIC_TESTS=ON
# cmake --build build-logic --target SerialProgramsLogicTests
# build-logic/SerialProgramsLogicTests [--filter <substring>]
#
# It links CoreLib plus LogicLib, a static library of ${LOGIC_LIBRARY_SOURCES}
# (see SourceFiles.cmake). The full build does not use this file.
#
# Requires CoreLib.cmake (CoreLib and pa_apply_gui_free_target_properties()).

add_library(LogicLib STATIC ${LOGIC_LIBRARY_SOURCES})
pa_apply_gui_free_target_properties(LogicLib)

add_executable(SerialProgramsLogicTests ${LOGIC_TEST_SOURCES})
pa_apply_gui_free_target_properties(SerialProgramsLogicTests)
target_link_libraries(SerialProgramsLogicTests PRIVATE LogicLib CoreLib)

foreach(logic_target LogicLib SerialProgramsLogicTests)
if (MSVC)
target_compile_options(${logic_target} PRIVATE /W4 /WX /external:W0 /utf-8 /wd5054 /wd4505)
elseif(APPLE)
target_compile_options(${logic_target} PRIVATE -Wall -Wextra -Wpedantic -Werror -Wshorten-64-to-32)
else()
target_compile_options(${logic_target} PRIVATE -Wall -Wextra -Wpedantic -Werror -fno-strict-aliasing)
endif()
endforeach()
56 changes: 56 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3257,3 +3257,59 @@ file(GLOB LIBRARY_SOURCES
Source/VideoGames/ZeldaTotK/ZeldaTotK_Settings.cpp
Source/VideoGames/ZeldaTotK/ZeldaTotK_Settings.h
)

# Logic tests (-DPA_CORE_ONLY=ON -DPA_LOGIC_TESTS=ON, see cmake/LogicTests.cmake):
# unit tests that build and run with no Qt, OpenCV, ONNX Runtime or Tesseract.
# A file joins these lists only when everything it includes is pure as well.
# These files stay in LIBRARY_SOURCES above too; the lists below only add a
# second, smaller target.

# Pure sources the logic tests need beyond CORE_LIBRARY_SOURCES.
# The kernels are the portable *_Default versions: the logic target turns on no
# run-time ISA dispatch, so the SIMD versions are never called.
set(LOGIC_LIBRARY_SOURCES
../Common/Cpp/Rectangle.tpp
Source/CommonFramework/ImageTools/FloatPixel.cpp
Source/CommonFramework/ImageTools/ImageBoxes.cpp
Source/CommonFramework/ImageTools/ImageDiff.cpp
Source/CommonFramework/ImageTools/ImageStats.cpp
Source/CommonFramework/ImageTypes/ImageHSV32.cpp
Source/CommonFramework/ImageTypes/ImageViewHSV32.cpp
Source/CommonFramework/VideoPipeline/VideoOverlay.cpp
Source/CommonFramework/VideoPipeline/VideoOverlayTypes.cpp
Source/CommonTools/Images/SolidColorTest.cpp
Source/CommonTools/ScreenEvents/ScreenEvents.cpp
Source/CommonTools/ScreenMachine/ScreenMachine.cpp
Source/Kernels/BinaryMatrix/Kernels_BinaryMatrix.cpp
Source/Kernels/BinaryMatrix/Kernels_BinaryMatrix_Core_64xH_Default.cpp
Source/Kernels/BinaryMatrix/Kernels_PackedBinaryMatrixCore.tpp
Source/Kernels/BinaryMatrix/Kernels_SparseBinaryMatrixCore.tpp
Source/Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness.cpp
Source/Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness_Default.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqr.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqr_Default.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqr_TestInfra.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqrDev.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqrDev_Default.cpp
Source/Kernels/ImageStats/Kernels_ImagePixelSumSqrDev_TestInfra.cpp
Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.cpp
Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp
Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp
Source/Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions.cpp
Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp
Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.cpp
)

# The logic test suites and their runner. Each suite here is also registered in
# make_logic_tests() (Source/Tests/LogicTests/LogicTests_Main.cpp).
set(LOGIC_TEST_SOURCES
Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp
Source/CommonTools/ScreenMachine/ScreenMachine_Tests.cpp
Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.cpp
Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp
Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp
Source/Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions_Tests.cpp
Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp
Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp
Source/Tests/LogicTests/LogicTests_Main.cpp
)