test/automated/SDL_at.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 3482 78db4f7ae2f3
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
slouken@3259
     1
/*
slouken@3259
     2
 * Common code for automated test suite.
slouken@3259
     3
 *
slouken@3259
     4
 * Written by Edgar Simo "bobbens"
slouken@3259
     5
 *
slouken@3259
     6
 * Released under Public Domain.
slouken@3259
     7
 */
slouken@3259
     8
slouken@3259
     9
slouken@3259
    10
#include "SDL_at.h"
slouken@3452
    11
#include "SDL_stdinc.h"
lestat@3463
    12
#include "SDL_error.h"
slouken@3259
    13
slouken@3259
    14
#include <stdio.h> /* printf/fprintf */
slouken@3259
    15
#include <stdarg.h> /* va_list */
slouken@3259
    16
slouken@3259
    17
slouken@3259
    18
/*
slouken@3259
    19
 * Internal usage SDL_AT variables.
slouken@3259
    20
 */
slouken@3259
    21
static char *at_suite_msg = NULL; /**< Testsuite message. */
slouken@3259
    22
static char *at_test_msg = NULL; /**< Testcase message. */
slouken@3259
    23
static int at_success = 0; /**< Number of successful testcases. */
slouken@3259
    24
static int at_failure = 0; /**< Number of failed testcases. */
slouken@3259
    25
slouken@3259
    26
slouken@3259
    27
/*
slouken@3259
    28
 * Global properties.
slouken@3259
    29
 */
slouken@3259
    30
static int at_verbose = 0; /**< Verbosity. */
slouken@3259
    31
static int at_quiet = 0; /**< Quietness. */
slouken@3259
    32
slouken@3259
    33
slouken@3259
    34
/*
slouken@3259
    35
 * Prototypes.
slouken@3259
    36
 */
slouken@3259
    37
static void SDL_ATcleanup (void);
slouken@3259
    38
static void SDL_ATendWith( int success );
slouken@3259
    39
static void SDL_ATassertFailed( const char *msg );
slouken@3259
    40
slouken@3259
    41
slouken@3259
    42
/**
slouken@3259
    43
 * @brief Cleans up the automated testsuite state.
slouken@3259
    44
 */
slouken@3259
    45
static void SDL_ATcleanup (void)
slouken@3259
    46
{
slouken@3259
    47
   if (at_suite_msg != NULL)
slouken@3452
    48
      SDL_free(at_suite_msg);
slouken@3259
    49
   at_suite_msg   = NULL;
slouken@3259
    50
   if (at_test_msg != NULL)
slouken@3452
    51
      SDL_free(at_test_msg);
slouken@3259
    52
   at_test_msg    = NULL;
slouken@3259
    53
   at_success     = 0;
slouken@3259
    54
   at_failure     = 0;
slouken@3259
    55
}
slouken@3259
    56
slouken@3259
    57
slouken@3259
    58
/**
slouken@3259
    59
 * @brief Begin testsuite.
slouken@3259
    60
 */
slouken@3259
    61
void SDL_ATinit( const char *suite )
slouken@3259
    62
{
slouken@3259
    63
   /* Do not open twice. */
slouken@3259
    64
   if (at_suite_msg) {
slouken@3259
    65
      SDL_ATprintErr( "AT suite '%s' not closed before opening suite '%s'\n",
slouken@3259
    66
            at_suite_msg, suite );
slouken@3259
    67
   }
slouken@3259
    68
   /* Must have a name. */
slouken@3259
    69
   if (suite == NULL) {
slouken@3259
    70
      SDL_ATprintErr( "AT testsuite does not have a name.\n");
slouken@3259
    71
   }
slouken@3259
    72
   SDL_ATcleanup();
slouken@3452
    73
   at_suite_msg = SDL_strdup(suite);
slouken@3259
    74
slouken@3259
    75
   /* Verbose message. */
slouken@3259
    76
   SDL_ATprintVerbose( 2, "--+---> Started Test Suite '%s'\n", at_suite_msg );
slouken@3259
    77
}
slouken@3259
    78
