]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/ms-gsl/tests/algorithm_tests.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
5 // This code is licensed under the MIT License (MIT).
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
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <gtest/gtest.h>
18 #include <gsl/gsl_algorithm> // for copy
19 #include <gsl/span> // for span
20 #include <array> // for array
21 #include <cstddef> // for size_t
25 static constexpr char deathstring
[] = "Expected Death";
36 TEST(algorithm_tests
, same_type
)
38 // dynamic source and destination span
40 std::array
<int, 5> src
{1, 2, 3, 4, 5};
41 std::array
<int, 10> dst
{};
43 const span
<int> src_span(src
);
44 const span
<int> dst_span(dst
);
46 copy(src_span
, dst_span
);
47 copy(src_span
, dst_span
.subspan(src_span
.size()));
49 for (std::size_t i
= 0; i
< src
.size(); ++i
)
51 EXPECT_TRUE(dst
[i
] == src
[i
]);
52 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
56 // static source and dynamic destination span
58 std::array
<int, 5> src
{1, 2, 3, 4, 5};
59 std::array
<int, 10> dst
{};
61 const span
<int, 5> src_span(src
);
62 const span
<int> dst_span(dst
);
64 copy(src_span
, dst_span
);
65 copy(src_span
, dst_span
.subspan(src_span
.size()));
67 for (std::size_t i
= 0; i
< src
.size(); ++i
)
69 EXPECT_TRUE(dst
[i
] == src
[i
]);
70 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
74 // dynamic source and static destination span
76 std::array
<int, 5> src
{1, 2, 3, 4, 5};
77 std::array
<int, 10> dst
{};
79 const span
<int> src_span(src
);
80 const span
<int, 10> dst_span(dst
);
82 copy(src_span
, dst_span
);
83 copy(src_span
, dst_span
.subspan(src_span
.size()));
85 for (std::size_t i
= 0; i
< src
.size(); ++i
)
87 EXPECT_TRUE(dst
[i
] == src
[i
]);
88 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
92 // static source and destination span
94 std::array
<int, 5> src
{1, 2, 3, 4, 5};
95 std::array
<int, 10> dst
{};
97 const span
<int, 5> src_span(src
);
98 const span
<int, 10> dst_span(dst
);
100 copy(src_span
, dst_span
);
101 copy(src_span
, dst_span
.subspan(src_span
.size()));
103 for (std::size_t i
= 0; i
< src
.size(); ++i
)
105 EXPECT_TRUE(dst
[i
] == src
[i
]);
106 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
111 TEST(algorithm_tests
, compatible_type
)
113 // dynamic source and destination span
115 std::array
<short, 5> src
{1, 2, 3, 4, 5};
116 std::array
<int, 10> dst
{};
118 const span
<short> src_span(src
);
119 const span
<int> dst_span(dst
);
121 copy(src_span
, dst_span
);
122 copy(src_span
, dst_span
.subspan(src_span
.size()));
124 for (std::size_t i
= 0; i
< src
.size(); ++i
)
126 EXPECT_TRUE(dst
[i
] == src
[i
]);
127 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
131 // static source and dynamic destination span
133 std::array
<short, 5> src
{1, 2, 3, 4, 5};
134 std::array
<int, 10> dst
{};
136 const span
<short, 5> src_span(src
);
137 const span
<int> dst_span(dst
);
139 copy(src_span
, dst_span
);
140 copy(src_span
, dst_span
.subspan(src_span
.size()));
142 for (std::size_t i
= 0; i
< src
.size(); ++i
)
144 EXPECT_TRUE(dst
[i
] == src
[i
]);
145 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
149 // dynamic source and static destination span
151 std::array
<short, 5> src
{1, 2, 3, 4, 5};
152 std::array
<int, 10> dst
{};
154 const span
<short> src_span(src
);
155 const span
<int, 10> dst_span(dst
);
157 copy(src_span
, dst_span
);
158 copy(src_span
, dst_span
.subspan(src_span
.size()));
160 for (std::size_t i
= 0; i
< src
.size(); ++i
)
162 EXPECT_TRUE(dst
[i
] == src
[i
]);
163 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
167 // static source and destination span
169 std::array
<short, 5> src
{1, 2, 3, 4, 5};
170 std::array
<int, 10> dst
{};
172 const span
<short, 5> src_span(src
);
173 const span
<int, 10> dst_span(dst
);
175 copy(src_span
, dst_span
);
176 copy(src_span
, dst_span
.subspan(src_span
.size()));
178 for (std::size_t i
= 0; i
< src
.size(); ++i
)
180 EXPECT_TRUE(dst
[i
] == src
[i
]);
181 EXPECT_TRUE(dst
[i
+ src
.size()] == src
[i
]);
186 #ifdef CONFIRM_COMPILATION_ERRORS
187 TEST(algorithm_tests
, incompatible_type
)
189 std::array
<int, 4> src
{1, 2, 3, 4};
190 std::array
<int*, 12> dst
{};
192 span
<int> src_span_dyn(src
);
193 span
<int, 4> src_span_static(src
);
194 span
<int*> dst_span_dyn(dst
);
195 span
<int*, 4> dst_span_static(dst
);
197 // every line should produce a compilation error
198 copy(src_span_dyn
, dst_span_dyn
);
199 copy(src_span_dyn
, dst_span_static
);
200 copy(src_span_static
, dst_span_dyn
);
201 copy(src_span_static
, dst_span_static
);
205 TEST(algorithm_tests
, small_destination_span
)
207 std::set_terminate([] {
208 std::cerr
<< "Expected Death. small_destination_span";
212 std::array
<int, 12> src
{1, 2, 3, 4};
213 std::array
<int, 4> dst
{};
215 const span
<int> src_span_dyn(src
);
216 const span
<int, 12> src_span_static(src
);
217 const span
<int> dst_span_dyn(dst
);
218 const span
<int, 4> dst_span_static(dst
);
220 EXPECT_DEATH(copy(src_span_dyn
, dst_span_dyn
), deathstring
);
221 EXPECT_DEATH(copy(src_span_dyn
, dst_span_static
), deathstring
);
222 EXPECT_DEATH(copy(src_span_static
, dst_span_dyn
), deathstring
);
224 #ifdef CONFIRM_COMPILATION_ERRORS
225 copy(src_span_static
, dst_span_static
);