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

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

1#include "../Viewscreen/CargoBayVisibility.h"
2
3#include <algorithm>
4#include <fstream>
5#include <iostream>
6#include <iterator>
7#include <stdexcept>
8#include <vector>
9
10namespace
11{
12 struct Component
13 {
14 unsigned char type = 0;
15 float extended = 0.0f;
16 bool visible = true;
17 };
18
19 void Check(bool condition, const char* message)
20 {
21 if (!condition)
22 throw std::runtime_error(message);
23 }
24
25 std::vector<Component> ReadE10()
26 {
27 std::ifstream input("Meshes/e-10.dix", std::ios::binary);
28 Check(input.is_open(), "run from the CustomUI directory to load the actual E-10 DIX");
29 const std::vector<unsigned char> bytes{std::istreambuf_iterator<char>(input), {}};
30 Check(bytes.size() >= 99, "truncated DIX header");
31 const unsigned count = bytes[0] | (static_cast<unsigned>(bytes[1]) << 8);
32 Check(bytes.size() >= 99 + count * 100, "truncated DIX components");
33 std::vector<Component> components;
34 for (unsigned i = 0; i < count; ++i)
35 components.push_back({bytes[99 + i * 100 + 58], 0.0f, true});
36 return components;
37 }
38
39 void CheckInteriors(const std::vector<Component>& components, bool expected)
40 {
41 for (const auto& component : components)
42 if (component.type == 54)
43 Check(component.visible == expected, "incorrect cargo-interior visibility");
44 }
45
46 void TestDoorCycle(std::vector<Component> components)
47 {
48 // Includes the first opening frame, partial closing, and the final closed frame.
49 for (float extent : {0.0f, 0.01f, 0.5f, 1.0f, 0.5f, 0.01f, 0.0f})
50 {
51 for (auto& component : components)
52 {
53 component.visible = true; // reset base visibility each frame, as OnFrameMove does
54 if (component.type == 30)
55 component.extended = extent;
56 }
57 Check(CargoBayVisibility::Update(components.data(), components.size(), false) == extent,
58 "incorrect aggregate door extent");
59 CheckInteriors(components, extent > 0.0f);
60 }
61 }
62}
63
64int main()
65{
66 try
67 {
68 auto e10 = ReadE10();
69 const auto firstInterior = std::find_if(e10.begin(), e10.end(), [](const auto& c) { return c.type == 54; });
70 const auto firstDoor = std::find_if(e10.begin(), e10.end(), [](const auto& c) { return c.type == 30; });
71 Check(firstInterior != e10.end() && firstDoor != e10.end() && firstInterior < firstDoor,
72 "E-10 fixture no longer reproduces interiors preceding doors");
73 TestDoorCycle(e10);
74 std::reverse(e10.begin(), e10.end());
75 TestDoorCycle(e10);
76
77 // A fully closed door must not hide the interior while another door is still open.
78 std::vector<Component> mixed{{54}, {30, 0.25f}, {30, 0.0f}, {0, 1.0f}, {54, 0.0f, false}};
79 Check(CargoBayVisibility::Update(mixed.data(), mixed.size(), false) == 0.25f,
80 "last closed door hid an open door");
81 Check(mixed[0].visible && mixed[3].visible && !mixed[4].visible,
82 "mixed doors or base visibility restriction failed");
83 std::reverse(mixed.begin(), mixed.end());
84 Check(CargoBayVisibility::Update(mixed.data(), mixed.size(), false) == 0.25f,
85 "mixed-door extent depends on ordering");
86
87 std::vector<Component> inside{{54}, {30}, {54, 0.0f, false}};
88 Check(CargoBayVisibility::Update(inside.data(), inside.size(), true) == 0.0f,
89 "viewer inside changed physical door extent");
90 Check(inside[0].visible && !inside[2].visible, "inside view or base visibility restriction failed");
91
92 std::vector<Component> noDoor{{54}, {0, 1.0f}, {30, 1.0f, false}};
93 Check(CargoBayVisibility::Update(noDoor.data(), noDoor.size(), false) == 0.0f,
94 "non-door or suppressed door affected extent");
95 Check(!noDoor[0].visible && noDoor[1].visible, "no-door visibility failed");
96 Check(CargoBayVisibility::Update<Component>(nullptr, 0, false) == 0.0f, "empty component list failed");
97 std::cout << "Cargo bay visibility tests passed (actual E-10 DIX, reordered components, door cycle, mixed doors, inside view).\n";
98 return 0;
99 }
100 catch (const std::exception& error)
101 {
102 std::cerr << error.what() << '\n';
103 return 1;
104 }
105}
float Update(Component *components, std::size_t count, bool viewerInside)