TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / tests / anbox / common / scope_ptr_tests.cpp
1 // Copyright (C) 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "anbox/common/scope_ptr.h"
16
17 #include <gtest/gtest.h>
18
19 #include <type_traits>
20 #include <utility>
21
22 namespace anbox {
23 namespace common {
24
25 TEST(ScopedPtr, FuncDelete_conversions) {
26     // This test makes sure the code compiles, it doesn't do any runtime checks.
27     auto lambda = [](int i) { return i; };
28     FuncDelete<decltype(lambda)> lambdaFd(lambda);
29     // unary + converts a captureless lambda into a raw function pointer
30     FuncDelete<decltype(+lambda)> funcFd1(+lambda);
31
32     // copy ctor
33     FuncDelete<decltype(+lambda)> funcFd2(funcFd1);
34     // assignment operator
35     funcFd2 = funcFd1;
36     // move operator
37     funcFd2 = std::move(funcFd1);
38
39     // conversion ctor
40     FuncDelete<decltype(+lambda)> funcFd3(lambdaFd);
41     // conversion move ctor
42     FuncDelete<decltype(+lambda)> funcFd4(std::move(lambdaFd));
43     // conversion assignment
44     funcFd3 = lambdaFd;
45     // conversion move
46     funcFd3 = std::move(lambdaFd);
47 }
48
49 TEST(ScopedPtr, makeCustomScopedPtr_fromLambda) {
50     auto freeAsLambda = [](void* ptr) { free(ptr); };
51
52     auto ptr1 = makeCustomScopedPtr(malloc(1), freeAsLambda);
53
54     ScopedPtr<void, FuncDelete<void(*)(void*)>> ptr2 =
55             makeCustomScopedPtr(malloc(1), freeAsLambda);
56
57     ScopedPtr<void, FuncDelete<void(*)(void*)>> ptr3 =
58             makeCustomScopedPtr(malloc(1), +freeAsLambda);
59
60     static_assert(!std::is_same<decltype(ptr1), decltype(ptr2)>::value,
61                   "Custom ScopedPtr<> from a lambda expression type may not "
62                   "be the same as with a function pointer");
63 }
64 }  // namespace common
65 }  // namespace anbox