File: e_basis_test.cpp

package info (click to toggle)
eureka 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,912 kB
  • sloc: cpp: 84,842; python: 495; sh: 91; makefile: 21; ansic: 3
file content (217 lines) | stat: -rw-r--r-- 6,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//  Eureka DOOM Editor
//
//  Copyright (C) 2025 Ioan Chera
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//------------------------------------------------------------------------

#include "gtest/gtest.h"
#include "Document.h"
#include "Instance.h"
#include "e_basis.h"
#include "m_vector.h"

class BasisLumpChangeFixture : public ::testing::TestWithParam<LumpType>
{
protected:
	Instance inst;
	Document doc{inst};

	std::vector<byte> &lump(LumpType type)
	{
		switch(type)
		{
		case LumpType::header:
			return doc.headerData;
		case LumpType::behavior:
			return doc.behaviorData;
		case LumpType::scripts:
			return doc.scriptsData;
		}
		// Should never reach here
		return doc.headerData;
	}
};

TEST_P(BasisLumpChangeFixture, ChangeUndoRedo)
{
	LumpType type = GetParam();
	auto &data = lump(type);

	std::vector<byte> original{1, 2, 3};
	data = original;

	std::vector<byte> replacement{4, 5};
	{
		EditOperation op(doc.basis);
		op.changeLump(type, std::vector<byte>(replacement));
	}

	EXPECT_EQ(data, replacement);

	ASSERT_TRUE(doc.basis.undo());
	EXPECT_EQ(data, original);

	ASSERT_TRUE(doc.basis.redo());
	EXPECT_EQ(data, replacement);

	ASSERT_TRUE(doc.basis.undo());
	EXPECT_EQ(data, original);

	// Now change something to see that redo becomes unavailable
	{
		EditOperation op(doc.basis);
		op.changeLump(type, std::vector<byte>{6, 7});
	}
	ASSERT_FALSE(doc.basis.redo());
	ASSERT_TRUE(doc.basis.undo());
	EXPECT_EQ(data, original);

	ASSERT_FALSE(doc.basis.undo());
}

INSTANTIATE_TEST_SUITE_P(
	BasisLumpChanges,
	BasisLumpChangeFixture,
	::testing::Values(LumpType::header, LumpType::behavior, LumpType::scripts));

class BasisChangeStatusFixture : public ::testing::Test
{
protected:
	Instance inst;
	Document doc{inst};

	void changeHeader(const std::vector<byte> &data)
	{
		EditOperation op(doc.basis);
		op.changeLump(LumpType::header, std::vector<byte>(data));
	}
};

// Saving should update the saved stack and clear MadeChanges
TEST_F(BasisChangeStatusFixture, SavingResetsMadeChanges)
{
	doc.headerData = {1};
	changeHeader({2});
	EXPECT_TRUE(doc.hasChanges());

	doc.markSaved();
	EXPECT_FALSE(doc.hasChanges());
}

// Undoing and redoing toggles MadeChanges depending on the saved stack
TEST_F(BasisChangeStatusFixture, UndoRedoTogglesMadeChanges)
{
	doc.headerData = {1};

	changeHeader({2});
	doc.markSaved();

	changeHeader({3});
	EXPECT_TRUE(doc.hasChanges());

	ASSERT_TRUE(doc.basis.undo());
	EXPECT_FALSE(doc.hasChanges());

	ASSERT_TRUE(doc.basis.undo());
	EXPECT_TRUE(doc.hasChanges());

	ASSERT_TRUE(doc.basis.redo());
	EXPECT_FALSE(doc.hasChanges());

	ASSERT_TRUE(doc.basis.redo());
	EXPECT_TRUE(doc.hasChanges());
}

// Repeating a redo operation manually can clear the MadeChanges flag
TEST_F(BasisChangeStatusFixture, ManualRedoActionClearsMadeChanges)
{
	doc.headerData = {1};

	changeHeader({2});
	changeHeader({3});
	doc.markSaved();

	ASSERT_TRUE(doc.basis.undo());
	EXPECT_TRUE(doc.hasChanges());

	changeHeader({3});
	EXPECT_FALSE(doc.hasChanges());

	changeHeader({4});
	EXPECT_TRUE(doc.hasChanges());
}

TEST(CoordsMatchTest, DoomFormatExactMatch)
{
	// In DOOM format, coordinates are rounded to integers
	EXPECT_TRUE(CoordsMatch(MapFormat::doom, v2double_t{100.0, 200.0}, v2double_t{100.0, 200.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::doom, v2double_t{100.3, 200.7}, v2double_t{100.0, 201.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::doom, v2double_t{100.4, 200.4}, v2double_t{100.0, 200.0}));
}

TEST(CoordsMatchTest, DoomFormatNoMatch)
{
	// Different integers should not match
	EXPECT_FALSE(CoordsMatch(MapFormat::doom, v2double_t{100.0, 200.0}, v2double_t{101.0, 200.0}));
	EXPECT_FALSE(CoordsMatch(MapFormat::doom, v2double_t{100.0, 200.0}, v2double_t{100.0, 201.0}));
	EXPECT_FALSE(CoordsMatch(MapFormat::doom, v2double_t{100.6, 200.0}, v2double_t{100.0, 200.0}));
}

TEST(CoordsMatchTest, HexenFormatExactMatch)
{
	// Hexen format also rounds to integers
	EXPECT_TRUE(CoordsMatch(MapFormat::hexen, v2double_t{100.0, 200.0}, v2double_t{100.0, 200.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::hexen, v2double_t{100.3, 200.7}, v2double_t{100.0, 201.0}));
}

TEST(CoordsMatchTest, HexenFormatNoMatch)
{
	EXPECT_FALSE(CoordsMatch(MapFormat::hexen, v2double_t{100.0, 200.0}, v2double_t{100.7, 200.0}));
	EXPECT_FALSE(CoordsMatch(MapFormat::hexen, v2double_t{100.0, 200.0}, v2double_t{100.0, 201.0}));
}

TEST(CoordsMatchTest, UdmfFormatWithinThreshold)
{
	// UDMF uses threshold of kFracUnit / GEOM_EPSILON_DIVIDER
	// With kFracUnit=4096 and GEOM_EPSILON_DIVIDER=1024, threshold = 4 fixed-point units
	// 4 / 4096 = 0.0009765625 in floating point
	constexpr double threshold = 1.0 / 1024.0;

	EXPECT_TRUE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0, 200.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::udmf, v2double_t{100.0005, 200.0005}, v2double_t{100.0, 200.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0 + threshold * 0.5, 200.0}));
}

TEST(CoordsMatchTest, UdmfFormatBeyondThreshold)
{
	// Values beyond threshold should not match
	constexpr double threshold = 1.0 / 1024.0;

	EXPECT_FALSE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0 + threshold * 2, 200.0}));
	EXPECT_FALSE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0, 200.0 + threshold * 2}));
	EXPECT_FALSE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.01, 200.0}));
}

TEST(CoordsMatchTest, UdmfFormatEdgeCases)
{
	// Test exactly at threshold boundary
	constexpr double threshold = 1.0 / 1024.0;

	// Should match at exactly the threshold (<=)
	EXPECT_TRUE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0 + threshold, 200.0}));
	EXPECT_TRUE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0, 200.0 + threshold}));

	// Should not match just beyond threshold
	EXPECT_FALSE(CoordsMatch(MapFormat::udmf, v2double_t{100.0, 200.0}, v2double_t{100.0 + threshold * 1.2, 200.0}));
}