From d7551ce7958fc44a3a9e070258d3c8a45a23f755 Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Sun, 12 Jun 2011 20:42:53 +0200 Subject: [PATCH] Add separate pseudorandom generator for testing inputs --- include/core/GP_TestingRandom.h | 47 +++++++++++++++++++++++++++++++++ libs/core/GP_TestingRandom.c | 58 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 include/core/GP_TestingRandom.h create mode 100644 libs/core/GP_TestingRandom.c diff --git a/include/core/GP_TestingRandom.h b/include/core/GP_TestingRandom.h new file mode 100644 index 00000000..9f897f8c --- /dev/null +++ b/include/core/GP_TestingRandom.h @@ -0,0 +1,47 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2011 Tomas Gavenciak * + * * + *****************************************************************************/ + +/* + * These routines use a pseudorandom generator with internal state. + * Use this only to provide tests with pseudo-random inputs. + * The seed is initialized with the test name, so the random numbers + * (and therefore inputs) are the same for a test, and do not depend + * on random() called in the tested routines or other tests. + */ + +#ifndef CORE_GP_TESTING_RANDOM_H +#define CORE_GP_TESTING_RANDOM_H + +/* + * Return next random value from the testing random generator + */ +long int GP_TestingRandom(void); + +/* + * Initialize the random generator to a seed computed from the given string + * and the given seed value. Needs to be called at least once. + * + * The string may be for example the name of the test, the seed the number of test iteration. + */ +void GP_InitTestingRandom(const char *text, uint64_t seed); + +#endif /* CORE_GP_TESTING_RANDOM_H */ diff --git a/libs/core/GP_TestingRandom.c b/libs/core/GP_TestingRandom.c new file mode 100644 index 00000000..17281a9c --- /dev/null +++ b/libs/core/GP_TestingRandom.c @@ -0,0 +1,58 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2011 Tomas Gavenciak * + * * + *****************************************************************************/ + +#include + +#include "GP_Common.h" +#include "GP_TestingRandom.h" + +/* + * State array for testing random generator + * + * The idea is to have a seperate random generator for generating + * random inputs for tests in case tested algorithms also use + * the random generator. Change in the tested algorithm must not + * change the input data generated for this or other part of the test. + * + * Take care when changing the values - unrelated test might start + * exhibiting a bug, some tests may rely on the exact result. + */ +#define GP_RandomStateSize 256 +static char GP_RandomTestingState[GP_RandomStateSize]; +static struct random_data GP_RandomTestingData; + +long int GP_TestingRandom(void) +{ + int32_t r; + GP_ASSERT(random_r(&GP_RandomTestingData, &r) == 0); + return r; +} + +void GP_InitTestingRandom(const char *text, uint64_t seed) +{ + const char *p = text; + for (; (p) && (*p); p++) + seed = ((seed * 327) + *p) % 0x7B391D50A10A3LL; // TODO replace with large primes + GP_ASSERT(initstate_r(seed, GP_RandomTestingState, GP_RandomStateSize, + &GP_RandomTestingData) == 0); +} + -- 2.11.4.GIT