[NFC][libc++] Refactor some future tests to reduce code duplication
authorLouis Dionne <ldionne@apple.com>
Tue, 25 Feb 2020 23:15:56 +0000 (18:15 -0500)
committerLouis Dionne <ldionne@apple.com>
Tue, 25 Feb 2020 23:16:45 +0000 (18:16 -0500)
The same test was being repeated over and over again.
That's what functions are for.

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

index 956e62e..10bbc11 100644 (file)
@@ -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;
 }
index 2bc0566..31222ef 100644 (file)
@@ -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;
 }