[libcxx-commits] [libcxx] b051cc9 - [NFC][libc++] Refactor some future tests to reduce code duplication

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 25 15:17:04 PST 2020


Author: Louis Dionne
Date: 2020-02-25T18:16:45-05:00
New Revision: b051cc932782998764d2d297bda1e9a18469a1ce

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

LOG: [NFC][libc++] Refactor some future tests to reduce code duplication

The same test was being repeated over and over again.
That's what functions are for.

Added: 
    

Modified: 
    libcxx/test/std/thread/futures/futures.unique_future/wait.pass.cpp
    libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/thread/futures/futures.unique_future/wait.pass.cpp b/libcxx/test/std/thread/futures/futures.unique_future/wait.pass.cpp
index 956e62eb41aa..10bbc11c9e17 100644
--- a/libcxx/test/std/thread/futures/futures.unique_future/wait.pass.cpp
+++ b/libcxx/test/std/thread/futures/futures.unique_future/wait.pass.cpp
@@ -41,52 +41,28 @@ void func5(std::promise<void> p)
     p.set_value();
 }
 
-int main(int, char**)
-{
+template <typename T, typename F>
+void test(F func) {
     typedef std::chrono::high_resolution_clock Clock;
     typedef std::chrono::duration<double, std::milli> ms;
-    {
-        typedef int T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func1, std::move(p)).detach();
-        assert(f.valid());
-        f.wait();
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(5));
-    }
-    {
-        typedef int& T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func3, std::move(p)).detach();
-        assert(f.valid());
-        f.wait();
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(5));
-    }
-    {
-        typedef void T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func5, std::move(p)).detach();
-        assert(f.valid());
-        f.wait();
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(5));
-    }
 
-  return 0;
+    std::promise<T> p;
+    std::future<T> f = p.get_future();
+    std::thread(func, std::move(p)).detach();
+    assert(f.valid());
+    f.wait();
+    assert(f.valid());
+    Clock::time_point t0 = Clock::now();
+    f.wait();
+    Clock::time_point t1 = Clock::now();
+    assert(f.valid());
+    assert(t1-t0 < ms(5));
+}
+
+int main(int, char**)
+{
+    test<int>(func1);
+    test<int&>(func3);
+    test<void>(func5);
+    return 0;
 }

diff  --git a/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp b/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp
index 2bc05664681b..31222ef1b6c9 100644
--- a/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp
+++ b/libcxx/test/std/thread/futures/futures.unique_future/wait_for.pass.cpp
@@ -45,57 +45,28 @@ void func5(std::promise<void> p)
     p.set_value();
 }
 
-int main(int, char**)
-{
+template <typename T, typename F>
+void test(F func) {
     typedef std::chrono::high_resolution_clock Clock;
-    {
-        typedef int T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func1, std::move(p)).detach();
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::timeout);
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::ready);
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(50));
-    }
-    {
-        typedef int& T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func3, std::move(p)).detach();
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::timeout);
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::ready);
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(50));
-    }
-    {
-        typedef void T;
-        std::promise<T> p;
-        std::future<T> f = p.get_future();
-        std::thread(func5, std::move(p)).detach();
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::timeout);
-        assert(f.valid());
-        assert(f.wait_for(ms(300)) == std::future_status::ready);
-        assert(f.valid());
-        Clock::time_point t0 = Clock::now();
-        f.wait();
-        Clock::time_point t1 = Clock::now();
-        assert(f.valid());
-        assert(t1-t0 < ms(50));
-    }
+    std::promise<T> p;
+    std::future<T> f = p.get_future();
+    std::thread(func, std::move(p)).detach();
+    assert(f.valid());
+    assert(f.wait_for(ms(300)) == std::future_status::timeout);
+    assert(f.valid());
+    assert(f.wait_for(ms(300)) == std::future_status::ready);
+    assert(f.valid());
+    Clock::time_point t0 = Clock::now();
+    f.wait();
+    Clock::time_point t1 = Clock::now();
+    assert(f.valid());
+    assert(t1-t0 < ms(50));
+}
 
-  return 0;
+int main(int, char**)
+{
+    test<int>(func1);
+    test<int&>(func3);
+    test<void>(func5);
+    return 0;
 }


        


More information about the libcxx-commits mailing list