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