test_instance_tracker_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/container/internal/test_instance_tracker.h"
00016 
00017 #include "gtest/gtest.h"
00018 
00019 namespace {
00020 
00021 using absl::test_internal::CopyableMovableInstance;
00022 using absl::test_internal::CopyableOnlyInstance;
00023 using absl::test_internal::InstanceTracker;
00024 using absl::test_internal::MovableOnlyInstance;
00025 
00026 TEST(TestInstanceTracker, CopyableMovable) {
00027   InstanceTracker tracker;
00028   CopyableMovableInstance src(1);
00029   EXPECT_EQ(1, src.value()) << src;
00030   CopyableMovableInstance copy(src);
00031   CopyableMovableInstance move(std::move(src));
00032   EXPECT_EQ(1, tracker.copies());
00033   EXPECT_EQ(1, tracker.moves());
00034   EXPECT_EQ(0, tracker.swaps());
00035   EXPECT_EQ(3, tracker.instances());
00036   EXPECT_EQ(2, tracker.live_instances());
00037   tracker.ResetCopiesMovesSwaps();
00038 
00039   CopyableMovableInstance copy_assign(1);
00040   copy_assign = copy;
00041   CopyableMovableInstance move_assign(1);
00042   move_assign = std::move(move);
00043   EXPECT_EQ(1, tracker.copies());
00044   EXPECT_EQ(1, tracker.moves());
00045   EXPECT_EQ(0, tracker.swaps());
00046   EXPECT_EQ(5, tracker.instances());
00047   EXPECT_EQ(3, tracker.live_instances());
00048   tracker.ResetCopiesMovesSwaps();
00049 
00050   {
00051     using std::swap;
00052     swap(move_assign, copy);
00053     swap(copy, move_assign);
00054     EXPECT_EQ(2, tracker.swaps());
00055     EXPECT_EQ(0, tracker.copies());
00056     EXPECT_EQ(0, tracker.moves());
00057     EXPECT_EQ(5, tracker.instances());
00058     EXPECT_EQ(3, tracker.live_instances());
00059   }
00060 }
00061 
00062 TEST(TestInstanceTracker, CopyableOnly) {
00063   InstanceTracker tracker;
00064   CopyableOnlyInstance src(1);
00065   EXPECT_EQ(1, src.value()) << src;
00066   CopyableOnlyInstance copy(src);
00067   CopyableOnlyInstance copy2(std::move(src));  // NOLINT
00068   EXPECT_EQ(2, tracker.copies());
00069   EXPECT_EQ(0, tracker.moves());
00070   EXPECT_EQ(3, tracker.instances());
00071   EXPECT_EQ(3, tracker.live_instances());
00072   tracker.ResetCopiesMovesSwaps();
00073 
00074   CopyableOnlyInstance copy_assign(1);
00075   copy_assign = copy;
00076   CopyableOnlyInstance copy_assign2(1);
00077   copy_assign2 = std::move(copy2);  // NOLINT
00078   EXPECT_EQ(2, tracker.copies());
00079   EXPECT_EQ(0, tracker.moves());
00080   EXPECT_EQ(5, tracker.instances());
00081   EXPECT_EQ(5, tracker.live_instances());
00082   tracker.ResetCopiesMovesSwaps();
00083 
00084   {
00085     using std::swap;
00086     swap(src, copy);
00087     swap(copy, src);
00088     EXPECT_EQ(2, tracker.swaps());
00089     EXPECT_EQ(0, tracker.copies());
00090     EXPECT_EQ(0, tracker.moves());
00091     EXPECT_EQ(5, tracker.instances());
00092     EXPECT_EQ(5, tracker.live_instances());
00093   }
00094 }
00095 
00096 TEST(TestInstanceTracker, MovableOnly) {
00097   InstanceTracker tracker;
00098   MovableOnlyInstance src(1);
00099   EXPECT_EQ(1, src.value()) << src;
00100   MovableOnlyInstance move(std::move(src));
00101   MovableOnlyInstance move_assign(2);
00102   move_assign = std::move(move);
00103   EXPECT_EQ(3, tracker.instances());
00104   EXPECT_EQ(1, tracker.live_instances());
00105   EXPECT_EQ(2, tracker.moves());
00106   EXPECT_EQ(0, tracker.copies());
00107   tracker.ResetCopiesMovesSwaps();
00108 
00109   {
00110     using std::swap;
00111     MovableOnlyInstance other(2);
00112     swap(move_assign, other);
00113     swap(other, move_assign);
00114     EXPECT_EQ(2, tracker.swaps());
00115     EXPECT_EQ(0, tracker.copies());
00116     EXPECT_EQ(0, tracker.moves());
00117     EXPECT_EQ(4, tracker.instances());
00118     EXPECT_EQ(2, tracker.live_instances());
00119   }
00120 }
00121 
00122 TEST(TestInstanceTracker, ExistingInstances) {
00123   CopyableMovableInstance uncounted_instance(1);
00124   CopyableMovableInstance uncounted_live_instance(
00125       std::move(uncounted_instance));
00126   InstanceTracker tracker;
00127   EXPECT_EQ(0, tracker.instances());
00128   EXPECT_EQ(0, tracker.live_instances());
00129   EXPECT_EQ(0, tracker.copies());
00130   {
00131     CopyableMovableInstance instance1(1);
00132     EXPECT_EQ(1, tracker.instances());
00133     EXPECT_EQ(1, tracker.live_instances());
00134     EXPECT_EQ(0, tracker.copies());
00135     EXPECT_EQ(0, tracker.moves());
00136     {
00137       InstanceTracker tracker2;
00138       CopyableMovableInstance instance2(instance1);
00139       CopyableMovableInstance instance3(std::move(instance2));
00140       EXPECT_EQ(3, tracker.instances());
00141       EXPECT_EQ(2, tracker.live_instances());
00142       EXPECT_EQ(1, tracker.copies());
00143       EXPECT_EQ(1, tracker.moves());
00144       EXPECT_EQ(2, tracker2.instances());
00145       EXPECT_EQ(1, tracker2.live_instances());
00146       EXPECT_EQ(1, tracker2.copies());
00147       EXPECT_EQ(1, tracker2.moves());
00148     }
00149     EXPECT_EQ(1, tracker.instances());
00150     EXPECT_EQ(1, tracker.live_instances());
00151     EXPECT_EQ(1, tracker.copies());
00152     EXPECT_EQ(1, tracker.moves());
00153   }
00154   EXPECT_EQ(0, tracker.instances());
00155   EXPECT_EQ(0, tracker.live_instances());
00156   EXPECT_EQ(1, tracker.copies());
00157   EXPECT_EQ(1, tracker.moves());
00158 }
00159 
00160 TEST(TestInstanceTracker, Comparisons) {
00161   InstanceTracker tracker;
00162   MovableOnlyInstance one(1), two(2);
00163 
00164   EXPECT_EQ(0, tracker.comparisons());
00165   EXPECT_FALSE(one == two);
00166   EXPECT_EQ(1, tracker.comparisons());
00167   EXPECT_TRUE(one != two);
00168   EXPECT_EQ(2, tracker.comparisons());
00169   EXPECT_TRUE(one < two);
00170   EXPECT_EQ(3, tracker.comparisons());
00171   EXPECT_FALSE(one > two);
00172   EXPECT_EQ(4, tracker.comparisons());
00173   EXPECT_TRUE(one <= two);
00174   EXPECT_EQ(5, tracker.comparisons());
00175   EXPECT_FALSE(one >= two);
00176   EXPECT_EQ(6, tracker.comparisons());
00177   EXPECT_TRUE(one.compare(two) < 0);  // NOLINT
00178   EXPECT_EQ(7, tracker.comparisons());
00179 
00180   tracker.ResetCopiesMovesSwaps();
00181   EXPECT_EQ(0, tracker.comparisons());
00182 }
00183 
00184 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15