slouken@3259
    79
slouken@3259
    80
/**
slouken@3259
    81
 * @brief Finish testsuite.
slouken@3259
    82
 */
slouken@3259
    83
int SDL_ATfinish (void)
slouken@3259
    84
{
slouken@3259
    85
   int failed;
slouken@3259
    86
slouken@3259
    87
   /* Make sure initialized. */
slouken@3259
    88
   if (at_suite_msg == NULL) {
slouken@3259
    89
      SDL_ATprintErr("Ended testcase without initializing.\n");
slouken@3259
    90
      return 1;
slouken@3259
    91
   }
slouken@3259
    92
slouken@3259
    93
   /* Finished without closing testcase. */
slouken@3259
    94
   if (at_test_msg) {
slouken@3259
    95
      SDL_ATprintErr( "AT suite '%s' finished without closing testcase '%s'\n",
slouken@3259
    96
            at_suite_msg, at_test_msg );
slouken@3259
    97
   }
slouken@3259
    98
slouken@3259
    99
   /* Verbose message. */
slouken@3259
   100
   SDL_ATprintVerbose( 2, "<-+---- Finished Test Suite '%s'\n", at_suite_msg );
slouken@3259
   101
slouken@3259
   102
   /* Display message if verbose on failed. */
slouken@3259
   103
   failed = at_failure;
slouken@3259
   104
   if (at_failure > 0) {
slouken@3259
   105
      SDL_ATprintErr( "%s : Failed %d out of %d testcases!\n",
slouken@3259
   106
            at_suite_msg, at_failure, at_failure+at_success );
slouken@3259
   107
   }
slouken@3259
   108
   else {
slouken@3259
   109
      SDL_ATprint( "%s : All tests successful (%d)\n",
slouken@3259
   110
            at_suite_msg, at_success );
slouken@3259
   111
   }
slouken@3259
   112
slouken@3259
   113
   /* Clean up. */
slouken@3259
   114
   SDL_ATcleanup();
slouken@3259
   115
slouken@3259
   116
   /* Return failed. */
slouken@3259
   117
   return failed;
slouken@3259
   118
}
slouken@3259
   119
slouken@3259
   120
slouken@3259
   121
/**
slouken@3259
   122
 * @brief Sets a property.
slouken@3259
   123
 */
slouken@3259
   124
void SDL_ATseti( int property, int value )
slouken@3259
   125
{
slouken@3259
   126
   switch (property) {
slouken@3259
   127
      case SDL_AT_VERBOSE:
slouken@3259
   128
         at_verbose = value;
slouken@3259
   129
         break;
slouken@3259
   130
slouken@3259
   131
      case SDL_AT_QUIET:
slouken@3259
   132
         at_quiet = value;
slouken@3259
   133
         break;
slouken@3259
   134
   }
slouken@3259
   135
}
slouken@3259
   136
slouken@3259
   137
slouken@3259
   138
/**
slouken@3259
   139
 * @brief Gets a property.
slouken@3259
   140
 */
slouken@3259
   141
void SDL_ATgeti( int property, int *value )
slouken@3259
   142
{
slouken@3259
   143
   switch (property) {
slouken@3259
   144
      case SDL_AT_VERBOSE:
slouken@3259
   145
         *value = at_verbose;
slouken@3259
   146
         break;
slouken@3259
   147
slouken@3259
   148
      case SDL_AT_QUIET:
slouken@3259
   149
         *value = at_quiet;
slouken@3259
   150
         break;
slouken@3259
   151
   }
slouken@3259
   152
}
slouken@3259
   153
slouken@3259
   154
slouken@3259
   155
/**
slouken@3259
   156
 * @brief Begin testcase.
slouken@3259
   157
 */
slouken@3259
   158
