UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_events.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "test_shared.h"
26 #include "../common/common.h"
27 #include "../client/battlescape/events/e_parse.h"
28 #include "../client/cl_shared.h"
29 #include <string>
30 
31 class EventsTest: public ::testing::Test {
32 protected:
33  static void SetUpTestCase() {
34  TEST_Init();
35  cl_genericPool = Mem_CreatePool("Client: Generic");
36  }
37 
38  static void TearDownTestCase() {
39  TEST_Shutdown();
40  }
41 };
42 
44 {
45  ASSERT_TRUE(EV_NUM_EVENTS < EVENT_INSTANTLY);
46 }
47 
49 {
51  for (int i = 0; i < lengthof(events); i++) {
52  dbuffer buf;
53  NET_WriteByte(&buf, events[i]);
54  CL_ParseEvent(&buf);
55  }
56  ASSERT_EQ(CL_ClearBattlescapeEvents(), lengthof(events));
57 }
58 
60 
61 TEST_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 
83 static 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 
95 TEST_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 
128 static bool delayCheckBlockedVal = false;
129 static bool delayCheckBlocked (int now, void* data)
130 {
131  return delayCheckBlockedVal;
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 
164  delayCheckBlockedVal = true;
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 }
static void SetUpTestCase()
Definition: test_events.cpp:33
TEST_F(EventsTest, Range)
Definition: test_events.cpp:43
void event_func(int now, void *data)
Definition: common.h:324
static void TearDownTestCase()
Definition: test_events.cpp:38
voidpf void * buf
Definition: ioapi.h:42
memPool_t * cl_genericPool
Definition: cl_main.cpp:86
static bool delayCheckBlocked(int now, void *data)
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:1415
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
event_t
Possible event values.
Definition: q_shared.h:79
#define Mem_CreatePool(name)
Definition: mem.h:32
#define EVENT_INSTANTLY
Definition: q_shared.h:73
static bool delayCheck(int now, void *data)
Definition: test_events.cpp:83
static bool delayCheckBlockedVal
const eventRegister_t events[]
List of functions to register nodes.
Definition: e_main.cpp:92
QGL_EXTERN GLint i
Definition: r_gl.h:113
void TEST_Init(void)
Definition: test_shared.cpp:72
void NET_WriteByte(dbuffer *buf, byte c)
Definition: netpack.cpp:39
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
#define lengthof(x)
Definition: shared.h:105
GLsizei const GLvoid * data
Definition: r_gl.h:152
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:1362