michael@0: // Copyright (c) 2006, 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: // address_map_unittest.cc: Unit tests for AddressMap. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "processor/address_map-inl.h" michael@0: #include "processor/linked_ptr.h" michael@0: #include "processor/logging.h" michael@0: michael@0: #define ASSERT_TRUE(condition) \ michael@0: if (!(condition)) { \ michael@0: fprintf(stderr, "FAIL: %s @ %s:%d\n", #condition, __FILE__, __LINE__); \ michael@0: return false; \ michael@0: } michael@0: michael@0: #define ASSERT_FALSE(condition) ASSERT_TRUE(!(condition)) michael@0: michael@0: #define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2)) michael@0: michael@0: namespace { michael@0: michael@0: using google_breakpad::AddressMap; michael@0: using google_breakpad::linked_ptr; michael@0: michael@0: // A CountedObject holds an int. A global (not thread safe!) count of michael@0: // allocated CountedObjects is maintained to help test memory management. michael@0: class CountedObject { michael@0: public: michael@0: explicit CountedObject(int id) : id_(id) { ++count_; } michael@0: ~CountedObject() { --count_; } michael@0: michael@0: static int count() { return count_; } michael@0: int id() const { return id_; } michael@0: michael@0: private: michael@0: static int count_; michael@0: int id_; michael@0: }; michael@0: michael@0: int CountedObject::count_; michael@0: michael@0: typedef int AddressType; michael@0: typedef AddressMap< AddressType, linked_ptr > TestMap; michael@0: michael@0: static bool DoAddressMapTest() { michael@0: ASSERT_EQ(CountedObject::count(), 0); michael@0: michael@0: TestMap test_map; michael@0: linked_ptr entry; michael@0: AddressType address; michael@0: michael@0: // Check that a new map is truly empty. michael@0: ASSERT_FALSE(test_map.Retrieve(0, &entry, &address)); michael@0: ASSERT_FALSE(test_map.Retrieve(INT_MIN, &entry, &address)); michael@0: ASSERT_FALSE(test_map.Retrieve(INT_MAX, &entry, &address)); michael@0: michael@0: // Check that Clear clears the map without leaking. michael@0: ASSERT_EQ(CountedObject::count(), 0); michael@0: ASSERT_TRUE(test_map.Store(1, michael@0: linked_ptr(new CountedObject(0)))); michael@0: ASSERT_TRUE(test_map.Retrieve(1, &entry, &address)); michael@0: ASSERT_EQ(CountedObject::count(), 1); michael@0: test_map.Clear(); michael@0: ASSERT_EQ(CountedObject::count(), 1); // still holding entry in this scope michael@0: michael@0: // Check that a cleared map is truly empty. michael@0: ASSERT_FALSE(test_map.Retrieve(0, &entry, &address)); michael@0: ASSERT_FALSE(test_map.Retrieve(INT_MIN, &entry, &address)); michael@0: ASSERT_FALSE(test_map.Retrieve(INT_MAX, &entry, &address)); michael@0: michael@0: // Check a single-element map. michael@0: ASSERT_TRUE(test_map.Store(10, michael@0: linked_ptr(new CountedObject(1)))); michael@0: ASSERT_FALSE(test_map.Retrieve(9, &entry, &address)); michael@0: ASSERT_TRUE(test_map.Retrieve(10, &entry, &address)); michael@0: ASSERT_EQ(CountedObject::count(), 1); michael@0: ASSERT_EQ(entry->id(), 1); michael@0: ASSERT_EQ(address, 10); michael@0: ASSERT_TRUE(test_map.Retrieve(11, &entry, &address)); michael@0: ASSERT_TRUE(test_map.Retrieve(11, &entry, NULL)); // NULL ok here michael@0: michael@0: // Add some more elements. michael@0: ASSERT_TRUE(test_map.Store(5, michael@0: linked_ptr(new CountedObject(2)))); michael@0: ASSERT_EQ(CountedObject::count(), 2); michael@0: ASSERT_TRUE(test_map.Store(20, michael@0: linked_ptr(new CountedObject(3)))); michael@0: ASSERT_TRUE(test_map.Store(15, michael@0: linked_ptr(new CountedObject(4)))); michael@0: ASSERT_FALSE(test_map.Store(10, michael@0: linked_ptr(new CountedObject(5)))); // already in map michael@0: ASSERT_TRUE(test_map.Store(16, michael@0: linked_ptr(new CountedObject(6)))); michael@0: ASSERT_TRUE(test_map.Store(14, michael@0: linked_ptr(new CountedObject(7)))); michael@0: michael@0: // Nothing was stored with a key under 5. Don't use ASSERT inside loops michael@0: // because it won't show exactly which key/entry/address failed. michael@0: for (AddressType key = 0; key < 5; ++key) { michael@0: if (test_map.Retrieve(key, &entry, &address)) { michael@0: fprintf(stderr, michael@0: "FAIL: retrieve %d expected false observed true @ %s:%d\n", michael@0: key, __FILE__, __LINE__); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // Check everything that was stored. michael@0: const int id_verify[] = { 0, 0, 0, 0, 0, // unused michael@0: 2, 2, 2, 2, 2, // 5 - 9 michael@0: 1, 1, 1, 1, 7, // 10 - 14 michael@0: 4, 6, 6, 6, 6, // 15 - 19 michael@0: 3, 3, 3, 3, 3, // 20 - 24 michael@0: 3, 3, 3, 3, 3 }; // 25 - 29 michael@0: const AddressType address_verify[] = { 0, 0, 0, 0, 0, // unused michael@0: 5, 5, 5, 5, 5, // 5 - 9 michael@0: 10, 10, 10, 10, 14, // 10 - 14 michael@0: 15, 16, 16, 16, 16, // 15 - 19 michael@0: 20, 20, 20, 20, 20, // 20 - 24 michael@0: 20, 20, 20, 20, 20 }; // 25 - 29 michael@0: michael@0: for (AddressType key = 5; key < 30; ++key) { michael@0: if (!test_map.Retrieve(key, &entry, &address)) { michael@0: fprintf(stderr, michael@0: "FAIL: retrieve %d expected true observed false @ %s:%d\n", michael@0: key, __FILE__, __LINE__); michael@0: return false; michael@0: } michael@0: if (entry->id() != id_verify[key]) { michael@0: fprintf(stderr, michael@0: "FAIL: retrieve %d expected entry %d observed %d @ %s:%d\n", michael@0: key, id_verify[key], entry->id(), __FILE__, __LINE__); michael@0: return false; michael@0: } michael@0: if (address != address_verify[key]) { michael@0: fprintf(stderr, michael@0: "FAIL: retrieve %d expected address %d observed %d @ %s:%d\n", michael@0: key, address_verify[key], address, __FILE__, __LINE__); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // The stored objects should still be in the map. michael@0: ASSERT_EQ(CountedObject::count(), 6); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool RunTests() { michael@0: if (!DoAddressMapTest()) michael@0: return false; michael@0: michael@0: // Leak check. michael@0: ASSERT_EQ(CountedObject::count(), 0); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: int main(int argc, char **argv) { michael@0: BPLOG_INIT(&argc, &argv); michael@0: michael@0: return RunTests() ? 0 : 1; michael@0: }