[libcxx-commits] [libcxx] 84654f2 - [libc++] Refactor the tests for std::random_device

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 10 13:34:22 PST 2022


Author: Louis Dionne
Date: 2022-01-10T16:34:16-05:00
New Revision: 84654f2733f63dc725a7b3d7c55d56849d2d9358

URL: https://github.com/llvm/llvm-project/commit/84654f2733f63dc725a7b3d7c55d56849d2d9358
DIFF: https://github.com/llvm/llvm-project/commit/84654f2733f63dc725a7b3d7c55d56849d2d9358.diff

LOG: [libc++] Refactor the tests for std::random_device

That will make it easier to change the behavior of the arc4random()
based implementation. Note that in particular, the eval.pass.cpp test
used to work with non "/dev/random" based implementations because we'd
throw an exception upon constructing the random_device. This patch makes
the intent of the test clearer.

Added: 
    

Modified: 
    libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
    libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp b/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
index fab4e15f680b3..6abd57827c336 100644
--- a/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.device/ctor.pass.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 // See https://llvm.org/PR20183
-//
 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
 
 // UNSUPPORTED: libcpp-has-no-random-device
@@ -38,15 +37,6 @@
 #include "test_convertible.h"
 #endif
 
-bool is_valid_random_device(const std::string &token) {
-#if defined(_LIBCPP_USING_DEV_RANDOM)
-  // Not an exhaustive list: they're the only tokens that are tested below.
-  return token == "/dev/urandom" || token == "/dev/random";
-#else
-  return token == "/dev/urandom";
-#endif
-}
-
 void check_random_device_valid(const std::string &token) {
   std::random_device r(token);
 }
@@ -67,24 +57,18 @@ int main(int, char**) {
   {
     std::random_device r;
   }
+  // Check the validity of various tokens
   {
-    std::string token = "wrong file";
-    check_random_device_invalid(token);
-  }
-  {
-    std::string token = "/dev/urandom";
-    if (is_valid_random_device(token))
-      check_random_device_valid(token);
-    else
-      check_random_device_invalid(token);
-  }
-  {
-    std::string token = "/dev/random";
-    if (is_valid_random_device(token))
-      check_random_device_valid(token);
-    else
-      check_random_device_invalid(token);
+    check_random_device_invalid("wrong file");
+    check_random_device_invalid("/dev/whatever");
+    check_random_device_valid("/dev/urandom");
+#if defined(_LIBCPP_USING_DEV_RANDOM)
+    check_random_device_valid("/dev/random");
+#else
+    check_random_device_invalid("/dev/random");
+#endif
   }
+
 #if !defined(_WIN32)
 // Test that random_device(const string&) properly handles getting
 // a file descriptor with the value '0'. Do this by closing the standard

diff  --git a/libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp b/libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp
index 4a41ee10cdb2d..8d0b507d94613 100644
--- a/libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp
+++ b/libcxx/test/std/numerics/rand/rand.device/eval.pass.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 // See https://llvm.org/PR20183
-//
 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
 
 // UNSUPPORTED: libcpp-has-no-random-device
@@ -32,15 +31,16 @@ int main(int, char**)
         ((void)e); // Prevent unused warning
     }
 
-#ifndef TEST_HAS_NO_EXCEPTIONS
-    try
+    // When using the `/dev/urandom` implementation, make sure that we throw
+    // an exception when we hit EOF while reading the custom-provided file.
+#if !defined(TEST_HAS_NO_EXCEPTIONS) && defined(_LIBCPP_USING_DEV_RANDOM)
     {
         std::random_device r("/dev/null");
-        (void)r();
-        LIBCPP_ASSERT(false);
-    }
-    catch (const std::system_error&)
-    {
+        try {
+            (void)r();
+            LIBCPP_ASSERT(false);
+        } catch (const std::system_error&) {
+        }
     }
 #endif
 


        


More information about the libcxx-commits mailing list