void SDL_ATbegin( const char *testcase )
slouken@3259
   159
{
slouken@3259
   160
   /* Do not open twice. */
slouken@3259
   161
   if (at_test_msg) {
slouken@3259
   162
      SDL_ATprintErr( "AT testcase '%s' not closed before opening testcase '%s'\n",
slouken@3259
   163
            at_test_msg, testcase );
slouken@3259
   164
   }
slouken@3259
   165
   /* Must have a name. */
slouken@3259
   166
   if (testcase == NULL) {
slouken@3259
   167
      SDL_ATprintErr( "AT testcase does not have a name.\n");
slouken@3259
   168
   }
slouken@3452
   169
   at_test_msg = SDL_strdup(testcase);
slouken@3259
   170
slouken@3259
   171
   /* Verbose message. */
slouken@3259
   172
   SDL_ATprintVerbose( 2, "  +---> StartedTest Case '%s'\n", testcase );
slouken@3259
   173
}
slouken@3259
   174
slouken@3259
   175
slouken@3259
   176
/**
slouken@3259
   177
 * @brief Ends the testcase with a succes or failure.
slouken@3259
   178
 */
slouken@3259
   179
static void SDL_ATendWith( int success )
slouken@3259
   180
{
slouken@3259
   181
   /* Make sure initialized. */
slouken@3259
   182
   if (at_test_msg == NULL) {
slouken@3259
   183
      SDL_ATprintErr("Ended testcase without initializing.\n");
slouken@3259
   184
      return;
slouken@3259
   185
   }
slouken@3259
   186
slouken@3259
   187
   /* Mark as success or failure. */
slouken@3259
   188
   if (success)
slouken@3259
   189
      at_success++;
slouken@3259
   190
   else
slouken@3259
   191
      at_failure++;
slouken@3259
   192
slouken@3259
   193
   /* Verbose message. */
slouken@3259
   194
   SDL_ATprintVerbose( 2, "  +---- Finished Test Case '%s'\n", at_test_msg );
slouken@3259
   195
slouken@3259
   196
   /* Clean up. */
slouken@3259
   197
   if (at_test_msg != NULL)
slouken@3452
   198
      SDL_free(at_test_msg);
slouken@3259
   199
   at_test_msg = NULL;
slouken@3259
   200
}
slouken@3259
   201
slouken@3259
   202
slouken@3259
   203
/**
slouken@3259
   204
 * @brief Display failed assert message.
slouken@3259
   205
 */
slouken@3259
   206
static void SDL_ATassertFailed( const char *msg )
slouken@3259
   207
{
slouken@3259
   208
   /* Print. */
slouken@3259
   209
   SDL_ATprintErr( "Assert Failed!\n" );
slouken@3259
   210
   SDL_ATprintErr( "   %s\n", msg );
slouken@3259
   211
   SDL_ATprintErr( "   Test Case '%s'\n", at_test_msg );
slouken@3259
   212
   SDL_ATprintErr( "   Test Suite '%s'\n", at_suite_msg );
lestat@3463
   213
   SDL_ATprintErr( "   Last SDL error '%s'\n", SDL_GetError() );
slouken@3259
   214
   /* End. */
slouken@3259
   215
   SDL_ATendWith(0);
slouken@3259
   216
}
slouken@3259
   217
slouken@3259
   218
slouken@3259
   219
/**
slouken@3259
   220
 * @brief Testcase test.
slouken@3259
   221
 */
slouken@3259
   222
int SDL_ATassert( const char *msg, int condition )
slouken@3259
   223
{
slouken@3259
   224
   /* Condition failed. */
slouken@3259
   225
   if (!condition) {
slouken@3259
   226
      /* Failed message. */
slouken@3259
   227
      SDL_ATassertFailed(msg);
slouken@3259
   228
   }
slouken@3259
   229
   return !condition;
slouken@3259
   230
}
slouken@3259
   231
slouken@3259
   232
slouken@3259
   233
/**
slouken@3259
   234
 * @brief Testcase test.
slouken@3259
   235
 */
slouken@3259
   236
