UFO: Alien Invasion
Loading...
Searching...
No Matches
test_events.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25#include "test_shared.h"
26#include "../common/common.h"
28#include "../client/cl_shared.h"
29#include <string>
30
31class EventsTest: public ::testing::Test {
32protected:
33 static void SetUpTestCase() {
34 TEST_Init();
35 cl_genericPool = Mem_CreatePool("Client: Generic");
36 }
37
38 static void TearDownTestCase() {
40 }
41};
42
44{
45 ASSERT_TRUE(EV_NUM_EVENTS < EVENT_INSTANTLY);
46}
47
49{
51 for (int i = 0; i < lengthof(events); i++) {
55 }
57}
58
60
61TEST_F(EventsTest, Scheduler)
62{
63 std::string s_one("one");
64 std::string s_two("two");
65 std::string s_three("three");
66 std::string s_four("four");
67 std::string s_five("five");
68
69 ScheduleEventPtr one = Schedule_Event(3, nullptr, nullptr, nullptr, static_cast<void*>(&s_one));
70 ScheduleEventPtr two = Schedule_Event(3, nullptr, nullptr, nullptr, static_cast<void*>(&s_two));
71 ScheduleEventPtr three = Schedule_Event(4, nullptr, nullptr, nullptr, static_cast<void*>(&s_three));
72 ScheduleEventPtr four = Schedule_Event(4, nullptr, nullptr, nullptr, static_cast<void*>(&s_four));
73 ScheduleEventPtr five = Schedule_Event(5, nullptr, nullptr, nullptr, static_cast<void*>(&s_five));
74
75 ASSERT_EQ(Dequeue_Event(1000), one);
76 ASSERT_EQ(Dequeue_Event(1000), two);
77 ASSERT_EQ(Dequeue_Event(1000), three);
78 ASSERT_EQ(Dequeue_Event(1000), four);
79 ASSERT_EQ(Dequeue_Event(1000), five);
80 ASSERT_FALSE(Dequeue_Event(1000));
81}
82
83static bool delayCheck (int now, void* data)
84{
85 static bool check = false;
86 const bool ret = check;
87
88 if (!check) {
89 check = true;
90 }
91
92 return ret;
93}
94
95TEST_F(EventsTest, SchedulerCheck)
96{
97 std::string s_one("one");
98 std::string s_two("two");
99 std::string s_three("three");
100 std::string s_four("four");
101 std::string s_five("five");
102
103 ScheduleEventPtr three = Schedule_Event(4, nullptr, nullptr, nullptr, static_cast<void*>(&s_three));
104 ScheduleEventPtr four = Schedule_Event(4, nullptr, nullptr, nullptr, static_cast<void*>(&s_four));
105 ScheduleEventPtr five = Schedule_Event(4, nullptr, nullptr, nullptr, static_cast<void*>(&s_five));
106 ScheduleEventPtr one = Schedule_Event(3, nullptr, delayCheck, nullptr, static_cast<void*>(&s_one));
107 ScheduleEventPtr two = Schedule_Event(3, nullptr, nullptr, nullptr, static_cast<void*>(&s_two));
108
110 ASSERT_FALSE(e);
111 e = Dequeue_Event(2);
112 ASSERT_FALSE(e);
113 /* one is delayed via check function - so we get the 2nd event at the first dequeue here */
114 e = Dequeue_Event(3);
115 ASSERT_EQ(e, two);
116 /* now we are ready for the 1st event */
117 e = Dequeue_Event(5);
118 ASSERT_EQ(e, one);
119 /* the remaining events are in order */
120 e = Dequeue_Event(5);
121 ASSERT_EQ(e, three);
122 e = Dequeue_Event(5);
123 ASSERT_EQ(e, four);
124 e = Dequeue_Event(5);
125 ASSERT_EQ(e, five);
126}
127
128static bool delayCheckBlockedVal = false;
129static bool delayCheckBlocked (int now, void* data)
130{
132}
133
135{
136 std::string s_one("one");
137 std::string s_two("two");
138 std::string s_three("three");
139 std::string s_four("three");
140
141 const int delay = 10;
142
143 event_func* f_oneFour = (event_func*)0xCAFED00D;
144 event_func* f_twoThree = (event_func*)0xB16B00B5;
145
146 ScheduleEventPtr one = Schedule_Event(3, f_oneFour, delayCheckBlocked, nullptr, static_cast<void*>(&s_one));
147 one->delayFollowing = delay;
148 ScheduleEventPtr two = Schedule_Event(4 + delay, f_twoThree, nullptr, nullptr, static_cast<void*>(&s_two));
149 two->delayFollowing = delay;
150 ScheduleEventPtr three = Schedule_Event(5 + delay, f_twoThree, nullptr, nullptr, static_cast<void*>(&s_three));
151 three->delayFollowing = delay;
152 ScheduleEventPtr four = Schedule_Event(5, f_oneFour, delayCheckBlocked, nullptr, static_cast<void*>(&s_four));
153 four->delayFollowing = delay;
154
156 ASSERT_FALSE(e);
157 e = Dequeue_Event(2);
158 ASSERT_FALSE(e);
159 e = Dequeue_Event(3);
160 ASSERT_FALSE(e);
161 e = Dequeue_Event(5);
162 ASSERT_FALSE(e);
163
165
166 e = Dequeue_Event(5);
167 ASSERT_FALSE(e);
168 e = Dequeue_Event(5 + delay);
169 ASSERT_EQ(e, one);
170 e = Dequeue_Event(4 + delay);
171 ASSERT_EQ(e, two);
172 e = Dequeue_Event(4 + delay);
173 ASSERT_FALSE(e);
174 e = Dequeue_Event(5 + delay);
175 ASSERT_EQ(e, three);
176 e = Dequeue_Event(5 + delay);
177 ASSERT_EQ(e, four);
178}
memPool_t * cl_genericPool
Definition cl_main.cpp:86
Share stuff between the different cgame implementations.
static void SetUpTestCase()
static void TearDownTestCase()
ScheduleEventPtr Schedule_Event(int when, event_func *func, event_check_func *check, event_clean_func *clean, void *data)
Schedules an event to run on or after the given time, and when its check function returns true.
Definition common.cpp:1380
definitions common between client and server, but not game lib
SharedPtr< scheduleEvent_t > ScheduleEventPtr
Definition common.h:331
void event_func(int now, void *data)
Definition common.h:302
const eventRegister_t events[]
List of functions to register nodes.
Definition e_main.cpp:92
event_t CL_ParseEvent(dbuffer *msg)
Called in case a svc_event was send via the network buffer.
Definition e_parse.cpp:261
int CL_ClearBattlescapeEvents(void)
Definition e_parse.cpp:215
voidpf void * buf
Definition ioapi.h:42
#define Mem_CreatePool(name)
Definition mem.h:32
void NET_WriteByte(dbuffer *buf, byte c)
Definition netpack.cpp:39
event_t
Possible event values.
Definition q_shared.h:79
@ EV_NUM_EVENTS
Definition q_shared.h:142
@ EV_ENDROUND
Definition q_shared.h:83
@ EV_ENDROUNDANNOUNCE
Definition q_shared.h:84
@ EV_START
Definition q_shared.h:82
@ EV_RESET
Definition q_shared.h:81
#define EVENT_INSTANTLY
Definition q_shared.h:73
QGL_EXTERN GLsizei const GLvoid * data
Definition r_gl.h:89
QGL_EXTERN GLint i
Definition r_gl.h:113
#define lengthof(x)
Definition shared.h:105
int delayFollowing
Definition common.h:314
TEST_F(EventsTest, Range)
ScheduleEventPtr Dequeue_Event(int now)
Finds and returns the first event in the event_queue that is due. If the event has a check function,...
Definition common.cpp:1433
static bool delayCheckBlockedVal
static bool delayCheck(int now, void *data)
static bool delayCheckBlocked(int now, void *data)
void TEST_Shutdown(void)
void TEST_Init(void)