1#include "../Viewscreen/ReentryRadio.h"
2#include "../Viewscreen/WeatherOverrides.h"
3#include <iostream>
4#include <limits>
5#include <stdexcept>
6#include <string>
7
8namespace
9{
10 unsigned checks = 0;
11 void Check(bool condition, const char* message)
12 {
13 ++checks;
14 if (!condition) throw std::runtime_error(message);
15 }
16 bool Near(float a, float b) { return std::abs(a-b) < .000001f; }
17 void OverridesAndRecovery()
18 {
19
20
21 for (float load : {0.f, 100.f, 140000.f, 280000.f, 840000.f, 1540000.f, 2000000.f})
22 {
23 for (float expected : {0.f, .1f, .5f, 1.f})
24 {
25 float gamma = -1.f;
28 Check(blackout == expected, "flight conditions override diagnostic radio loss");
29 Check(
Near(1.f-blackout, 1.f-expected),
"radio quality has wrong direction");
30 for (const auto* invalid : {L"nan", L"inf", L"1.1", L"-0.1", L".5junk", L".5 1"})
31 {
34 }
37 Check(automatic >= 0 && automatic <= 1, "automatic bounds");
38 gamma = expected;
40 "simulator must clear override and blackout");
42 "simulator exit replays diagnostic loss");
43 }
44 }
45 float gamma = -1;
46 for (float load : {-100.f, 0.f, 139999.f, 140000.f})
54 const float nan = std::numeric_limits<float>::quiet_NaN();
56 }
57 void MessageLoss()
58 {
59
60
61 for (float draw : {0.f, .25f, .5f, .99999f, 1.f})
62 for (float gamma : {0.f, .1f, .5f, 1.f})
63 {
64 wchar_t message[] = L"ABCDEFGHIJKLMNOPQRST";
66 const std::wstring result(message);
67 Check(result.size() == 20 && message[20] == 0, "garbler changed length or terminator");
68 Check(std::count(result.begin(), result.end(), L'?') == static_cast<int>(20.f*gamma),
69 "loss fraction does not replace distinct characters");
70 for (size_t i = 0; i < result.size(); ++i)
71 Check(result[i] == L'?' || result[i] == L'A'+i, "garbler moved uncorrupted text");
72 }
73 for (unsigned seed = 1; seed <= 64; ++seed)
74 {
75 std::wstring text(100, L'X');
76 unsigned state = seed;
78 state = state*1664525u+1013904223u;
79 return static_cast<float>(state >> 8) / 16777216.f;
80 });
81 Check(std::count(text.begin(), text.end(), L'?') == 50, "randomized fractional loss");
82 }
83 wchar_t unicode[] = {0xd83d, 0xde80, L'A', 0};
85 Check(unicode[0] == L'?' && unicode[1] == L'?' && unicode[2] == L'A' && unicode[3] == 0,
86 "garbling split a UTF-16 pair");
87 wchar_t empty[] = L"";
89 Check(empty[0] == 0, "empty message changed");
90 wchar_t message[] = L"RECOVER";
91 ReentryRadio::Garble(message, 0, []() ->
float {
throw std::runtime_error(
"clear reception used RNG"); });
92 Check(std::wstring(message) == L"RECOVER", "clear reception changes text");
93 }
94}
96{
97 try
98 {
99 OverridesAndRecovery(); MessageLoss();
100 std::cout << "PASS: " << checks << " reentry radio checks\n";
101 return 0;
102 }
103 catch (const std::exception& error) { std::cerr << "FAIL: " << error.what() << '\n'; return 1; }
104}
float UpdateBlackout(float thermalLoad, float &gammaOverride, bool simulator)
void Garble(wchar_t *text, float blackout, Random random)
bool Near(const Body &b, const Triangle &t, double extra)
bool ParseValue(const wchar_t *text, float minimum, float maximum, float &value)