UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_rma.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 "../server/server.h"
27 #include "../server/sv_rma.h"
28 #include "../ports/system.h"
29 
30 #define MAX_ALLOWED_TIME_TO_ASSEMBLE 30000
31 #define TEST_THEME "forest"
32 #define TEST_ASSEMBLY "nature_large_b"
33 
36 static cvar_t svt;
38 
39 class RandomMapAssemblyTest: public ::testing::Test {
40 protected:
41  static void SetUpTestCase() {
42  TEST_Init();
43  Com_ParseScripts(true);
44 
45  sv_dumpmapassembly = Cvar_Get("sv_dumpassembly", "0");
46  sv_rma = Cvar_Get("sv_rma_tmp", "2");
47  sv_rmadisplaythemap = Cvar_Get("sv_rmadisplaythemap", "1");
48 
49  if (!sv_threads) {
50  sv_threads = &svt;
51  sv_threads->integer = 0;
52  }
53 
54  if (!sv_maxclients) {
57  }
58  }
59 
60  long testAssembly(const char *source, const unsigned int numRuns, const char *mapTheme, const char *mapAssembly);
61 
62  static void TearDownTestCase() {
63  TEST_Shutdown();
64  }
65 };
66 
67 long RandomMapAssemblyTest::testAssembly(const char *source, const unsigned int numRuns, const char *mapTheme, const char *mapAssembly)
68 {
69  char entityString[MAX_TOKEN_CHARS];
70  long time = 0;
71 
72  for (int i = 0; i < numRuns; i++) {
74  srand(i);
75  Com_Printf("%s - assembling map: theme: %s assembly: %s seed: %i\n", source, mapTheme, mapAssembly, i);
76 
77  long time = Sys_Milliseconds();
78  int numPlaced = SV_AssembleMap(mapTheme, mapAssembly, mapStr, posStr, entityString, i, true);
79  time = Sys_Milliseconds() - time;
80 
81  if (numPlaced < 1) {
82  Com_Printf("%s - error: No tiles placed.\n", source);
83  ADD_FAILURE() << source << " - error: No tiles placed (theme: " << mapTheme
84  << " assembly: " << mapAssembly << " seed: " << i << ").";
85  }
86  if (time > MAX_ALLOWED_TIME_TO_ASSEMBLE) {
87  Com_Printf("%s - error: Assembly %s in map theme +%s failed to assemble in a reasonable time with seed %i\n", source, mapTheme, mapAssembly, i);
88  ADD_FAILURE() << "Assembly " << mapTheme << "in map theme +" << mapAssembly
89  << " failed " << source << "using seed: %i " << i
90  << "(time measured: " << time << " ms).";
91  }
92 
93  Com_Printf("%s - result: seed: %i tiles placed: %i time measured: %li ms\n", source, i, numPlaced, time);
94  fflush(stdout);
95  }
96  return time;
97 }
98 
100 {
101  char entityString[MAX_TOKEN_CHARS];
102 
103  srand(0);
104  int numPlaced = SV_AssembleMap("test_extends", "default", mapStr, posStr, entityString, 0, true);
105  ASSERT_TRUE(numPlaced != 0);
106 }
107 
109 {
110  char entityString[MAX_TOKEN_CHARS];
111 
112  srand(0);
113  int numPlaced = SV_AssembleMap(TEST_THEME, TEST_ASSEMBLY, mapStr, posStr, entityString, 0, true);
114  ASSERT_TRUE(numPlaced != 0);
115 }
116 
117 /* timeout version */
118 TEST_F(RandomMapAssemblyTest, MassAssemblyTimeout)
119 {
120  const char *self = "RandomMapAssemblyTest.MassAssemblyTimeout";
121  unsigned const int numRuns = 10;
122  sv_threads->integer = 1;
123  SCOPED_TRACE(va(self));
124  testAssembly(self, numRuns, TEST_THEME, TEST_ASSEMBLY);
125 }
126 
127 TEST_F(RandomMapAssemblyTest, MassAssemblyParallel)
128 {
129  const char *self = "RandomMapAssemblyTest.MassAssemblyParallel";
130  unsigned const int numRuns = 10;
131  sv_threads->integer = 2;
132  SCOPED_TRACE(va(self));
133  testAssembly(self, numRuns, TEST_THEME, TEST_ASSEMBLY);
134 }
135 
136 /* sequential version */
137 TEST_F(RandomMapAssemblyTest, MassAssemblySequential)
138 {
139  const char *self = "RandomMapAssemblyTest.MassAssemblySequential";
140  unsigned const int numRuns = 10;
141  sv_threads->integer = 0;
142  SCOPED_TRACE(va(self));
143  testAssembly(self, numRuns, TEST_THEME, TEST_ASSEMBLY);
144 }
145 
146 /* test the maps that have seedlists */
148 {
149  const char* assNames[][2] = {
150  {"farm", "medium"},
151  {"farm", "large"},
153  {"forest", "nature_medium_b"},
154  {"oriental", "large"},
155  {"village", "large"},
156  {"village", "small"}
157  };
158 
159  size_t length = sizeof(assNames) / (2 * sizeof(char*));
160  const char *self = "RandomMapAssemblyTest.Seedlists";
161  unsigned const int numRuns = 20;
162  sv_threads->integer = 0;
163  long timeSum = 0;
164 
165  SCOPED_TRACE(va(self));
166  for (int n = 0; n < length; n++) {
167  timeSum += testAssembly(self, numRuns, assNames[n][0], assNames[n][1]);
168  }
169  Com_Printf("%s - result: time total %li ms\n", self, timeSum);
170 }
171 
172 #define SEED_TEST 0
173 #if SEED_TEST
174 
178 TEST_F(RandomMapAssemblyTest, NewSeedlists)
179 {
180  long time, timeSum = 0;
181  char entityString[MAX_TOKEN_CHARS];
182 
183  sv_rmadisplaythemap->integer = 1; /* print out the RMA analysis */
184  sv_threads->integer = 0;
185  for (int i = 0; i < RMA_HIGHEST_SUPPORTED_SEED; i++) {
186  srand(i);
187  time = Sys_Milliseconds();
188  Com_Printf("Seed: %i\n", i);
189  Cvar_Set("rm_drop", Com_GetRandomMapAssemblyNameForCraft("craft_drop_herakles"));
190  Cvar_Set("rm_ufo", Com_GetRandomMapAssemblyNameForCraft("craft_ufo_fighter"));
191  const char* mapTheme = "industrial";
192  const char* asmName = "medium";
193 #if 0
194  mapTheme = "tropic"; asmName = "river";
195  mapTheme = "village"; asmName = "large";
196  mapTheme = "desert"; asmName = "large";
197 #endif
198  int numPlaced = SV_AssembleMap(mapTheme, asmName, mapStr, posStr, entityString, i, false);
199  ASSERT_TRUE(numPlaced != 0);
200  time = Sys_Milliseconds() - time;
201  timeSum += time;
202  ASSERT_TRUE(time < MAX_ALLOWED_TIME_TO_ASSEMBLE) << mapTheme << " fails to assemble in a reasonable time with seed " << i << "(time: " << time << " ms)";
203  if (time > 10000)
204  Com_Printf("Seed %i: tiles: %i ms: %li\n", i, numPlaced, time);
205  }
206  Com_Printf("TotalTime: %li\n", timeSum);
207 }
208 #endif
#define TEST_THEME
Definition: test_rma.cpp:31
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
Definition: shared.cpp:410
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
cvar_t * sv_maxclients
Definition: g_main.cpp:43
#define MAX_TILESTRINGS
Definition: q_shared.h:298
#define RMA_HIGHEST_SUPPORTED_SEED
Definition: sv_rma.h:31
cvar_t * sv_rma
Definition: sv_main.cpp:49
void Com_ParseScripts(bool onlyServer)
Definition: scripts.cpp:3641
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
static cvar_t maxclients
Definition: test_rma.cpp:37
int integer
Definition: cvar.h:81
const char * Com_GetRandomMapAssemblyNameForCraft(const char *craftID)
Returns the name of an aircraft or an ufo that is used in the ump files for the random map assembly...
Definition: scripts.cpp:3299
cvar_t * sv_threads
Definition: sv_main.cpp:48
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition: cvar.cpp:342
#define MAX_ALLOWED_TIME_TO_ASSEMBLE
Definition: test_rma.cpp:30
QGL_EXTERN GLuint GLsizei GLsizei * length
Definition: r_gl.h:110
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
static cvar_t svt
Definition: test_rma.cpp:36
#define MAX_TOKEN_CHARS
Definition: defines.h:372
#define TEST_ASSEMBLY
Definition: test_rma.cpp:32
Stores the parsed data of an assembly definition. See *.ump files.
Definition: sv_rma.cpp:108
static char mapStr[MAX_TOKEN_CHARS *MAX_TILESTRINGS]
Definition: test_rma.cpp:34
QGL_EXTERN GLint i
Definition: r_gl.h:113
void TEST_Init(void)
Definition: test_shared.cpp:72
int SV_AssembleMap(const char *mapTheme, const char *assembly, char *asmTiles, char *asmPos, char *entityString, const unsigned int seed, bool print)
Definition: sv_rma.cpp:2212
cvar_t * sv_rmadisplaythemap
display a character graphic of the tiles placed when RMA2 reaches a dead end.
Definition: sv_main.cpp:50
long testAssembly(const char *source, const unsigned int numRuns, const char *mapTheme, const char *mapAssembly)
Definition: test_rma.cpp:67
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
Definition: cvar.cpp:615
static char posStr[MAX_TOKEN_CHARS *MAX_TILESTRINGS]
Definition: test_rma.cpp:35
TEST_F(RandomMapAssemblyTest, UMPExtends)
Definition: test_rma.cpp:99
static void SetUpTestCase()
Definition: test_rma.cpp:41
cvar_t * sv_dumpmapassembly
Definition: sv_main.cpp:47
static void TearDownTestCase()
Definition: test_rma.cpp:62
int Sys_Milliseconds(void)
Definition: unix_shared.cpp:41