Files
Dreierpack-Manager/tests/calendar_epoch_tests.cpp
T
ClaudeandClaude 9870d90d65 Tests share one check helper; stale references in comments corrected
116 test files had their own identical copy of check(bool, const char*); they
use bmh::test_support::check from src/testing/check.hpp. Seven comments pointed
to documents that no longer exist or named a former author.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-11 14:52:42 +01:00

56 lines
3.2 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#include "game/calendar.hpp"
#include "game/week.hpp"
#include "game/save.hpp"
#include "formats/executable_layout.hpp"
#include "formats/le.hpp"
#include "ui/desk_state.hpp"
#include <iostream>
#include <stdexcept>
#include <utility>
#include "testing/check.hpp"
using bmh::test_support::check;
bmh::LeImage image(int year,int weekday) {
constexpr uint32_t base=0x10000;
constexpr size_t image_size=0xb0000;
auto exe=bmh::LeImage::from_bytes(base,bmh::Bytes(image_size,0));
const uint8_t setup[]={0xc6,0x44,0x24,0x04,uint8_t(weekday),0xc6,0x44,0x24,0x0c,0x07,0xc7,0x04,0x24,
uint8_t(year),uint8_t(year>>8),0,0};
exe.patch(bmh::layout::CalendarDateSetup,setup,sizeof setup);
const uint8_t months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
exe.patch(bmh::layout::DeskState_DeskDate_RuntimeField,months,sizeof months);
return exe;
}
int main()try {
for(const auto [year,day]:{std::pair{1995,5},std::pair{2026,2}}) {
auto exe=image(year,day);bmh::install_calendar_rules(exe);
check(bmh::weekday_code(0)==day && bmh::weekday_code(7)==day,"engine weekday ignores edition epoch");
const auto date=bmh::ui::desk_date(0,exe);
check(date.year==year && date.month==7 && date.day==1 && date.weekday==day,"desktop date ignores edition epoch");
bmh::GameState g;
g.memory.add_block(bmh::layout::ManagerCount,1);
const auto header=bmh::save_header(g);
const auto expected="01.07."+std::to_string(year);
check(std::string(reinterpret_cast<const char*>(header.data()+1))==expected,"save-list date ignores edition epoch");
const auto rollover=bmh::ui::desk_date(184,exe);
check(rollover.year==year+1 && rollover.month==1 && rollover.day==1,"edition year rollover incorrect");
}
auto bad=image(2026,7);bool rejected=false;
try{bmh::install_calendar_rules(bad);}catch(const bmh::FormatError&){rejected=true;}
check(rejected && bmh::calendar_epoch().year==2026 && bmh::calendar_epoch().weekday==2,"invalid epoch partially installed");
auto different=image(1995,5);const uint8_t unknown=0;different.patch(bmh::layout::CalendarDateSetup,&unknown,1);
rejected=false;try{bmh::calendar_epoch_from_executable(different);}catch(const bmh::FormatError&){rejected=true;}
check(rejected,"unknown date routine was guessed");
auto english=image(1995,5);
const uint8_t english_setup[]={0xc6,0x45,0xf6,5,0xc6,0x45,0xf7,7,0x66,0xc7,0x45,0xf8,0xcb,7};
english.patch(bmh::layout::CalendarDateSetup,english_setup,sizeof english_setup);
const auto english_epoch=bmh::calendar_epoch_from_executable(english);
check(english_epoch.year==1995&&english_epoch.weekday==5,"translated English date operands rejected");
const uint8_t invalid_weekday=7;
english.patch(bmh::layout::CalendarDateSetup+3,&invalid_weekday,1);
rejected=false;try{bmh::calendar_epoch_from_executable(english);}catch(const bmh::FormatError&){rejected=true;}
check(rejected,"invalid translated English weekday accepted");
std::cout<<"Edition calendar epoch, save header and year rollover passed\n";return 0;
}catch(const std::exception&e){std::cerr<<e.what()<<'\n';return 1;}