1#include "../EarPressure.h"
2#include <cstdlib>
3#include <iostream>
4#include <limits>
5
6static void Check(bool condition, const char* message)
7{
8 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
9}
10
11static int Ramp(int hz, float speed)
12{
14 ear.Update(true, 1, 1000.0f, 30, 1, 0.0f);
15 int pops = 0;
16 for (int i = 1; i <= hz * 60; ++i)
17 pops += ear.Update(true, 1, 1000.0f + speed * i / hz, 30, 1, 1.0f / hz);
18 return pops;
19}
20
21static void CheckPopRelease(int hz, float popFrameSeconds)
22{
24 ear.Update(true, 1, 2576, 30, 1, 0);
25 for (int i = 0; i < hz; ++i)
26 ear.Update(true, 1, 2577, 30, 1, 1.0f / hz);
27 const float beforePop = ear.Muffle();
28 Check(beforePop > 0.49f, "muffle built up before release test");
29 Check(ear.Update(true, 1, 2580, 30, 1, popFrameSeconds), "pressure step triggers pop");
30 Check(ear.Muffle() == beforePop, "even a long pop frame must preserve the filter for playback");
31 float previous = beforePop;
32 for (int i = 1; i <= hz / 2; ++i)
33 {
34 Check(!ear.Update(true, 1, 2580, 30, 1, 1.0f / hz), "release does not trigger another pop");
35 const float elapsed = static_cast<float>(i) / hz;
36 Check(ear.Muffle() >= 0 && ear.Muffle() <= previous, "release is monotonic and bounded");
37 if (elapsed <= 0.1f)
38 Check(ear.Muffle() > beforePop * 0.75f, "pop begins audibly muffled rather than clearing instantly");
39 if (elapsed >= 0.15f && elapsed <= 0.2f)
40 Check(ear.Muffle() > 0 && ear.Muffle() < beforePop * 0.7f, "filter relaxes during release");
41 if (elapsed >= 0.4f)
42 Check(ear.Muffle() == 0, "filter fully removed within half a second");
43 previous = ear.Muffle();
44 }
45 ear.Update(true, 1, 2581, 30, 1, 0.1f);
46 Check(ear.Muffle() > 0, "new pressure mismatch can build muffle after release");
47 Check(ear.Update(true, 1, 2584, 30, 1, 0.1f), "second pop starts another release");
48 ear.Reset();
49 Check(ear.Muffle() == 0, "reset cancels an active release immediately");
50 ear.Update(true, 2, 2500, 30, 1, 0.1f);
51 ear.Update(true, 2, 2500, 30, 1, 0.1f);
52 Check(ear.Muffle() == 0, "reset release cannot leak into a new character");
53}
54
56{
57 for (int hz : {20, 30, 60, 120})
58 for (float popFrameSeconds : {1.0f / hz, 0.4f})
59 CheckPopRelease(hz, popFrameSeconds);
60 std::cout << "PASS: pop keeps the filter, then smoothly clears within 0.4 s at 20/30/60/120 Hz.\n";
61 for (unsigned seed = 0; seed < 1000; ++seed)
62 {
63 EarPressure male(seed), female(seed), child(seed), childFemale(seed);
64 male.Update(true, 1, 1000, 30, 1, 0);
65 female.Update(true, 1, 1000, 30, 2, 0);
66 child.Update(true, 1, 1000, 12, 1, 0);
67 childFemale.Update(true, 1, 1000, 12, 2, 0);
68 Check(male.Baseline() >= 2 && male.Baseline() <= 3, "adult male range");
69 Check(female.Baseline() >= 1.8f && female.Baseline() <= 2.6f, "adult female range");
70 Check(child.Baseline() >= 1.5f && child.Baseline() <= 2.2f, "child range");
71 Check(child.Baseline() == childFemale.Baseline(), "child takes precedence over sex");
72 }
74 Check(!ear.Update(true, 1, 1000, 30, 1, 0.1f), "spawn must equalize silently");
75 const float baseline = ear.Baseline(), threshold = ear.Threshold();
76 for (int i = 0; i < 600; ++i)
77 Check(!ear.Update(true, 1, 1000, 30, 1, 1.0f / 60), "steady pressure cannot pop");
78 Check(ear.Threshold() == threshold, "threshold must not redraw per frame");
79 for (int i = 0; i < 60; ++i)
80 Check(!ear.Update(true, 1, 1001, 30, 1, 1.0f / 60), "subthreshold pressure");
81 Check(ear.Muffle() > 0.49f && ear.Muffle() <= 0.5f, "gradual muffle buildup");
82 Check(ear.Update(true, 1, 1004, 30, 1, 0.05f), "compression pops");
83 Check(ear.Muffle() > 0.49f, "pop retains the filter for playback before release starts");
84 Check(ear.Baseline() == baseline, "baseline stable across pops");
85 Check(std::fabs(ear.Threshold() - baseline) <= 0.200001f, "per-pop variance bounded");
86 Check(!ear.Update(true, 1, 900, 30, 1, 0.1f), "rapid step cooldown");
87 Check(ear.Update(true, 1, 900, 30, 1, 0.3f), "decompression pops after cooldown");
88 for (int i = 0; i < 60; ++i)
89 Check(!ear.Update(true, 1, 900, 30, 1, 0.1f), "no queued burst after a large step");
90 Check(ear.Muffle() == 0, "flat response restored after equalization");
91 Check(!ear.Update(true, 2, 500, 30, 1, 0.1f), "character change equalizes silently");
92 Check(!ear.Update(false, 2, 900, 30, 1, 0.1f) && ear.Muffle() == 0, "death/menu reset");
93 Check(!ear.Update(true, 2, 900, 30, 1, 0.1f), "respawn equalizes silently");
94 Check(!ear.Update(true, 2, 100, 30, 1, 2.0f), "suspend suppresses false pop");
95 Check(!ear.Update(true, 2, 100, 30, 1, 0.1f), "resume uses new pressure");
96 Check(!ear.Update(true, 2, -1, 30, 1, 0.1f), "reject negative pressure");
97 Check(!ear.Update(true, 2, std::numeric_limits<float>::quiet_NaN(), 30, 1, 0.1f), "reject NaN");
98 Check(!ear.Update(true, 2, std::numeric_limits<float>::infinity(), 30, 1, 0.1f), "reject infinity");
99 Check(!ear.Update(true, 2, 0, 30, 1, 0.1f), "zero pressure initializes without error");
100 Check(!ear.Update(true, 2, 1000, 30, 1, 0), "zero elapsed time cannot pop");
101 Check(!ear.Update(true, 2, 1000, 30, 1, -0.1f), "reject negative time");
102
105 logged.Update(true, 1, 2576, 30, 1, 0);
106 unlogged.Update(true, 1, 2576, 30, 1, 0);
107 const float firstThreshold = logged.Threshold();
108 Check(logged.Update(true, 1, 2580, 30, 1, 0.25f, &pop), "first logged pop");
109 Check(unlogged.Update(true, 1, 2580, 30, 1, 0.25f), "unlogged comparison pop");
111 "diagnostics capture pre-equalization pressure");
113 "first pop reports triggering threshold and no previous interval");
114 const float secondThreshold = logged.Threshold();
115 Check(!logged.Update(true, 1, 2580, 30, 1, 0.2f, &pop), "quiet frame has no diagnostic event");
116 unlogged.Update(true, 1, 2580, 30, 1, 0.2f);
117 Check(pop.
middlePressureMb == 2576,
"quiet frame does not overwrite event snapshot");
118 Check(logged.Update(true, 1, 2576, 30, 1, 0.3f, &pop), "second logged pop");
119 Check(unlogged.Update(true, 1, 2576, 30, 1, 0.3f), "unlogged reverse-pressure pop");
121 "reverse pressure keeps signed delta and original threshold");
123 Check(logged.Threshold() == unlogged.Threshold() && logged.Muffle() == unlogged.Muffle(),
124 "diagnostics do not change random draws or hearing behavior");
125 logged.Update(true, 1, 2500, 30, 1, 2.0f);
127 "suspend re-anchor clears diagnostic interval");
128 logged.Reset();
129 logged.Update(true, 1, 2500, 30, 1, 0.1f);
131 "reset clears diagnostic interval");
132 logged.Update(true, 2, 2500, 30, 1, 0.1f);
134 "character change clears diagnostic interval");
135 std::cout << "PASS: pop diagnostic snapshots, signed deltas, intervals and unchanged behavior.\n";
136 const int slow = Ramp(60, 0.322f), fast = Ramp(60, 1.0f);
137 Check(slow >= 5 && slow <= 11, "normal cabin rate gives plausible cadence");
138 Check(fast > slow * 2, "faster pressure changes pop more often");
139 Check(std::abs(Ramp(30, 0.322f) - Ramp(120, 0.322f)) <= 1, "frame-rate independent cadence");
140 Check(std::abs(Ramp(60, -0.322f) - slow) <= 1, "symmetric ascent/descent cadence");
141 std::cout << "PASS: 1000 demographic seeds; pressure, cadence, smoothing and lifecycle cases. "
142 << "60 s cabin ramp: " << slow << " pops, fast ramp: " << fast << " pops.\n";
143}
double secondsSincePreviousPop