src/test/SDL_test_fuzzer.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 11903 732b6ef4aae7
child 12503 806492103856
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

This makes an unchanged set of hardware always report devices in the same
order on each run.
aschiffler@6711
     1
/*
aschiffler@6711
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
{
sezero@11903
   129
    union {
sezero@11903
   130
        Uint64 v64;
sezero@11903
   131
        Uint32 v32[2];
sezero@11903
   132
    } value;
sezero@11903
   133
    value.v64 = 0;
aschiffler@6711
   134
slouken@7191
   135
    fuzzerInvocationCounter++;
aschiffler@6711
   136
sezero@11903
   137
    value.v32[0] = SDLTest_RandomSint32();
sezero@11903
   138
    value.v32[1] = SDLTest_RandomSint32();
aschiffler@6711
   139
sezero@11903
   140
    return value.v64;
aschiffler@6711
   141
}
aschiffler@6711
   142
aschiffler@6711
   143
Sint64
aschiffler@6711
   144
SDLTest_RandomSint64()
aschiffler@6711
   145
{
sezero@11903
   146
    union {
sezero@11903
   147
        Uint64 v64;
sezero@11903
   148
        Uint32 v32[2];
sezero@11903
   149
    } value;
sezero@11903
   150
    value.v64 = 0;
aschiffler@6711
   151
slouken@7191
   152
    fuzzerInvocationCounter++;
aschiffler@6711
   153
sezero@11903
   154
    value.v32[0] = SDLTest_RandomSint32();
sezero@11903
   155
    value.v32[1] = SDLTest_RandomSint32();
aschiffler@6711
   156
sezero@11903
   157
    return (Sint64)value.v64;
aschiffler@6711
   158
}
aschiffler@6711
   159
aschiffler@6711
   160
aschiffler@6711
   161
aschiffler@6711
   162
Sint32
aschiffler@6711
   163
SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
aschiffler@6711
   164
{
slouken@7191
   165
    Sint64 min = pMin;
slouken@7191
   166
    Sint64 max = pMax;
slouken@7191
   167
    Sint64 temp;
slouken@7191
   168
    Sint64 number;
aschiffler@6711
   169
slouken@7191
   170
    if(pMin > pMax) {
slouken@7191
   171
        temp = min;
slouken@7191
   172
        min = max;
slouken@7191
   173
        max = temp;
slouken@7191
   174
    } else if(pMin == pMax) {
slouken@7191
   175
        return (Sint32)min;
slouken@7191
   176
    }
aschiffler@6711
   177
slouken@7191
   178
    number = SDLTest_RandomUint32();
slouken@7191
   179
    /* invocation count increment in preceeding call */
aschiffler@6711
   180
slouken@7191
   181
    return (Sint32)((number % ((max + 1) - min)) + min);
aschiffler@6711
   182
}
aschiffler@6711
   183
gabomdq@7678
   184
/* !
aschiffler@6872
   185
 * Generates a unsigned boundary value between the given boundaries.
aschiffler@6711
   186
 * Boundary values are inclusive. See the examples below.
aschiffler@6711
   187
 * If boundary2 < boundary1, the values are swapped.
aschiffler@6711
   188
 * If boundary1 == boundary2, value of boundary1 will be returned
aschiffler@6711
   189
 *
aschiffler@6711
   190
 * Generating boundary values for Uint8:
aschiffler@6872
   191
 * BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
aschiffler@6872
   192
 * BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
aschiffler@6872
   193
 * BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
aschiffler@6872
   194
 * BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
aschiffler@6872
   195
 * BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
aschiffler@6711
   196
 *
aschiffler@6711
   197
 * Generator works the same for other types of unsigned integers.
aschiffler@6711
   198
 *
aschiffler@6711
   199
 * \param maxValue The biggest value that is acceptable for this data type.
slouken@7191
   200
 *                  For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
aschiffler@6872
   201
 * \param boundary1 defines lower boundary
aschiffler@6872
   202
 * \param boundary2 defines upper boundary
aschiffler@6711
   203
 * \param validDomain Generate only for valid domain (for the data type)
aschiffler@6711
   204
 *
aschiffler@6872
   205
 * \returns Returns a random boundary value for the domain or 0 in case of error
aschiffler@6711
   206
 */
