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

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

1#pragma once
2#include <limits>
3
4namespace
5{
6 void TestBoltAnimation()
7 {
8 using namespace LightningStrike;
9 for(uint32_t seed:{0u,123u,456u,32767u,0xffffffffu})for(int fps:{24,30,60,144,240,1000})
10 {
11 Animation strike;strike.Start(seed);
12 float previousGrowth=0;bool bright=false;int flashes=0;
13 while(strike.inuse)
14 {
15 strike.Update(1./fps);
16 Animation direct;direct.Start(seed);direct.Update(strike.age);
17 Check(strike.seed==seed,"animation changed strike identity and would rebuild its branches");
18 Check(std::abs(strike.flashBrightness-direct.flashBrightness)<.00001f
19 && strike.growth==direct.growth,"flash depends on frame rate/history");
20 Check(strike.growth>=previousGrowth && strike.growth<=1
21 && strike.flashBrightness>=0 && strike.flashBrightness<=1,"invalid/retracted lightning growth or pulse");
22 previousGrowth=strike.growth;
23 if(strike.flashBrightness>.45f && !bright)++flashes;
24 bright=strike.flashBrightness>.45f;
25 if(strike.age>=strike.growthSeconds && strike.inuse)
26 Check(strike.growth==1 && strike.flashBrightness>0,"formed branches vanished between return strokes");
27 }
28 // A 100 ms four-stroke burst cannot expose every peak at 24 fps.
29 // Verify every peak at sufficient temporal resolution, while all
30 // frame rates must evaluate the same continuous envelope.
31 Check((fps<1000 || flashes==strike.strokeCount) && strike.flashBrightness==0,
32 "wrong return-stroke count or stuck flash");
33 }
34 Animation strike;strike.Start(456);
35 strike.Update(std::numeric_limits<double>::quiet_NaN());strike.Update(-1);
36 Check(strike.age==0 && strike.seed==456,"invalid frame time advanced strike");
37 strike.Update(100);
38 Check(!strike.inuse && strike.flashBrightness==0,"long frame replayed stale flashes");
39 strike.Start(789);strike.Update(strike.strokes[0].start+strike.strokes[0].attack+strike.strokes[0].hold*.25);
40 const auto held=strike.flashBrightness;strike.Update(strike.strokes[0].hold*.5);
41 Check(held==1 && strike.flashBrightness==held,"return stroke has no readable hold");
42 strike.Start(900);
43 Check(strike.seed==900 && strike.age==0 && strike.growth==0 && strike.flashBrightness==0,
44 "new strike inherited the previous animation");
45 // Exhaust every seed emitted by BOLT and modern weather. Assert both
46 // individual limits and the population mean, not one lucky example.
47 double durationSum=0,countSum=0,minimum=1,maximum=0;unsigned irregular=0;
48 constexpr uint32_t population=32768;
49 for(uint32_t seed=0;seed<population;++seed)
50 {
51 Animation sample;sample.Start(seed);
52 Check(sample.strokeCount>=Timing::MinStrokes && sample.strokeCount<=Timing::MaxStrokes,
53 "burst ignored shared stroke-count bounds");
54 Check(sample.Duration()>=Timing::MinDurationSeconds && sample.Duration()<=Timing::MaxDurationSeconds,
55 "complete flash exceeded the shared duration budget");
56 durationSum+=sample.Duration();countSum+=sample.strokeCount;
57 minimum=(std::min)(minimum,sample.Duration());maximum=(std::max)(maximum,sample.Duration());
58 if(sample.strokeCount>=3)
59 {
60 const double first=sample.strokes[1].start-sample.strokes[0].start;
61 const double second=sample.strokes[2].start-sample.strokes[1].start;
62 if(std::abs(first-second)>.002)++irregular;
63 }
64 for(int i=0;i<sample.strokeCount;++i)
65 {
66 const auto& pulse=sample.strokes[i];
67 Check(pulse.attack>=Timing::Attack.minimumSeconds && pulse.hold>=Timing::Hold.minimumSeconds
68 && pulse.decay>=Timing::Decay.minimumSeconds,"flash lost its minimum readable phase");
69 if(i)Check(pulse.start-sample.strokes[i-1].End()>=Timing::Gap.minimumSeconds-1e-12,
70 "return strokes overlapped or erased their gap");
71 auto peak=sample;peak.Update(pulse.start+pulse.attack+pulse.hold*.5);
72 Check(std::abs(peak.flashBrightness-pulse.peak)<.00001f,"seeded stroke missed its peak");
73 if(i)
74 {
75 auto gap=sample;gap.Update((sample.strokes[i-1].End()+pulse.start)*.5);
76 Check(std::abs(gap.flashBrightness-Timing::AfterglowBrightness)<.00001f,
77 "branches did not hold through a return-stroke gap");
78 }
79 }
80 auto end=sample;end.Update(sample.Duration());
81 Check(!end.inuse && end.flashBrightness==0 && end.growth==1,"burst did not end at its complete duration");
82 }
83 Check(std::abs(durationSum/population-Timing::MeanDurationSeconds)<.002,"mean burst duration drifted from shared tuning");
84 Check(std::abs(countSum/population-(Timing::MinStrokes+Timing::MaxStrokes)*.5)<.02,
85 "flash count is not balanced across the shared stroke range");
86 std::cout<<"Timing population: "<<population<<" seeds; flashes="<<countSum/population
87 <<"; mean="<<durationSum/population<<" s; range="<<minimum<<"-"<<maximum<<" s; irregular="
88 <<double(irregular)/population<<'\n';
89 Check(minimum<maximum && (Timing::MinStrokes<3 || irregular>population*.65),
90 "bursts lost their range or irregular cadence");
91 }
92}
void Start(uint32_t strikeSeed, double durationSeconds=0)
std::array< Stroke, Timing::MaxStrokes > strokes
void Update(double elapsed)