| Dominik Laskowski | 4e2b71f | 2020-11-10 15:05:32 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2020 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <ftl/future.h> | 
 | 18 | #include <gtest/gtest.h> | 
 | 19 |  | 
 | 20 | #include <algorithm> | 
 | 21 | #include <future> | 
 | 22 | #include <string> | 
 | 23 | #include <thread> | 
 | 24 | #include <vector> | 
 | 25 |  | 
 | 26 | namespace android::test { | 
 | 27 |  | 
 | 28 | // Keep in sync with example usage in header file. | 
 | 29 | TEST(Future, Example) { | 
 | 30 |   { | 
 | 31 |     auto future = ftl::defer([](int x) { return x + 1; }, 99); | 
 | 32 |     EXPECT_EQ(future.get(), 100); | 
 | 33 |   } | 
 | 34 |   { | 
 | 35 |     auto future = ftl::yield(42); | 
 | 36 |     EXPECT_EQ(future.get(), 42); | 
 | 37 |   } | 
 | 38 |   { | 
 | 39 |     auto ptr = std::make_unique<char>('!'); | 
 | 40 |     auto future = ftl::yield(std::move(ptr)); | 
 | 41 |     EXPECT_EQ(*future.get(), '!'); | 
 | 42 |   } | 
 | 43 |   { | 
 | 44 |     auto future = ftl::yield(123); | 
 | 45 |     std::future<char> futures[] = {ftl::yield('a'), ftl::yield('b')}; | 
 | 46 |  | 
 | 47 |     std::future<char> chain = ftl::chain(std::move(future)) | 
 | 48 |                                   .then([](int x) { return static_cast<size_t>(x % 2); }) | 
 | 49 |                                   .then([&futures](size_t i) { return std::move(futures[i]); }); | 
 | 50 |  | 
 | 51 |     EXPECT_EQ(chain.get(), 'b'); | 
 | 52 |   } | 
 | 53 | } | 
 | 54 |  | 
 | 55 | namespace { | 
 | 56 |  | 
 | 57 | using ByteVector = std::vector<uint8_t>; | 
 | 58 |  | 
 | 59 | ByteVector decrement(ByteVector bytes) { | 
 | 60 |   std::transform(bytes.begin(), bytes.end(), bytes.begin(), [](auto b) { return b - 1; }); | 
 | 61 |   return bytes; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | }  // namespace | 
 | 65 |  | 
 | 66 | TEST(Future, Chain) { | 
 | 67 |   std::packaged_task<const char*()> fetch_string([] { return "ifmmp-"; }); | 
 | 68 |  | 
 | 69 |   std::packaged_task<ByteVector(std::string)> append_string([](std::string str) { | 
 | 70 |     str += "!xpsme"; | 
 | 71 |     return ByteVector{str.begin(), str.end()}; | 
 | 72 |   }); | 
 | 73 |  | 
 | 74 |   std::packaged_task<std::future<ByteVector>(ByteVector)> decrement_bytes( | 
 | 75 |       [](ByteVector bytes) { return ftl::defer(decrement, std::move(bytes)); }); | 
 | 76 |  | 
 | 77 |   auto fetch = fetch_string.get_future(); | 
 | 78 |   std::thread fetch_thread(std::move(fetch_string)); | 
 | 79 |  | 
 | 80 |   std::thread append_thread, decrement_thread; | 
 | 81 |  | 
 | 82 |   EXPECT_EQ( | 
 | 83 |       "hello, world", | 
 | 84 |       ftl::chain(std::move(fetch)) | 
 | 85 |           .then([](const char* str) { return std::string(str); }) | 
 | 86 |           .then([&](std::string str) { | 
 | 87 |             auto append = append_string.get_future(); | 
 | 88 |             append_thread = std::thread(std::move(append_string), std::move(str)); | 
 | 89 |             return append; | 
 | 90 |           }) | 
 | 91 |           .then([&](ByteVector bytes) { | 
 | 92 |             auto decrement = decrement_bytes.get_future(); | 
 | 93 |             decrement_thread = std::thread(std::move(decrement_bytes), std::move(bytes)); | 
 | 94 |             return decrement; | 
 | 95 |           }) | 
 | 96 |           .then([](std::future<ByteVector> bytes) { return bytes; }) | 
 | 97 |           .then([](const ByteVector& bytes) { return std::string(bytes.begin(), bytes.end()); }) | 
 | 98 |           .get()); | 
 | 99 |  | 
 | 100 |   fetch_thread.join(); | 
 | 101 |   append_thread.join(); | 
 | 102 |   decrement_thread.join(); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | }  // namespace android::test |