slouken@10609
   207
static Uint64
aschiffler@6872
   208
SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   209
{
aschiffler@6872
   210
        Uint64 b1, b2;
slouken@7191
   211
    Uint64 delta;
slouken@7191
   212
    Uint64 tempBuf[4];
slouken@7191
   213
    Uint8 index;
slouken@7191
   214
aschiffler@6872
   215
        /* Maybe swap */
slouken@7191
   216
    if (boundary1 > boundary2) {
slouken@7191
   217
        b1 = boundary2;
slouken@7191
   218
        b2 = boundary1;
slouken@7191
   219
    } else {
slouken@7191
   220
        b1 = boundary1;
slouken@7191
   221
        b2 = boundary2;
aschiffler@6872
   222
        }
aschiffler@6711
   223
slouken@7191
   224
    index = 0;
slouken@7191
   225
    if (validDomain == SDL_TRUE) {
slouken@7191
   226
            if (b1 == b2) {
slouken@7191
   227
                return b1;
slouken@7191
   228
            }
slouken@7191
   229
slouken@7191
   230
            /* Generate up to 4 values within bounds */
slouken@7191
   231
            delta = b2 - b1;
slouken@7191
   232
            if (delta < 4) {
slouken@7191
   233
                do {
slouken@7191
   234
                tempBuf[index] = b1 + index;
slouken@7191
   235
                index++;
aschiffler@6872
   236
                    } while (index < delta);
slouken@7191
   237
            } else {
slouken@7191
   238
          tempBuf[index] = b1;
slouken@7191
   239
          index++;
slouken@7191
   240
          tempBuf[index] = b1 + 1;
slouken@7191
   241
          index++;
slouken@7191
   242
          tempBuf[index] = b2 - 1;
slouken@7191
   243
          index++;
slouken@7191
   244
          tempBuf[index] = b2;
slouken@7191
   245
          index++;
slouken@7191
   246
            }
slouken@7191
   247
        } else {
slouken@7191
   248
            /* Generate up to 2 values outside of bounds */
slouken@7191
   249
        if (b1 > 0) {
slouken@7191
   250
            tempBuf[index] = b1 - 1;
slouken@7191
   251
            index++;
slouken@7191
   252
        }
aschiffler@6711
   253
slouken@7191
   254
        if (b2 < maxValue) {
slouken@7191
   255
            tempBuf[index] = b2 + 1;
slouken@7191
   256
            index++;
slouken@7191
   257
        }
slouken@7191
   258
    }
aschiffler@6711
   259
slouken@7191
   260
    if (index == 0) {
slouken@7191
   261
        /* There are no valid boundaries */
slouken@7191
   262
        SDL_Unsupported();
slouken@7191
   263
        return 0;
slouken@7191
   264
    }
aschiffler@6711
   265
slouken@7191
   266
    return tempBuf[SDLTest_RandomUint8() % index];
aschiffler@6872
   267
}
aschiffler@6711
   268
aschiffler@6711
   269
aschiffler@6711
   270
Uint8
aschiffler@6711
   271
SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
aschiffler@6711
   272
{
slouken@7191
   273
    /* max value for Uint8 */
slouken@7191
   274
    const Uint64 maxValue = UCHAR_MAX;
slouken@7191
   275
    return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   276
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   277
                validDomain);
aschiffler@6711
   278
}
aschiffler@6711
   279
aschiffler@6711
   280
Uint16
aschiffler@6711
   281
SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
aschiffler@6711
   282
{
slouken@7191
   283
    /* max value for Uint16 */
slouken@7191
   284
    const Uint64 maxValue = USHRT_MAX;
slouken@7191
   285
    return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   286
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   287
                validDomain);
aschiffler@6711
   288
}
aschiffler@6711
   289
aschiffler@6711
   290
