Delete gsl/string_span (#1185)
[GSL.git] / tests / utils_tests.cpp
blob5b64e207eda9ef0e8935701647d6d4bed567960e
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.
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <gtest/gtest.h>
19 #include <algorithm> // for move
20 #include <complex>
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
29 using namespace gsl;
31 namespace
33 void f(int& i) { i += 1; }
34 static int j = 0;
35 void g() { j += 1; }
36 } // namespace
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)
46 int i = 0;
48 auto _ = finally([&]() { f(i); });
49 EXPECT_TRUE(i == 0);
51 EXPECT_TRUE(i == 1);
54 TEST(utils_tests, finally_lambda_move)
56 int i = 0;
58 auto _1 = finally([&]() { f(i); });
60 auto _2 = std::move(_1);
61 EXPECT_TRUE(i == 0);
63 EXPECT_TRUE(i == 1);
65 auto _2 = std::move(_1);
66 EXPECT_TRUE(i == 1);
68 EXPECT_TRUE(i == 1);
70 EXPECT_TRUE(i == 1);
73 TEST(utils_tests, finally_const_lvalue_lambda)
75 int i = 0;
77 const auto const_lvalue_lambda = [&]() { f(i); };
78 auto _ = finally(const_lvalue_lambda);
79 EXPECT_TRUE(i == 0);
81 EXPECT_TRUE(i == 1);
84 TEST(utils_tests, finally_mutable_lvalue_lambda)
86 int i = 0;
88 auto mutable_lvalue_lambda = [&]() { f(i); };
89 auto _ = finally(mutable_lvalue_lambda);
90 EXPECT_TRUE(i == 0);
92 EXPECT_TRUE(i == 1);
95 TEST(utils_tests, finally_function_with_bind)
97 int i = 0;
99 auto _ = finally([&i] { return f(i); });
100 EXPECT_TRUE(i == 0);
102 EXPECT_TRUE(i == 1);
105 TEST(utils_tests, finally_function_ptr)
107 j = 0;
109 auto _ = finally(&g);
110 EXPECT_TRUE(j == 0);
112 EXPECT_TRUE(j == 1);
115 TEST(utils_tests, finally_function)
117 j = 0;
119 auto _ = finally(g);
120 EXPECT_TRUE(j == 0);
122 EXPECT_TRUE(j == 1);
125 TEST(utils_tests, narrow_cast)
127 int n = 120;
128 char c = narrow_cast<char>(n);
129 EXPECT_TRUE(c == 120);
131 n = 300;
132 unsigned char uc = narrow_cast<unsigned char>(n);
133 EXPECT_TRUE(uc == 44);
136 #ifndef GSL_KERNEL_MODE
137 TEST(utils_tests, narrow)
139 int n = 120;
140 const char c = narrow<char>(n);
141 EXPECT_TRUE(c == 120);
143 n = 300;
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);
156 n = -42;
157 EXPECT_THROW(narrow<unsigned>(n), narrowing_error);
159 EXPECT_TRUE(
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