William A. Kennington III | 8eaf5fc | 2019-03-29 15:36:39 -0700 | [diff] [blame^] | 1 | #include <functional> |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 2 | #include <gtest/gtest.h> |
William A. Kennington III | 8eaf5fc | 2019-03-29 15:36:39 -0700 | [diff] [blame^] | 3 | #include <memory> |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 4 | #include <sdeventplus/internal/utils.hpp> |
| 5 | #include <stdexcept> |
| 6 | #include <system_error> |
William A. Kennington III | 8eaf5fc | 2019-03-29 15:36:39 -0700 | [diff] [blame^] | 7 | #include <utility> |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 8 | |
| 9 | namespace sdeventplus |
| 10 | { |
| 11 | namespace internal |
| 12 | { |
| 13 | namespace |
| 14 | { |
| 15 | |
| 16 | TEST(UtilsTest, PerformCallbackSuccess) |
| 17 | { |
William A. Kennington III | 4e9e604 | 2018-07-17 16:54:36 -0700 | [diff] [blame] | 18 | EXPECT_EQ(0, performCallback(nullptr, []() {})); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 19 | } |
| 20 | |
William A. Kennington III | 8eaf5fc | 2019-03-29 15:36:39 -0700 | [diff] [blame^] | 21 | TEST(UtilsTest, PerformCallbackAcceptsReference) |
| 22 | { |
| 23 | auto f = |
| 24 | std::bind([](const std::unique_ptr<int>&) {}, std::make_unique<int>(1)); |
| 25 | EXPECT_EQ(0, performCallback(nullptr, f)); |
| 26 | } |
| 27 | |
| 28 | TEST(UtilsTest, PerformCallbackAcceptsMove) |
| 29 | { |
| 30 | auto f = |
| 31 | std::bind([](const std::unique_ptr<int>&) {}, std::make_unique<int>(1)); |
| 32 | EXPECT_EQ(0, performCallback(nullptr, std::move(f))); |
| 33 | } |
| 34 | |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 35 | TEST(UtilsTest, SetPrepareSystemError) |
| 36 | { |
William A. Kennington III | 4e9e604 | 2018-07-17 16:54:36 -0700 | [diff] [blame] | 37 | EXPECT_EQ(-EBUSY, performCallback("system_error", []() { |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 38 | throw std::system_error(EBUSY, std::generic_category()); |
| 39 | })); |
| 40 | } |
| 41 | |
| 42 | TEST(UtilsTest, SetPrepareException) |
| 43 | { |
William A. Kennington III | 4e9e604 | 2018-07-17 16:54:36 -0700 | [diff] [blame] | 44 | EXPECT_EQ(-ENOSYS, performCallback("runtime_error", []() { |
| 45 | throw std::runtime_error("Exception"); |
| 46 | })); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(UtilsTest, SetPrepareUnknownException) |
| 50 | { |
William A. Kennington III | 4e9e604 | 2018-07-17 16:54:36 -0700 | [diff] [blame] | 51 | EXPECT_EQ(-ENOSYS, |
| 52 | performCallback("unknown", []() { throw static_cast<int>(1); })); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | } // namespace internal |
| 57 | } // namespace sdeventplus |