Uint32
aschiffler@6711
   291
SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
aschiffler@6711
   292
{
slouken@7191
   293
    /* max value for Uint32 */
slouken@7191
   294
    #if ((ULONG_MAX) == (UINT_MAX))
slouken@7191
   295
      const Uint64 maxValue = ULONG_MAX;
aschiffler@7172
   296
        #else
slouken@7191
   297
      const Uint64 maxValue = UINT_MAX;
aschiffler@7172
   298
        #endif
slouken@7191
   299
    return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   300
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   301
                validDomain);
aschiffler@6711
   302
}
aschiffler@6711
   303
aschiffler@6711
   304
Uint64
aschiffler@6711
   305
SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   306
{
slouken@7191
   307
    /* max value for Uint64 */
slouken@10635
   308
    const Uint64 maxValue = UINT64_MAX;
slouken@7191
   309
    return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
slouken@7191
   310
                (Uint64) boundary1, (Uint64) boundary2,
slouken@7191
   311
                validDomain);
aschiffler@6711
   312
}
aschiffler@6711
   313
gabomdq@7678
   314
/* !
aschiffler@6872
   315
 * Generates a signed boundary value between the given boundaries.
aschiffler@6711
   316
 * Boundary values are inclusive. See the examples below.
aschiffler@6711
   317
 * If boundary2 < boundary1, the values are swapped.
aschiffler@6711
   318
 * If boundary1 == boundary2, value of boundary1 will be returned
aschiffler@6711
   319
 *
aschiffler@6711
   320
 * Generating boundary values for Sint8:
aschiffler@6872
   321
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
aschiffler@6872
   322
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
aschiffler@6872
   323
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
aschiffler@6872
   324
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
aschiffler@6872
   325
 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
aschiffler@6711
   326
 *
aschiffler@6711
   327
 * Generator works the same for other types of signed integers.
aschiffler@6711
   328
 *
aschiffler@6872
   329
 * \param minValue The smallest value that is acceptable for this data type.
slouken@7191
   330
 *                  For instance, for Uint8 -> -127, etc.
aschiffler@6711
   331
 * \param maxValue The biggest value that is acceptable for this data type.
slouken@7191
   332
 *                  For instance, for Uint8 -> 127, etc.
aschiffler@6872
   333
 * \param boundary1 defines lower boundary
aschiffler@6872
   334
 * \param boundary2 defines upper boundary
aschiffler@6711
   335
 * \param validDomain Generate only for valid domain (for the data type)
aschiffler@6711
   336
 *
aschiffler@6872
   337
 * \returns Returns a random boundary value for the domain or 0 in case of error
aschiffler@6711
   338
 */
slouken@10609
   339
static Sint64
aschiffler@6872
   340
SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   341
{
aschiffler@6872
   342
        Sint64 b1, b2;
slouken@7191
   343
    Sint64 delta;
slouken@7191
   344
    Sint64 tempBuf[4];
slouken@7191
   345
    Uint8 index;
slouken@7191
   346
aschiffler@6872
   347
        /* Maybe swap */
slouken@7191
   348
    if (boundary1 > boundary2) {
slouken@7191
   349
        b1 = boundary2;
slouken@7191
   350
        b2 = boundary1;
slouken@7191
   351
    } else {
slouken@7191
   352
        b1 = boundary1;
slouken@7191
   353
        b2 = boundary2;
aschiffler@6872
   354
        }
aschiffler@6711
   355
slouken@7191
   356
    index = 0;
slouken@7191
   357
    if (validDomain == SDL_TRUE) {
slouken@7191
   358
            if (b1 == b2) {
slouken@7191
   359
                return b1;
slouken@7191
   360
            }
slouken@7191
   361
slouken@7191
   362
            /* Generate up to 4 values within bounds */
slouken@7191
   363
            delta = b2 - b1;
slouken@7191
   364
            if (delta < 4) {
slouken@7191
   365
                do {
slouken@7191
   366
                tempBuf[index] = b1 + index;
slouken@7191
   367
                index++;
aschiffler@6872
   368
                    } while (index < delta);
slouken@7191
   369
            } else {
slouken@7191
   370
          tempBuf[index] = b1;
slouken@7191
   371
          index++;
slouken@7191
   372
          tempBuf[index] = b1 + 1;
slouken@7191
   373
          index++;
slouken@7191
   374
          tempBuf[index] = b2 - 1;
slouken@7191
   375
          index++;
slouken@7191
   376
          tempBuf[index] = b2;
slouken@7191
   377
          index++;
slouken@7191
   378
            }
slouken@7191
   379
        } else {
slouken@7191
   380
            /* Generate up to 2 values outside of bounds */
slouken@7191
   381
        if (b1 > minValue) {
slouken@7191
   382
            tempBuf[index] = b1 - 1;
slouken@7191
   383
            index++;
slouken@7191
   384
        }
aschiffler@6711
   385
slouken@7191
   386
        if (b2 < maxValue) {
slouken@7191
   387
            tempBuf[index] = b2 + 1;
slouken@7191
   388
            index++;
slouken@7191
   389
        }
slouken@7191
   390
    }
aschiffler@6711
   391
slouken@7191
   392
    if (index == 0) {
slouken@7191
   393
        /* There are no valid boundaries */
slouken@7191
   394
        SDL_Unsupported();
slouken@7191
   395
        return minValue;
slouken@7191
   396
    }
aschiffler@6711
   397
slouken@7191
   398
    return tempBuf[SDLTest_RandomUint8() % index];
aschiffler@6872
   399
}
aschiffler@6711
   400
aschiffler@6711
   401
aschiffler@6711
   402
Sint8
aschiffler@6711
   403
SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
aschiffler@6711
   404
{
slouken@7191
   405
    /* min & max values for Sint8 */
slouken@7191
   406
    const Sint64 maxValue = SCHAR_MAX;
slouken@7191
   407
    const Sint64 minValue = SCHAR_MIN;
slouken@7191
   408
    return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   409
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   410
                validDomain);
aschiffler@6711
   411
}
aschiffler@6711
   412
aschiffler@6711
   413
Sint16
aschiffler@6711
   414
SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
aschiffler@6711
   415
{
slouken@7191
   416
    /* min & max values for Sint16 */
slouken@7191
   417
    const Sint64 maxValue = SHRT_MAX;
slouken@7191
   418
    const Sint64 minValue = SHRT_MIN;
slouken@7191
   419
    return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   420
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   421
                validDomain);
aschiffler@6711
   422
}
aschiffler@6711
   423
aschiffler@6711
   424
Sint32
aschiffler@6711
   425
SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
aschiffler@6711
   426
{
slouken@7191
   427
    /* min & max values for Sint32 */
slouken@7191
   428
    #if ((ULONG_MAX) == (UINT_MAX))
slouken@7191
   429
      const Sint64 maxValue = LONG_MAX;
slouken@7191
   430
      const Sint64 minValue = LONG_MIN;
aschiffler@7172
   431
        #else
slouken@7191
   432
      const Sint64 maxValue = INT_MAX;
slouken@7191
   433
      const Sint64 minValue = INT_MIN;
aschiffler@7172
   434
        #endif
slouken@7191
   435
    return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   436
                (Sint64) boundary1, (Sint64) boundary2,
slouken@7191
   437
                validDomain);
aschiffler@6711
   438
}
aschiffler@6711
   439
aschiffler@6711
   440
Sint64
aschiffler@6711
   441
SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
aschiffler@6711
   442
{
slouken@7191
   443
    /* min & max values for Sint64 */
slouken@10635
   444
    const Sint64 maxValue = INT64_MAX;
slouken@10635
   445
    const Sint64 minValue = INT64_MIN;
slouken@7191
   446
    return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
slouken@7191
   447
                boundary1, boundary2,
slouken@7191
   448
                validDomain);
aschiffler@6711
   449
}
aschiffler@6711
   450
aschiffler@6711
   451
float
aschiffler@6711
   452
