src/test/SDL_test_fuzzer.c
author Ryan C. Gordon
Tue, 29 Dec 2015 01:09:58 -0500
changeset 9983 bbe9ef8c2ecb
parent 9635 564b57497f2b
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Mac: don't ignore mouse clicks on the top pixel of a window (thanks, Joshua!).

Fixes Bugzilla #3190.
aschiffler@6711
     1
/*
aschiffler@6711
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
aschiffler@6711
     4
aschiffler@6711
     5
  This software is provided 'as-is', without any express or implied
aschiffler@6711
     6
  warranty.  In no event will the authors be held liable for any damages
aschiffler@6711
     7
  arising from the use of this software.
aschiffler@6711
     8
aschiffler@6711
     9
  Permission is granted to anyone to use this software for any purpose,
aschiffler@6711
    10
  including commercial applications, and to alter it and redistribute it
aschiffler@6711
    11
  freely, subject to the following restrictions:
aschiffler@6711
    12
aschiffler@6711
    13
  1. The origin of this software must not be misrepresented; you must not
aschiffler@6711
    14
     claim that you wrote the original software. If you use this software
aschiffler@6711
    15
     in a product, an acknowledgment in the product documentation would be
aschiffler@6711
    16
     appreciated but is not required.
aschiffler@6711
    17
  2. Altered source versions must be plainly marked as such, and must not be
aschiffler@6711
    18
     misrepresented as being the original software.
aschiffler@6711
    19
  3. This notice may not be removed or altered from any source distribution.
aschiffler@6711
    20
*/
aschiffler@6711
    21
slouken@7191
    22
/*
aschiffler@6711
    23
aschiffler@6711
    24
  Data generators for fuzzing test data in a reproducible way.
slouken@7191
    25
aschiffler@6711
    26
*/
aschiffler@6711
    27
aschiffler@6711
    28
#include "SDL_config.h"
aschiffler@6711
    29
slouken@6789
    30
/* Visual Studio 2008 doesn't have stdint.h */
slouken@6789
    31
#if defined(_MSC_VER) && _MSC_VER <= 1500
slouken@6789
    32
#define UINT8_MAX   ~(Uint8)0
slouken@6789
    33
#define UINT16_MAX  ~(Uint16)0
slouken@6789
    34
#define UINT32_MAX  ~(Uint32)0
slouken@6789
    35
#define UINT64_MAX  ~(Uint64)0
slouken@6789
    36
#else
icculus@9635
    37
#define _GNU_SOURCE
aschiffler@6711
    38
#include <stdint.h>
slouken@6789
    39
#endif
aschiffler@6711
    40
#include <stdio.h>
aschiffler@6711
    41
#include <stdlib.h>
aschiffler@6711
    42
#include <limits.h>
aschiffler@6711
    43
#include <float.h>
aschiffler@6711
    44
aschiffler@6711
    45
#include "SDL_test.h"
aschiffler@6711
    46
slouken@7191
    47
/**
aschiffler@6872
    48
 * Counter for fuzzer invocations
aschiffler@6711
    49
 */
aschiffler@6757
    50
static int fuzzerInvocationCounter = 0;
aschiffler@6711
    51
aschiffler@6711
    52
/**
aschiffler@6711
    53
 * Context for shared random number generator
aschiffler@6711
    54
 */
aschiffler@6711
    55
static SDLTest_RandomContext rndContext;
aschiffler@6711
    56
aschiffler@6711
    57
/*
aschiffler@6711
    58
 * Note: doxygen documentation markup for functions is in the header file.
aschiffler@6711
    59
 */
aschiffler@6711
    60
aschiffler@6711
    61
void
aschiffler@6711
    62
SDLTest_FuzzerInit(Uint64 execKey)
aschiffler@6711
    63
{
slouken@7191
    64
    Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
slouken@7191
    65
    Uint32 b = execKey & 0x00000000FFFFFFFF;
slouken@7191
    66
    SDL_memset((void *)&rndContext, 0, sizeof(SDLTest_RandomContext));
slouken@7191
    67
    SDLTest_RandomInit(&rndContext, a, b);
slouken@7191
    68
    fuzzerInvocationCounter = 0;
aschiffler@6711
    69
}
aschiffler@6711
    70
aschiffler@6711
    71
int
aschiffler@6756
    72
