yapf/unittest/unittest.cpp
changeset 3900 2c84ed52709d
child 3971 77061ae9b534
equal deleted inserted replaced
3899:5ba7f20a14ca 3900:2c84ed52709d
       
     1 /* $Id$ */
       
     2 
       
     3 #define UNITTEST
       
     4 
       
     5 #include "../../stdafx.h"
       
     6 
       
     7 EXTERN_C_BEGIN
       
     8 #include "../../macros.h"
       
     9 #include "../../tile.h"
       
    10 #include "../../openttd.h"
       
    11 #include "../../map.h"
       
    12 #include "../../rail.h"
       
    13 EXTERN_C_END
       
    14 
       
    15 //#include "../track_dir.hpp"
       
    16 
       
    17 #include "../yapf.hpp"
       
    18 
       
    19 #include "../autocopyptr.hpp"
       
    20 
       
    21 #include "unittest.h"
       
    22 
       
    23 #include "test_autocopyptr.h"
       
    24 #include "test_binaryheap.h"
       
    25 #include "test_fixedsizearray.h"
       
    26 #include "test_blob.h"
       
    27 #include "test_hashtable.h"
       
    28 #include "test_yapf.h"
       
    29 
       
    30 int _total_pf_time_us = 0;
       
    31 
       
    32 int num_tests_failed = 0;
       
    33 int num_tests_total = 0;
       
    34 bool _dbg = false;
       
    35 
       
    36 int do_test(const char* name, TESTPROC test_proc, bool silent)
       
    37 {
       
    38 	printf("%s ", name);
       
    39 	if (!silent) {printf("[enter]:"); getc(stdin);}
       
    40 	_dbg = !silent;
       
    41 	fflush(stdout);
       
    42 	int res = test_proc(silent);
       
    43 	if (res == 0)
       
    44 	{
       
    45 		printf("%s OK\n", silent ? "..." : "\n");
       
    46 	}
       
    47 	else {
       
    48 		printf("\n ERROR! (0x%X)\n", res);
       
    49 		printf("\nFailed cases:");
       
    50 		int num_failed = 0;
       
    51 		for(int i = 0; i < 32; i++) {
       
    52 			if (((1 << i) & res) != 0) {
       
    53 				printf(" %d", i);
       
    54 				num_failed++;
       
    55 			}
       
    56 		}
       
    57 		printf("\n\nTotal: %d cases failed\n\n", num_failed);
       
    58 	}
       
    59 
       
    60 	num_tests_total++;
       
    61 	if (res != 0) num_tests_failed++;
       
    62 
       
    63 	return (res == 0) ? 0 : 1;
       
    64 }
       
    65 
       
    66 struct TEST_RECORD {
       
    67 	const char* name;
       
    68 	TESTPROC    testproc;
       
    69 };
       
    70 
       
    71 TEST_RECORD tests[] = {
       
    72 	{"AutoCopyPtr test"   , &TestAutoCopyPtr         },
       
    73 	{"BinaryHeap test 1"  , &TestBinaryHeap1         },
       
    74 	{"BinaryHeap test 2"  , &TestBinaryHeap2         },
       
    75 	{"FixedSizeArray test", &TestFixedSizeArray      },
       
    76 	{"Array test"         , &TestArray               },
       
    77 	{"Blob test 1"        , &TestBlob1               },
       
    78 	{"Blob test 2"        , &TestBlob2               },
       
    79 	{"HashTable test 1"   , &TestHashTable1          },
       
    80 	{"Yapf test 1"        , &CTestYapf1::stTestAstar },
       
    81 	{"Yapf test 2"        , &CTestYapf2::stTestAstar },
       
    82 
       
    83 	{NULL                 , NULL                     },
       
    84 };
       
    85 
       
    86 int main(int argc, char** argv)
       
    87 {
       
    88 	bool silent = (argc == 1);
       
    89 
       
    90 	for (TEST_RECORD* tr = tests; tr->name != NULL; tr++)
       
    91 		do_test(tr->name, tr->testproc, silent);
       
    92 
       
    93 	if (num_tests_failed == 0)
       
    94 		printf("\nALL %d TESTS PASSED OK!\n\n", num_tests_total);
       
    95 	else
       
    96 		printf("\n****** %d (from %d of total) TEST(S) FAILED! ******\n", num_tests_failed, num_tests_total);
       
    97 	return 0;
       
    98 }
       
    99 
       
   100 
       
   101 
       
   102 extern "C"
       
   103 const TileIndexDiffC _tileoffs_by_dir[] = {
       
   104 	{-1,  0},
       
   105 	{ 0,  1},
       
   106 	{ 1,  0},
       
   107 	{ 0, -1}
       
   108 };
       
   109 
       
   110 extern "C"
       
   111 const byte _ffb_64[128] = {
       
   112 	0,0,1,0,2,0,1,0,
       
   113 	3,0,1,0,2,0,1,0,
       
   114 	4,0,1,0,2,0,1,0,
       
   115 	3,0,1,0,2,0,1,0,
       
   116 	5,0,1,0,2,0,1,0,
       
   117 	3,0,1,0,2,0,1,0,
       
   118 	4,0,1,0,2,0,1,0,
       
   119 	3,0,1,0,2,0,1,0,
       
   120 
       
   121 	0,0,0,2,0,4,4,6,
       
   122 	0,8,8,10,8,12,12,14,
       
   123 	0,16,16,18,16,20,20,22,
       
   124 	16,24,24,26,24,28,28,30,
       
   125 	0,32,32,34,32,36,36,38,
       
   126 	32,40,40,42,40,44,44,46,
       
   127 	32,48,48,50,48,52,52,54,
       
   128 	48,56,56,58,56,60,60,62,
       
   129 };
       
   130 
       
   131 /* Maps a trackdir to the (4-way) direction the tile is exited when following
       
   132 * that trackdir */
       
   133 extern "C"
       
   134 const DiagDirection _trackdir_to_exitdir[] = {
       
   135 	DIAGDIR_NE,DIAGDIR_SE,DIAGDIR_NE,DIAGDIR_SE,DIAGDIR_SW,DIAGDIR_SE, DIAGDIR_NE,DIAGDIR_NE,
       
   136 	DIAGDIR_SW,DIAGDIR_NW,DIAGDIR_NW,DIAGDIR_SW,DIAGDIR_NW,DIAGDIR_NE,
       
   137 };
       
   138 
       
   139 /* Maps a diagonal direction to the all trackdirs that are connected to any
       
   140 * track entering in this direction (including those making 90 degree turns)
       
   141 */
       
   142 extern "C"
       
   143 const TrackdirBits _exitdir_reaches_trackdirs[] = {
       
   144 	TRACKDIR_BIT_X_NE | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_LEFT_N,  /* DIAGDIR_NE */
       
   145 	TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E, /* DIAGDIR_SE */
       
   146 	TRACKDIR_BIT_X_SW | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_RIGHT_S, /* DIAGDIR_SW */
       
   147 	TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W  /* DIAGDIR_NW */
       
   148 };
       
   149 
       
   150 /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
       
   151 extern "C"
       
   152 const TrackdirBits _track_crosses_trackdirs[] = {
       
   153 	TRACKDIR_BIT_Y_SE     | TRACKDIR_BIT_Y_NW,                                                   /* TRACK_X     */
       
   154 	TRACKDIR_BIT_X_NE     | TRACKDIR_BIT_X_SW,                                                   /* TRACK_Y     */
       
   155 	TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,  /* TRACK_UPPER */
       
   156 	TRACKDIR_BIT_RIGHT_N  | TRACKDIR_BIT_RIGHT_S  | TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LEFT_S,  /* TRACK_LOWER */
       
   157 	TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E, /* TRACK_LEFT  */
       
   158 	TRACKDIR_BIT_UPPER_W  | TRACKDIR_BIT_UPPER_E  | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_LOWER_E  /* TRACK_RIGHT */
       
   159 };
       
   160 
       
   161 
       
   162 
       
   163 
       
   164 
       
   165 
       
   166