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 <UnitTest++/UnitTest++.h>
18#include <gsl/gsl>
19#include <functional>
20
21using namespace gsl;
22
23SUITE(utils_tests)
24{
25    void f(int& i)
26    {
27        i += 1;
28    }
29
30    TEST(finally_lambda)
31    {
32        int i = 0;
33        {
34            auto _ = finally([&]() {f(i);});
35            CHECK(i == 0);
36        }
37        CHECK(i == 1);
38    }
39
40    TEST(finally_lambda_move)
41    {
42        int i = 0;
43        {
44            auto _1 = finally([&]() {f(i);});
45            {
46                auto _2 = std::move(_1);
47                CHECK(i == 0);
48            }
49            CHECK(i == 1);
50            {
51                auto _2 = std::move(_1);
52                CHECK(i == 1);
53            }
54            CHECK(i == 1);
55        }
56        CHECK(i == 1);
57    }
58
59    TEST(finally_function_with_bind)
60    {
61        int i = 0;
62        {
63            auto _ = finally(std::bind(&f, std::ref(i)));
64            CHECK(i == 0);
65        }
66        CHECK(i == 1);
67    }
68
69    int j = 0;
70    void g() { j += 1; };
71    TEST(finally_function_ptr)
72    {
73        j = 0;
74        {
75            auto _ = finally(&g);
76            CHECK(j == 0);
77        }
78        CHECK(j == 1);
79    }
80
81    TEST(narrow_cast)
82    {
83        int n = 120;
84        char c = narrow_cast<char>(n);
85        CHECK(c == 120);
86
87        n = 300;
88        unsigned char uc = narrow_cast<unsigned char>(n);
89        CHECK(uc == 44);
90    }
91
92    TEST(narrow)
93    {
94        int n = 120;
95        char c = narrow<char>(n);
96        CHECK(c == 120);
97
98        n = 300;
99        CHECK_THROW(narrow<char>(n), narrowing_error);
100
101        const auto int32_max = std::numeric_limits<int32_t>::max();
102        const auto int32_min = std::numeric_limits<int32_t>::min();
103
104        CHECK(narrow<uint32_t>(int32_t(0)) == 0);
105        CHECK(narrow<uint32_t>(int32_t(1)) == 1);
106        CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
107
108        CHECK_THROW(narrow<uint32_t>(int32_t(-1)), narrowing_error);
109        CHECK_THROW(narrow<uint32_t>(int32_min), narrowing_error);
110
111        n = -42;
112        CHECK_THROW(narrow<unsigned>(n), narrowing_error);
113    }
114}
115
116int main(int, const char *[])
117{
118    return UnitTest::RunAllTests();
119}
120