OSDN Git Service

test: use C random library for random numbers
authorU. Artie Eoff <ullysses.a.eoff@intel.com>
Wed, 26 Oct 2016 20:24:16 +0000 (13:24 -0700)
committerSean V Kelley <seanvk@posteo.de>
Fri, 28 Oct 2016 20:12:11 +0000 (13:12 -0700)
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>
test/i965_test_environment.cpp
test/object_heap_test.cpp
test/test_utils.h

index 0049c14..ee8b1cc 100644 (file)
@@ -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);
 
index 70257f6..89fd8d7 100644 (file)
@@ -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 = [&]{
index 333106c..8083591 100644 (file)
 #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