src/test/SDL_test_fuzzer.c
author Ryan C. Gordon
Thu, 23 Feb 2017 22:38:04 -0500
changeset 10907 e64cf9261cc7
parent 10737 3406a0f8b041
child 11465 9843b1895c45
permissions -rw-r--r--
cmake: add WASAPI audio target to the build (thanks, Martin!).

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