SDLTest_GetFuzzerInvocationCount()
aschiffler@6711
    73
{
slouken@7191
    74
    return fuzzerInvocationCounter;
aschiffler@6711
    75
}
aschiffler@6711
    76
aschiffler@6711
    77
Uint8
aschiffler@6711
    78
SDLTest_RandomUint8()
aschiffler@6711
    79
{
slouken@7191
    80
    fuzzerInvocationCounter++;
aschiffler@6711
    81
slouken@7191
    82
    return (Uint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
aschiffler@6711
    83
}
aschiffler@6711
    84
aschiffler@6711
    85
Sint8
aschiffler@6711
    86
SDLTest_RandomSint8()
aschiffler@6711
    87
{
slouken@7191
    88
    fuzzerInvocationCounter++;
aschiffler@6711
    89
slouken@7191
    90
    return (Sint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
aschiffler@6711
    91
}
aschiffler@6711
    92
aschiffler@6711
    93
Uint16
aschiffler@6711
    94
SDLTest_RandomUint16()
aschiffler@6711
    95
{
slouken@7191
    96
    fuzzerInvocationCounter++;
aschiffler@6711
    97
slouken@7191
    98
    return (Uint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
aschiffler@6711
    99
}
aschiffler@6711
   100
aschiffler@6711
   101
Sint16
aschiffler@6711
   102
SDLTest_RandomSint16()
aschiffler@6711
   103
{
slouken@7191
   104
    fuzzerInvocationCounter++;
aschiffler@6711
   105
slouken@7191
   106
    return (Sint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
aschiffler@6711
   107
}
aschiffler@6711
   108
aschiffler@6711
   109
Sint32
aschiffler@6711
   110
SDLTest_RandomSint32()
aschiffler@6711
   111
{
slouken@7191
   112
    fuzzerInvocationCounter++;
aschiffler@6711
   113
slouken@7191
   114
    return (Sint32) SDLTest_RandomInt(&rndContext);
aschiffler@6711
   115
}
aschiffler@6711
   116
aschiffler@6711
   117
Uint32
aschiffler@6711
   118
SDLTest_RandomUint32()
aschiffler@6711
   119
{
slouken@7191
   120
    fuzzerInvocationCounter++;
aschiffler@6711
   121
slouken@7191
   122
    return (Uint32) SDLTest_RandomInt(&rndContext);
aschiffler@6711
   123
}
aschiffler@6711
   124
aschiffler@6711
   125
Uint64
aschiffler@6711
   126
SDLTest_RandomUint64()
aschiffler@6711
   127
{
slouken@7191
   128
    Uint64 value = 0;
slouken@7191
   129
    Uint32 *vp = (void *)&value;
aschiffler@6711
   130
slouken@7191
   131
    fuzzerInvocationCounter++;
aschiffler@6711
   132
slouken@7191
   133
    vp[0] = SDLTest_RandomSint32();
slouken@7191
   134
    vp[1] = SDLTest_RandomSint32();
aschiffler@6711
   135
slouken@7191
   136
    return value;
aschiffler@6711
   137
}
aschiffler@6711
   138
aschiffler@6711
   139
Sint64
aschiffler@6711
   140
SDLTest_RandomSint64()
aschiffler@6711
   141
{
slouken@7191
   142
    Uint64 value = 0;
slouken@7191
   143
    Uint32 *vp = (void *)&value;
aschiffler@6711
   144
slouken@7191
   145
    fuzzerInvocationCounter++;
aschiffler@6711
   146
slouken@7191
   147
    vp[0] = SDLTest_RandomSint32();
slouken@7191
   148
    vp[1] = SDLTest_RandomSint32();
aschiffler@6711
   149
slouken@7191
   150
    return value;
aschiffler@6711
   151
}
aschiffler@6711
   152
aschiffler@6711
   153
aschiffler@6711
   154
aschiffler@6711
   155
Sint32
aschiffler@6711
   156
SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
aschiffler@6711
   157
{
slouken@7191
   158
    Sint64 min = pMin;
slouken@7191
   159
    Sint64 max = pMax;
slouken@7191
   160
    Sint64 temp;
slouken@7191
   161
    Sint64 number;
aschiffler@6711
   162
slouken@7191
   163
    if(pMin > pMax) {
slouken@7191
   164
        temp = min;
slouken@7191
   165
        min = max;
slouken@7191
   166
        max = temp;
slouken@7191
   167
    } else if(pMin == pMax) {
slouken@7191
   168
        return (Sint32)min;
slouken@7191
   169
    }
aschiffler@6711
   170
slouken@7191
   171
    number = SDLTest_RandomUint32();
slouken@7191
   172
    /* invocation count increment in preceeding call */
aschiffler@6711
   173
slouken@7191
   174
    return (Sint32)((number % ((max + 1) - min)) + min);
aschiffler@6711
   175
}
aschiffler@6711
   176
gabomdq@7678
   177
/* !
aschiffler@6872
   178
 * Generates a unsigned boundary value between the given boundaries.
aschiffler@6711
   179
 * Boundary values are inclusive. See the examples below.
aschiffler@6711
   180
 * If boundary2 < boundary1, the values are swapped.
aschiffler@6711
   181
 * If boundary1 == boundary2, value of boundary1 will be returned
aschiffler@6711
   182
 *
aschiffler@6711
   183
 * Generating boundary values for Uint8:
aschiffler@6872
   184
 * BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
aschiffler@6872
   185
 * BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
aschiffler@6872
   186
 * BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
aschiffler@6872
   187
 * BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
aschiffler@6872
   188
 * BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
aschiffler@6711
   189
 *
aschiffler@6711
   190
 * Generator works the same for other types of unsigned integers.
aschiffler@6711
   191
 *
aschiffler@6711
   192
 * \param maxValue The biggest value that is acceptable for this data type.
slouken@7191
   193
 *                  For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
aschiffler@6872
   194
 * \param boundary1 defines lower boundary
aschiffler@6872
   195
 * \param boundary2 defines upper boundary
aschiffler@6711
   196
 * \param validDomain Generate only for valid domain (for the data type)
aschiffler@6711
   197
 *
aschiffler@6872
   198
 * \returns Returns a random boundary value for the domain or 0 in case of error
aschiffler@6711
   199
 */
aschiffler@6872
   200
Uint64
aschiffler@6872
   201
SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   202
{
aschiffler@6872
   203
        Uint64 b1, b2;
slouken@7191
   204
    Uint64 delta;
slouken@7191
   205
    Uint64 tempBuf[4];
slouken@7191
   206
    Uint8 index;
slouken@7191
   207
aschiffler@6872
   208
        /* Maybe swap */
slouken@7191
   209
    if (boundary1 > boundary2) {
slouken@7191
   210
        b1 = boundary2;
slouken@7191
   211
        b2 = boundary1;
slouken@7191
   212
    } else {
slouken@7191
   213
        b1 = boundary1;
slouken@7191
   214
        b2 = boundary2;
aschiffler@6872
   215
        }
aschiffler@6711
   216
slouken@7191
   217
    index = 0;
slouken@7191
   218
    if (validDomain == SDL_TRUE) {
slouken@7191
   219
            if (b1 == b2) {
slouken@7191
   220
                return b1;
slouken@7191
   221
            }
slouken@7191
   222
slouken@7191
   223
            /* Generate up to 4 values within bounds */
slouken@7191
   224
            delta = b2 - b1;
slouken@7191
   225
            if (delta < 4) {
slouken@7191
   226
                do {
slouken@7191
   227
                tempBuf[index] = b1 + index;
slouken@7191
   228
                index++;
aschiffler@6872
   229
                    } while (index < delta);
slouken@7191
   230
            } else {
slouken@7191
   231
          tempBuf[index] = b1;
slouken@7191
   232
          index++;
slouken@7191
   233
          tempBuf[index] = b1 + 1;
slouken@7191
   234
          index++;
slouken@7191
   235
          tempBuf[index] = b2 - 1;
slouken@7191
   236
          index++;
slouken@7191
   237
          tempBuf[index] = b2;
slouken@7191
   238
          index++;
slouken@7191
   239
            }
slouken@7191
   240
        } else {
slouken@7191
   241
            /* Generate up to 2 values outside of bounds */
slouken@7191
   242
        if (b1 > 0) {
slouken@7191
   243
            tempBuf[index] = b1 - 1;
slouken@7191
   244
            index++;
slouken@7191
   245
        }
aschiffler@6711
   246
slouken@7191
   247
        if (b2 < maxValue) {
slouken@7191
   248
            tempBuf[index] = b2 + 1;
slouken@7191
   249
            index++;
slouken@7191
   250
        }
slouken@7191
   251
    }
aschiffler@6711
   252
slouken@7191
   253
    if (index == 0) {
slouken@7191
   254
        /* There are no valid boundaries */
slouken@7191
   255
        SDL_Unsupported();
slouken@7191
   256
        return 0;
slouken@7191
   257
    }
aschiffler@6711
   258
slouken@7191
   259
    return tempBuf[SDLTest_RandomUint8() % index];
aschiffler@6872
   260
}
aschiffler@6711
   261
aschiffler@6711
   262
aschiffler@6711
   263
Uint8
aschiffler@6711
   264
SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
aschiffler@6711
   265
{
slouken@7191
   266
    /* max value for Uint8 */
slouken@7191
   267
    const Uint64 maxValue = UCHAR_MAX;
slouken@7191
   268
    return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   269
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   270
                validDomain);
aschiffler@6711
   271
}
aschiffler@6711
   272
aschiffler@6711
   273
Uint16
aschiffler@6711
   274
SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
aschiffler@6711
   275
{
slouken@7191
   276
    /* max value for Uint16 */
slouken@7191
   277
    const Uint64 maxValue = USHRT_MAX;
slouken@7191
   278
    return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   279
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   280
                validDomain);
aschiffler@6711
   281
}
aschiffler@6711
   282
aschiffler@6711
   283
Uint32
aschiffler@6711
   284
SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
aschiffler@6711
   285
{
slouken@7191
   286
    /* max value for Uint32 */
slouken@7191
   287
    #if ((ULONG_MAX) == (UINT_MAX))
slouken@7191
   288
      const Uint64 maxValue = ULONG_MAX;
aschiffler@7172
   289
        #else
slouken@7191
   290
      const Uint64 maxValue = UINT_MAX;
aschiffler@7172
   291
        #endif
slouken@7191
   292
    return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   293
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   294
                validDomain);
aschiffler@6711
   295
}
aschiffler@6711
   296
aschiffler@6711
   297
Uint64
aschiffler@6711
   298
SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   299
{
slouken@7191
   300
    /* max value for Uint64 */
slouken@7191
   301
    const Uint64 maxValue = ULLONG_MAX;
slouken@7191
   302
    return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   303
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   304
                validDomain);
aschiffler@6711
   305
}
aschiffler@6711
   306
