Add random module to test lib
authorAndreas Schiffler <aschiffler@ferzkopp.net>
Mon, 26 Nov 2012 23:12:47 -0800
changeset 6699f5c53e855de7
parent 6698 28ab2ef7bfc9
child 6704 14e3ac5e1daa
Add random module to test lib
include/SDL_test.h
include/SDL_test_random.h
src/test/SDL_test_random.c
     1.1 --- a/include/SDL_test.h	Mon Nov 26 22:27:49 2012 -0800
     1.2 +++ b/include/SDL_test.h	Mon Nov 26 23:12:47 2012 -0800
     1.3 @@ -32,6 +32,7 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  #include "SDL_test_font.h"
     1.7 +#include "SDL_test_random.h"
     1.8  
     1.9  #include "begin_code.h"
    1.10  /* Set up for C function definitions, even when using C++ */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/include/SDL_test_random.h	Mon Nov 26 23:12:47 2012 -0800
     2.3 @@ -0,0 +1,123 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +
    2.25 +/**
    2.26 + *  \file SDL_test_random.h
    2.27 + *  
    2.28 + *  Include file for SDL test framework.
    2.29 + *
    2.30 + *  This code is a part of the SDL2_test library, not the main SDL library.
    2.31 + */
    2.32 +
    2.33 +/* 
    2.34 +
    2.35 + A "32-bit Multiply with carry random number generator. Very fast. 
    2.36 + Includes a list of recommended multipliers.                         
    2.37 +
    2.38 + multiply-with-carry generator: x(n) = a*x(n-1) + carry mod 2^32.
    2.39 + period: (a*2^31)-1
    2.40 + 
    2.41 +*/
    2.42 +
    2.43 +#ifndef _SDL_test_random_h
    2.44 +#define _SDL_test_random_h
    2.45 +
    2.46 +#include "begin_code.h"
    2.47 +/* Set up for C function definitions, even when using C++ */
    2.48 +#ifdef __cplusplus
    2.49 +/* *INDENT-OFF* */
    2.50 +extern "C" {
    2.51 +/* *INDENT-ON* */
    2.52 +#endif
    2.53 +
    2.54 +/* Function prototypes */
    2.55 +
    2.56 +
    2.57 +/* ------- Definitions ------- */
    2.58 +
    2.59 +/*
    2.60 + * Macros that return random number in a specific format.
    2.61 + * Float values are in the range [0.0-1.0].
    2.62 + */
    2.63 +#define SDL_TestRandomInt(c)		((int)SDL_TestRandom(c))
    2.64 +#define SDL_TestRandomFloat(c)		((double)SDL_TestRandom(c)/(unsigned long)0xffffffff)
    2.65 +
    2.66 +/*
    2.67 + * Context structure for the random number generator state.
    2.68 + */
    2.69 +  typedef struct {
    2.70 +    unsigned int a;
    2.71 +    unsigned int x;
    2.72 +    unsigned int c;
    2.73 +    unsigned int ah;
    2.74 +    unsigned int al;
    2.75 +  } SDLTest_RandomContext;
    2.76 +
    2.77 +/* ---- Function Prototypes -------- */
    2.78 +
    2.79 +/**
    2.80 + *  \brief Initialize random number generator with two integers. 
    2.81 + *
    2.82 + * Note: The random sequence of numbers returned by ...Random() is the
    2.83 + *       same for the same two integers and has a period of 2^31.
    2.84 + *
    2.85 + *  \param rndContext     pointer to context structure
    2.86 + *  \param xi         integer that defines the random sequence
    2.87 + *  \param ci         integer that defines the random sequence
    2.88 + *
    2.89 + */
    2.90 + void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi,
    2.91 +				  unsigned int ci);
    2.92 +
    2.93 +/**
    2.94 + *  \brief Initialize random number generator based on current system time. 
    2.95 + *
    2.96 + *  \param rndContext     pointer to context structure
    2.97 + *
    2.98 + */
    2.99 + void SDLTest_RandomInitTime(SDLTest_RandomContext *rndContext);
   2.100 +
   2.101 +
   2.102 +/**
   2.103 + *  \brief Initialize random number generator based on current system time. 
   2.104 + *
   2.105 + * Note: ...RandomInit() or ...RandomInitTime() must have been called
   2.106 + *       before using this function.
   2.107 + *
   2.108 + *  \param rndContext     pointer to context structure
   2.109 + *
   2.110 + *  \returns A random number (32bit unsigned integer)
   2.111 + *
   2.112 + */
   2.113 + unsigned int SDLTest_Random(SDLTest_RandomContext *rndContext);
   2.114 +
   2.115 +
   2.116 +/* Ends C function definitions when using C++ */
   2.117 +#ifdef __cplusplus
   2.118 +/* *INDENT-OFF* */
   2.119 +}
   2.120 +/* *INDENT-ON* */
   2.121 +#endif
   2.122 +#include "close_code.h"
   2.123 +
   2.124 +#endif /* _SDL_test_random_h */
   2.125 +
   2.126 +/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/test/SDL_test_random.c	Mon Nov 26 23:12:47 2012 -0800
     3.3 @@ -0,0 +1,92 @@
     3.4 +/*
     3.5 +  Simple DirectMedia Layer
     3.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     3.7 +
     3.8 +  This software is provided 'as-is', without any express or implied
     3.9 +  warranty.  In no event will the authors be held liable for any damages
    3.10 +  arising from the use of this software.
    3.11 +
    3.12 +  Permission is granted to anyone to use this software for any purpose,
    3.13 +  including commercial applications, and to alter it and redistribute it
    3.14 +  freely, subject to the following restrictions:
    3.15 +
    3.16 +  1. The origin of this software must not be misrepresented; you must not
    3.17 +     claim that you wrote the original software. If you use this software
    3.18 +     in a product, an acknowledgment in the product documentation would be
    3.19 +     appreciated but is not required.
    3.20 +  2. Altered source versions must be plainly marked as such, and must not be
    3.21 +     misrepresented as being the original software.
    3.22 +  3. This notice may not be removed or altered from any source distribution.
    3.23 +*/
    3.24 +
    3.25 +/*
    3.26 +
    3.27 + A portable "32-bit Multiply with carry" random number generator.
    3.28 +
    3.29 + Used by the fuzzer component. 
    3.30 + Original source code contributed by A. Schiffler for GSOC project.
    3.31 +
    3.32 +*/
    3.33 +
    3.34 +#include "SDL_config.h"
    3.35 +
    3.36 +#include <time.h>
    3.37 +
    3.38 +#include "SDL_test.h"
    3.39 +
    3.40 +/* Initialize random number generator with two integer variables */
    3.41 +
    3.42 +void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi, unsigned int ci)
    3.43 +{
    3.44 +  if (rndContext==NULL) return;
    3.45 +  
    3.46 +  /*
    3.47 +   * Choose a value for 'a' from this list
    3.48 +   * 1791398085 1929682203 1683268614 1965537969 1675393560
    3.49 +   * 1967773755 1517746329 1447497129 1655692410 1606218150
    3.50 +   * 2051013963 1075433238 1557985959 1781943330 1893513180
    3.51 +   * 1631296680 2131995753 2083801278 1873196400 1554115554 
    3.52 +   */
    3.53 +  rndContext->a = 1655692410;
    3.54 +  rndContext->x = 30903;
    3.55 +  rndContext->c = 0;
    3.56 +  if (xi != 0) {
    3.57 +      rndContext->x = xi;
    3.58 +  }
    3.59 +  rndContext->c = ci;
    3.60 +  rndContext->ah = rndContext->a >> 16;
    3.61 +  rndContext->al = rndContext->a & 65535;
    3.62 +}
    3.63 +
    3.64 +/* Initialize random number generator from system time */
    3.65 +
    3.66 +void SDLTest_RandomInitTime(SDLTest_RandomContext * rndContext)
    3.67 +{
    3.68 +  int a, b;
    3.69 +  
    3.70 +  if (rndContext==NULL) return;
    3.71 +  
    3.72 +  srand((unsigned int)time(NULL));
    3.73 +  a=rand();
    3.74 +  srand(clock());
    3.75 +  b=rand();
    3.76 +  SDLTest_RandomInit(rndContext, a, b);
    3.77 +}
    3.78 +
    3.79 +/* Returns random numbers */
    3.80 +
    3.81 +unsigned int SDLTest_Random(SDLTest_RandomContext * rndContext)
    3.82 +{
    3.83 +  unsigned int xh, xl;
    3.84 +
    3.85 +  if (rndContext==NULL) return -1;
    3.86 +  
    3.87 +  xh = rndContext->x >> 16, xl = rndContext->x & 65535;
    3.88 +  rndContext->x = rndContext->x * rndContext->a + rndContext->c;
    3.89 +  rndContext->c =
    3.90 +    xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
    3.91 +    ((xl * rndContext->ah) >> 16);
    3.92 +  if (xl * rndContext->al >= (~rndContext->c + 1))
    3.93 +    rndContext->c++;
    3.94 +  return (rndContext->x);
    3.95 +}