Update testsprite2 for visual testing: user fuzzer and add --iteration parameter to enable deterministic screen end-state
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Sun, 23 Jun 2013 15:00:23 -0700
changeset 7315417fa81cf17e
parent 7314 b0726d41aa2e
child 7316 c3052ed2c310
Update testsprite2 for visual testing: user fuzzer and add --iteration parameter to enable deterministic screen end-state
test/testsprite2.c
     1.1 --- a/test/testsprite2.c	Sun Jun 23 14:05:27 2013 -0700
     1.2 +++ b/test/testsprite2.c	Sun Jun 23 15:00:23 2013 -0700
     1.3 @@ -15,6 +15,7 @@
     1.4  #include <stdio.h>
     1.5  #include <time.h>
     1.6  
     1.7 +#include "SDL_test.h"
     1.8  #include "SDL_test_common.h"
     1.9  
    1.10  #define NUM_SPRITES    100
    1.11 @@ -33,6 +34,10 @@
    1.12  static int sprite_w, sprite_h;
    1.13  static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
    1.14  
    1.15 +/* Number of iterations to move sprites - used for visual tests. */
    1.16 +/* -1: infinite random moves (default); >=0: enables N deterministic moves */
    1.17 +static int iterations = -1;
    1.18 +
    1.19  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.20  static void
    1.21  quit(int rc)
    1.22 @@ -105,7 +110,7 @@
    1.23  void
    1.24  MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
    1.25  {
    1.26 -    int i, n;
    1.27 +    int i;
    1.28      SDL_Rect viewport, temp;
    1.29      SDL_Rect *position, *velocity;
    1.30  
    1.31 @@ -191,22 +196,38 @@
    1.32      SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
    1.33                         sprite_w, viewport.h-sprite_h-2);
    1.34  
    1.35 -    /* Move the sprite, bounce at the wall, and draw */
    1.36 -    n = 0;
    1.37 +    /* Conditionally move the sprites, bounce at the wall */
    1.38 +    if (iterations == -1 || iterations > 0) {
    1.39 +        for (i = 0; i < num_sprites; ++i) {
    1.40 +            position = &positions[i];
    1.41 +            velocity = &velocities[i];
    1.42 +            position->x += velocity->x;
    1.43 +            if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
    1.44 +            	velocity->x = -velocity->x;
    1.45 +            	position->x += velocity->x;
    1.46 +            }
    1.47 +            position->y += velocity->y;
    1.48 +            if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
    1.49 +            	velocity->y = -velocity->y;
    1.50 +            	position->y += velocity->y;
    1.51 +            }
    1.52 +
    1.53 +        }
    1.54 +        
    1.55 +        /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
    1.56 +        if (iterations > 0) {
    1.57 +            iterations--;
    1.58 +            if (iterations == 0) {
    1.59 +                cycle_alpha = SDL_FALSE;
    1.60 +                cycle_color = SDL_FALSE;
    1.61 +            }
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +    /* Draw sprites */
    1.66      for (i = 0; i < num_sprites; ++i) {
    1.67          position = &positions[i];
    1.68 -        velocity = &velocities[i];
    1.69 -        position->x += velocity->x;
    1.70 -        if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
    1.71 -            velocity->x = -velocity->x;
    1.72 -            position->x += velocity->x;
    1.73 -        }
    1.74 -        position->y += velocity->y;
    1.75 -        if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
    1.76 -            velocity->y = -velocity->y;
    1.77 -            position->y += velocity->y;
    1.78 -        }
    1.79 -
    1.80 +		
    1.81          /* Blit the sprite onto the screen */
    1.82          SDL_RenderCopy(renderer, sprite, NULL, position);
    1.83      }
    1.84 @@ -221,6 +242,7 @@
    1.85      int i, done;
    1.86      SDL_Event event;
    1.87      Uint32 then, now, frames;
    1.88 +	Uint64 seed;
    1.89  
    1.90      /* Initialize parameters */
    1.91      num_sprites = NUM_SPRITES;
    1.92 @@ -255,6 +277,12 @@
    1.93                          consumed = 2;
    1.94                      }
    1.95                  }
    1.96 +            } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
    1.97 +                if (argv[i + 1]) {
    1.98 +                    iterations = SDL_atoi(argv[i + 1]);
    1.99 +                    if (iterations < -1) iterations = -1;
   1.100 +                    consumed = 2;
   1.101 +                }
   1.102              } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   1.103                  cycle_color = SDL_TRUE;
   1.104                  consumed = 1;
   1.105 @@ -268,7 +296,7 @@
   1.106          }
   1.107          if (consumed < 0) {
   1.108              fprintf(stderr,
   1.109 -                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   1.110 +                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha] [--iterations N]\n",
   1.111                      argv[0], SDLTest_CommonUsage(state));
   1.112              quit(1);
   1.113          }
   1.114 @@ -301,17 +329,26 @@
   1.115          fprintf(stderr, "Out of memory!\n");
   1.116          quit(2);
   1.117      }
   1.118 -    srand((unsigned int)time(NULL));
   1.119 +
   1.120 +    /* Position sprites and set their velocities using the fuzzer */ 
   1.121 +    if (iterations >= 0) {
   1.122 +        /* Deterministic seed - used for visual tests */
   1.123 +        seed = (Uint64)iterations;
   1.124 +    } else {
   1.125 +        /* Pseudo-random seed generated from the time */
   1.126 +        seed = (Uint64)time(NULL);
   1.127 +    }
   1.128 +    SDLTest_FuzzerInit(seed);
   1.129      for (i = 0; i < num_sprites; ++i) {
   1.130 -        positions[i].x = rand() % (state->window_w - sprite_w);
   1.131 -        positions[i].y = rand() % (state->window_h - sprite_h);
   1.132 +        positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
   1.133 +        positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
   1.134          positions[i].w = sprite_w;
   1.135          positions[i].h = sprite_h;
   1.136          velocities[i].x = 0;
   1.137          velocities[i].y = 0;
   1.138          while (!velocities[i].x && !velocities[i].y) {
   1.139 -            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.140 -            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
   1.141 +            velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
   1.142 +            velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
   1.143          }
   1.144      }
   1.145