gabomdq@7678
   307
/* !
aschiffler@6872
   308
 * Generates a signed boundary value between the given boundaries.
aschiffler@6711
   309
 * Boundary values are inclusive. See the examples below.
aschiffler@6711
   310
 * If boundary2 < boundary1, the values are swapped.
aschiffler@6711
   311
 * If boundary1 == boundary2, value of boundary1 will be returned
aschiffler@6711
   312
 *
aschiffler@6711
   313
 * Generating boundary values for Sint8:
aschiffler@6872
   314
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
aschiffler@6872
   315
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
aschiffler@6872
   316
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
aschiffler@6872
   317
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
aschiffler@6872
   318
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
aschiffler@6711
   319
 *
aschiffler@6711
   320
 * Generator works the same for other types of signed integers.
aschiffler@6711
   321
 *
aschiffler@6872
   322
 * \param minValue The smallest value that is acceptable for this data type.
slouken@7191
   323
 *                  For instance, for Uint8 -> -127, etc.
aschiffler@6711
   324
 * \param maxValue The biggest value that is acceptable for this data type.
slouken@7191
   325
 *                  For instance, for Uint8 -> 127, etc.
aschiffler@6872
   326
 * \param boundary1 defines lower boundary
aschiffler@6872
   327
 * \param boundary2 defines upper boundary
aschiffler@6711
   328
 * \param validDomain Generate only for valid domain (for the data type)
aschiffler@6711
   329
 *
aschiffler@6872
   330
 * \returns Returns a random boundary value for the domain or 0 in case of error
aschiffler@6711
   331
 */
