michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // map_serializers_unittest.cc: Unit tests for std::map serializer and michael@0: // std::map wrapper serializers. michael@0: // michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "breakpad_googletest_includes.h" michael@0: #include "map_serializers-inl.h" michael@0: michael@0: #include "processor/address_map-inl.h" michael@0: #include "processor/range_map-inl.h" michael@0: #include "processor/contained_range_map-inl.h" michael@0: michael@0: typedef int32_t AddrType; michael@0: typedef int32_t EntryType; michael@0: michael@0: const int kSizeOfInt = 4; michael@0: michael@0: class TestStdMapSerializer : public ::testing::Test { michael@0: protected: michael@0: void SetUp() { michael@0: serialized_size_ = 0; michael@0: serialized_data_ = NULL; michael@0: } michael@0: michael@0: void TearDown() { michael@0: delete [] serialized_data_; michael@0: } michael@0: michael@0: std::map std_map_; michael@0: google_breakpad::StdMapSerializer serializer_; michael@0: uint32_t serialized_size_; michael@0: char *serialized_data_; michael@0: }; michael@0: michael@0: TEST_F(TestStdMapSerializer, EmptyMapTestCase) { michael@0: const int32_t correct_data[] = { 0 }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: // std_map_ is empty. michael@0: serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestStdMapSerializer, MapWithTwoElementsTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 2, michael@0: // Offsets michael@0: 20, 24, michael@0: // Keys michael@0: 1, 3, michael@0: // Values michael@0: 2, 6 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: std_map_.insert(std::make_pair(1, 2)); michael@0: std_map_.insert(std::make_pair(3, 6)); michael@0: michael@0: serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestStdMapSerializer, MapWithFiveElementsTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 5, michael@0: // Offsets michael@0: 44, 48, 52, 56, 60, michael@0: // Keys michael@0: 1, 2, 3, 4, 5, michael@0: // Values michael@0: 11, 12, 13, 14, 15 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: for (int i = 1; i < 6; ++i) michael@0: std_map_.insert(std::make_pair(i, 10 + i)); michael@0: michael@0: serialized_data_ = serializer_.Serialize(std_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: class TestAddressMapSerializer : public ::testing::Test { michael@0: protected: michael@0: void SetUp() { michael@0: serialized_size_ = 0; michael@0: serialized_data_ = 0; michael@0: } michael@0: michael@0: void TearDown() { michael@0: delete [] serialized_data_; michael@0: } michael@0: michael@0: google_breakpad::AddressMap address_map_; michael@0: google_breakpad::AddressMapSerializer serializer_; michael@0: uint32_t serialized_size_; michael@0: char *serialized_data_; michael@0: }; michael@0: michael@0: TEST_F(TestAddressMapSerializer, EmptyMapTestCase) { michael@0: const int32_t correct_data[] = { 0 }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: // std_map_ is empty. michael@0: serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestAddressMapSerializer, MapWithTwoElementsTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 2, michael@0: // Offsets michael@0: 20, 24, michael@0: // Keys michael@0: 1, 3, michael@0: // Values michael@0: 2, 6 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: address_map_.Store(1, 2); michael@0: address_map_.Store(3, 6); michael@0: michael@0: serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestAddressMapSerializer, MapWithFourElementsTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 4, michael@0: // Offsets michael@0: 36, 40, 44, 48, michael@0: // Keys michael@0: -6, -4, 8, 123, michael@0: // Values michael@0: 2, 3, 5, 8 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: address_map_.Store(-6, 2); michael@0: address_map_.Store(-4, 3); michael@0: address_map_.Store(8, 5); michael@0: address_map_.Store(123, 8); michael@0: michael@0: serialized_data_ = serializer_.Serialize(address_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: michael@0: class TestRangeMapSerializer : public ::testing::Test { michael@0: protected: michael@0: void SetUp() { michael@0: serialized_size_ = 0; michael@0: serialized_data_ = 0; michael@0: } michael@0: michael@0: void TearDown() { michael@0: delete [] serialized_data_; michael@0: } michael@0: michael@0: google_breakpad::RangeMap range_map_; michael@0: google_breakpad::RangeMapSerializer serializer_; michael@0: uint32_t serialized_size_; michael@0: char *serialized_data_; michael@0: }; michael@0: michael@0: TEST_F(TestRangeMapSerializer, EmptyMapTestCase) { michael@0: const int32_t correct_data[] = { 0 }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: // range_map_ is empty. michael@0: serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestRangeMapSerializer, MapWithOneRangeTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 1, michael@0: // Offsets michael@0: 12, michael@0: // Keys: high address michael@0: 10, michael@0: // Values: (low address, entry) pairs michael@0: 1, 6 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: range_map_.StoreRange(1, 10, 6); michael@0: michael@0: serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestRangeMapSerializer, MapWithThreeRangesTestCase) { michael@0: const int32_t correct_data[] = { michael@0: // # of nodes michael@0: 3, michael@0: // Offsets michael@0: 28, 36, 44, michael@0: // Keys: high address michael@0: 5, 9, 20, michael@0: // Values: (low address, entry) pairs michael@0: 2, 1, 6, 2, 10, 3 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: ASSERT_TRUE(range_map_.StoreRange(2, 4, 1)); michael@0: ASSERT_TRUE(range_map_.StoreRange(6, 4, 2)); michael@0: ASSERT_TRUE(range_map_.StoreRange(10, 11, 3)); michael@0: michael@0: serialized_data_ = serializer_.Serialize(range_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: michael@0: class TestContainedRangeMapSerializer : public ::testing::Test { michael@0: protected: michael@0: void SetUp() { michael@0: serialized_size_ = 0; michael@0: serialized_data_ = 0; michael@0: } michael@0: michael@0: void TearDown() { michael@0: delete [] serialized_data_; michael@0: } michael@0: michael@0: google_breakpad::ContainedRangeMap crm_map_; michael@0: google_breakpad::ContainedRangeMapSerializer serializer_; michael@0: uint32_t serialized_size_; michael@0: char *serialized_data_; michael@0: }; michael@0: michael@0: TEST_F(TestContainedRangeMapSerializer, EmptyMapTestCase) { michael@0: const int32_t correct_data[] = { michael@0: 0, // base address of root michael@0: 4, // size of entry michael@0: 0, // entry stored at root michael@0: 0 // empty map stored at root michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: // crm_map_ is empty. michael@0: serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestContainedRangeMapSerializer, MapWithOneRangeTestCase) { michael@0: const int32_t correct_data[] = { michael@0: 0, // base address of root michael@0: 4, // size of entry michael@0: 0, // entry stored at root michael@0: // Map stored at root node: michael@0: 1, // # of nodes michael@0: 12, // offset michael@0: 9, // key michael@0: // value: a child ContainedRangeMap michael@0: 3, // base address of child CRM michael@0: 4, // size of entry michael@0: -1, // entry stored in child CRM michael@0: 0 // empty sub-map stored in child CRM michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: crm_map_.StoreRange(3, 7, -1); michael@0: michael@0: serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: TEST_F(TestContainedRangeMapSerializer, MapWithTwoLevelsTestCase) { michael@0: // Tree structure of ranges: michael@0: // root level 0 michael@0: // | michael@0: // map michael@0: // / \ level 1: child1, child2 michael@0: // 2~8 10~20 michael@0: // | | michael@0: // map map michael@0: // / \ | michael@0: // 3~4 6~7 16-20 level 2: grandchild1, grandchild2, grandchild3 michael@0: michael@0: const int32_t correct_data[] = { michael@0: // root: base, entry_size, entry michael@0: 0, 4, 0, michael@0: // root's map: # of nodes, offset1, offset2, key1, key2 michael@0: 2, 20, 84, 8, 20, michael@0: // child1: base, entry_size, entry: michael@0: 2, 4, -1, michael@0: // child1's map: # of nodes, offset1, offset2, key1, key2 michael@0: 2, 20, 36, 4, 7, michael@0: // grandchild1: base, entry_size, entry, empty_map michael@0: 3, 4, -1, 0, michael@0: // grandchild2: base, entry_size, entry, empty_map michael@0: 6, 4, -1, 0, michael@0: // child2: base, entry_size, entry: michael@0: 10, 4, -1, michael@0: // child2's map: # of nodes, offset1, key1 michael@0: 1, 12, 20, michael@0: // grandchild3: base, entry_size, entry, empty_map michael@0: 16, 4, -1, 0 michael@0: }; michael@0: uint32_t correct_size = sizeof(correct_data); michael@0: michael@0: // Store child1. michael@0: ASSERT_TRUE(crm_map_.StoreRange(2, 7, -1)); michael@0: // Store child2. michael@0: ASSERT_TRUE(crm_map_.StoreRange(10, 11, -1)); michael@0: // Store grandchild1. michael@0: ASSERT_TRUE(crm_map_.StoreRange(3, 2, -1)); michael@0: // Store grandchild2. michael@0: ASSERT_TRUE(crm_map_.StoreRange(6, 2, -1)); michael@0: // Store grandchild3. michael@0: ASSERT_TRUE(crm_map_.StoreRange(16, 5, -1)); michael@0: michael@0: serialized_data_ = serializer_.Serialize(&crm_map_, &serialized_size_); michael@0: michael@0: EXPECT_EQ(correct_size, serialized_size_); michael@0: EXPECT_EQ(memcmp(correct_data, serialized_data_, correct_size), 0); michael@0: } michael@0: michael@0: michael@0: int main(int argc, char *argv[]) { michael@0: ::testing::InitGoogleTest(&argc, argv); michael@0: michael@0: return RUN_ALL_TESTS(); michael@0: }