1#pragma once
2#include "../Viewscreen/BiometerDiagnostics.h"
3#include "../Viewscreen/BiometerState.h"
4#include <limits>
5
7{
19 return s;
20}
21
22static void RunBiometerGLoadTests()
23{
25 for (const float gy : {-3.f, -2.f, -1.f, -.001f, 0.f})
27 "Normal negative-G response changed");
28 for (const float gy : {.001f, .5f, .999f})
30 for (const float gy : {1.f, 1.15f, 2.f, 6.f, 12.f})
32 "Ground, hover, ascent or re-entry positive-G response changed");
33
34
35
36 for (const float gy : {-3.001f, -19.999f, -20.f, -20.001f, -100.f, -3236.94507f,
37 -std::numeric_limits<float>::max()})
38 {
40 "Extreme negative G escaped the bounded reduction");
41 for (const float otherG : {0.f, -2.f, 2.f, -1000.f, 1000.f})
42 {
43 auto s = HealthyBiometerSample();
44 s.gy = gy; s.gx = s.gz = otherG;
46 (1.f + .15f * std::fabs(s.gx)) * (1.f + .1f * std::fabs(s.gz));
47 s.vaso = s.target - s.base;
48 const auto step =
Advance(s.previous, s.target, s.base, s.maximum, s.dt,
false, .8f);
49 s.integrationStart = step.start; s.current = s.integrated = step.value;
51 "Finite negative-G load still generated an invalid target or BPM");
52 s.simulator = true;
54 }
55 }
56
57
58 float bpm = 110.6128f;
59 for (const float gy : {1.15f, -2.f, -3.f, -20.f, -100.f, -3.f, -2.f, 0.f, .5f, 6.f, 1.15f})
60 {
62 const float next =
Advance(bpm, target, 80.f, 180.f, .0165832f,
false, .8f).
value;
63 Check(std::isfinite(next) && next > 0 && next <= 180,
64 "Changing G direction corrupted the BPM");
65 Check((target > bpm && next > bpm && next <= target) ||
66 (target < bpm && next < bpm && next >= target) || target == bpm,
67 "BPM failed to follow the bounded target across a G transition");
68 bpm = next;
69 }
70 for (const float invalid : {std::numeric_limits<float>::quiet_NaN(),
71 std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity()})
72 {
74 Check(!std::isfinite(target), "Nonfinite G input was hidden by saturation");
75 Check(
Advance(110.6128f, target, 80, 180, .0165832f,
false, .8f).value == 110.6128f,
76 "Nonfinite G input escaped the existing BPM guard");
77 }
78 puts("PASS: Signed G response, negative-G saturation, combined axes, transitions and invalid-input guard.");
79}
80
81static void RunBiometerStateTests()
82{
84 const float nan = std::numeric_limits<float>::quiet_NaN();
85 const float inf = std::numeric_limits<float>::infinity();
86
87
88 auto step =
Advance(80.3131485f, -9.01147443e10f, 64.1711197f, 172.729996f, .0694210976f,
false, .8f);
89 Check(step.value == 80.3131485f, "Reposition's negative target corrupted a healthy BPM");
90 auto s = HealthyBiometerSample();
91 s.previous = step.start; s.current = s.integrated = step.value;
92 s.target = s.vaso = -9.01147443e10f; s.gy = -3236.94507f;
95 "Protecting BPM hid the raw reposition fault from diagnostics");
96 step =
Advance(145.176224f, inf, 64.1711197f, 172.729996f, .00379849994f,
false, .8f);
97 Check(step.value == 145.176224f, "Overflowed reposition target changed a healthy BPM");
98 step =
Advance(80.3131485f, 70.1450043f, 64.1711197f, 172.729996f, .016f,
false, .8f);
99 Check(step.value > 70.1450043f && step.value < 80.3131485f, "BPM did not resume normal recovery after reposition");
100 step =
Advance(-312793120.f, 70.f, 64.f, 172.73f, .016f,
false, .8f);
101 Check(step.value >= 64 && step.value <= 70, "Previously corrupted BPM was not repaired immediately");
102
103 Check(
Advance(160, 60, 80, 180, 60,
false, .8f).value == 60,
"Long frame overshot the recovery target");
104 Check(std::fabs(
Advance(90, 100, 80, 180, .016f,
false, .8f).value - 90.032f) < .0001f,
105 "Normal heartbeat acceleration changed");
106 Check(std::fabs(
Advance(0, 100, 80, 180, .016f,
false, .8f).value - 80.064f) < .0001f,
107 "Normal wake-up heartbeat changed");
108 Check(
Advance(90, 500, 80, 180, 100,
false, .8f).value == 180,
"High target exceeded character maximum");
109 Check(
Advance(90, 100, 80, 180, .016f,
true, .8f).value == 0,
"Dead character's heartbeat restarted");
110 step =
Advance(nan, nan, nan, nan, nan,
false, nan);
111 Check(std::isfinite(step.value) && step.value > 0, "Invalid stored state escaped recovery");
112 Check(
Effort(-312793120.f, 64, 172.73f) == 0 &&
Effort(500, 64, 172.73f) == 1 &&
113 Effort(80, 80, 80) == 0 &&
Effort(nan, 80, 180) == 0,
"Heartbeat audio effort escaped its valid range");
114
115 float frame =
AdvanceFrame(-2.47487104e9f, .016f, 80, 29);
116 Check(frame >= 0 && frame < 29, "Corrupt animation phase was not repaired");
118 Check(frame >= 0 && frame < 29, "Animation did not wrap across multiple heartbeat cycles");
120 "Invalid animation input escaped normalization");
121 puts("PASS: Biometer reposition replay, recovery, normal timing, long frames, audio bounds, and animation wrapping.");
122}
123
124static void RunBiometerDiagnosticTests()
125{
126 RunBiometerGLoadTests();
127 RunBiometerStateTests();
129 auto s = HealthyBiometerSample();
130 Check(!
Faults(s),
"Healthy simulated BPM generated a diagnostic");
131 s.current = s.maximum; s.integrationStart = s.maximum; s.integrated = 181; s.target = 500;
132 Check(!
Faults(s),
"Normal upper BPM cap was reported as a fault");
133 s = HealthyBiometerSample(); s.previous = 0; s.integrationStart = s.integrated = s.current = 80;
134 Check(!
Faults(s),
"Initial wake-up BPM was reported as a fault");
135 s.current = s.integrated = s.target = 0; s.dead = true;
136 Check(!
Faults(s),
"Intentional zero BPM at death was reported as a fault");
137
138
139 s = HealthyBiometerSample(); s.gy = -100; s.gx = s.gz = 1000;
140 s.target = s.base * (1 + .05f * s.gy) * (1 + .15f * s.gx) * (1 + .1f * s.gz);
141 s.vaso = s.target - s.base;
142 s.current = s.integrated = s.previous + (s.target - s.previous) * s.dt / 20;
144 std::string(
Cause(s,
Faults(s))) ==
"negative-g-multiplier",
"Negative G sign reversal was not diagnosed");
145 s = HealthyBiometerSample(); s.previous = s.integrationStart = 160; s.target = 60; s.dt = 60;
146 s.current = s.integrated = s.previous + (s.target - s.previous) * s.dt / 20;
148 std::string(
Cause(s,
Faults(s))) ==
"frame-step-overshoot",
"Long-frame overshoot was not diagnosed");
149 s = HealthyBiometerSample(); s.previous = s.current = -14449059;
150 Check(std::string(
Cause(s,
Faults(s))) ==
"carried-invalid-bpm",
"Previously corrupted BPM was not identified");
151 s = HealthyBiometerSample(); s.maximum = 0;
152 Check(
Faults(s) &
Maximum,
"Invalid model maximum was not diagnosed");
153 s = HealthyBiometerSample(); s.current = std::numeric_limits<float>::quiet_NaN();
155 s = HealthyBiometerSample(); s.target = s.vaso = std::numeric_limits<float>::infinity();
156 Check(std::string(
Cause(s,
Faults(s))) ==
"nonfinite-g-load-term",
"Overflowed G term was not identified");
157 s = HealthyBiometerSample(); s.pressure = std::numeric_limits<float>::quiet_NaN();
158 Check(
Faults(s) &
Inputs,
"Nonfinite calculation input escaped validation");
159 s = HealthyBiometerSample(); s.base = -1;
160 Check(
Faults(s) &
Baseline,
"Negative baseline escaped validation");
161 s = HealthyBiometerSample(); s.heartFrame = -12;
162 Check(
Faults(s) &
HeartFrame,
"Invalid animation state escaped validation");
163
164 struct Line { std::string text; bool error; };
165 std::vector<Line> lines;
166 const auto emit = [&](const char* text, bool error) { lines.push_back({text, error}); };
168 for (unsigned i = 0; i < 3; ++i)
169 {
170 s = HealthyBiometerSample(); s.time = i; monitor.
Observe(s, i, emit);
171 }
172 Check(lines.empty(), "Healthy frames were written to disk");
173 s.current = s.integrated = -14449059; s.time = 3;
175 Check(lines.size() == 6 && !lines.front().error && lines.back().error && lines.back().text.size() <= 190,
176 "Fault context was not emitted before the bounded server summary");
177 Check(lines[1].text.find("time=0 ") != std::string::npos &&
178 lines[4].text.find("current=-14449059 ") != std::string::npos,
179 "Biometer report lost preceding healthy frames or the raw negative value");
180 monitor.
Observe(s, 60099, emit);
181 Check(lines.size() == 6, "Sustained fault spammed the log each frame");
182 monitor.
Observe(s, 60100, emit);
183 Check(lines.size() == 12 && lines.back().error, "Persistent fault was never reported again");
184 monitor.
Observe(HealthyBiometerSample(), 60101, emit);
185 Check(lines.back().text.find("BIOMETER recovered") == 0 && !lines.back().error,
186 "Recovery was not recorded locally");
187 monitor.
Observe(s, 60102, emit);
188 Check(lines.back().error && lines.size() == 19, "A new fault episode was not recorded");
189 puts("PASS: Biometer bounds, negative-G and long-frame diagnosis, finite values, history, throttling, and recovery.");
190}
void Observe(const Sample &sample, std::uint64_t now, Emit emit)
unsigned Faults(const Sample &s)
const char * Cause(const Sample &s, unsigned faults)
float Effort(float current, float baseline, float maximum)
Step Advance(float previous, float target, float baseline, float maximum, float dt, bool dead, float wakeFactor)
float VerticalGMultiplier(float verticalG)
float AdvanceFrame(float frame, float dt, float bpm, unsigned frameCount)