File: test_pointer_traits.cpp

package info (click to toggle)
ros2-rcpputils 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: cpp: 5,160; xml: 31; ansic: 20; makefile: 4
file content (134 lines) | stat: -rw-r--r-- 5,083 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <string>
#include <memory>

#include "rcpputils/pointer_traits.hpp"

TEST(TestPointerTraits, is_pointer) {
  auto ptr = new int(13);
  auto ptr_ptr = &ptr;
  ASSERT_TRUE(ptr_ptr == &ptr);  // to quiet clang static analysis
  const auto * const cptrc = new int(13);
  auto sptr = std::make_shared<int>(13);
  const auto csptr = std::make_shared<int>(13);
  const volatile auto cvsptr = std::make_shared<int>(13);
  const volatile auto cvsptrc = std::make_shared<int const>(13);
  auto uptr = std::make_unique<int>(13);
  const auto cuptr = std::make_unique<int>(13);
  const volatile auto cvuptr = std::make_unique<int>(13);
  const volatile auto cvuptrc = std::make_unique<const int>(13);

  auto b_ptr = rcpputils::is_pointer<decltype(ptr)>::value;
  auto b_ptr_ptr = rcpputils::is_pointer<decltype(ptr_ptr)>::value;
  auto b_ptr2 = rcpputils::is_pointer<decltype(*ptr_ptr)>::value;
  auto b_cptrc = rcpputils::is_pointer<decltype(cptrc)>::value;
  auto b_sptr = rcpputils::is_pointer<decltype(sptr)>::value;
  auto b_sptr_ref = rcpputils::is_pointer<decltype(sptr) &>::value;
  auto b_csptr = rcpputils::is_pointer<decltype(csptr)>::value;
  auto b_cvsptr = rcpputils::is_pointer<decltype(cvsptr)>::value;
  auto b_cvsptrc = rcpputils::is_pointer<decltype(cvsptrc)>::value;
  auto b_uptr = rcpputils::is_pointer<decltype(uptr)>::value;
  auto b_cuptr = rcpputils::is_pointer<decltype(cuptr)>::value;
  auto b_cvuptr = rcpputils::is_pointer<decltype(cvuptr)>::value;
  auto b_cvuptrc = rcpputils::is_pointer<decltype(cvuptrc)>::value;

  EXPECT_TRUE(b_ptr);
  EXPECT_TRUE(b_ptr_ptr);
  EXPECT_TRUE(b_ptr2);
  EXPECT_TRUE(b_cptrc);
  EXPECT_TRUE(b_sptr);
  EXPECT_TRUE(b_sptr_ref);
  EXPECT_TRUE(b_csptr);
  EXPECT_TRUE(b_cvsptr);
  EXPECT_TRUE(b_cvsptrc);
  EXPECT_TRUE(b_uptr);
  EXPECT_TRUE(b_cuptr);
  EXPECT_TRUE(b_cvuptr);
  EXPECT_TRUE(b_cvuptrc);

  // cleanup raw pointers
  delete ptr;
  delete cptrc;
}

struct POD
{
  int i = 0;
  bool b = false;
};

TEST(TestPointerTraits, is_no_pointer) {
  int i = 13;
  std::string s = "hello world";
  POD pod;

  auto b_i = rcpputils::is_pointer<decltype(i)>::value;
  auto b_s = rcpputils::is_pointer<decltype(s)>::value;
  auto b_pod = rcpputils::is_pointer<decltype(pod)>::value;

  EXPECT_FALSE(b_i);
  EXPECT_FALSE(b_s);
  EXPECT_FALSE(b_pod);
}

TEST(TestPointerTraits, remove_pointer) {
  auto non_ptr = 13;
  auto ptr = new int(13);
  auto ptr_ptr = &ptr;
  ASSERT_TRUE(ptr_ptr == &ptr);  // to quiet clang static analysis
  const auto * const cptrc = new int(13);
  auto sptr = std::make_shared<int>(13);
  const auto csptr = std::make_shared<int>(13);
  const volatile auto cvsptr = std::make_shared<int>(13);
  const volatile auto cvsptrc = std::make_shared<int const>(13);
  auto uptr = std::make_unique<int>(13);
  const auto cuptr = std::make_unique<int>(13);
  const volatile auto cvuptr = std::make_unique<int>(13);
  const volatile auto cvuptrc = std::make_unique<const int>(13);

  auto b_non_ptr = std::is_same<rcpputils::remove_pointer<decltype(non_ptr)>::type, int>::value;
  auto b_ptr = std::is_same<rcpputils::remove_pointer<decltype(ptr)>::type, int>::value;
  auto b_ptr_ptr = std::is_same<rcpputils::remove_pointer<decltype(ptr_ptr)>::type, int>::value;
  auto b_cptrc = std::is_same<rcpputils::remove_pointer<decltype(cptrc)>::type, const int>::value;
  auto b_sptr = std::is_same<rcpputils::remove_pointer<decltype(sptr)>::type, int>::value;
  auto b_csptr = std::is_same<rcpputils::remove_pointer<decltype(csptr)>::type, int>::value;
  auto b_cvsptr = std::is_same<rcpputils::remove_pointer<decltype(cvsptr)>::type, int>::value;
  auto b_cvsptrc =
    std::is_same<rcpputils::remove_pointer<decltype(cvsptrc)>::type, const int>::value;
  auto b_uptr = std::is_same<rcpputils::remove_pointer<decltype(uptr)>::type, int>::value;
  auto b_cuptr = std::is_same<rcpputils::remove_pointer<decltype(cuptr)>::type, int>::value;
  auto b_cvuptr = std::is_same<rcpputils::remove_pointer<decltype(cvuptr)>::type, int>::value;
  auto b_cvuptrc =
    std::is_same<rcpputils::remove_pointer<decltype(cvuptrc)>::type, const int>::value;

  EXPECT_TRUE(b_non_ptr);
  EXPECT_TRUE(b_ptr);
  EXPECT_FALSE(b_ptr_ptr);
  EXPECT_TRUE(b_cptrc);
  EXPECT_TRUE(b_sptr);
  EXPECT_TRUE(b_csptr);
  EXPECT_TRUE(b_cvsptr);
  EXPECT_TRUE(b_cvsptrc);
  EXPECT_TRUE(b_uptr);
  EXPECT_TRUE(b_cuptr);
  EXPECT_TRUE(b_cvuptr);
  EXPECT_TRUE(b_cvuptrc);

  delete ptr;
  delete cptrc;
}