]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/ms-gsl/tests/utils_tests.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / ms-gsl / tests / utils_tests.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <gtest/gtest.h>
18
19 #include <gsl/gsl_util> // finally, narrow_cast
20 #include <gsl/gsl_narrow> // for narrow, narrowing_error
21 #include <algorithm> // for move
22 #include <functional> // for reference_wrapper, _Bind_helper<>::type
23 #include <limits> // for numeric_limits
24 #include <stdint.h> // for uint32_t, int32_t
25 #include <type_traits> // for is_same
26 #include <cstddef> // for std::ptrdiff_t
27
28 using namespace gsl;
29
30 namespace
31 {
32 void f(int& i) { i += 1; }
33 static int j = 0;
34 void g() { j += 1; }
35 }
36
37
38 TEST(utils_tests, sanity_check_for_gsl_index_typedef)
39 {
40 static_assert(std::is_same<gsl::index, std::ptrdiff_t>::value,
41 "gsl::index represents wrong arithmetic type");
42 }
43
44 TEST(utils_tests, finally_lambda)
45 {
46 int i = 0;
47 {
48 auto _ = finally([&]() { f(i); });
49 EXPECT_TRUE(i == 0);
50 }
51 EXPECT_TRUE(i == 1);
52 }
53
54 TEST(utils_tests, finally_lambda_move)
55 {
56 int i = 0;
57 {
58 auto _1 = finally([&]() { f(i); });
59 {
60 auto _2 = std::move(_1);
61 EXPECT_TRUE(i == 0);
62 }
63 EXPECT_TRUE(i == 1);
64 {
65 auto _2 = std::move(_1);
66 EXPECT_TRUE(i == 1);
67 }
68 EXPECT_TRUE(i == 1);
69 }
70 EXPECT_TRUE(i == 1);
71 }
72
73 TEST(utils_tests, finally_const_lvalue_lambda)
74 {
75 int i = 0;
76 {
77 const auto const_lvalue_lambda = [&]() { f(i); };
78 auto _ = finally(const_lvalue_lambda);
79 EXPECT_TRUE(i == 0);
80 }
81 EXPECT_TRUE(i == 1);
82 }
83
84 TEST(utils_tests, finally_mutable_lvalue_lambda)
85 {
86 int i = 0;
87 {
88 auto mutable_lvalue_lambda = [&]() { f(i); };
89 auto _ = finally(mutable_lvalue_lambda);
90 EXPECT_TRUE(i == 0);
91 }
92 EXPECT_TRUE(i == 1);
93 }
94
95 TEST(utils_tests, finally_function_with_bind)
96 {
97 int i = 0;
98 {
99 auto _ = finally(std::bind(&f, std::ref(i)));
100 EXPECT_TRUE(i == 0);
101 }
102 EXPECT_TRUE(i == 1);
103 }
104
105 TEST(utils_tests, finally_function_ptr)
106 {
107 j = 0;
108 {
109 auto _ = finally(&g);
110 EXPECT_TRUE(j == 0);
111 }
112 EXPECT_TRUE(j == 1);
113 }
114
115 TEST(utils_tests, narrow_cast)
116 {
117 int n = 120;
118 char c = narrow_cast<char>(n);
119 EXPECT_TRUE(c == 120);
120
121 n = 300;
122 unsigned char uc = narrow_cast<unsigned char>(n);
123 EXPECT_TRUE(uc == 44);
124 }
125
126 TEST(utils_tests, narrow)
127 {
128 int n = 120;
129 const char c = narrow<char>(n);
130 EXPECT_TRUE(c == 120);
131
132 n = 300;
133 EXPECT_THROW(narrow<char>(n), narrowing_error);
134
135 const auto int32_max = std::numeric_limits<int32_t>::max();
136 const auto int32_min = std::numeric_limits<int32_t>::min();
137
138 EXPECT_TRUE(narrow<uint32_t>(int32_t(0)) == 0);
139 EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
140 EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
141
142 EXPECT_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
143 EXPECT_THROW(narrow<uint32_t>(int32_min), narrowing_error);
144
145 n = -42;
146 EXPECT_THROW(narrow<unsigned>(n), narrowing_error);
147 }