int SDL_ATvassert( int condition, const char *msg, ... )
slouken@3259
   237
{
slouken@3259
   238
   va_list args;
slouken@3259
   239
   char buf[256];
slouken@3259
   240
slouken@3259
   241
   /* Condition failed. */
slouken@3259
   242
   if (!condition) {
slouken@3259
   243
      /* Get message. */
slouken@3259
   244
      va_start( args, msg );
slouken@3482
   245
      SDL_vsnprintf( buf, sizeof(buf), msg, args );
slouken@3259
   246
      va_end( args );
slouken@3259
   247
      /* Failed message. */
slouken@3259
   248
      SDL_ATassertFailed( buf );
slouken@3259
   249
   }
slouken@3259
   250
   return !condition;
slouken@3259
   251
}
slouken@3259
   252
slouken@3259
   253
slouken@3259
   254
/**
slouken@3259
   255
 * @brief End testcase.
slouken@3259
   256
 */
slouken@3259
   257
void SDL_ATend (void)
slouken@3259
   258
{
slouken@3259
   259
   SDL_ATendWith(1);
slouken@3259
   260
}
slouken@3259
   261
slouken@3259
   262
slouken@3259
   263
/**
slouken@3259
   264
 * @brief Displays an error.
slouken@3259
   265
 */
slouken@3259
   266
int SDL_ATprintErr( const char *msg, ... )
slouken@3259
   267
{
slouken@3259
   268
   va_list ap;
slouken@3259
   269
   int ret;
slouken@3259
   270
slouken@3259
   271
   /* Make sure there is something to print. */
slouken@3259
   272
   if (msg == NULL)
slouken@3259
   273
      return 0;
slouken@3259
   274
   else {
slouken@3259
   275
      va_start(ap, msg);
slouken@3259
   276
      ret = vfprintf( stderr, msg, ap );
slouken@3259
   277
      va_end(ap);
slouken@3259
   278
   }
slouken@3259
   279
slouken@3259
   280
   return ret;
slouken@3259
   281
}
slouken@3259
   282
slouken@3259
   283
slouken@3259
   284
/**
slouken@3482
   285
 * @brief Displays a message.
slouken@3482
   286
 */
slouken@3482
   287
int SDL_ATprint( const char *msg, ... )
slouken@3482
   288
{
slouken@3482
   289
   va_list ap;
slouken@3482
   290
   int ret;
slouken@3482
   291
slouken@3482
   292
   /* Only print if not quiet. */
slouken@3482
   293
   if (at_quiet)
slouken@3482
   294
      return 0;
slouken@3482
   295
slouken@3482
   296
   /* Make sure there is something to print. */
slouken@3482
   297
   if (msg == NULL)
slouken@3482
   298
      return 0;
slouken@3482
   299
   else {
slouken@3482
   300
      va_start(ap, msg);
slouken@3482
   301
      ret = vfprintf( stdout, msg, ap );
slouken@3482
   302
      va_end(ap);
slouken@3482
   303
   }
slouken@3482
   304
slouken@3482
   305
   return ret;
slouken@3482
   306
}
slouken@3482
   307
slouken@3482
   308
slouken@3482
   309
/**
slouken@3259
   310
 * @brief Displays a verbose message.
slouken@3259
   311
 */
slouken@3259
   312
int SDL_ATprintVerbose( int level, const char *msg, ... )
slouken@3259
   313
{
slouken@3259
   314
   va_list ap;
slouken@3259
   315
   int ret;
slouken@3259
   316
slouken@3259
   317
   /* Only print if not quiet. */
slouken@3259
   318
   if (at_quiet || (at_verbose < level))
slouken@3259
   319
      return 0;
slouken@3259
   320
slouken@3259
   321
   /* Make sure there is something to print. */
slouken@3259
   322
   if (msg == NULL)
slouken@3259
   323
      return 0;
slouken@3259
   324
   else {
slouken@3259
   325
      va_start(ap, msg);
slouken@3259
   326
      ret = vfprintf( stdout, msg, ap );
slouken@3259
   327
      va_end(ap);
slouken@3259
   328
   }
slouken@3259
   329
slouken@3259
   330
   return ret;
slouken@3259
   331
}