File: Tester.java

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (370 lines) | stat: -rw-r--r-- 12,882 bytes parent folder | download | duplicates (7)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright Hugh Perkins 2009
// hughperkins@gmail.com http://manageddreams.com
//
// 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.
//
// You should have received a copy of the GNU General Public License along
// with this program in the file licence.txt; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
// 1307 USA
// You can find the licence also on the web at:
// http://www.opensource.org/licenses/gpl-license.php
//
// ======================================================================================
//

package hughai.test;

import java.util.List;

import com.springrts.ai.*;
import com.springrts.ai.oo.*;
import com.springrts.ai.oo.clb.*;

import hughai.*;
import hughai.unitdata.*;
import hughai.utils.*;
import hughai.basictypes.*;
import hughai.ui.*;

// performance tests and such...
public class Tester {
   Config config;
   PlayerObjects playerObjects;
   OOAICallback aicallback;
   LogFile logfile;
   CSAI csai;

   public Tester( PlayerObjects playerObjects ) {
      this.playerObjects = playerObjects;

      csai = playerObjects.getCSAI();
      config = playerObjects.getConfig();
      aicallback = playerObjects.getAicallback();
      logfile = playerObjects.getLogFile();

      //      csai.RegisterVoiceCommand( "runtests", new VoiceGo() );
      playerObjects.getMainUI().registerButton( "Run tests",
            new ButtonRunTests() );
      playerObjects.getMainUI().registerButton( "Run drawline tests",
            new ButtonRunDrawlineTests() );
   }

   class ButtonRunTests implements MainUI.ButtonHandler {
      @Override
      public void go() {
         csai.sendTextMessage( "Running tests.   please check console" );
         runTests();
      }
   }

   class ButtonRunDrawlineTests implements MainUI.ButtonHandler {
      @Override
      public void go() {
         csai.sendTextMessage( "Running drawline tests." );
         drawlineTests();
      }
   }

   void drawlineTests() {
      drawlineTests3();
      //    drawlineTests2();
      //      drawmapTests1();
//      drawmapTests2();
   }

   // This will crash spring, at least with Java interface 0.1,
   // running against Spring 0.80.02 :
   void drawlineTests2() {
      aicallback.getMap().getDrawer().getFigure().drawLine(
          new AIFloat3( 0,100,0 ),
          new AIFloat3( 100, 100 , 0 ),
          1,
          false,
          200,
          0 );
      //      AIFloat3 pos1, AIFloat3 pos2, float width, 
      //      boolean arrow, int lifeTime, int figureGroupId, 
      //      int ret_newFigureGroupId)
   }

   void drawmapTests1() {
      Map gamemap = aicallback.getMap();
      int mapwidth = gamemap.getWidth();
      int mapheight = gamemap.getHeight();
      boolean[][]map = new boolean[mapwidth][mapheight];
      DrawingUtils drawingUtils = playerObjects.getDrawingUtils();

      for( int x = 100; x < 320; x++ ) {
         for( int z = 100; z < 320; z++ ) {
            map[x][z] = true;
         }
      }
      drawingUtils.DrawMap( map );
   }

   void drawmapTests2() {
      Map gamemap = aicallback.getMap();
      int mapwidth = gamemap.getWidth();
      int mapheight = gamemap.getHeight();
      boolean[][]map = new boolean[mapwidth][mapheight];
      DrawingUtils drawingUtils = playerObjects.getDrawingUtils();

      int thismapwidth = mapwidth;
      int thismapheight = mapheight;

      for( int x = 100; x < 320; x++ ) {
         for( int z = 100; z < 320; z++ ) {
            map[x][z] = true;
         }
      }
      final int multiplier = 8;
      for( int z = 0; z < thismapheight; z += 1 )
      {
         for (int x = 0; x < thismapwidth; x+= 1 )
         {
            //            float elevation = gamemap.getElevationAt( x *multiplier, y * multiplier ) + 10;
            //float elevation = heightMapArray[x][z];
            float elevation = 100;
            if (x < (thismapwidth - 1) &&
                  map[x][z] != map[x + 1][z] )
            {
               drawingUtils.AddLine(new TerrainPos((x + 1) * multiplier, elevation, z * multiplier),
                     new TerrainPos((x + 1) * multiplier, elevation, (z + 1) * multiplier) );
            }
            if (z < (thismapheight - 1) &&
                  map[x][z] != map[x][z + 1] )
            {
               drawingUtils.AddLine(new TerrainPos(x * multiplier, elevation, (z + 1) * multiplier),
                     new TerrainPos((x + 1) * multiplier, elevation, (z + 1) * multiplier) );
            }
         }
      }
   }

   // this crashes spring as soon as the area where the lines are being
   // drawn comes into view...
   void drawlineTests3() {
      Map gamemap = aicallback.getMap();
      int mapwidth = gamemap.getWidth();
      int mapheight = gamemap.getHeight();
      DrawingUtils drawingUtils = playerObjects.getDrawingUtils();
      for( int i = 0; i < 8193; i++ ) {
         drawingUtils.AddLine( new TerrainPos( 100, 100, 100 ), 
               new TerrainPos( 200, 100, 100 ) );
      }
   }
   
