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>
19 #include <algorithm> // for move
21 #include <cstddef> // for std::ptrdiff_t
22 #include <cstdint> // for uint32_t, int32_t
23 #include <functional> // for reference_wrapper, _Bind_helper<>::type
24 #include <gsl/narrow> // for narrow, narrowing_error
25 #include <gsl/util> // finally, narrow_cast
26 #include <limits> // for numeric_limits
27 #include <type_traits> // for is_same
33 void f(int& i
) { i
+= 1; }
38 TEST(utils_tests
, sanity_check_for_gsl_index_typedef
)
40 static_assert(std::is_same
<gsl::index
, std::ptrdiff_t>::value
,
41 "gsl::index represents wrong arithmetic type");
44 TEST(utils_tests
, finally_lambda
)
48 auto _
= finally([&]() { f(i
); });
54 TEST(utils_tests
, finally_lambda_move
)
58 auto _1
= finally([&]() { f(i
); });
60 auto _2
= std::move(_1
);
65 auto _2
= std::move(_1
);
73 TEST(utils_tests
, finally_const_lvalue_lambda
)
77 const auto const_lvalue_lambda
= [&]() { f(i
); };
78 auto _
= finally(const_lvalue_lambda
);
84 TEST(utils_tests
, finally_mutable_lvalue_lambda
)
88 auto mutable_lvalue_lambda
= [&]() { f(i
); };
89 auto _
= finally(mutable_lvalue_lambda
);
95 TEST(utils_tests
, finally_function_with_bind
)
99 auto _
= finally([&i
] { return f(i
); });
105 TEST(utils_tests
, finally_function_ptr
)
109 auto _
= finally(&g
);
115 TEST(utils_tests
, finally_function
)
125 TEST(utils_tests
, narrow_cast
)
128 char c
= narrow_cast
<char>(n
);
129 EXPECT_TRUE(c
== 120);
132 unsigned char uc
= narrow_cast
<unsigned char>(n
);
133 EXPECT_TRUE(uc
== 44);
136 #ifndef GSL_KERNEL_MODE
137 TEST(utils_tests
, narrow
)
140 const char c
= narrow
<char>(n
);
141 EXPECT_TRUE(c
== 120);
144 EXPECT_THROW(narrow
<char>(n
), narrowing_error
);
146 const auto int32_max
= std::numeric_limits
<int32_t>::max();
147 const auto int32_min
= std::numeric_limits
<int32_t>::min();
149 EXPECT_TRUE(narrow
<uint32_t>(int32_t(0)) == 0);
150 EXPECT_TRUE(narrow
<uint32_t>(int32_t(1)) == 1);
151 EXPECT_TRUE(narrow
<uint32_t>(int32_max
) == static_cast<uint32_t>(int32_max
));
153 EXPECT_THROW(narrow
<uint32_t>(int32_t(-1)), narrowing_error
);
154 EXPECT_THROW(narrow
<uint32_t>(int32_min
), narrowing_error
);
157 EXPECT_THROW(narrow
<unsigned>(n
), narrowing_error
);
160 narrow
<std::complex<float>>(std::complex<double>(4, 2)) == std::complex<float>(4, 2));
161 EXPECT_THROW(narrow
<std::complex<float>>(std::complex<double>(4.2)), narrowing_error
);
163 EXPECT_TRUE(narrow
<int>(float(1)) == 1);
165 #endif // GSL_KERNEL_MODE