diff options
author | U. Artie Eoff <ullysses.a.eoff@intel.com> | 2016-10-26 13:24:16 -0700 |
---|---|---|
committer | Xiang, Haihao <haihao.xiang@intel.com> | 2016-10-31 10:00:08 +0800 |
commit | 1062d29f4fa4a3543eeb7b2203b0b80d223113e0 (patch) | |
tree | c0c7eac1cf770fc78057c6d9c8dda338640908c5 | |
parent | bf17b32b7b008451cbbc3f3e139611f41b450a0b (diff) | |
download | libva-intel-driver-1062d29f4fa4a3543eeb7b2203b0b80d223113e0.tar.gz |
test: use C random library for random numbers
The speed of random number generation can have a
significant impact on test execution time when
initializing large arrays of random values. The C++
random number engines and generators are much slower
than std::rand.
Thus, use C's rand() to generate pseudo-random values
within a given [min,max] range. For testing purposes,
deterministic sequences would be preferable anyway.
That is, if a particular sequence exposes a test failure,
then we can reproduce it later. Also, we seed the
pseudo-random number generator with the current time
so that the sequence is not always the same across
executions. The seed is then recorded in the test
results so that the sequence can be reproduced if
needed.
Signed-off-by: U. Artie Eoff <ullysses.a.eoff@intel.com>
Reviewed-by: Sean V Kelley <seanvk@posteo.de>
(cherry picked from commit 9edf73284039f31720eb03f4c2196bb7c4dd033f)
-rw-r--r-- | test/i965_test_environment.cpp | 7 | ||||
-rw-r--r-- | test/object_heap_test.cpp | 4 | ||||
-rw-r--r-- | test/test_utils.h | 19 |
3 files changed, 18 insertions, 12 deletions
diff --git a/test/i965_test_environment.cpp b/test/i965_test_environment.cpp index 0049c143..ee8b1cc4 100644 --- a/test/i965_test_environment.cpp +++ b/test/i965_test_environment.cpp @@ -24,6 +24,8 @@ #include "i965_test_environment.h" +#include <cstdlib> +#include <ctime> #include <fcntl.h> // for O_RDWR #include <unistd.h> // for close() #include <va/va_drm.h> @@ -44,6 +46,11 @@ I965TestEnvironment::I965TestEnvironment() void I965TestEnvironment::SetUp() { + std::time_t seed(std::time(0)); + std::srand(seed); + ::testing::Test::RecordProperty("rand_seed", seed); + std::cout << "Seeded std::rand() with " << seed << "." << std::endl; + ASSERT_EQ(-1, m_handle); ASSERT_PTR_NULL(m_vaDisplay); diff --git a/test/object_heap_test.cpp b/test/object_heap_test.cpp index 70257f63..89fd8d78 100644 --- a/test/object_heap_test.cpp +++ b/test/object_heap_test.cpp @@ -181,10 +181,6 @@ TEST(ObjectHeapTest, DataIntegrity) ASSERT_EQ(0, object_heap_init(&heap, sizeof(test_object), 0)); - std::time_t seed = std::time(0); - std::srand(seed); - RecordProperty("seed", seed); - std::vector<int> values; auto generator = [&]{ diff --git a/test/test_utils.h b/test/test_utils.h index 333106c2..8083591f 100644 --- a/test/test_utils.h +++ b/test/test_utils.h @@ -26,25 +26,28 @@ #define TEST_UTILS_H #include <chrono> -#include <random> +#include <cstdlib> template <typename T> class RandomValueGenerator { public: RandomValueGenerator(const T& min, const T& max) - : dis(min, max) - { } + : minVal(min) + , maxVal(max) + { + return; + } - const T operator()() + const T operator()() const { - static std::random_device rd; - static std::default_random_engine gen(rd()); - return dis(gen); + return static_cast<T>( + std::rand() % (maxVal + 1 - minVal) + minVal); } private: - std::uniform_int_distribution<T> dis; + T minVal; + T maxVal; }; class Timer |