[Libva] [PATCH intel-driver 2/5] test: use C random library for random numbers

U. Artie Eoff ullysses.a.eoff at intel.com
Wed Oct 26 20:24:16 UTC 2016


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 at intel.com>
---
 test/i965_test_environment.cpp |  7 +++++++
 test/object_heap_test.cpp      |  4 ----
 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 0049c1435957..ee8b1cc4465a 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 70257f63f409..89fd8d78bdbb 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 333106c239d6..8083591f587b 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
-- 
2.1.0



More information about the Libva mailing list