X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ffutures%2Ftest%2FPromiseTest.cpp;h=858f9533bf63cf5058279051d7575ba0953c08fd;hp=3f41c0721587c7769bdf2eb2a526a8d7cbd3de65;hb=7da4ef82aee382777bb50aadd4af14a482739d10;hpb=116ae4634a0287e35f378cb4c68c1ed7a48668ec diff --git a/folly/futures/test/PromiseTest.cpp b/folly/futures/test/PromiseTest.cpp index 3f41c072..858f9533 100644 --- a/folly/futures/test/PromiseTest.cpp +++ b/folly/futures/test/PromiseTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,12 @@ #include #include +#include + using namespace folly; -using std::unique_ptr; using std::string; +using std::unique_ptr; typedef FutureException eggs_t; static eggs_t eggs("eggs"); @@ -36,6 +38,12 @@ TEST(Promise, special) { EXPECT_TRUE(std::is_move_assignable>::value); } +TEST(Promise, getSemiFuture) { + Promise p; + SemiFuture f = p.getSemiFuture(); + EXPECT_FALSE(f.isReady()); +} + TEST(Promise, getFuture) { Promise p; Future f = p.getFuture(); @@ -47,6 +55,44 @@ TEST(Promise, setValueUnit) { p.setValue(); } +TEST(Promise, setValueSemiFuture) { + Promise fund; + auto ffund = fund.getSemiFuture(); + fund.setValue(42); + EXPECT_EQ(42, ffund.value()); + + struct Foo { + string name; + int value; + }; + + Promise pod; + auto fpod = pod.getSemiFuture(); + Foo f = {"the answer", 42}; + pod.setValue(f); + Foo f2 = fpod.value(); + EXPECT_EQ(f.name, f2.name); + EXPECT_EQ(f.value, f2.value); + + pod = Promise(); + fpod = pod.getSemiFuture(); + pod.setValue(std::move(f2)); + Foo f3 = fpod.value(); + EXPECT_EQ(f.name, f3.name); + EXPECT_EQ(f.value, f3.value); + + Promise> mov; + auto fmov = mov.getSemiFuture(); + mov.setValue(std::make_unique(42)); + unique_ptr ptr = std::move(fmov.value()); + EXPECT_EQ(42, *ptr); + + Promise v; + auto fv = v.getSemiFuture(); + v.setValue(); + EXPECT_TRUE(fv.isReady()); +} + TEST(Promise, setValue) { Promise fund; auto ffund = fund.getFuture(); @@ -75,7 +121,7 @@ TEST(Promise, setValue) { Promise> mov; auto fmov = mov.getFuture(); - mov.setValue(unique_ptr(new int(42))); + mov.setValue(std::make_unique(42)); unique_ptr ptr = std::move(fmov.value()); EXPECT_EQ(42, *ptr); @@ -95,7 +141,12 @@ TEST(Promise, setException) { { Promise p; auto f = p.getFuture(); + // Calling setException() with an exception_ptr is deprecated, + // but don't complain about this in the test for this function. + FOLLY_PUSH_WARNING + FOLLY_GCC_DISABLE_WARNING("-Wdeprecated-declarations") p.setException(std::make_exception_ptr(eggs)); + FOLLY_POP_WARNING EXPECT_THROW(f.value(), eggs_t); } {