blob: 587b0a144bafac7f6bca56ebc56ba3de680eb054 [file] [log] [blame]
Brian Silvermana6f7ce02018-07-07 15:04:00 -07001///////////////////////////////////////////////////////////////////////////////
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 <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
18
19#include <gsl/gsl_util> // for narrow, finally, narrow_cast, narrowing_e...
20
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
27using namespace gsl;
28
29TEST_CASE("sanity check for gsl::index typedef")
30{
31 static_assert(std::is_same<gsl::index, std::ptrdiff_t>::value,
32 "gsl::index represents wrong arithmetic type");
33}
34
35void f(int& i) { i += 1; }
36
37TEST_CASE("finally_lambda")
38{
39 int i = 0;
40 {
41 auto _ = finally([&]() { f(i); });
42 CHECK(i == 0);
43 }
44 CHECK(i == 1);
45}
46
47TEST_CASE("finally_lambda_move")
48{
49 int i = 0;
50 {
51 auto _1 = finally([&]() { f(i); });
52 {
53 auto _2 = std::move(_1);
54 CHECK(i == 0);
55 }
56 CHECK(i == 1);
57 {
58 auto _2 = std::move(_1);
59 CHECK(i == 1);
60 }
61 CHECK(i == 1);
62 }
63 CHECK(i == 1);
64}
65
66TEST_CASE("finally_function_with_bind")
67{
68 int i = 0;
69 {
70 auto _ = finally(std::bind(&f, std::ref(i)));
71 CHECK(i == 0);
72 }
73 CHECK(i == 1);
74}
75
76int j = 0;
77void g() { j += 1; }
78TEST_CASE("finally_function_ptr")
79{
80 j = 0;
81 {
82 auto _ = finally(&g);
83 CHECK(j == 0);
84 }
85 CHECK(j == 1);
86}
87
88TEST_CASE("narrow_cast")
89{
90 int n = 120;
91 char c = narrow_cast<char>(n);
92 CHECK(c == 120);
93
94 n = 300;
95 unsigned char uc = narrow_cast<unsigned char>(n);
96 CHECK(uc == 44);
97}
98
99TEST_CASE("narrow")
100{
101 int n = 120;
102 const char c = narrow<char>(n);
103 CHECK(c == 120);
104
105 n = 300;
106 CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
107
108 const auto int32_max = std::numeric_limits<int32_t>::max();
109 const auto int32_min = std::numeric_limits<int32_t>::min();
110
111 CHECK(narrow<uint32_t>(int32_t(0)) == 0);
112 CHECK(narrow<uint32_t>(int32_t(1)) == 1);
113 CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
114
115 CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
116 CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
117
118 n = -42;
119 CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
120}