Home | History | Annotate | Download | only in gprpp
      1 /*
      2  *
      3  * Copyright 2017 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #include "src/core/lib/gprpp/memory.h"
     20 #include <gtest/gtest.h>
     21 #include "test/core/util/test_config.h"
     22 
     23 namespace grpc_core {
     24 namespace testing {
     25 
     26 struct Foo {
     27   Foo(int p, int q) : a(p), b(q) {}
     28   int a;
     29   int b;
     30 };
     31 
     32 TEST(MemoryTest, NewDeleteTest) { Delete(New<int>()); }
     33 
     34 TEST(MemoryTest, NewDeleteWithArgTest) {
     35   int* i = New<int>(42);
     36   EXPECT_EQ(42, *i);
     37   Delete(i);
     38 }
     39 
     40 TEST(MemoryTest, NewDeleteWithArgsTest) {
     41   Foo* p = New<Foo>(1, 2);
     42   EXPECT_EQ(1, p->a);
     43   EXPECT_EQ(2, p->b);
     44   Delete(p);
     45 }
     46 
     47 TEST(MemoryTest, MakeUniqueTest) { MakeUnique<int>(); }
     48 
     49 TEST(MemoryTest, MakeUniqueWithArgTest) {
     50   auto i = MakeUnique<int>(42);
     51   EXPECT_EQ(42, *i);
     52 }
     53 
     54 TEST(MemoryTest, UniquePtrWithCustomDeleter) {
     55   int n = 0;
     56   class IncrementingDeleter {
     57    public:
     58     void operator()(int* p) { ++*p; }
     59   };
     60   {
     61     UniquePtr<int, IncrementingDeleter> p(&n);
     62     EXPECT_EQ(0, n);
     63   }
     64   EXPECT_EQ(1, n);
     65 }
     66 
     67 }  // namespace testing
     68 }  // namespace grpc_core
     69 
     70 int main(int argc, char** argv) {
     71   grpc_test_init(argc, argv);
     72   ::testing::InitGoogleTest(&argc, argv);
     73   return RUN_ALL_TESTS();
     74 }
     75