aschiffler@6872
   332
Sint64
aschiffler@6872
   333
SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   334
{
aschiffler@6872
   335
        Sint64 b1, b2;
slouken@7191
   336
    Sint64 delta;
slouken@7191
   337
    Sint64 tempBuf[4];
slouken@7191
   338
    Uint8 index;
slouken@7191
   339
aschiffler@6872
   340
        /* Maybe swap */
slouken@7191
   341
    if (boundary1 > boundary2) {
slouken@7191
   342
        b1 = boundary2;
slouken@7191
   343
        b2 = boundary1;
slouken@7191
   344
    } else {
slouken@7191
   345
        b1 = boundary1;
slouken@7191
   346
        b2 = boundary2;
aschiffler@6872
   347
        }
aschiffler@6711
   348
slouken@7191
   349
    index = 0;
slouken@7191
   350
    if (validDomain == SDL_TRUE) {
slouken@7191
   351
            if (b1 == b2) {
slouken@7191
   352
                return b1;
slouken@7191
   353
            }
slouken@7191
   354
slouken@7191
   355
            /* Generate up to 4 values within bounds */
slouken@7191
   356
            delta = b2 - b1;
slouken@7191
   357
            if (delta < 4) {
slouken@7191
   358
                do {
slouken@7191
   359
                tempBuf[index] = b1 + index;
slouken@7191
   360
                index++;
aschiffler@6872
   361
                    } while (index < delta);
slouken@7191
   362
            } else {
slouken@7191
   363
          tempBuf[index] = b1;
slouken@7191
   364
          index++;
slouken@7191
   365
          tempBuf[index] = b1 + 1;
slouken@7191
   366
          index++;
slouken@7191
   367
          tempBuf[index] = b2 - 1;
slouken@7191
   368
          index++;
slouken@7191
   369
          tempBuf[index] = b2;
slouken@7191
   370
          index++;
slouken@7191
   371
            }
slouken@7191
   372
        } else {
slouken@7191
   373
            /* Generate up to 2 values outside of bounds */
slouken@7191
   374
        if (b1 > minValue) {
slouken@7191
   375
            tempBuf[index] = b1 - 1;
slouken@7191
   376
            index++;
slouken@7191
   377
        }
aschiffler@6711
   378
slouken@7191
   379
        if (b2 < maxValue) {
slouken@7191
   380
            tempBuf[index] = b2 + 1;
slouken@7191
   381
            index++;
slouken@7191
   382
        }
slouken@7191
   383
    }
aschiffler@6711
   384
slouken@7191
   385
    if (index == 0) {
slouken@7191
   386
        /* There are no valid boundaries */
slouken@7191
   387
        SDL_Unsupported();
slouken@7191
   388
        return minValue;
slouken@7191
   389
    }
aschiffler@6711
   390
slouken@7191
   391
    return tempBuf[SDLTest_RandomUint8() % index];
aschiffler@6872
   392
}
aschiffler@6711
   393
