Add separate pseudorandom generator for testing inputs
[gfxprim.git] / libs / core / GP_TestingRandom.c
blob17281a9c77524bc9fd6719d56f5851ac7576caeb
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2011 Tomas Gavenciak <gavento@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdlib.h>
25 #include "GP_Common.h"
26 #include "GP_TestingRandom.h"
29 * State array for testing random generator
31 * The idea is to have a seperate random generator for generating
32 * random inputs for tests in case tested algorithms also use
33 * the random generator. Change in the tested algorithm must not
34 * change the input data generated for this or other part of the test.
36 * Take care when changing the values - unrelated test might start
37 * exhibiting a bug, some tests may rely on the exact result.
39 #define GP_RandomStateSize 256
40 static char GP_RandomTestingState[GP_RandomStateSize];
41 static struct random_data GP_RandomTestingData;
43 long int GP_TestingRandom(void)
45 int32_t r;
46 GP_ASSERT(random_r(&GP_RandomTestingData, &r) == 0);
47 return r;
50 void GP_InitTestingRandom(const char *text, uint64_t seed)
52 const char *p = text;
53 for (; (p) && (*p); p++)
54 seed = ((seed * 327) + *p) % 0x7B391D50A10A3LL; // TODO replace with large primes
55 GP_ASSERT(initstate_r(seed, GP_RandomTestingState, GP_RandomStateSize,
56 &GP_RandomTestingData) == 0);