Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2010 Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // range_map_unittest.cc: Unit tests for RangeMap |
michael@0 | 31 | // |
michael@0 | 32 | // Author: Mark Mentovai |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | #include <limits.h> |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | |
michael@0 | 38 | #include "processor/range_map-inl.h" |
michael@0 | 39 | |
michael@0 | 40 | #include "common/scoped_ptr.h" |
michael@0 | 41 | #include "processor/linked_ptr.h" |
michael@0 | 42 | #include "processor/logging.h" |
michael@0 | 43 | |
michael@0 | 44 | namespace { |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | using google_breakpad::linked_ptr; |
michael@0 | 48 | using google_breakpad::scoped_ptr; |
michael@0 | 49 | using google_breakpad::RangeMap; |
michael@0 | 50 | |
michael@0 | 51 | |
michael@0 | 52 | // A CountedObject holds an int. A global (not thread safe!) count of |
michael@0 | 53 | // allocated CountedObjects is maintained to help test memory management. |
michael@0 | 54 | class CountedObject { |
michael@0 | 55 | public: |
michael@0 | 56 | explicit CountedObject(int id) : id_(id) { ++count_; } |
michael@0 | 57 | ~CountedObject() { --count_; } |
michael@0 | 58 | |
michael@0 | 59 | static int count() { return count_; } |
michael@0 | 60 | int id() const { return id_; } |
michael@0 | 61 | |
michael@0 | 62 | private: |
michael@0 | 63 | static int count_; |
michael@0 | 64 | int id_; |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | int CountedObject::count_; |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | typedef int AddressType; |
michael@0 | 71 | typedef RangeMap< AddressType, linked_ptr<CountedObject> > TestMap; |
michael@0 | 72 | |
michael@0 | 73 | |
michael@0 | 74 | // RangeTest contains data to use for store and retrieve tests. See |
michael@0 | 75 | // RunTests for descriptions of the tests. |
michael@0 | 76 | struct RangeTest { |
michael@0 | 77 | // Base address to use for test |
michael@0 | 78 | AddressType address; |
michael@0 | 79 | |
michael@0 | 80 | // Size of range to use for test |
michael@0 | 81 | AddressType size; |
michael@0 | 82 | |
michael@0 | 83 | // Unique ID of range - unstorable ranges must have unique IDs too |
michael@0 | 84 | int id; |
michael@0 | 85 | |
michael@0 | 86 | // Whether this range is expected to be stored successfully or not |
michael@0 | 87 | bool expect_storable; |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | // A RangeTestSet encompasses multiple RangeTests, which are run in |
michael@0 | 92 | // sequence on the same RangeMap. |
michael@0 | 93 | struct RangeTestSet { |
michael@0 | 94 | // An array of RangeTests |
michael@0 | 95 | const RangeTest *range_tests; |
michael@0 | 96 | |
michael@0 | 97 | // The number of tests in the set |
michael@0 | 98 | unsigned int range_test_count; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | |
michael@0 | 102 | // StoreTest uses the data in a RangeTest and calls StoreRange on the |
michael@0 | 103 | // test RangeMap. It returns true if the expected result occurred, and |
michael@0 | 104 | // false if something else happened. |
michael@0 | 105 | static bool StoreTest(TestMap *range_map, const RangeTest *range_test) { |
michael@0 | 106 | linked_ptr<CountedObject> object(new CountedObject(range_test->id)); |
michael@0 | 107 | bool stored = range_map->StoreRange(range_test->address, |
michael@0 | 108 | range_test->size, |
michael@0 | 109 | object); |
michael@0 | 110 | |
michael@0 | 111 | if (stored != range_test->expect_storable) { |
michael@0 | 112 | fprintf(stderr, "FAILED: " |
michael@0 | 113 | "StoreRange id %d, expected %s, observed %s\n", |
michael@0 | 114 | range_test->id, |
michael@0 | 115 | range_test->expect_storable ? "storable" : "not storable", |
michael@0 | 116 | stored ? "stored" : "not stored"); |
michael@0 | 117 | return false; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | return true; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | |
michael@0 | 124 | // RetrieveTest uses the data in RangeTest and calls RetrieveRange on the |
michael@0 | 125 | // test RangeMap. If it retrieves the expected value (which can be no |
michael@0 | 126 | // map entry at the specified range,) it returns true, otherwise, it returns |
michael@0 | 127 | // false. RetrieveTest will check the values around the base address and |
michael@0 | 128 | // the high address of a range to guard against off-by-one errors. |
michael@0 | 129 | static bool RetrieveTest(TestMap *range_map, const RangeTest *range_test) { |
michael@0 | 130 | for (unsigned int side = 0; side <= 1; ++side) { |
michael@0 | 131 | // When side == 0, check the low side (base address) of each range. |
michael@0 | 132 | // When side == 1, check the high side (base + size) of each range. |
michael@0 | 133 | |
michael@0 | 134 | // Check one-less and one-greater than the target address in addition |
michael@0 | 135 | // to the target address itself. |
michael@0 | 136 | |
michael@0 | 137 | // If the size of the range is only 1, don't check one greater than |
michael@0 | 138 | // the base or one less than the high - for a successfully stored |
michael@0 | 139 | // range, these tests would erroneously fail because the range is too |
michael@0 | 140 | // small. |
michael@0 | 141 | AddressType low_offset = -1; |
michael@0 | 142 | AddressType high_offset = 1; |
michael@0 | 143 | if (range_test->size == 1) { |
michael@0 | 144 | if (!side) // When checking the low side, |
michael@0 | 145 | high_offset = 0; // don't check one over the target. |
michael@0 | 146 | else // When checking the high side, |
michael@0 | 147 | low_offset = 0; // don't check one under the target. |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | for (AddressType offset = low_offset; offset <= high_offset; ++offset) { |
michael@0 | 151 | AddressType address = |
michael@0 | 152 | offset + |
michael@0 | 153 | (!side ? range_test->address : |
michael@0 | 154 | range_test->address + range_test->size - 1); |
michael@0 | 155 | |
michael@0 | 156 | bool expected_result = false; // This is correct for tests not stored. |
michael@0 | 157 | if (range_test->expect_storable) { |
michael@0 | 158 | if (offset == 0) // When checking the target address, |
michael@0 | 159 | expected_result = true; // test should always succeed. |
michael@0 | 160 | else if (offset == -1) // When checking one below the target, |
michael@0 | 161 | expected_result = side; // should fail low and succeed high. |
michael@0 | 162 | else // When checking one above the target, |
michael@0 | 163 | expected_result = !side; // should succeed low and fail high. |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | linked_ptr<CountedObject> object; |
michael@0 | 167 | AddressType retrieved_base = AddressType(); |
michael@0 | 168 | AddressType retrieved_size = AddressType(); |
michael@0 | 169 | bool retrieved = range_map->RetrieveRange(address, &object, |
michael@0 | 170 | &retrieved_base, |
michael@0 | 171 | &retrieved_size); |
michael@0 | 172 | |
michael@0 | 173 | bool observed_result = retrieved && object->id() == range_test->id; |
michael@0 | 174 | |
michael@0 | 175 | if (observed_result != expected_result) { |
michael@0 | 176 | fprintf(stderr, "FAILED: " |
michael@0 | 177 | "RetrieveRange id %d, side %d, offset %d, " |
michael@0 | 178 | "expected %s, observed %s\n", |
michael@0 | 179 | range_test->id, |
michael@0 | 180 | side, |
michael@0 | 181 | offset, |
michael@0 | 182 | expected_result ? "true" : "false", |
michael@0 | 183 | observed_result ? "true" : "false"); |
michael@0 | 184 | return false; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | // If a range was successfully retrieved, check that the returned |
michael@0 | 188 | // bounds match the range as stored. |
michael@0 | 189 | if (observed_result == true && |
michael@0 | 190 | (retrieved_base != range_test->address || |
michael@0 | 191 | retrieved_size != range_test->size)) { |
michael@0 | 192 | fprintf(stderr, "FAILED: " |
michael@0 | 193 | "RetrieveRange id %d, side %d, offset %d, " |
michael@0 | 194 | "expected base/size %d/%d, observed %d/%d\n", |
michael@0 | 195 | range_test->id, |
michael@0 | 196 | side, |
michael@0 | 197 | offset, |
michael@0 | 198 | range_test->address, range_test->size, |
michael@0 | 199 | retrieved_base, retrieved_size); |
michael@0 | 200 | return false; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | // Now, check RetrieveNearestRange. The nearest range is always |
michael@0 | 204 | // expected to be different from the test range when checking one |
michael@0 | 205 | // less than the low side. |
michael@0 | 206 | bool expected_nearest = range_test->expect_storable; |
michael@0 | 207 | if (!side && offset < 0) |
michael@0 | 208 | expected_nearest = false; |
michael@0 | 209 | |
michael@0 | 210 | linked_ptr<CountedObject> nearest_object; |
michael@0 | 211 | AddressType nearest_base = AddressType(); |
michael@0 | 212 | AddressType nearest_size = AddressType(); |
michael@0 | 213 | bool retrieved_nearest = range_map->RetrieveNearestRange(address, |
michael@0 | 214 | &nearest_object, |
michael@0 | 215 | &nearest_base, |
michael@0 | 216 | &nearest_size); |
michael@0 | 217 | |
michael@0 | 218 | // When checking one greater than the high side, RetrieveNearestRange |
michael@0 | 219 | // should usually return the test range. When a different range begins |
michael@0 | 220 | // at that address, though, then RetrieveNearestRange should return the |
michael@0 | 221 | // range at the address instead of the test range. |
michael@0 | 222 | if (side && offset > 0 && nearest_base == address) { |
michael@0 | 223 | expected_nearest = false; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | bool observed_nearest = retrieved_nearest && |
michael@0 | 227 | nearest_object->id() == range_test->id; |
michael@0 | 228 | |
michael@0 | 229 | if (observed_nearest != expected_nearest) { |
michael@0 | 230 | fprintf(stderr, "FAILED: " |
michael@0 | 231 | "RetrieveNearestRange id %d, side %d, offset %d, " |
michael@0 | 232 | "expected %s, observed %s\n", |
michael@0 | 233 | range_test->id, |
michael@0 | 234 | side, |
michael@0 | 235 | offset, |
michael@0 | 236 | expected_nearest ? "true" : "false", |
michael@0 | 237 | observed_nearest ? "true" : "false"); |
michael@0 | 238 | return false; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | // If a range was successfully retrieved, check that the returned |
michael@0 | 242 | // bounds match the range as stored. |
michael@0 | 243 | if (expected_nearest && |
michael@0 | 244 | (nearest_base != range_test->address || |
michael@0 | 245 | nearest_size != range_test->size)) { |
michael@0 | 246 | fprintf(stderr, "FAILED: " |
michael@0 | 247 | "RetrieveNearestRange id %d, side %d, offset %d, " |
michael@0 | 248 | "expected base/size %d/%d, observed %d/%d\n", |
michael@0 | 249 | range_test->id, |
michael@0 | 250 | side, |
michael@0 | 251 | offset, |
michael@0 | 252 | range_test->address, range_test->size, |
michael@0 | 253 | nearest_base, nearest_size); |
michael@0 | 254 | return false; |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | return true; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | |
michael@0 | 263 | // Test RetrieveRangeAtIndex, which is supposed to return objects in order |
michael@0 | 264 | // according to their addresses. This test is performed by looping through |
michael@0 | 265 | // the map, calling RetrieveRangeAtIndex for all possible indices in sequence, |
michael@0 | 266 | // and verifying that each call returns a different object than the previous |
michael@0 | 267 | // call, and that ranges are returned with increasing base addresses. Returns |
michael@0 | 268 | // false if the test fails. |
michael@0 | 269 | static bool RetrieveIndexTest(TestMap *range_map, int set) { |
michael@0 | 270 | linked_ptr<CountedObject> object; |
michael@0 | 271 | CountedObject *last_object = NULL; |
michael@0 | 272 | AddressType last_base = 0; |
michael@0 | 273 | |
michael@0 | 274 | int object_count = range_map->GetCount(); |
michael@0 | 275 | for (int object_index = 0; object_index < object_count; ++object_index) { |
michael@0 | 276 | AddressType base; |
michael@0 | 277 | if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { |
michael@0 | 278 | fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
michael@0 | 279 | "expected success, observed failure\n", |
michael@0 | 280 | set, object_index); |
michael@0 | 281 | return false; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | if (!object.get()) { |
michael@0 | 285 | fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
michael@0 | 286 | "expected object, observed NULL\n", |
michael@0 | 287 | set, object_index); |
michael@0 | 288 | return false; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // It's impossible to do these comparisons unless there's a previous |
michael@0 | 292 | // object to compare against. |
michael@0 | 293 | if (last_object) { |
michael@0 | 294 | // The object must be different from the last one. |
michael@0 | 295 | if (object->id() == last_object->id()) { |
michael@0 | 296 | fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
michael@0 | 297 | "expected different objects, observed same objects (%d)\n", |
michael@0 | 298 | set, object_index, object->id()); |
michael@0 | 299 | return false; |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | // Each object must have a base greater than the previous object's base. |
michael@0 | 303 | if (base <= last_base) { |
michael@0 | 304 | fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
michael@0 | 305 | "expected different bases, observed same bases (%d)\n", |
michael@0 | 306 | set, object_index, base); |
michael@0 | 307 | return false; |
michael@0 | 308 | } |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | last_object = object.get(); |
michael@0 | 312 | last_base = base; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that |
michael@0 | 316 | // are too high. |
michael@0 | 317 | if (range_map->RetrieveRangeAtIndex(object_count, &object, NULL, NULL)) { |
michael@0 | 318 | fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d (too large), " |
michael@0 | 319 | "expected failure, observed success\n", |
michael@0 | 320 | set, object_count); |
michael@0 | 321 | return false; |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | return true; |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | // Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex(). |
michael@0 | 328 | // Bug info: RetrieveRangeAtIndex() previously retrieves the high address of |
michael@0 | 329 | // entry, however, it is supposed to retrieve the base address of entry as |
michael@0 | 330 | // stated in the comment in range_map.h. |
michael@0 | 331 | static bool RetriveAtIndexTest2() { |
michael@0 | 332 | scoped_ptr<TestMap> range_map(new TestMap()); |
michael@0 | 333 | |
michael@0 | 334 | // Store ranges with base address = 2 * object_id: |
michael@0 | 335 | const int range_size = 2; |
michael@0 | 336 | for (int object_id = 0; object_id < 100; ++object_id) { |
michael@0 | 337 | linked_ptr<CountedObject> object(new CountedObject(object_id)); |
michael@0 | 338 | int base_address = 2 * object_id; |
michael@0 | 339 | range_map->StoreRange(base_address, range_size, object); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | linked_ptr<CountedObject> object; |
michael@0 | 343 | int object_count = range_map->GetCount(); |
michael@0 | 344 | for (int object_index = 0; object_index < object_count; ++object_index) { |
michael@0 | 345 | AddressType base; |
michael@0 | 346 | if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { |
michael@0 | 347 | fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, " |
michael@0 | 348 | "expected success, observed failure\n", object_index); |
michael@0 | 349 | return false; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | int expected_base = 2 * object->id(); |
michael@0 | 353 | if (base != expected_base) { |
michael@0 | 354 | fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, " |
michael@0 | 355 | "expected base %d, observed base %d", |
michael@0 | 356 | object_index, expected_base, base); |
michael@0 | 357 | return false; |
michael@0 | 358 | } |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | return true; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | // RunTests runs a series of test sets. |
michael@0 | 366 | static bool RunTests() { |
michael@0 | 367 | // These tests will be run sequentially. The first set of tests exercises |
michael@0 | 368 | // most functions of RangeTest, and verifies all of the bounds-checking. |
michael@0 | 369 | const RangeTest range_tests_0[] = { |
michael@0 | 370 | { INT_MIN, 16, 1, true }, // lowest possible range |
michael@0 | 371 | { -2, 5, 2, true }, // a range through zero |
michael@0 | 372 | { INT_MAX - 9, 11, 3, false }, // tests anti-overflow |
michael@0 | 373 | { INT_MAX - 9, 10, 4, true }, // highest possible range |
michael@0 | 374 | { 5, 0, 5, false }, // tests anti-zero-size |
michael@0 | 375 | { 5, 1, 6, true }, // smallest possible range |
michael@0 | 376 | { -20, 15, 7, true }, // entirely negative |
michael@0 | 377 | |
michael@0 | 378 | { 10, 10, 10, true }, // causes the following tests to fail |
michael@0 | 379 | { 9, 10, 11, false }, // one-less base, one-less high |
michael@0 | 380 | { 9, 11, 12, false }, // one-less base, identical high |
michael@0 | 381 | { 9, 12, 13, false }, // completely contains existing |
michael@0 | 382 | { 10, 9, 14, false }, // identical base, one-less high |
michael@0 | 383 | { 10, 10, 15, false }, // exactly identical to existing range |
michael@0 | 384 | { 10, 11, 16, false }, // identical base, one-greater high |
michael@0 | 385 | { 11, 8, 17, false }, // contained completely within |
michael@0 | 386 | { 11, 9, 18, false }, // one-greater base, identical high |
michael@0 | 387 | { 11, 10, 19, false }, // one-greater base, one-greater high |
michael@0 | 388 | { 9, 2, 20, false }, // overlaps bottom by one |
michael@0 | 389 | { 10, 1, 21, false }, // overlaps bottom by one, contained |
michael@0 | 390 | { 19, 1, 22, false }, // overlaps top by one, contained |
michael@0 | 391 | { 19, 2, 23, false }, // overlaps top by one |
michael@0 | 392 | |
michael@0 | 393 | { 9, 1, 24, true }, // directly below without overlap |
michael@0 | 394 | { 20, 1, 25, true }, // directly above without overlap |
michael@0 | 395 | |
michael@0 | 396 | { 6, 3, 26, true }, // exactly between two ranges, gapless |
michael@0 | 397 | { 7, 3, 27, false }, // tries to span two ranges |
michael@0 | 398 | { 7, 5, 28, false }, // tries to span three ranges |
michael@0 | 399 | { 4, 20, 29, false }, // tries to contain several ranges |
michael@0 | 400 | |
michael@0 | 401 | { 30, 50, 30, true }, |
michael@0 | 402 | { 90, 25, 31, true }, |
michael@0 | 403 | { 35, 65, 32, false }, // tries to span two noncontiguous |
michael@0 | 404 | { 120, 10000, 33, true }, // > 8-bit |
michael@0 | 405 | { 20000, 20000, 34, true }, // > 8-bit |
michael@0 | 406 | { 0x10001, 0x10001, 35, true }, // > 16-bit |
michael@0 | 407 | |
michael@0 | 408 | { 27, -1, 36, false } // tests high < base |
michael@0 | 409 | }; |
michael@0 | 410 | |
michael@0 | 411 | // Attempt to fill the entire space. The entire space must be filled with |
michael@0 | 412 | // three stores because AddressType is signed for these tests, so RangeMap |
michael@0 | 413 | // treats the size as signed and rejects sizes that appear to be negative. |
michael@0 | 414 | // Even if these tests were run as unsigned, two stores would be needed |
michael@0 | 415 | // to fill the space because the entire size of the space could only be |
michael@0 | 416 | // described by using one more bit than would be present in AddressType. |
michael@0 | 417 | const RangeTest range_tests_1[] = { |
michael@0 | 418 | { INT_MIN, INT_MAX, 50, true }, // From INT_MIN to -2, inclusive |
michael@0 | 419 | { -1, 2, 51, true }, // From -1 to 0, inclusive |
michael@0 | 420 | { 1, INT_MAX, 52, true }, // From 1 to INT_MAX, inclusive |
michael@0 | 421 | { INT_MIN, INT_MAX, 53, false }, // Can't fill the space twice |
michael@0 | 422 | { -1, 2, 54, false }, |
michael@0 | 423 | { 1, INT_MAX, 55, false }, |
michael@0 | 424 | { -3, 6, 56, false }, // -3 to 2, inclusive - spans 3 ranges |
michael@0 | 425 | }; |
michael@0 | 426 | |
michael@0 | 427 | // A light round of testing to verify that RetrieveRange does the right |
michael@0 | 428 | // the right thing at the extremities of the range when nothing is stored |
michael@0 | 429 | // there. Checks are forced without storing anything at the extremities |
michael@0 | 430 | // by setting size = 0. |
michael@0 | 431 | const RangeTest range_tests_2[] = { |
michael@0 | 432 | { INT_MIN, 0, 100, false }, // makes RetrieveRange check low end |
michael@0 | 433 | { -1, 3, 101, true }, |
michael@0 | 434 | { INT_MAX, 0, 102, false }, // makes RetrieveRange check high end |
michael@0 | 435 | }; |
michael@0 | 436 | |
michael@0 | 437 | // Similar to the previous test set, but with a couple of ranges closer |
michael@0 | 438 | // to the extremities. |
michael@0 | 439 | const RangeTest range_tests_3[] = { |
michael@0 | 440 | { INT_MIN + 1, 1, 110, true }, |
michael@0 | 441 | { INT_MAX - 1, 1, 111, true }, |
michael@0 | 442 | { INT_MIN, 0, 112, false }, // makes RetrieveRange check low end |
michael@0 | 443 | { INT_MAX, 0, 113, false } // makes RetrieveRange check high end |
michael@0 | 444 | }; |
michael@0 | 445 | |
michael@0 | 446 | // The range map is cleared between sets of tests listed here. |
michael@0 | 447 | const RangeTestSet range_test_sets[] = { |
michael@0 | 448 | { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) }, |
michael@0 | 449 | { range_tests_1, sizeof(range_tests_1) / sizeof(RangeTest) }, |
michael@0 | 450 | { range_tests_2, sizeof(range_tests_2) / sizeof(RangeTest) }, |
michael@0 | 451 | { range_tests_3, sizeof(range_tests_3) / sizeof(RangeTest) }, |
michael@0 | 452 | { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) } // Run again |
michael@0 | 453 | }; |
michael@0 | 454 | |
michael@0 | 455 | // Maintain the range map in a pointer so that deletion can be meaningfully |
michael@0 | 456 | // tested. |
michael@0 | 457 | scoped_ptr<TestMap> range_map(new TestMap()); |
michael@0 | 458 | |
michael@0 | 459 | // Run all of the test sets in sequence. |
michael@0 | 460 | unsigned int range_test_set_count = sizeof(range_test_sets) / |
michael@0 | 461 | sizeof(RangeTestSet); |
michael@0 | 462 | for (unsigned int range_test_set_index = 0; |
michael@0 | 463 | range_test_set_index < range_test_set_count; |
michael@0 | 464 | ++range_test_set_index) { |
michael@0 | 465 | const RangeTest *range_tests = |
michael@0 | 466 | range_test_sets[range_test_set_index].range_tests; |
michael@0 | 467 | unsigned int range_test_count = |
michael@0 | 468 | range_test_sets[range_test_set_index].range_test_count; |
michael@0 | 469 | |
michael@0 | 470 | // Run the StoreRange test, which validates StoreRange and initializes |
michael@0 | 471 | // the RangeMap with data for the RetrieveRange test. |
michael@0 | 472 | int stored_count = 0; // The number of ranges successfully stored |
michael@0 | 473 | for (unsigned int range_test_index = 0; |
michael@0 | 474 | range_test_index < range_test_count; |
michael@0 | 475 | ++range_test_index) { |
michael@0 | 476 | const RangeTest *range_test = &range_tests[range_test_index]; |
michael@0 | 477 | if (!StoreTest(range_map.get(), range_test)) |
michael@0 | 478 | return false; |
michael@0 | 479 | |
michael@0 | 480 | if (range_test->expect_storable) |
michael@0 | 481 | ++stored_count; |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | // There should be exactly one CountedObject for everything successfully |
michael@0 | 485 | // stored in the RangeMap. |
michael@0 | 486 | if (CountedObject::count() != stored_count) { |
michael@0 | 487 | fprintf(stderr, "FAILED: " |
michael@0 | 488 | "stored object counts don't match, expected %d, observed %d\n", |
michael@0 | 489 | stored_count, |
michael@0 | 490 | CountedObject::count()); |
michael@0 | 491 | |
michael@0 | 492 | return false; |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | // The RangeMap's own count of objects should also match. |
michael@0 | 496 | if (range_map->GetCount() != stored_count) { |
michael@0 | 497 | fprintf(stderr, "FAILED: stored object count doesn't match GetCount, " |
michael@0 | 498 | "expected %d, observed %d\n", |
michael@0 | 499 | stored_count, range_map->GetCount()); |
michael@0 | 500 | |
michael@0 | 501 | return false; |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | // Run the RetrieveRange test |
michael@0 | 505 | for (unsigned int range_test_index = 0; |
michael@0 | 506 | range_test_index < range_test_count; |
michael@0 | 507 | ++range_test_index) { |
michael@0 | 508 | const RangeTest *range_test = &range_tests[range_test_index]; |
michael@0 | 509 | if (!RetrieveTest(range_map.get(), range_test)) |
michael@0 | 510 | return false; |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | if (!RetrieveIndexTest(range_map.get(), range_test_set_index)) |
michael@0 | 514 | return false; |
michael@0 | 515 | |
michael@0 | 516 | // Clear the map between test sets. If this is the final test set, |
michael@0 | 517 | // delete the map instead to test destruction. |
michael@0 | 518 | if (range_test_set_index < range_test_set_count - 1) |
michael@0 | 519 | range_map->Clear(); |
michael@0 | 520 | else |
michael@0 | 521 | range_map.reset(); |
michael@0 | 522 | |
michael@0 | 523 | // Test that all stored objects are freed when the RangeMap is cleared |
michael@0 | 524 | // or deleted. |
michael@0 | 525 | if (CountedObject::count() != 0) { |
michael@0 | 526 | fprintf(stderr, "FAILED: " |
michael@0 | 527 | "did not free all objects after %s, %d still allocated\n", |
michael@0 | 528 | range_test_set_index < range_test_set_count - 1 ? "clear" |
michael@0 | 529 | : "delete", |
michael@0 | 530 | CountedObject::count()); |
michael@0 | 531 | |
michael@0 | 532 | return false; |
michael@0 | 533 | } |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | if (!RetriveAtIndexTest2()) { |
michael@0 | 537 | fprintf(stderr, "FAILED: did not pass RetrieveAtIndexTest2()\n"); |
michael@0 | 538 | return false; |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | return true; |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | |
michael@0 | 545 | } // namespace |
michael@0 | 546 | |
michael@0 | 547 | |
michael@0 | 548 | int main(int argc, char **argv) { |
michael@0 | 549 | BPLOG_INIT(&argc, &argv); |
michael@0 | 550 | |
michael@0 | 551 | return RunTests() ? 0 : 1; |
michael@0 | 552 | } |