1+ #include < gtest/gtest.h>
2+ #include < SDL.h>
3+ #include < SDL_image.h>
4+ #include " Entity.hpp"
5+
6+ // Test fixture for Entity tests that handles SDL initialization
7+ class EntityTest : public ::testing::Test {
8+ protected:
9+ void SetUp () override {
10+ // Initialize SDL for texture operations
11+ if (SDL_Init (SDL_INIT_VIDEO) != 0 ) {
12+ FAIL () << " SDL_Init failed: " << SDL_GetError ();
13+ }
14+
15+ if (!(IMG_Init (IMG_INIT_PNG) & IMG_INIT_PNG)) {
16+ SDL_Quit ();
17+ FAIL () << " IMG_Init failed: " << IMG_GetError ();
18+ }
19+
20+ // Create a minimal window and renderer for texture operations
21+ window = SDL_CreateWindow (" Test Window" ,
22+ SDL_WINDOWPOS_UNDEFINED,
23+ SDL_WINDOWPOS_UNDEFINED,
24+ 100 , 100 ,
25+ SDL_WINDOW_HIDDEN);
26+ if (!window) {
27+ IMG_Quit ();
28+ SDL_Quit ();
29+ FAIL () << " SDL_CreateWindow failed: " << SDL_GetError ();
30+ }
31+
32+ renderer = SDL_CreateRenderer (window, -1 , SDL_RENDERER_SOFTWARE);
33+ if (!renderer) {
34+ SDL_DestroyWindow (window);
35+ IMG_Quit ();
36+ SDL_Quit ();
37+ FAIL () << " SDL_CreateRenderer failed: " << SDL_GetError ();
38+ }
39+ }
40+
41+ void TearDown () override {
42+ if (testTexture) {
43+ SDL_DestroyTexture (testTexture);
44+ }
45+ if (renderer) {
46+ SDL_DestroyRenderer (renderer);
47+ }
48+ if (window) {
49+ SDL_DestroyWindow (window);
50+ }
51+ IMG_Quit ();
52+ SDL_Quit ();
53+ }
54+
55+ // Helper function to create a test texture
56+ SDL_Texture* createTestTexture (int width, int height) {
57+ SDL_Surface* surface = SDL_CreateRGBSurface (0 , width, height, 32 , 0 , 0 , 0 , 0 );
58+ if (!surface) return nullptr ;
59+
60+ SDL_Texture* texture = SDL_CreateTextureFromSurface (renderer, surface);
61+ SDL_FreeSurface (surface);
62+ return texture;
63+ }
64+
65+ SDL_Window* window = nullptr ;
66+ SDL_Renderer* renderer = nullptr ;
67+ SDL_Texture* testTexture = nullptr ;
68+ };
69+
70+ // Test Entity construction with valid texture
71+ TEST_F (EntityTest, ConstructorWithValidTexture) {
72+ testTexture = createTestTexture (64 , 48 );
73+ ASSERT_NE (testTexture, nullptr );
74+
75+ Entity entity (10 .5f , 20 .3f , testTexture);
76+
77+ EXPECT_FLOAT_EQ (entity.getX (), 10 .5f );
78+ EXPECT_FLOAT_EQ (entity.getY (), 20 .3f );
79+ EXPECT_EQ (entity.getTexture (), testTexture);
80+
81+ SDL_Rect frame = entity.getCurrentFrame ();
82+ EXPECT_EQ (frame.x , 0 );
83+ EXPECT_EQ (frame.y , 0 );
84+ EXPECT_EQ (frame.w , 64 ); // Should match texture width
85+ EXPECT_EQ (frame.h , 48 ); // Should match texture height
86+ }
87+
88+ // Test Entity construction with null texture
89+ TEST_F (EntityTest, ConstructorWithNullTexture) {
90+ Entity entity (15 .7f , 25 .9f , nullptr );
91+
92+ EXPECT_FLOAT_EQ (entity.getX (), 15 .7f );
93+ EXPECT_FLOAT_EQ (entity.getY (), 25 .9f );
94+ EXPECT_EQ (entity.getTexture (), nullptr );
95+
96+ SDL_Rect frame = entity.getCurrentFrame ();
97+ EXPECT_EQ (frame.x , 0 );
98+ EXPECT_EQ (frame.y , 0 );
99+ EXPECT_EQ (frame.w , 0 ); // Should be 0 for null texture
100+ EXPECT_EQ (frame.h , 0 ); // Should be 0 for null texture
101+ }
102+
103+ // Test Entity construction with zero coordinates
104+ TEST_F (EntityTest, ConstructorWithZeroCoordinates) {
105+ testTexture = createTestTexture (32 , 32 );
106+ ASSERT_NE (testTexture, nullptr );
107+
108+ Entity entity (0 .0f , 0 .0f , testTexture);
109+
110+ EXPECT_FLOAT_EQ (entity.getX (), 0 .0f );
111+ EXPECT_FLOAT_EQ (entity.getY (), 0 .0f );
112+ EXPECT_EQ (entity.getTexture (), testTexture);
113+ }
114+
115+ // Test Entity construction with negative coordinates
116+ TEST_F (EntityTest, ConstructorWithNegativeCoordinates) {
117+ testTexture = createTestTexture (16 , 16 );
118+ ASSERT_NE (testTexture, nullptr );
119+
120+ Entity entity (-10 .5f , -20 .3f , testTexture);
121+
122+ EXPECT_FLOAT_EQ (entity.getX (), -10 .5f );
123+ EXPECT_FLOAT_EQ (entity.getY (), -20 .3f );
124+ EXPECT_EQ (entity.getTexture (), testTexture);
125+ }
126+
127+ // Test setTexture functionality
128+ TEST_F (EntityTest, SetTexture) {
129+ Entity entity (5 .0f , 10 .0f , nullptr );
130+ EXPECT_EQ (entity.getTexture (), nullptr );
131+
132+ testTexture = createTestTexture (128 , 96 );
133+ ASSERT_NE (testTexture, nullptr );
134+
135+ entity.setTexture (testTexture);
136+ EXPECT_EQ (entity.getTexture (), testTexture);
137+
138+ // Setting to nullptr should also work
139+ entity.setTexture (nullptr );
140+ EXPECT_EQ (entity.getTexture (), nullptr );
141+ }
142+
143+ // Test getCurrentFrame returns copy, not reference
144+ TEST_F (EntityTest, GetCurrentFrameReturnsCopy) {
145+ testTexture = createTestTexture (50 , 60 );
146+ ASSERT_NE (testTexture, nullptr );
147+
148+ Entity entity (0 .0f , 0 .0f , testTexture);
149+
150+ SDL_Rect frame1 = entity.getCurrentFrame ();
151+ SDL_Rect frame2 = entity.getCurrentFrame ();
152+
153+ // Should be equal values
154+ EXPECT_EQ (frame1.x , frame2.x );
155+ EXPECT_EQ (frame1.y , frame2.y );
156+ EXPECT_EQ (frame1.w , frame2.w );
157+ EXPECT_EQ (frame1.h , frame2.h );
158+
159+ // But different memory addresses (copies)
160+ EXPECT_NE (&frame1, &frame2);
161+
162+ // Modifying one shouldn't affect the other
163+ frame1.w = 999 ;
164+ frame2 = entity.getCurrentFrame ();
165+ EXPECT_NE (frame1.w , frame2.w );
166+ EXPECT_EQ (frame2.w , 50 );
167+ }
168+
169+ // Test setCurrentFrameW
170+ TEST_F (EntityTest, SetCurrentFrameW) {
171+ testTexture = createTestTexture (32 , 32 );
172+ ASSERT_NE (testTexture, nullptr );
173+
174+ Entity entity (0 .0f , 0 .0f , testTexture);
175+
176+ SDL_Rect originalFrame = entity.getCurrentFrame ();
177+ EXPECT_EQ (originalFrame.w , 32 );
178+
179+ entity.setCurrentFrameW (64 );
180+ SDL_Rect newFrame = entity.getCurrentFrame ();
181+ EXPECT_EQ (newFrame.w , 64 );
182+ EXPECT_EQ (newFrame.h , 32 ); // Height should remain unchanged
183+ EXPECT_EQ (newFrame.x , 0 ); // X should remain unchanged
184+ EXPECT_EQ (newFrame.y , 0 ); // Y should remain unchanged
185+ }
186+
187+ // Test setCurrentFrameH
188+ TEST_F (EntityTest, SetCurrentFrameH) {
189+ testTexture = createTestTexture (32 , 32 );
190+ ASSERT_NE (testTexture, nullptr );
191+
192+ Entity entity (0 .0f , 0 .0f , testTexture);
193+
194+ SDL_Rect originalFrame = entity.getCurrentFrame ();
195+ EXPECT_EQ (originalFrame.h , 32 );
196+
197+ entity.setCurrentFrameH (128 );
198+ SDL_Rect newFrame = entity.getCurrentFrame ();
199+ EXPECT_EQ (newFrame.h , 128 );
200+ EXPECT_EQ (newFrame.w , 32 ); // Width should remain unchanged
201+ EXPECT_EQ (newFrame.x , 0 ); // X should remain unchanged
202+ EXPECT_EQ (newFrame.y , 0 ); // Y should remain unchanged
203+ }
204+
205+ // Test setting frame dimensions to zero
206+ TEST_F (EntityTest, SetFrameDimensionsToZero) {
207+ testTexture = createTestTexture (100 , 100 );
208+ ASSERT_NE (testTexture, nullptr );
209+
210+ Entity entity (0 .0f , 0 .0f , testTexture);
211+
212+ entity.setCurrentFrameW (0 );
213+ entity.setCurrentFrameH (0 );
214+
215+ SDL_Rect frame = entity.getCurrentFrame ();
216+ EXPECT_EQ (frame.w , 0 );
217+ EXPECT_EQ (frame.h , 0 );
218+ }
219+
220+ // Test setting negative frame dimensions
221+ TEST_F (EntityTest, SetNegativeFrameDimensions) {
222+ testTexture = createTestTexture (50 , 50 );
223+ ASSERT_NE (testTexture, nullptr );
224+
225+ Entity entity (0 .0f , 0 .0f , testTexture);
226+
227+ entity.setCurrentFrameW (-10 );
228+ entity.setCurrentFrameH (-20 );
229+
230+ SDL_Rect frame = entity.getCurrentFrame ();
231+ EXPECT_EQ (frame.w , -10 ); // Should accept negative values
232+ EXPECT_EQ (frame.h , -20 ); // Should accept negative values
233+ }
234+
235+ // Test multiple frame dimension changes
236+ TEST_F (EntityTest, MultipleFrameDimensionChanges) {
237+ testTexture = createTestTexture (25 , 25 );
238+ ASSERT_NE (testTexture, nullptr );
239+
240+ Entity entity (0 .0f , 0 .0f , testTexture);
241+
242+ // Test multiple width changes
243+ entity.setCurrentFrameW (50 );
244+ EXPECT_EQ (entity.getCurrentFrame ().w , 50 );
245+
246+ entity.setCurrentFrameW (75 );
247+ EXPECT_EQ (entity.getCurrentFrame ().w , 75 );
248+
249+ entity.setCurrentFrameW (100 );
250+ EXPECT_EQ (entity.getCurrentFrame ().w , 100 );
251+
252+ // Test multiple height changes
253+ entity.setCurrentFrameH (30 );
254+ EXPECT_EQ (entity.getCurrentFrame ().h , 30 );
255+
256+ entity.setCurrentFrameH (60 );
257+ EXPECT_EQ (entity.getCurrentFrame ().h , 60 );
258+
259+ entity.setCurrentFrameH (90 );
260+ EXPECT_EQ (entity.getCurrentFrame ().h , 90 );
261+
262+ // Final state check
263+ SDL_Rect finalFrame = entity.getCurrentFrame ();
264+ EXPECT_EQ (finalFrame.w , 100 );
265+ EXPECT_EQ (finalFrame.x , 0 );
266+ EXPECT_EQ (finalFrame.y , 0 );
267+ EXPECT_EQ (finalFrame.h , 90 );
268+ }
269+
270+ // Test with different texture sizes
271+ TEST_F (EntityTest, DifferentTextureSizes) {
272+ // Test very small texture
273+ SDL_Texture* smallTexture = createTestTexture (1 , 1 );
274+ ASSERT_NE (smallTexture, nullptr );
275+
276+ Entity smallEntity (0 .0f , 0 .0f , smallTexture);
277+ SDL_Rect smallFrame = smallEntity.getCurrentFrame ();
278+ EXPECT_EQ (smallFrame.w , 1 );
279+ EXPECT_EQ (smallFrame.h , 1 );
280+
281+ SDL_DestroyTexture (smallTexture);
282+
283+ // Test large texture
284+ SDL_Texture* largeTexture = createTestTexture (512 , 256 );
285+ ASSERT_NE (largeTexture, nullptr );
286+
287+ Entity largeEntity (0 .0f , 0 .0f , largeTexture);
288+ SDL_Rect largeFrame = largeEntity.getCurrentFrame ();
289+ EXPECT_EQ (largeFrame.w , 512 );
290+ EXPECT_EQ (largeFrame.h , 256 );
291+
292+ SDL_DestroyTexture (largeTexture);
293+
294+ // Test non-square texture
295+ SDL_Texture* rectTexture = createTestTexture (200 , 50 );
296+ ASSERT_NE (rectTexture, nullptr );
297+
298+ Entity rectEntity (0 .0f , 0 .0f , rectTexture);
299+ SDL_Rect rectFrame = rectEntity.getCurrentFrame ();
300+ EXPECT_EQ (rectFrame.w , 200 );
301+ EXPECT_EQ (rectFrame.h , 50 );
302+
303+ SDL_DestroyTexture (rectTexture);
304+ }
305+
306+ // Test Entity with floating point precision
307+ TEST_F (EntityTest, FloatingPointPrecision) {
308+ testTexture = createTestTexture (32 , 32 );
309+ ASSERT_NE (testTexture, nullptr );
310+
311+ float preciseX = 10 .123456789f ;
312+ float preciseY = 20 .987654321f ;
313+
314+ Entity entity (preciseX, preciseY, testTexture);
315+
316+ // Should maintain reasonable floating point precision
317+ EXPECT_NEAR (entity.getX (), preciseX, 0 .000001f );
318+ EXPECT_NEAR (entity.getY (), preciseY, 0 .000001f );
319+ }
0 commit comments