xref: /aosp_15_r20/external/abseil-cpp/absl/log/die_if_null_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 //
2 // Copyright 2022 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "absl/log/die_if_null.h"
17 
18 #include <stdint.h>
19 
20 #include <memory>
21 #include <utility>
22 
23 #include "gtest/gtest.h"
24 #include "absl/base/attributes.h"
25 #include "absl/log/internal/test_helpers.h"
26 
27 namespace {
28 
29 auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
30     new absl::log_internal::LogTestEnvironment);
31 
32 // TODO(b/69907837): Revisit these tests with the goal of making them less
33 // convoluted.
TEST(AbslDieIfNull,Simple)34 TEST(AbslDieIfNull, Simple) {
35   int64_t t;
36   void* ptr = static_cast<void*>(&t);
37   void* ref = ABSL_DIE_IF_NULL(ptr);
38   ASSERT_EQ(ptr, ref);
39 
40   char* t_as_char;
41   t_as_char = ABSL_DIE_IF_NULL(reinterpret_cast<char*>(&t));
42   (void)t_as_char;
43 
44   unsigned char* t_as_uchar;
45   t_as_uchar = ABSL_DIE_IF_NULL(reinterpret_cast<unsigned char*>(&t));
46   (void)t_as_uchar;
47 
48   int* t_as_int;
49   t_as_int = ABSL_DIE_IF_NULL(reinterpret_cast<int*>(&t));
50   (void)t_as_int;
51 
52   int64_t* t_as_int64_t;
53   t_as_int64_t = ABSL_DIE_IF_NULL(reinterpret_cast<int64_t*>(&t));
54   (void)t_as_int64_t;
55 
56   std::unique_ptr<int64_t> sptr(new int64_t);
57   EXPECT_EQ(sptr.get(), ABSL_DIE_IF_NULL(sptr).get());
58   ABSL_DIE_IF_NULL(sptr).reset();
59 
60   int64_t* int_ptr = new int64_t();
61   EXPECT_EQ(int_ptr, ABSL_DIE_IF_NULL(std::unique_ptr<int64_t>(int_ptr)).get());
62 }
63 
64 #if GTEST_HAS_DEATH_TEST
TEST(DeathCheckAbslDieIfNull,Simple)65 TEST(DeathCheckAbslDieIfNull, Simple) {
66   void* ptr;
67   ASSERT_DEATH({ ptr = ABSL_DIE_IF_NULL(nullptr); }, "");
68   (void)ptr;
69 
70   std::unique_ptr<int64_t> sptr;
71   ASSERT_DEATH(ptr = ABSL_DIE_IF_NULL(sptr).get(), "");
72 }
73 #endif
74 
75 // Ensures that ABSL_DIE_IF_NULL works with C++11's std::unique_ptr and
76 // std::shared_ptr.
TEST(AbslDieIfNull,DoesNotCompareSmartPointerToNULL)77 TEST(AbslDieIfNull, DoesNotCompareSmartPointerToNULL) {
78   std::unique_ptr<int> up(new int);
79   EXPECT_EQ(&up, &ABSL_DIE_IF_NULL(up));
80   ABSL_DIE_IF_NULL(up).reset();
81 
82   std::shared_ptr<int> sp(new int);
83   EXPECT_EQ(&sp, &ABSL_DIE_IF_NULL(sp));
84   ABSL_DIE_IF_NULL(sp).reset();
85 }
86 
87 // Verifies that ABSL_DIE_IF_NULL returns an rvalue reference if its argument is
88 // an rvalue reference.
TEST(AbslDieIfNull,PreservesRValues)89 TEST(AbslDieIfNull, PreservesRValues) {
90   int64_t* ptr = new int64_t();
91   auto uptr = ABSL_DIE_IF_NULL(std::unique_ptr<int64_t>(ptr));
92   EXPECT_EQ(ptr, uptr.get());
93 }
94 
95 // Verifies that ABSL_DIE_IF_NULL returns an lvalue if its argument is an
96 // lvalue.
TEST(AbslDieIfNull,PreservesLValues)97 TEST(AbslDieIfNull, PreservesLValues) {
98   int64_t array[2] = {0};
99   int64_t* a = array + 0;
100   int64_t* b = array + 1;
101   using std::swap;
102   swap(ABSL_DIE_IF_NULL(a), ABSL_DIE_IF_NULL(b));
103   EXPECT_EQ(array + 1, a);
104   EXPECT_EQ(array + 0, b);
105 }
106 
107 }  // namespace
108