   void drawlineTests1() {
      Map gamemap = aicallback.getMap();
      int mapwidth = gamemap.getWidth();
      int mapheight = gamemap.getHeight();
      DrawingUtils drawingUtils = playerObjects.getDrawingUtils();
      drawingUtils.AddLine( new TerrainPos( -100, 100, -100 ), 
            new TerrainPos( 5000, 100, 5000 ) );
      for( int z = 0; z < mapheight; z++ ) {
         drawingUtils.AddLine( new TerrainPos( 0, 100, z * 8 ), 
               new TerrainPos( 100, 100, z * 8 ) );         
         drawingUtils.AddLine( new TerrainPos( (mapwidth - 10 ) * 8, 100, z * 8 ), 
               new TerrainPos( mapwidth * 8, 100, z * 8 ) );         
         drawingUtils.AddLine( new TerrainPos( 0, 100, z * 8 ), 
               new TerrainPos( mapwidth * 8, 100, z * 8 ) );         
      }
      for( int x = 0; x < mapheight; x++ ) {
         drawingUtils.AddLine( new TerrainPos( x * 8, 100, 0 ), 
               new TerrainPos( x * 8, 100, 1000 ) );         
         drawingUtils.AddLine( new TerrainPos( x * 8, 100, (mapheight - 10 ) * 8 ), 
               new TerrainPos( x * 8, 100, (mapheight - 0 ) * 8 ) );         
         drawingUtils.AddLine( new TerrainPos( x * 8, 100, 0 ), 
               new TerrainPos( x * 8, 100, (mapheight - 0 ) * 8 ) );         
      }
   }

   class VoiceGo implements VoiceCommandHandler {
      @Override
      public void commandReceived( String command, String[] args, int team ) {
         runTests();
      }
   }

   void runTests() {
      testgetmilliseconds();
      testgetunitdefs();
      testgetfriendlyunits();
      testgetpos();      
      testgetunitdef();     
      testgetsquareddistance();
      testismobile();
      testunithash();
      testgethumanname();
      testgetspeed();
      testCheats();
   }
   
   void debug( Object message ) {
      logfile.WriteLine( "" + message );      
   }
   
   void dumpEnemyUnits() {
      debug("num units: " + aicallback.getEnemyUnits().size() );
      for( Unit unit : aicallback.getEnemyUnits() ) {
         debug( unit.getUnitId() + " " + unit.getDef().getHumanName() + " " + unit.getPos() );
      }      
   }
   
   void testCheats() {
      boolean previousCheatState = aicallback.getCheats().isEnabled();
      
      logfile.WriteLine( "Enemy units with cheating off:" );
      aicallback.getCheats().setEnabled( false );
      dumpEnemyUnits();
      logfile.WriteLine( "Enemy units with cheating ON:" );
      aicallback.getCheats().setEnabled( true );
      dumpEnemyUnits();
      
      aicallback.getCheats().setEnabled( previousCheatState );
   }

   // control test: no test can go faster than this one
   void testgetmilliseconds() {
      //    logfile.WriteLine( "testgetmilliseconds >>>" );
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         count++;
      }
      logfile.WriteLine( "testgetmilliseconds count: " + count );
   }

   void testgetunitdefs() {
      //      logfile.WriteLine( "testgetunitdefs >>>" );
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         aicallback.getUnitDefs();
         count++;
      }
      logfile.WriteLine( "testgetunitdefs count: " + count );
   }

   void testgetfriendlyunits() {
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         aicallback.getFriendlyUnits();
         count++;
      }
      logfile.WriteLine( "testgetfriendlyunits count: " + count );      
   }

   void testgetpos() {
      Unit unit = aicallback.getFriendlyUnits().get( 0 );
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         AIFloat3 pos = unit.getPos();
         count++;
      }
      logfile.WriteLine( "testgetpos count: " + count );      
   }

   void testgetunitdef() {
      Unit unit = aicallback.getFriendlyUnits().get( 0 );
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         UnitDef unitdef = unit.getDef();
         count++;
      }
      logfile.WriteLine( "testgetunitdef count: " + count );            
   }

   void testgethumanname() {
      UnitDef unitdef = aicallback.getFriendlyUnits().get( 0 ).getDef();
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         String name = unitdef.getHumanName();
         if( name.equals( "foo" ) ) {
            logfile.WriteLine( "blah" );
         }
         count++;
      }
      logfile.WriteLine( "testgethumanname count: " + count );            
   }

   void testgetspeed() {
      UnitDef unitdef = aicallback.getFriendlyUnits().get( 0 ).getDef();
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         float speed = unitdef.getSpeed();
         count++;
      }
      logfile.WriteLine( "testgetspeed count: " + count );            
   }

   void testgetsquareddistance() {
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      Float3 a = new Float3( 1000.3353f, 125.25f, 2353.35f );
      Float3 b = new Float3( 3550.3353f, 130.25f, 3235.35f );
      long totaldistance = 0;
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         float distance = a.GetSquaredDistance( b );
         totaldistance += distance;
         b.x += 1;
         count++;
      }
      logfile.WriteLine( "testgetsquareddistance count: " + count );            
   }

   void testismobile() {
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      Float3 a = new Float3( 1000.3353f, 125.25f, 2353.35f );
      Float3 b = new Float3( 3550.3353f, 130.25f, 3235.35f );
      long totaldistance = 0;
      UnitDefHelp unitDefHelp = playerObjects.getUnitDefHelp();
      List<UnitDef> unitdefs = aicallback.getUnitDefs();
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         float distance = a.GetSquaredDistance( b );
         totaldistance += distance;
         b.x += 1;
         UnitDef unitdef = unitdefs.get( count % 100 );
         unitDefHelp.IsMobile( unitdef );
         count++;
      }
      logfile.WriteLine( "testismobile count: " + count );                  
   }

   void testunithash() {
      int count = 0;
      long start = System.currentTimeMillis();
      long finish = start + 1000;
      Unit unit1 = aicallback.getFriendlyUnits().get( 0 );
      Unit unit2 = aicallback.getFriendlyUnits().get( 1 );
      while( ( (byte)count != 0 ) || ( System.currentTimeMillis() < finish ) ) {
         int hashcode = unit1.hashCode();
         count++;
      }
      logfile.WriteLine( "testunithash count: " + count );                  
   }
}