aschiffler@6711
   394
aschiffler@6711
   395
Sint8
aschiffler@6711
   396
SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
aschiffler@6711
   397
{
slouken@7191
   398
    /* min & max values for Sint8 */
slouken@7191
   399
    const Sint64 maxValue = SCHAR_MAX;
slouken@7191
   400
    const Sint64 minValue = SCHAR_MIN;
slouken@7191
   401
    return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   402
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   403
                validDomain);
aschiffler@6711
   404
}
aschiffler@6711
   405
aschiffler@6711
   406
Sint16
aschiffler@6711
   407
SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
aschiffler@6711
   408
{
slouken@7191
   409
    /* min & max values for Sint16 */
slouken@7191
   410
    const Sint64 maxValue = SHRT_MAX;
slouken@7191
   411
    const Sint64 minValue = SHRT_MIN;
slouken@7191
   412
    return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   413
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   414
                validDomain);
aschiffler@6711
   415
}
aschiffler@6711
   416
aschiffler@6711
   417
Sint32
aschiffler@6711
   418
SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
aschiffler@6711
   419
{
slouken@7191
   420
    /* min & max values for Sint32 */
slouken@7191
   421
    #if ((ULONG_MAX) == (UINT_MAX))
slouken@7191
   422
      const Sint64 maxValue = LONG_MAX;
slouken@7191
   423
      const Sint64 minValue = LONG_MIN;
aschiffler@7172
   424
        #else
slouken@7191
   425
      const Sint64 maxValue = INT_MAX;
slouken@7191
   426
      const Sint64 minValue = INT_MIN;
aschiffler@7172
   427
        #endif
slouken@7191
   428
    return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   429
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   430
                validDomain);
aschiffler@6711
   431
}
aschiffler@6711
   432
aschiffler@6711
   433
Sint64
aschiffler@6711
   434
SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   435
{
slouken@7191
   436
    /* min & max values for Sint64 */
slouken@7191
   437
    const Sint64 maxValue = LLONG_MAX;
slouken@7191
   438
    const Sint64 minValue = LLONG_MIN;
slouken@7191
   439
    return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   440
                boundary1, boundary2,
slouken@7191
   441
                validDomain);
aschiffler@6711
   442
}
aschiffler@6711
   443
aschiffler@6711
   444
float
aschiffler@6711
   445
SDLTest_RandomUnitFloat()
aschiffler@6711
   446
{
slouken@7191
   447
    return (float) SDLTest_RandomUint32() / UINT_MAX;
aschiffler@6711
   448
}
aschiffler@6711
   449
aschiffler@6711
   450
float
aschiffler@6711
   451
SDLTest_RandomFloat()
aschiffler@6711
   452
{
aschiffler@6839
   453
        return (float) (SDLTest_RandomUnitDouble() * (double)2.0 * (double)FLT_MAX - (double)(FLT_MAX));
aschiffler@6711
   454
}
aschiffler@6711
   455
aschiffler@6711
   456
double
aschiffler@6711
   457
SDLTest_RandomUnitDouble()
aschiffler@6711
   458
{
slouken@7191
   459
    return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
aschiffler@6711
   460
}
aschiffler@6711
   461
aschiffler@6711
   462
double
aschiffler@6711
   463
SDLTest_RandomDouble()
aschiffler@6711
   464
{
slouken@7191
   465
    double r = 0.0;
slouken@7191
   466
    double s = 1.0;
slouken@7191
   467
    do {
slouken@7191
   468
      s /= UINT_MAX + 1.0;
slouken@7191
   469
      r += (double)SDLTest_RandomInt(&rndContext) * s;
slouken@7191
   470
    } while (s > DBL_EPSILON);
slouken@7191
   471
slouken@7191
   472
    fuzzerInvocationCounter++;
slouken@7191
   473
slouken@7191
   474
    return r;
aschiffler@6711
   475
}
aschiffler@6711
   476
aschiffler@6711
   477
aschiffler@6711
   478
char *
aschiffler@6711
   479
SDLTest_RandomAsciiString()
aschiffler@6711
   480
{
slouken@7191
   481
    return SDLTest_RandomAsciiStringWithMaximumLength(255);
aschiffler@6711
   482
}
aschiffler@6711
   483
aschiffler@6711
   484
char *
aschiffler@6811
   485
SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
aschiffler@6711
   486
{
slouken@7191
   487
    int size;
aschiffler@6811
   488
slouken@7191
   489
    if(maxLength < 1) {
aschiffler@6811
   490
                SDL_InvalidParamError("maxLength");
slouken@7191
   491
        return NULL;
slouken@7191
   492
    }
aschiffler@6811
   493
slouken@7191
   494
    size = (SDLTest_RandomUint32() % (maxLength + 1));
slouken@7191
   495
slouken@7191
   496
    return SDLTest_RandomAsciiStringOfSize(size);
aschiffler@6811
   497
}
aschiffler@6811
   498
aschiffler@6811
   499
char *
aschiffler@6811
   500
SDLTest_RandomAsciiStringOfSize(int size)
aschiffler@6811
   501
{
slouken@7191
   502
    char *string;
slouken@7191
   503
    int counter;
aschiffler@6711
   504
aschiffler@6711
   505
slouken@7191
   506
    if(size < 1) {
aschiffler@6811
   507
                SDL_InvalidParamError("size");
slouken@7191
   508
        return NULL;
slouken@7191
   509
    }
aschiffler@6711
   510
slouken@7191
   511
    string = (char *)SDL_malloc((size + 1) * sizeof(char));
slouken@7191
   512
    if (string==NULL) {
slouken@7191
   513
      return NULL;
aschiffler@6711
   514
        }
aschiffler@6711
   515
slouken@7191
   516
    for(counter = 0; counter < size; ++counter) {
slouken@7191
   517
        string[counter] = (char)SDLTest_RandomIntegerInRange(32, 126);
slouken@7191
   518
    }
aschiffler@6711
   519
slouken@7191
   520
    string[counter] = '\0';
aschiffler@6711
   521
slouken@7191
   522
    fuzzerInvocationCounter++;
aschiffler@6811
   523
slouken@7191
   524
    return string;
aschiffler@6711
   525
}