Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/VehicleWearTests.cpp

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

1#include "../Viewscreen/VehicleWear.h"
2
3#include <cmath>
4#include <iostream>
5#include <limits>
6#include <stdexcept>
7
8namespace
9{
10 void Check(bool condition, const char* message)
11 {
12 if (!condition)
13 throw std::runtime_error(message);
14 }
15
16 bool Near(float a, float b)
17 {
18 return std::fabs(a - b) < 0.00001f;
19 }
20
21 void CheckNew(const VehicleWear::Factors& wear)
22 {
23 Check(wear.thrust == 1.0f && wear.drag == 1.0f && wear.fuelFlow == 1.0f &&
24 wear.controlPlay == 0.0f, "new/invalid usage changed baseline performance");
25 }
26
27 void TestWear(bool ground)
28 {
29 const float serviceLife = ground ? 200000.0f : 10000.0f;
30 CheckNew(VehicleWear::Calculate(0.0f, ground));
31 for (float invalid : { -1.0f, -std::numeric_limits<float>::max(),
32 std::numeric_limits<float>::infinity(), -std::numeric_limits<float>::infinity(),
33 std::numeric_limits<float>::quiet_NaN() })
34 CheckNew(VehicleWear::Calculate(invalid, ground));
35
36 const auto half = VehicleWear::Calculate(serviceLife * 0.5f, ground);
37 Check(Near(half.thrust, 0.9f) && Near(half.drag, 1.1f) && Near(half.fuelFlow, 1.1f),
38 "half-life vehicle did not receive the expected performance penalties");
39 Check(Near(half.controlPlay, ground ? 0.0f : 0.05f), "incorrect flight-control play");
40
41 auto previous = VehicleWear::Calculate(0.0f, ground);
42 for (int step = 1; step <= 2000; ++step)
43 {
44 const auto wear = VehicleWear::Calculate(serviceLife * step / 1000.0f, ground);
45 Check(wear.thrust <= previous.thrust && wear.drag >= previous.drag &&
46 wear.fuelFlow >= previous.fuelFlow && wear.controlPlay >= previous.controlPlay,
47 "performance improved as usage increased");
48 Check(wear.thrust >= 0.8f && wear.drag <= 1.2f && wear.fuelFlow <= 1.2f &&
49 wear.controlPlay <= 0.1f, "wear exceeded its safe bounds");
50 previous = wear;
51 }
52 const auto extreme = VehicleWear::Calculate(std::numeric_limits<float>::max(), ground);
53 Check(Near(extreme.thrust, 0.8f) && Near(extreme.drag, 1.2f) &&
54 Near(extreme.fuelFlow, 1.2f), "extreme finite usage did not saturate safely");
55 CheckNew(VehicleWear::Calculate(0.0f, ground)); // Switching vehicles does not retain wear.
56 }
57
58 void TestControls()
59 {
60 for (float play : { 0.0f, 0.025f, 0.05f, 0.1f })
61 {
62 Check(VehicleWear::ApplyControlPlay(0.0f, play) == 0.0f, "neutral input moved controls");
63 Check(VehicleWear::ApplyControlPlay(play, play) == 0.0f &&
64 VehicleWear::ApplyControlPlay(-play, play) == 0.0f, "dead-zone boundary moved controls");
65 Check(Near(VehicleWear::ApplyControlPlay(1.0f, play), 1.0f) &&
66 Near(VehicleWear::ApplyControlPlay(-1.0f, play), -1.0f), "full travel lost authority");
67 float previous = -1.0f;
68 for (int step = -1000; step <= 1000; ++step)
69 {
70 const float input = step / 1000.0f;
71 const float output = VehicleWear::ApplyControlPlay(input, play);
72 Check(output >= previous && std::fabs(output) <= std::fabs(input) + 0.00001f &&
73 input * output >= 0.0f, "control play reversed or amplified a command");
74 Check(Near(output, -VehicleWear::ApplyControlPlay(-input, play)), "asymmetric control response");
76 VehicleWear::RestoreControlInput(output, play), play)),
77 "restoring saved deflection applied play twice");
78 if (play == 0.0f)
79 Check(output == input, "new vehicle input changed");
80 previous = output;
81 }
82 Check(std::fabs(VehicleWear::ApplyControlPlay(play + 0.00001f, play)) < 0.00002f,
83 "control response jumps at the dead-zone edge");
84 }
85 Check(VehicleWear::ApplyControlPlay(0.075f, 0.05f) > 0.0f &&
86 VehicleWear::ApplyControlPlay(0.075f, 0.1f) == 0.0f, "wear did not enlarge the null zone");
87
88 // Sustained keyboard/AFCS commands must not decay through repeated remapping.
89 for (int frame = 0; frame < 144 * 60; ++frame)
90 {
91 const auto wear = VehicleWear::Calculate(10000.0f, false);
92 Check(Near(VehicleWear::ApplyControlPlay(0.55f, wear.controlPlay), 0.5f),
93 "constant command compounded control play across frames");
94 }
95 }
96}
97
98int main()
99{
100 try
101 {
102 TestWear(false);
103 TestWear(true);
104 TestControls();
105 std::cout << "Vehicle wear tests passed: baseline, units, progression, caps, invalid usage, control travel and restoration.\n";
106 return 0;
107 }
108 catch (const std::exception& error)
109 {
110 std::cerr << error.what() << '\n';
111 return 1;
112 }
113}
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141
float ApplyControlPlay(float input, float play)
Definition VehicleWear.h:37
Factors Calculate(float usage, bool groundVehicle)
Definition VehicleWear.h:24
float RestoreControlInput(float output, float play)
Definition VehicleWear.h:49