summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVic (Chun-Ju) Yang <victoryang@chromium.org>2013-12-23 16:15:14 +0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2013-12-27 07:50:47 +0000
commit4e7e33f6e7fe114ef94b5e068d723f621e54e5ab (patch)
tree4110e0c2945e210d1f08967e426cce91a573c44b
parent1b1d2e999c7349e896121abec9515270a76072f9 (diff)
downloadchrome-ec-4e7e33f6e7fe114ef94b5e068d723f621e54e5ab.tar.gz
Move pseudo random number generator to common
We have three copies of the same pseudo random number generator in our test codes. Let's consolidate them into a single copy in test_util. BUG=chrome-os-partner:19235 TEST=Pass all tests BRANCH=None Change-Id: I7ea0b3476f3cfe6944855f19861e3c86af35807e Signed-off-by: Vic (Chun-Ju) Yang <victoryang@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/181085 Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r--common/test_util.c12
-rw-r--r--include/test_util.h4
-rw-r--r--test/mutex.c6
-rw-r--r--test/stress.c12
-rw-r--r--test/timer_dos.c7
5 files changed, 19 insertions, 22 deletions
diff --git a/common/test_util.c b/common/test_util.c
index 58a6e5e05c..3b64b16fec 100644
--- a/common/test_util.c
+++ b/common/test_util.c
@@ -140,6 +140,18 @@ int test_send_host_command(int command, int version, const void *params,
}
#endif /* TASK_HAS_HOSTCMD */
+/* Linear congruential pseudo random number generator */
+uint32_t prng(uint32_t seed)
+{
+ return 22695477 * seed + 1;
+}
+
+uint32_t prng_no_seed(void)
+{
+ static uint32_t seed = 0x1234abcd;
+ return seed = prng(seed);
+}
+
static int command_run_test(int argc, char **argv)
{
run_test();
diff --git a/include/test_util.h b/include/test_util.h
index 1dc737b986..a6b110f6d1 100644
--- a/include/test_util.h
+++ b/include/test_util.h
@@ -109,6 +109,10 @@ int test_get_error_count(void);
int test_send_host_command(int command, int version, const void *params,
int params_size, void *resp, int resp_size);
+uint32_t prng(uint32_t seed);
+
+uint32_t prng_no_seed(void);
+
/* Number of failed tests */
extern int __test_error_count;
diff --git a/test/mutex.c b/test/mutex.c
index 866a004a58..10cf300e22 100644
--- a/test/mutex.c
+++ b/test/mutex.c
@@ -15,12 +15,6 @@
static struct mutex mtx;
-/* Linear congruential pseudo random number generator*/
-static uint32_t prng(uint32_t x)
-{
- return 22695477 * x + 1;
-}
-
/* period between 50us and 3.2ms */
#define PERIOD_US(num) (((num % 64) + 1) * 50)
/* one of the 3 MTX3x tasks */
diff --git a/test/stress.c b/test/stress.c
index a51ab416eb..8712fda129 100644
--- a/test/stress.c
+++ b/test/stress.c
@@ -57,16 +57,8 @@ struct i2c_test_param_t {
/*****************************************************************************/
/* Test utilities */
-/* Linear congruential pseudo random number generator*/
-static uint32_t prng(void)
-{
- static uint32_t x = 1357;
- x = 22695477 * x + 1;
- return x;
-}
-
/* period between 500us and 32ms */
-#define RAND_US() (((prng() % 64) + 1) * 500)
+#define RAND_US() (((prng_no_seed() % 64) + 1) * 500)
static int stress(const char *name,
int (*test_routine)(void),
@@ -103,7 +95,7 @@ static int test_i2c(void)
int res = EC_ERROR_UNKNOWN;
int dummy_data;
struct i2c_test_param_t *param;
- param = i2c_test_params + (prng() % (sizeof(i2c_test_params) /
+ param = i2c_test_params + (prng_no_seed() % (sizeof(i2c_test_params) /
sizeof(struct i2c_test_param_t)));
if (param->width == 8 && param->data == -1)
res = i2c_read8(param->port, param->addr,
diff --git a/test/timer_dos.c b/test/timer_dos.c
index 69803f0a31..f941adea51 100644
--- a/test/timer_dos.c
+++ b/test/timer_dos.c
@@ -8,15 +8,10 @@
#include "common.h"
#include "console.h"
#include "task.h"
+#include "test_util.h"
#include "timer.h"
#include "util.h"
-/* Linear congruential pseudo random number generator*/
-static uint32_t prng(uint32_t x)
-{
- return 22695477 * x + 1;
-}
-
/* period between 500us and 128ms */
#define PERIOD_US(num) (((num % 256) + 1) * 500)