Home | History | Annotate | Download | only in memory
      1 /*
      2  * Copyright (C) 2018 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 "src/profiling/memory/interner.h"
     18 
     19 #include "gmock/gmock.h"
     20 #include "gtest/gtest.h"
     21 
     22 namespace perfetto {
     23 namespace profiling {
     24 namespace {
     25 
     26 TEST(InternerStringTest, Basic) {
     27   Interner<std::string> interner;
     28   {
     29     Interned<std::string> interned_str = interner.Intern("foo");
     30     ASSERT_EQ(interned_str.data(), "foo");
     31   }
     32   ASSERT_EQ(interner.entry_count_for_testing(), 0);
     33 }
     34 
     35 TEST(InternerStringTest, TwoStrings) {
     36   Interner<std::string> interner;
     37   {
     38     Interned<std::string> interned_str = interner.Intern("foo");
     39     Interned<std::string> other_interned_str = interner.Intern("bar");
     40     ASSERT_EQ(interned_str.data(), "foo");
     41     ASSERT_EQ(other_interned_str.data(), "bar");
     42   }
     43   ASSERT_EQ(interner.entry_count_for_testing(), 0);
     44 }
     45 
     46 TEST(InternerStringTest, TwoReferences) {
     47   Interner<std::string> interner;
     48   {
     49     Interned<std::string> interned_str = interner.Intern("foo");
     50     ASSERT_EQ(interned_str.data(), "foo");
     51     Interned<std::string> interned_str2 = interner.Intern("foo");
     52     ASSERT_EQ(interner.entry_count_for_testing(), 1);
     53     ASSERT_EQ(interned_str2.data(), "foo");
     54   }
     55   ASSERT_EQ(interner.entry_count_for_testing(), 0);
     56 }
     57 
     58 TEST(InternerStringTest, Move) {
     59   Interner<std::string> interner;
     60   {
     61     Interned<std::string> interned_str = interner.Intern("foo");
     62     {
     63       Interned<std::string> interned_str2(std::move(interned_str));
     64       ASSERT_EQ(interner.entry_count_for_testing(), 1);
     65       ASSERT_EQ(interned_str2.data(), "foo");
     66     }
     67     ASSERT_EQ(interner.entry_count_for_testing(), 0);
     68   }
     69 }
     70 
     71 TEST(InternerStringTest, Copy) {
     72   Interner<std::string> interner;
     73   {
     74     Interned<std::string> interned_str = interner.Intern("foo");
     75     {
     76       Interned<std::string> interned_str2(interned_str);
     77       ASSERT_EQ(interner.entry_count_for_testing(), 1);
     78       ASSERT_EQ(interned_str2.data(), "foo");
     79     }
     80     ASSERT_EQ(interner.entry_count_for_testing(), 1);
     81     ASSERT_EQ(interned_str.data(), "foo");
     82   }
     83 }
     84 
     85 TEST(InternerStringTest, MoveAssign) {
     86   Interner<std::string> interner;
     87   {
     88     Interned<std::string> interned_str = interner.Intern("foo");
     89     {
     90       Interned<std::string> interned_str2 = std::move(interned_str);
     91       ASSERT_EQ(interner.entry_count_for_testing(), 1);
     92       ASSERT_EQ(interned_str2.data(), "foo");
     93     }
     94     ASSERT_EQ(interner.entry_count_for_testing(), 0);
     95   }
     96 }
     97 
     98 TEST(InternerStringTest, CopyAssign) {
     99   Interner<std::string> interner;
    100   {
    101     Interned<std::string> interned_str = interner.Intern("foo");
    102     {
    103       Interned<std::string> interned_str2 = interned_str;
    104       ASSERT_EQ(interner.entry_count_for_testing(), 1);
    105       ASSERT_EQ(interned_str2.data(), "foo");
    106     }
    107     ASSERT_EQ(interner.entry_count_for_testing(), 1);
    108     ASSERT_EQ(interned_str.data(), "foo");
    109   }
    110 }
    111 
    112 TEST(InternerStringTest, IDsUnique) {
    113   Interner<std::string> interner;
    114   Interned<std::string> interned_str = interner.Intern("foo");
    115   Interned<std::string> same_interned_str = interner.Intern("foo");
    116   Interned<std::string> other_interned_str = interner.Intern("bar");
    117   EXPECT_EQ(interned_str.id(), same_interned_str.id());
    118   EXPECT_NE(interned_str.id(), other_interned_str.id());
    119 }
    120 
    121 TEST(InternerStringTest, IdsConsecutive) {
    122   Interner<std::string> interner;
    123   {
    124     Interned<std::string> interned_str = interner.Intern("foo");
    125     interner.Intern("foo");
    126     Interned<std::string> other_interned_str = interner.Intern("bar");
    127     ASSERT_EQ(interned_str.id() + 1, other_interned_str.id());
    128   }
    129   ASSERT_EQ(interner.entry_count_for_testing(), 0);
    130 }
    131 
    132 }  // namespace
    133 }  // namespace profiling
    134 }  // namespace perfetto
    135