Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      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  *      http://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 
     17 #include <gtest/gtest.h>
     18 
     19 #include <private/UniquePtr.h>
     20 
     21 static int cCount = 0;
     22 struct C {
     23   C() { ++cCount; }
     24   ~C() { --cCount; }
     25 };
     26 
     27 static bool freed = false;
     28 struct Freer {
     29   void operator() (int* p) {
     30     ASSERT_EQ(123, *p);
     31     free(p);
     32     freed = true;
     33   }
     34 };
     35 
     36 TEST(UniquePtr, smoke) {
     37   //
     38   // UniquePtr<T> tests...
     39   //
     40 
     41   // Can we free a single object?
     42   {
     43     UniquePtr<C> c(new C);
     44     ASSERT_TRUE(cCount == 1);
     45   }
     46   ASSERT_TRUE(cCount == 0);
     47   // Does release work?
     48   C* rawC;
     49   {
     50       UniquePtr<C> c(new C);
     51       ASSERT_TRUE(cCount == 1);
     52       rawC = c.release();
     53   }
     54   ASSERT_TRUE(cCount == 1);
     55   delete rawC;
     56   // Does reset work?
     57   {
     58       UniquePtr<C> c(new C);
     59       ASSERT_TRUE(cCount == 1);
     60       c.reset(new C);
     61       ASSERT_TRUE(cCount == 1);
     62   }
     63   ASSERT_TRUE(cCount == 0);
     64 
     65   //
     66   // UniquePtr<T[]> tests...
     67   //
     68 
     69   // Can we free an array?
     70   {
     71       UniquePtr<C[]> cs(new C[4]);
     72       ASSERT_TRUE(cCount == 4);
     73   }
     74   ASSERT_TRUE(cCount == 0);
     75   // Does release work?
     76   {
     77       UniquePtr<C[]> c(new C[4]);
     78       ASSERT_TRUE(cCount == 4);
     79       rawC = c.release();
     80   }
     81   ASSERT_TRUE(cCount == 4);
     82   delete[] rawC;
     83   // Does reset work?
     84   {
     85       UniquePtr<C[]> c(new C[4]);
     86       ASSERT_TRUE(cCount == 4);
     87       c.reset(new C[2]);
     88       ASSERT_TRUE(cCount == 2);
     89   }
     90   ASSERT_TRUE(cCount == 0);
     91 
     92   //
     93   // Custom deleter tests...
     94   //
     95   ASSERT_TRUE(!freed);
     96   {
     97       UniquePtr<int, Freer> i(reinterpret_cast<int*>(malloc(sizeof(int))));
     98       *i = 123;
     99   }
    100   ASSERT_TRUE(freed);
    101 }
    102