1 // Copyright (C) 2016 The Android Open Source Project
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "anbox/common/scope_ptr.h"
17 #include <gtest/gtest.h>
19 #include <type_traits>
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);
33 FuncDelete<decltype(+lambda)> funcFd2(funcFd1);
34 // assignment operator
37 funcFd2 = std::move(funcFd1);
40 FuncDelete<decltype(+lambda)> funcFd3(lambdaFd);
41 // conversion move ctor
42 FuncDelete<decltype(+lambda)> funcFd4(std::move(lambdaFd));
43 // conversion assignment
46 funcFd3 = std::move(lambdaFd);
49 TEST(ScopedPtr, makeCustomScopedPtr_fromLambda) {
50 auto freeAsLambda = [](void* ptr) { free(ptr); };
52 auto ptr1 = makeCustomScopedPtr(malloc(1), freeAsLambda);
54 ScopedPtr<void, FuncDelete<void(*)(void*)>> ptr2 =
55 makeCustomScopedPtr(malloc(1), freeAsLambda);
57 ScopedPtr<void, FuncDelete<void(*)(void*)>> ptr3 =
58 makeCustomScopedPtr(malloc(1), +freeAsLambda);
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");