]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/third_party/ms-gsl/tests/at_tests.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / ms-gsl / tests / at_tests.cpp
CommitLineData
1e59de90
TL
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> // for at
20
21#include <array> // for array
22#include <cstddef> // for size_t
23#include <initializer_list> // for initializer_list
24#include <vector> // for vector
25
26namespace
27{
28 static constexpr char deathstring[] = "Expected Death";
29}
30
31TEST(at_tests, static_array)
32{
33 int a[4] = {1, 2, 3, 4};
34 const int(&c_a)[4] = a;
35
36 for (int i = 0; i < 4; ++i) {
37 EXPECT_TRUE(&gsl::at(a, i) == &a[i]);
38 EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]);
39 }
40
41 std::set_terminate([] {
42 std::cerr << "Expected Death. static_array";
43 std::abort();
44 });
45
46 EXPECT_DEATH(gsl::at(a, -1), deathstring);
47 EXPECT_DEATH(gsl::at(a, 4), deathstring);
48 EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
49 EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
50}
51
52TEST(at_tests, std_array)
53{
54 std::array<int, 4> a = {1, 2, 3, 4};
55 const std::array<int, 4>& c_a = a;
56
57 for (int i = 0; i < 4; ++i) {
58 EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
59 EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
60 }
61
62 std::set_terminate([] {
63 std::cerr << "Expected Death. std_array";
64 std::abort();
65 });
66
67 EXPECT_DEATH(gsl::at(a, -1), deathstring);
68 EXPECT_DEATH(gsl::at(a, 4), deathstring);
69 EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
70 EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
71}
72
73TEST(at_tests, std_vector)
74{
75 std::vector<int> a = {1, 2, 3, 4};
76 const std::vector<int>& c_a = a;
77
78 for (int i = 0; i < 4; ++i) {
79 EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
80 EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
81 }
82
83 std::set_terminate([] {
84 std::cerr << "Expected Death. std_vector";
85 std::abort();
86 });
87
88 EXPECT_DEATH(gsl::at(a, -1), deathstring);
89 EXPECT_DEATH(gsl::at(a, 4), deathstring);
90 EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
91 EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
92}
93
94TEST(at_tests, InitializerList)
95{
96 const std::initializer_list<int> a = {1, 2, 3, 4};
97
98 for (int i = 0; i < 4; ++i) {
99 EXPECT_TRUE(gsl::at(a, i) == i + 1);
100 EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1);
101 }
102
103 std::set_terminate([] {
104 std::cerr << "Expected Death. InitializerList";
105 std::abort();
106 });
107
108 EXPECT_DEATH(gsl::at(a, -1), deathstring);
109 EXPECT_DEATH(gsl::at(a, 4), deathstring);
110 EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), deathstring);
111 EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), deathstring);
112}
113
114#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
115static constexpr bool test_constexpr()
116{
117 int a1[4] = {1, 2, 3, 4};
118 const int(&c_a1)[4] = a1;
119 std::array<int, 4> a2 = {1, 2, 3, 4};
120 const std::array<int, 4>& c_a2 = a2;
121
122 for (int i = 0; i < 4; ++i) {
123 if (&gsl::at(a1, i) != &a1[i]) return false;
124 if (&gsl::at(c_a1, i) != &a1[i]) return false;
125 // requires C++17:
126 // if (&gsl::at(a2, i) != &a2[static_cast<std::size_t>(i)]) return false;
127 if (&gsl::at(c_a2, i) != &c_a2[static_cast<std::size_t>(i)]) return false;
128 if (gsl::at({1, 2, 3, 4}, i) != i + 1) return false;
129 }
130
131 return true;
132}
133
134static_assert(test_constexpr(), "FAIL");
135#endif