Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/ModuleSchedulerTests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Modules/ModuleScheduler.h"
2
3static void ModuleSchedulingRegressions()
4{
5 for (size_t count : {size_t(1),size_t(7),size_t(50)})
6 for (int fps : {15,30,60,144,240})
7 {
8 ModuleScheduler scheduler;
9 std::vector<double> previous(count), longest(count);
10 std::vector<int> calls(count);
11 double now=0; bool ordered=true, timing=true, gaps=true, one=true;
12 int dispatched=0;
13 for (size_t frame=0;frame<count*5;++frame)
14 {
15 const float dt=float(1./fps*(frame%3==0 ? 1.7 : frame%3==1 ? .3 : 1.));
16 now+=dt;
17 for(auto& gap:longest) gap=(std::max)(gap,double(dt));
18 int thisFrame=0;
19 scheduler.Advance(dt,count,[&](size_t index,float elapsed,float maxFrame) {
20 ++thisFrame; ordered&=index==size_t(dispatched)%count; ++dispatched;
21 timing&=std::fabs(elapsed-(now-previous[index]))<1e-6;
22 gaps&=std::fabs(maxFrame-longest[index])<1e-7;
23 previous[index]=now; longest[index]=0; ++calls[index];
24 });
25 one&=thisFrame==1;
26 }
27 Check(one&&ordered,"scheduler dispatches exactly one module per frame in registration order");
28 Check(std::all_of(calls.begin(),calls.end(),[](int n){return n==5;}),"every module gets one turn per cycle, including 50-module loads");
29 Check(timing&&gaps,"variable FPS preserves accumulated elapsed and largest actual frame gap independently");
30 }
31 {
32 ModuleScheduler scheduler; int calls=0; float elapsed=0, gap=0; size_t selected=99;
33 const auto run=[&](size_t i,float dt,float frame){++calls;selected=i;elapsed=dt;gap=frame;};
34 scheduler.Advance(.1f,7,run); scheduler.Advance(0,7,run);
35 Check(calls==1,"zero-time frame does not consume a module turn");
36 scheduler.Advance(1.f,7,run);
37 Check(selected==1&&std::fabs(elapsed-1.1f)<1e-6&&gap==1,"a real stalled frame reaches its scheduled module without a catch-up burst");
38 for(int i=0;i<7;++i) scheduler.Advance(.1f,7,run);
39 Check(selected==1&&std::fabs(elapsed-.7f)<1e-6&&gap==.1f,"gap history clears after each module consumes it");
40 scheduler.Advance(.1f,2,run);
41 Check(selected==0&&elapsed==.1f,"module-count change restarts without carrying old timing debt");
42 for(float invalid:{-1.f,std::numeric_limits<float>::quiet_NaN(),std::numeric_limits<float>::infinity()})
43 {
44 const int before=calls; scheduler.Advance(invalid,2,run);
45 Check(calls==before,"invalid time never reaches a module");
46 scheduler.Advance(.1f,2,run); Check(selected==0&&elapsed==.1f,"invalid-time recovery starts fresh");
47 }
48 const int before=calls; scheduler.Advance(.1f,0,run); scheduler.Reset();
49 Check(calls==before,"empty or disabled dispatcher runs no modules");
50 }
51 {
52 Fixture f; ourcockpit.power=0; f.vmu.FrameMove(.1f); played.clear();
54 Command queued; queued.name="GPWSMode6Fifty"; f.bus->commandStream.push_back(queued);
55 f.vmu.FrameMove(2.5f);
56 Check(played==std::vector<SOUNDS>{SOUND_AURALUNITOKAY}&&f.bus->vmuHold==1.f,
57 "VMU warm-up starts a full phrase even with a long accumulated turn");
58 f.vmu.FrameMove(.5f); Check(played.size()==1,"scheduled VMU does not overlap its warm-up recording");
59 f.vmu.FrameMove(.5f); Check(played.back()==SOUND_GPWSFIFTY,"queued callout follows the completed warm-up phrase");
60 }
61 // Exercise real GPWS/VMU in separate scheduler slots with physics wind
62 // published every frame. Detection timing is independent of call frequency.
63 for(size_t count:{size_t(7),size_t(50)}) for(int fps:{30,60,144})
64 {
65 Fixture f; ModuleScheduler scheduler; double now=0;
66 f.bus->RadioAltitudeKm=.3f; playerships[0].geardown=true;
67 bool warned=false;
68 for(int frame=0;frame<fps*10;++frame)
69 {
70 const float dt=1.f/fps; now+=dt;
71 auto& wind=f.bus->WindshearWind;
72 wind.valid=true; wind.sequence=frame+1; wind.seconds=now;
73 wind.vehicle=playerships[0].vehicleId; wind.simulator=playerships[0].isSimulator;
74 wind.up={0,0,1}; wind.forward={0,1,0};
75 wind.windToMs={0,float(2*(std::min)(now,5.)),0};
76 scheduler.Advance(dt,count,[&](size_t index,float elapsed,float maxFrame) {
77 if(index==2) { f.gpws.maxFrameSeconds=maxFrame; f.gpws.FrameMove(elapsed); warned|=f.bus->GPWS.Alert==Bus::Gpws::Mode7_Windshear_Warning; }
78 if(index==5) { f.vmu.maxFrameSeconds=maxFrame; f.vmu.FrameMove(elapsed); }
79 });
80 }
81 Check(warned,"scheduled GPWS detects sustained signed headwind loss");
82 Check(std::find(played.begin(),played.end(),SOUND_GPWSWINDSHEARWARNING)!=played.end()&&stopped.empty(),
83 "scheduled VMU plays complete windshear phrases without restarting speech");
84 Check(f.bus->GPWS.Alert!=Bus::Gpws::Mode7_Windshear_Warning&&f.bus->commandStream.empty(),
85 "scheduled recovery clears warning and stale queued audio");
86 }
87}
@ SOUND_AURALUNITOKAY
Definition Sound.h:114
@ SOUND_GPWSFIFTY
Definition Sound.h:57
@ SOUND_GPWSWINDSHEARWARNING
Definition Sound.h:66
void Advance(float frameSeconds, size_t count, Dispatch dispatch)
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
Scockpit ourcockpit
Definition globals.cpp:114
@ Mode7_Windshear_Warning
Definition Bus.h:193
std::string name
Definition Command.h:11
float power
Definition globals.h:599
unsigned short vehicleId
Definition globals.h:537