1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/address_map_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,196 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// address_map_unittest.cc: Unit tests for AddressMap. 1.34 +// 1.35 +// Author: Mark Mentovai 1.36 + 1.37 +#include <limits.h> 1.38 +#include <stdio.h> 1.39 + 1.40 +#include "processor/address_map-inl.h" 1.41 +#include "processor/linked_ptr.h" 1.42 +#include "processor/logging.h" 1.43 + 1.44 +#define ASSERT_TRUE(condition) \ 1.45 + if (!(condition)) { \ 1.46 + fprintf(stderr, "FAIL: %s @ %s:%d\n", #condition, __FILE__, __LINE__); \ 1.47 + return false; \ 1.48 + } 1.49 + 1.50 +#define ASSERT_FALSE(condition) ASSERT_TRUE(!(condition)) 1.51 + 1.52 +#define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2)) 1.53 + 1.54 +namespace { 1.55 + 1.56 +using google_breakpad::AddressMap; 1.57 +using google_breakpad::linked_ptr; 1.58 + 1.59 +// A CountedObject holds an int. A global (not thread safe!) count of 1.60 +// allocated CountedObjects is maintained to help test memory management. 1.61 +class CountedObject { 1.62 + public: 1.63 + explicit CountedObject(int id) : id_(id) { ++count_; } 1.64 + ~CountedObject() { --count_; } 1.65 + 1.66 + static int count() { return count_; } 1.67 + int id() const { return id_; } 1.68 + 1.69 + private: 1.70 + static int count_; 1.71 + int id_; 1.72 +}; 1.73 + 1.74 +int CountedObject::count_; 1.75 + 1.76 +typedef int AddressType; 1.77 +typedef AddressMap< AddressType, linked_ptr<CountedObject> > TestMap; 1.78 + 1.79 +static bool DoAddressMapTest() { 1.80 + ASSERT_EQ(CountedObject::count(), 0); 1.81 + 1.82 + TestMap test_map; 1.83 + linked_ptr<CountedObject> entry; 1.84 + AddressType address; 1.85 + 1.86 + // Check that a new map is truly empty. 1.87 + ASSERT_FALSE(test_map.Retrieve(0, &entry, &address)); 1.88 + ASSERT_FALSE(test_map.Retrieve(INT_MIN, &entry, &address)); 1.89 + ASSERT_FALSE(test_map.Retrieve(INT_MAX, &entry, &address)); 1.90 + 1.91 + // Check that Clear clears the map without leaking. 1.92 + ASSERT_EQ(CountedObject::count(), 0); 1.93 + ASSERT_TRUE(test_map.Store(1, 1.94 + linked_ptr<CountedObject>(new CountedObject(0)))); 1.95 + ASSERT_TRUE(test_map.Retrieve(1, &entry, &address)); 1.96 + ASSERT_EQ(CountedObject::count(), 1); 1.97 + test_map.Clear(); 1.98 + ASSERT_EQ(CountedObject::count(), 1); // still holding entry in this scope 1.99 + 1.100 + // Check that a cleared map is truly empty. 1.101 + ASSERT_FALSE(test_map.Retrieve(0, &entry, &address)); 1.102 + ASSERT_FALSE(test_map.Retrieve(INT_MIN, &entry, &address)); 1.103 + ASSERT_FALSE(test_map.Retrieve(INT_MAX, &entry, &address)); 1.104 + 1.105 + // Check a single-element map. 1.106 + ASSERT_TRUE(test_map.Store(10, 1.107 + linked_ptr<CountedObject>(new CountedObject(1)))); 1.108 + ASSERT_FALSE(test_map.Retrieve(9, &entry, &address)); 1.109 + ASSERT_TRUE(test_map.Retrieve(10, &entry, &address)); 1.110 + ASSERT_EQ(CountedObject::count(), 1); 1.111 + ASSERT_EQ(entry->id(), 1); 1.112 + ASSERT_EQ(address, 10); 1.113 + ASSERT_TRUE(test_map.Retrieve(11, &entry, &address)); 1.114 + ASSERT_TRUE(test_map.Retrieve(11, &entry, NULL)); // NULL ok here 1.115 + 1.116 + // Add some more elements. 1.117 + ASSERT_TRUE(test_map.Store(5, 1.118 + linked_ptr<CountedObject>(new CountedObject(2)))); 1.119 + ASSERT_EQ(CountedObject::count(), 2); 1.120 + ASSERT_TRUE(test_map.Store(20, 1.121 + linked_ptr<CountedObject>(new CountedObject(3)))); 1.122 + ASSERT_TRUE(test_map.Store(15, 1.123 + linked_ptr<CountedObject>(new CountedObject(4)))); 1.124 + ASSERT_FALSE(test_map.Store(10, 1.125 + linked_ptr<CountedObject>(new CountedObject(5)))); // already in map 1.126 + ASSERT_TRUE(test_map.Store(16, 1.127 + linked_ptr<CountedObject>(new CountedObject(6)))); 1.128 + ASSERT_TRUE(test_map.Store(14, 1.129 + linked_ptr<CountedObject>(new CountedObject(7)))); 1.130 + 1.131 + // Nothing was stored with a key under 5. Don't use ASSERT inside loops 1.132 + // because it won't show exactly which key/entry/address failed. 1.133 + for (AddressType key = 0; key < 5; ++key) { 1.134 + if (test_map.Retrieve(key, &entry, &address)) { 1.135 + fprintf(stderr, 1.136 + "FAIL: retrieve %d expected false observed true @ %s:%d\n", 1.137 + key, __FILE__, __LINE__); 1.138 + return false; 1.139 + } 1.140 + } 1.141 + 1.142 + // Check everything that was stored. 1.143 + const int id_verify[] = { 0, 0, 0, 0, 0, // unused 1.144 + 2, 2, 2, 2, 2, // 5 - 9 1.145 + 1, 1, 1, 1, 7, // 10 - 14 1.146 + 4, 6, 6, 6, 6, // 15 - 19 1.147 + 3, 3, 3, 3, 3, // 20 - 24 1.148 + 3, 3, 3, 3, 3 }; // 25 - 29 1.149 + const AddressType address_verify[] = { 0, 0, 0, 0, 0, // unused 1.150 + 5, 5, 5, 5, 5, // 5 - 9 1.151 + 10, 10, 10, 10, 14, // 10 - 14 1.152 + 15, 16, 16, 16, 16, // 15 - 19 1.153 + 20, 20, 20, 20, 20, // 20 - 24 1.154 + 20, 20, 20, 20, 20 }; // 25 - 29 1.155 + 1.156 + for (AddressType key = 5; key < 30; ++key) { 1.157 + if (!test_map.Retrieve(key, &entry, &address)) { 1.158 + fprintf(stderr, 1.159 + "FAIL: retrieve %d expected true observed false @ %s:%d\n", 1.160 + key, __FILE__, __LINE__); 1.161 + return false; 1.162 + } 1.163 + if (entry->id() != id_verify[key]) { 1.164 + fprintf(stderr, 1.165 + "FAIL: retrieve %d expected entry %d observed %d @ %s:%d\n", 1.166 + key, id_verify[key], entry->id(), __FILE__, __LINE__); 1.167 + return false; 1.168 + } 1.169 + if (address != address_verify[key]) { 1.170 + fprintf(stderr, 1.171 + "FAIL: retrieve %d expected address %d observed %d @ %s:%d\n", 1.172 + key, address_verify[key], address, __FILE__, __LINE__); 1.173 + return false; 1.174 + } 1.175 + } 1.176 + 1.177 + // The stored objects should still be in the map. 1.178 + ASSERT_EQ(CountedObject::count(), 6); 1.179 + 1.180 + return true; 1.181 +} 1.182 + 1.183 +static bool RunTests() { 1.184 + if (!DoAddressMapTest()) 1.185 + return false; 1.186 + 1.187 + // Leak check. 1.188 + ASSERT_EQ(CountedObject::count(), 0); 1.189 + 1.190 + return true; 1.191 +} 1.192 + 1.193 +} // namespace 1.194 + 1.195 +int main(int argc, char **argv) { 1.196 + BPLOG_INIT(&argc, &argv); 1.197 + 1.198 + return RunTests() ? 0 : 1; 1.199 +}