crashpad/client/simple_address_range_bag_test.cc
Scott Graham f177d77c51 Fix Mac build after 7217cc0a8f
Forgot some u number suffixes.

BUG=crashpad:94

Change-Id: I2a3b47b4eab07bf5b9ced3859f5a8b388a840b35
Reviewed-on: https://chromium-review.googlesource.com/329760
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2016-02-29 22:27:34 +00:00

110 lines
3.7 KiB
C++

// Copyright 2016 The Crashpad Authors. All rights reserved.
//
// 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 "client/simple_address_range_bag.h"
#include "base/logging.h"
#include "gtest/gtest.h"
#include "test/gtest_death_check.h"
namespace crashpad {
namespace test {
namespace {
TEST(SimpleAddressRangeBag, Entry) {
using TestBag = TSimpleAddressRangeBag<15>;
TestBag bag;
const TestBag::Entry* entry = TestBag::Iterator(bag).Next();
EXPECT_FALSE(entry);
bag.Insert(reinterpret_cast<void*>(0x1000), 200);
entry = TestBag::Iterator(bag).Next();
ASSERT_TRUE(entry);
EXPECT_EQ(entry->base, 0x1000u);
EXPECT_EQ(entry->size, 200u);
bag.Remove(reinterpret_cast<void*>(0x1000), 200);
EXPECT_FALSE(entry->is_active());
EXPECT_EQ(entry->base, 0u);
EXPECT_EQ(entry->size, 0u);
}
TEST(SimpleAddressRangeBag, SimpleAddressRangeBag) {
SimpleAddressRangeBag bag;
EXPECT_TRUE(bag.Insert(reinterpret_cast<void*>(0x1000), 10));
EXPECT_TRUE(bag.Insert(reinterpret_cast<void*>(0x2000), 20));
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_EQ(bag.GetCount(), 3u);
// Duplicates added too.
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_EQ(bag.GetCount(), 5u);
// Can be removed 3 times, but not the 4th time.
EXPECT_TRUE(bag.Remove(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_TRUE(bag.Remove(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_TRUE(bag.Remove(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_EQ(bag.GetCount(), 2u);
EXPECT_FALSE(bag.Remove(CheckedRange<uint64_t>(0x3000, 30)));
EXPECT_EQ(bag.GetCount(), 2u);
EXPECT_TRUE(bag.Remove(reinterpret_cast<void*>(0x1000), 10));
EXPECT_TRUE(bag.Remove(reinterpret_cast<void*>(0x2000), 20));
EXPECT_EQ(bag.GetCount(), 0u);
}
TEST(SimpleAddressRangeBag, CopyAndAssign) {
TSimpleAddressRangeBag<10> bag;
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(1, 2)));
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(3, 4)));
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(5, 6)));
EXPECT_TRUE(bag.Remove(CheckedRange<uint64_t>(3, 4)));
EXPECT_EQ(2u, bag.GetCount());
// Test copy.
TSimpleAddressRangeBag<10> bag_copy(bag);
EXPECT_EQ(2u, bag_copy.GetCount());
EXPECT_TRUE(bag_copy.Remove(CheckedRange<uint64_t>(1, 2)));
EXPECT_TRUE(bag_copy.Remove(CheckedRange<uint64_t>(5, 6)));
EXPECT_EQ(0u, bag_copy.GetCount());
EXPECT_EQ(2u, bag.GetCount());
// Test assign.
TSimpleAddressRangeBag<10> bag_assign;
bag_assign = bag;
EXPECT_EQ(2u, bag_assign.GetCount());
EXPECT_TRUE(bag_assign.Remove(CheckedRange<uint64_t>(1, 2)));
EXPECT_TRUE(bag_assign.Remove(CheckedRange<uint64_t>(5, 6)));
EXPECT_EQ(0u, bag_assign.GetCount());
EXPECT_EQ(2u, bag.GetCount());
}
// Running out of space shouldn't crash.
TEST(SimpleAddressRangeBag, OutOfSpace) {
TSimpleAddressRangeBag<2> bag;
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(1, 2)));
EXPECT_TRUE(bag.Insert(CheckedRange<uint64_t>(3, 4)));
EXPECT_FALSE(bag.Insert(CheckedRange<uint64_t>(5, 6)));
EXPECT_EQ(2u, bag.GetCount());
EXPECT_FALSE(bag.Remove(CheckedRange<uint64_t>(5, 6)));
}
} // namespace
} // namespace test
} // namespace crashpad