f0f15c2778
-- 7fb969986d7d5a1b30233a94ae7ea29e1abf8937 by Greg Falcon <gfalcon@google.com>: Fix whitespace in str_join.h. PiperOrigin-RevId: 208082852 -- 85428003a8a29fbcd92c0b48708a69460eeb0e8f by Abseil Team <absl-team@google.com>: Add missing back-ticks to the comments. PiperOrigin-RevId: 207985417 -- 0bbac248d5649ac1d7bc71ff1025a11332fb2026 by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 207927484 -- f587324b99570b403b4626fb03fce16f0d950b05 by Abseil Team <absl-team@google.com>: Update BaseCountedInstance to have comparison operators and allow them to be tracked by InstanceTracker. PiperOrigin-RevId: 207919218 -- d7f410e4f15328412941f16e25ba3735d5bdfda6 by Derek Mauro <dmauro@google.com>: Internal import of GitHub PR #153 Removed "warning treated as error" flag from MSVC PiperOrigin-RevId: 207908806 -- 59eefc78f8571ffc51cb636894015ce2580508d7 by Abseil Team <absl-team@google.com>: Fix namespace references in examples from 'std::' to 'absl::'. PiperOrigin-RevId: 207895230 -- 151df17b6544222e29913b034a1296c0e14374d5 by Abseil Team <absl-team@google.com>: Internal Change PiperOrigin-RevId: 207894949 GitOrigin-RevId: 7fb969986d7d5a1b30233a94ae7ea29e1abf8937 Change-Id: I00097afbe9610ddb3f2330a2a86dcffde7bb6675
182 lines
5.5 KiB
C++
182 lines
5.5 KiB
C++
// Copyright 2017 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "absl/container/internal/test_instance_tracker.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
namespace {
|
|
|
|
using absl::test_internal::CopyableMovableInstance;
|
|
using absl::test_internal::CopyableOnlyInstance;
|
|
using absl::test_internal::InstanceTracker;
|
|
using absl::test_internal::MovableOnlyInstance;
|
|
|
|
TEST(TestInstanceTracker, CopyableMovable) {
|
|
InstanceTracker tracker;
|
|
CopyableMovableInstance src(1);
|
|
EXPECT_EQ(1, src.value()) << src;
|
|
CopyableMovableInstance copy(src);
|
|
CopyableMovableInstance move(std::move(src));
|
|
EXPECT_EQ(1, tracker.copies());
|
|
EXPECT_EQ(1, tracker.moves());
|
|
EXPECT_EQ(0, tracker.swaps());
|
|
EXPECT_EQ(3, tracker.instances());
|
|
EXPECT_EQ(2, tracker.live_instances());
|
|
tracker.ResetCopiesMovesSwaps();
|
|
|
|
CopyableMovableInstance copy_assign(1);
|
|
copy_assign = copy;
|
|
CopyableMovableInstance move_assign(1);
|
|
move_assign = std::move(move);
|
|
EXPECT_EQ(1, tracker.copies());
|
|
EXPECT_EQ(1, tracker.moves());
|
|
EXPECT_EQ(0, tracker.swaps());
|
|
EXPECT_EQ(5, tracker.instances());
|
|
EXPECT_EQ(3, tracker.live_instances());
|
|
tracker.ResetCopiesMovesSwaps();
|
|
|
|
{
|
|
using std::swap;
|
|
swap(move_assign, copy);
|
|
swap(copy, move_assign);
|
|
EXPECT_EQ(2, tracker.swaps());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
EXPECT_EQ(5, tracker.instances());
|
|
EXPECT_EQ(3, tracker.live_instances());
|
|
}
|
|
}
|
|
|
|
TEST(TestInstanceTracker, CopyableOnly) {
|
|
InstanceTracker tracker;
|
|
CopyableOnlyInstance src(1);
|
|
EXPECT_EQ(1, src.value()) << src;
|
|
CopyableOnlyInstance copy(src);
|
|
CopyableOnlyInstance copy2(std::move(src)); // NOLINT
|
|
EXPECT_EQ(2, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
EXPECT_EQ(3, tracker.instances());
|
|
EXPECT_EQ(3, tracker.live_instances());
|
|
tracker.ResetCopiesMovesSwaps();
|
|
|
|
CopyableOnlyInstance copy_assign(1);
|
|
copy_assign = copy;
|
|
CopyableOnlyInstance copy_assign2(1);
|
|
copy_assign2 = std::move(copy2); // NOLINT
|
|
EXPECT_EQ(2, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
EXPECT_EQ(5, tracker.instances());
|
|
EXPECT_EQ(5, tracker.live_instances());
|
|
tracker.ResetCopiesMovesSwaps();
|
|
|
|
{
|
|
using std::swap;
|
|
swap(src, copy);
|
|
swap(copy, src);
|
|
EXPECT_EQ(2, tracker.swaps());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
EXPECT_EQ(5, tracker.instances());
|
|
EXPECT_EQ(5, tracker.live_instances());
|
|
}
|
|
}
|
|
|
|
TEST(TestInstanceTracker, MovableOnly) {
|
|
InstanceTracker tracker;
|
|
MovableOnlyInstance src(1);
|
|
EXPECT_EQ(1, src.value()) << src;
|
|
MovableOnlyInstance move(std::move(src));
|
|
MovableOnlyInstance move_assign(2);
|
|
move_assign = std::move(move);
|
|
EXPECT_EQ(3, tracker.instances());
|
|
EXPECT_EQ(1, tracker.live_instances());
|
|
EXPECT_EQ(2, tracker.moves());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
tracker.ResetCopiesMovesSwaps();
|
|
|
|
{
|
|
using std::swap;
|
|
MovableOnlyInstance other(2);
|
|
swap(move_assign, other);
|
|
swap(other, move_assign);
|
|
EXPECT_EQ(2, tracker.swaps());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
EXPECT_EQ(4, tracker.instances());
|
|
EXPECT_EQ(2, tracker.live_instances());
|
|
}
|
|
}
|
|
|
|
TEST(TestInstanceTracker, ExistingInstances) {
|
|
CopyableMovableInstance uncounted_instance(1);
|
|
CopyableMovableInstance uncounted_live_instance(
|
|
std::move(uncounted_instance));
|
|
InstanceTracker tracker;
|
|
EXPECT_EQ(0, tracker.instances());
|
|
EXPECT_EQ(0, tracker.live_instances());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
{
|
|
CopyableMovableInstance instance1(1);
|
|
EXPECT_EQ(1, tracker.instances());
|
|
EXPECT_EQ(1, tracker.live_instances());
|
|
EXPECT_EQ(0, tracker.copies());
|
|
EXPECT_EQ(0, tracker.moves());
|
|
{
|
|
InstanceTracker tracker2;
|
|
CopyableMovableInstance instance2(instance1);
|
|
CopyableMovableInstance instance3(std::move(instance2));
|
|
EXPECT_EQ(3, tracker.instances());
|
|
EXPECT_EQ(2, tracker.live_instances());
|
|
EXPECT_EQ(1, tracker.copies());
|
|
EXPECT_EQ(1, tracker.moves());
|
|
EXPECT_EQ(2, tracker2.instances());
|
|
EXPECT_EQ(1, tracker2.live_instances());
|
|
EXPECT_EQ(1, tracker2.copies());
|
|
EXPECT_EQ(1, tracker2.moves());
|
|
}
|
|
EXPECT_EQ(1, tracker.instances());
|
|
EXPECT_EQ(1, tracker.live_instances());
|
|
EXPECT_EQ(1, tracker.copies());
|
|
EXPECT_EQ(1, tracker.moves());
|
|
}
|
|
EXPECT_EQ(0, tracker.instances());
|
|
EXPECT_EQ(0, tracker.live_instances());
|
|
EXPECT_EQ(1, tracker.copies());
|
|
EXPECT_EQ(1, tracker.moves());
|
|
}
|
|
|
|
TEST(TestInstanceTracker, Comparisons) {
|
|
InstanceTracker tracker;
|
|
MovableOnlyInstance one(1), two(2);
|
|
|
|
EXPECT_EQ(0, tracker.comparisons());
|
|
EXPECT_FALSE(one == two);
|
|
EXPECT_EQ(1, tracker.comparisons());
|
|
EXPECT_TRUE(one != two);
|
|
EXPECT_EQ(2, tracker.comparisons());
|
|
EXPECT_TRUE(one < two);
|
|
EXPECT_EQ(3, tracker.comparisons());
|
|
EXPECT_FALSE(one > two);
|
|
EXPECT_EQ(4, tracker.comparisons());
|
|
EXPECT_TRUE(one <= two);
|
|
EXPECT_EQ(5, tracker.comparisons());
|
|
EXPECT_FALSE(one >= two);
|
|
EXPECT_EQ(6, tracker.comparisons());
|
|
|
|
tracker.ResetCopiesMovesSwaps();
|
|
EXPECT_EQ(0, tracker.comparisons());
|
|
}
|
|
|
|
} // namespace
|