SDLTest_RandomUnitFloat()
aschiffler@6711
   453
{
slouken@10616
   454
    return SDLTest_RandomUint32() / (float) UINT_MAX;
aschiffler@6711
   455
}
aschiffler@6711
   456
aschiffler@6711
   457
float
aschiffler@6711
   458
SDLTest_RandomFloat()
aschiffler@6711
   459
{
aschiffler@6839
   460
        return (float) (SDLTest_RandomUnitDouble() * (double)2.0 * (double)FLT_MAX - (double)(FLT_MAX));
aschiffler@6711
   461
}
aschiffler@6711
   462
aschiffler@6711
   463
double
aschiffler@6711
   464
SDLTest_RandomUnitDouble()
aschiffler@6711
   465
{
slouken@7191
   466
    return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
aschiffler@6711
   467
}
aschiffler@6711
   468
aschiffler@6711
   469
double
aschiffler@6711
   470
SDLTest_RandomDouble()
aschiffler@6711
   471
{
slouken@7191
   472
    double r = 0.0;
slouken@7191
   473
    double s = 1.0;
slouken@7191
   474
    do {
slouken@7191
   475
      s /= UINT_MAX + 1.0;
slouken@7191
   476
      r += (double)SDLTest_RandomInt(&rndContext) * s;
slouken@7191
   477
    } while (s > DBL_EPSILON);
slouken@7191
   478
slouken@7191
   479
    fuzzerInvocationCounter++;
slouken@7191
   480
slouken@7191
   481
    return r;
aschiffler@6711
   482
}
aschiffler@6711
   483
aschiffler@6711
   484
aschiffler@6711
   485
char *
aschiffler@6711
   486
SDLTest_RandomAsciiString()
aschiffler@6711
   487
{
slouken@7191
   488
    return SDLTest_RandomAsciiStringWithMaximumLength(255);
aschiffler@6711
   489
}
aschiffler@6711
   490
aschiffler@6711
   491
char *
aschiffler@6811
   492
SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
aschiffler@6711
   493
{
slouken@7191
   494
    int size;
aschiffler@6811
   495
slouken@7191
   496
    if(maxLength < 1) {
aschiffler@6811
   497
                SDL_InvalidParamError("maxLength");
slouken@7191
   498
        return NULL;
slouken@7191
   499
    }
aschiffler@6811
   500
slouken@7191
   501
    size = (SDLTest_RandomUint32() % (maxLength + 1));
slouken@7191
   502
slouken@7191
   503
    return SDLTest_RandomAsciiStringOfSize(size);
aschiffler@6811
   504
}
aschiffler@6811
   505
aschiffler@6811
   506
char *
aschiffler@6811
   507
SDLTest_RandomAsciiStringOfSize(int size)
aschiffler@6811
   508
{
slouken@7191
   509
    char *string;
slouken@7191
   510
    int counter;
aschiffler@6711
   511
aschiffler@6711
   512
slouken@7191
   513
    if(size < 1) {
aschiffler@6811
   514
                SDL_InvalidParamError("size");
slouken@7191
   515
        return NULL;
slouken@7191
   516
    }
aschiffler@6711
   517
slouken@7191
   518
    string = (char *)SDL_malloc((size + 1) * sizeof(char));
slouken@7191
   519
    if (string==NULL) {
slouken@7191
   520
      return NULL;
aschiffler@6711
   521
        }
aschiffler@6711
   522
slouken@7191
   523
    for(counter = 0; counter < size; ++counter) {
slouken@7191
   524
        string[counter] = (char)SDLTest_RandomIntegerInRange(32, 126);
slouken@7191
   525
    }
aschiffler@6711
   526
slouken@7191
   527
    string[counter] = '\0';
aschiffler@6711
   528
slouken@7191
   529
    fuzzerInvocationCounter++;
aschiffler@6811
   530
slouken@7191
   531
    return string;
aschiffler@6711
   532
}
slouken@11465
   533
slouken@11465
   534
/* vi: set ts=4 sw=4 expandtab: */