| 1 |
// Copyright (c) Glyn Matthews 2016. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#include <gtest/gtest.h> |
| 7 |
#include <string> |
| 8 |
#include <network/optional.hpp> |
| 9 |
#include <network/string_view.hpp> |
| 10 |
|
| 11 |
TEST(optional_test, empty_optional) { |
| 12 |
network::optional<int> opt; |
| 13 |
ASSERT_FALSE(opt); |
| 14 |
} |
| 15 |
|
| 16 |
TEST(optional_test, empty_optional_constructed_with_nullopt) { |
| 17 |
network::optional<int> opt{network::nullopt}; |
| 18 |
ASSERT_FALSE(opt); |
| 19 |
} |
| 20 |
|
| 21 |
TEST(optional_test, empty_optional_string) { |
| 22 |
network::optional<std::string> opt{}; |
| 23 |
ASSERT_FALSE(opt); |
| 24 |
} |
| 25 |
|
| 26 |
TEST(optional_test, empty_optional_string_with_nullopt) { |
| 27 |
network::optional<std::string> opt{network::nullopt}; |
| 28 |
ASSERT_FALSE(opt); |
| 29 |
} |
| 30 |
|
| 31 |
TEST(optional_test, value_constructor) { |
| 32 |
network::optional<int> opt{42}; |
| 33 |
ASSERT_TRUE(opt); |
| 34 |
ASSERT_EQ(*opt, 42); |
| 35 |
} |
| 36 |
|
| 37 |
TEST(optional_test, value_constructor_string) { |
| 38 |
network::optional<std::string> opt{"banana"}; |
| 39 |
ASSERT_TRUE(opt); |
| 40 |
ASSERT_EQ(*opt, "banana"); |
| 41 |
} |
| 42 |
|
| 43 |
TEST(optional_test, rvalue_ref_constructor) { |
| 44 |
int value = 42; |
| 45 |
network::optional<int> opt{std::move(value)}; |
| 46 |
ASSERT_TRUE(opt); |
| 47 |
ASSERT_EQ(*opt, 42); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(optional_test, rvalue_ref_constructor_string) { |
| 51 |
std::string value = "banana"; |
| 52 |
network::optional<std::string> opt{std::move(value)}; |
| 53 |
ASSERT_TRUE(opt); |
| 54 |
ASSERT_EQ(*opt, "banana"); |
| 55 |
} |
| 56 |
|
| 57 |
TEST(optional_test, nullopt_copy_constructor) { |
| 58 |
network::optional<int> other{network::nullopt}; |
| 59 |
network::optional<int> opt{other}; |
| 60 |
ASSERT_FALSE(opt); |
| 61 |
} |
| 62 |
|
| 63 |
TEST(optional_test, nullopt_move_constructor) { |
| 64 |
network::optional<int> other{network::nullopt}; |
| 65 |
network::optional<int> opt{std::move(other)}; |
| 66 |
ASSERT_FALSE(opt); |
| 67 |
} |
| 68 |
|
| 69 |
TEST(optional_test, value_copy_constructor) { |
| 70 |
network::optional<int> other{42}; |
| 71 |
network::optional<int> opt{other}; |
| 72 |
ASSERT_TRUE(opt); |
| 73 |
ASSERT_EQ(*opt, 42); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(optional_test, value_move_constructor) { |
| 77 |
network::optional<int> other{42}; |
| 78 |
network::optional<int> opt{std::move(other)}; |
| 79 |
ASSERT_TRUE(opt); |
| 80 |
ASSERT_EQ(*opt, 42); |
| 81 |
} |
| 82 |
|
| 83 |
TEST(optional_test, value_copy_constructor_string) { |
| 84 |
network::optional<std::string> other{"banana"}; |
| 85 |
network::optional<std::string> opt{other}; |
| 86 |
ASSERT_TRUE(opt); |
| 87 |
ASSERT_EQ(*opt, "banana"); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(optional_test, value_move_constructor_string) { |
| 91 |
network::optional<std::string> other{"banana"}; |
| 92 |
network::optional<std::string> opt{std::move(other)}; |
| 93 |
ASSERT_TRUE(opt); |
| 94 |
ASSERT_EQ(*opt, "banana"); |
| 95 |
} |
| 96 |
|
| 97 |
TEST(optional_test, nullopt_assignment) { |
| 98 |
network::optional<int> opt(42); |
| 99 |
opt = network::nullopt; |
| 100 |
ASSERT_FALSE(opt); |
| 101 |
} |
| 102 |
|
| 103 |
TEST(optional_test, nullopt_assignment_string) { |
| 104 |
network::optional<std::string> opt("banana"); |
| 105 |
opt = network::nullopt; |
| 106 |
ASSERT_FALSE(opt); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(optional_test, value_copy_assigment) { |
| 110 |
network::optional<int> opt{}; |
| 111 |
network::optional<int> other{42}; |
| 112 |
opt = other; |
| 113 |
ASSERT_TRUE(opt); |
| 114 |
ASSERT_EQ(*opt, 42); |
| 115 |
} |
| 116 |
|
| 117 |
TEST(optional_test, value_move_assignment) { |
| 118 |
network::optional<int> opt{}; |
| 119 |
network::optional<int> other{42}; |
| 120 |
opt = std::move(other); |
| 121 |
ASSERT_TRUE(opt); |
| 122 |
ASSERT_EQ(*opt, 42); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(optional_test, value_copy_assignment_string) { |
| 126 |
network::optional<std::string> opt{}; |
| 127 |
network::optional<std::string> other{"banana"}; |
| 128 |
opt = other; |
| 129 |
ASSERT_TRUE(opt); |
| 130 |
ASSERT_EQ(*opt, "banana"); |
| 131 |
} |
| 132 |
|
| 133 |
TEST(optional_test, value_move_assignment_string) { |
| 134 |
network::optional<std::string> opt{}; |
| 135 |
network::optional<std::string> other{"banana"}; |
| 136 |
opt = std::move(other); |
| 137 |
ASSERT_TRUE(opt); |
| 138 |
ASSERT_EQ(*opt, "banana"); |
| 139 |
} |
| 140 |
|
| 141 |
TEST(optional_test, value_or_reference) { |
| 142 |
network::optional<std::string> opt; |
| 143 |
auto result = opt.value_or("other"); |
| 144 |
ASSERT_EQ("other", result); |
| 145 |
} |
| 146 |
|
| 147 |
TEST(optional_test, value_or_reference_with_value) { |
| 148 |
network::optional<std::string> opt("this"); |
| 149 |
auto result = opt.value_or("other"); |
| 150 |
ASSERT_EQ("this", result); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(optional_test, value_or_rvalue_reference) { |
| 154 |
std::string other("other"); |
| 155 |
auto result = network::optional<std::string>().value_or(other); |
| 156 |
ASSERT_EQ("other", result); |
| 157 |
} |
| 158 |
|
| 159 |
TEST(optional_test, value_or_rvalue_reference_with_value) { |
| 160 |
std::string other("other"); |
| 161 |
auto result = network::optional<std::string>("this").value_or(other); |
| 162 |
ASSERT_EQ("this", result); |
| 163 |
} |
| 164 |
|
| 165 |
TEST(optional_test, assign_nullopt_to_nullopt) { |
| 166 |
network::optional<std::string> opt; |
| 167 |
opt = network::nullopt; |
| 168 |
} |