toolkit/crashreporter/google-breakpad/src/processor/range_map_unittest.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/range_map_unittest.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,552 @@
     1.4 +// Copyright (c) 2010 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 +// range_map_unittest.cc: Unit tests for RangeMap
    1.34 +//
    1.35 +// Author: Mark Mentovai
    1.36 +
    1.37 +
    1.38 +#include <limits.h>
    1.39 +#include <stdio.h>
    1.40 +
    1.41 +#include "processor/range_map-inl.h"
    1.42 +
    1.43 +#include "common/scoped_ptr.h"
    1.44 +#include "processor/linked_ptr.h"
    1.45 +#include "processor/logging.h"
    1.46 +
    1.47 +namespace {
    1.48 +
    1.49 +
    1.50 +using google_breakpad::linked_ptr;
    1.51 +using google_breakpad::scoped_ptr;
    1.52 +using google_breakpad::RangeMap;
    1.53 +
    1.54 +
    1.55 +// A CountedObject holds an int.  A global (not thread safe!) count of
    1.56 +// allocated CountedObjects is maintained to help test memory management.
    1.57 +class CountedObject {
    1.58 + public:
    1.59 +  explicit CountedObject(int id) : id_(id) { ++count_; }
    1.60 +  ~CountedObject() { --count_; }
    1.61 +
    1.62 +  static int count() { return count_; }
    1.63 +  int id() const { return id_; }
    1.64 +
    1.65 + private:
    1.66 +  static int count_;
    1.67 +  int id_;
    1.68 +};
    1.69 +
    1.70 +int CountedObject::count_;
    1.71 +
    1.72 +
    1.73 +typedef int AddressType;
    1.74 +typedef RangeMap< AddressType, linked_ptr<CountedObject> > TestMap;
    1.75 +
    1.76 +
    1.77 +// RangeTest contains data to use for store and retrieve tests.  See
    1.78 +// RunTests for descriptions of the tests.
    1.79 +struct RangeTest {
    1.80 +  // Base address to use for test
    1.81 +  AddressType address;
    1.82 +
    1.83 +  // Size of range to use for test
    1.84 +  AddressType size;
    1.85 +
    1.86 +  // Unique ID of range - unstorable ranges must have unique IDs too
    1.87 +  int id;
    1.88 +
    1.89 +  // Whether this range is expected to be stored successfully or not
    1.90 +  bool expect_storable;
    1.91 +};
    1.92 +
    1.93 +
    1.94 +// A RangeTestSet encompasses multiple RangeTests, which are run in
    1.95 +// sequence on the same RangeMap.
    1.96 +struct RangeTestSet {
    1.97 +  // An array of RangeTests
    1.98 +  const RangeTest *range_tests;
    1.99 +
   1.100 +  // The number of tests in the set
   1.101 +  unsigned int range_test_count;
   1.102 +};
   1.103 +
   1.104 +
   1.105 +// StoreTest uses the data in a RangeTest and calls StoreRange on the
   1.106 +// test RangeMap.  It returns true if the expected result occurred, and
   1.107 +// false if something else happened.
   1.108 +static bool StoreTest(TestMap *range_map, const RangeTest *range_test) {
   1.109 +  linked_ptr<CountedObject> object(new CountedObject(range_test->id));
   1.110 +  bool stored = range_map->StoreRange(range_test->address,
   1.111 +                                      range_test->size,
   1.112 +                                      object);
   1.113 +
   1.114 +  if (stored != range_test->expect_storable) {
   1.115 +    fprintf(stderr, "FAILED: "
   1.116 +            "StoreRange id %d, expected %s, observed %s\n",
   1.117 +            range_test->id,
   1.118 +            range_test->expect_storable ? "storable" : "not storable",
   1.119 +            stored ? "stored" : "not stored");
   1.120 +    return false;
   1.121 +  }
   1.122 +
   1.123 +  return true;
   1.124 +}
   1.125 +
   1.126 +
   1.127 +// RetrieveTest uses the data in RangeTest and calls RetrieveRange on the
   1.128 +// test RangeMap.  If it retrieves the expected value (which can be no
   1.129 +// map entry at the specified range,) it returns true, otherwise, it returns
   1.130 +// false.  RetrieveTest will check the values around the base address and
   1.131 +// the high address of a range to guard against off-by-one errors.
   1.132 +static bool RetrieveTest(TestMap *range_map, const RangeTest *range_test) {
   1.133 +  for (unsigned int side = 0; side <= 1; ++side) {
   1.134 +    // When side == 0, check the low side (base address) of each range.
   1.135 +    // When side == 1, check the high side (base + size) of each range.
   1.136 +
   1.137 +    // Check one-less and one-greater than the target address in addition
   1.138 +    // to the target address itself.
   1.139 +
   1.140 +    // If the size of the range is only 1, don't check one greater than
   1.141 +    // the base or one less than the high - for a successfully stored
   1.142 +    // range, these tests would erroneously fail because the range is too
   1.143 +    // small.
   1.144 +    AddressType low_offset = -1;
   1.145 +    AddressType high_offset = 1;
   1.146 +    if (range_test->size == 1) {
   1.147 +      if (!side)          // When checking the low side,
   1.148 +        high_offset = 0;  // don't check one over the target.
   1.149 +      else                // When checking the high side,
   1.150 +        low_offset = 0;   // don't check one under the target.
   1.151 +    }
   1.152 +
   1.153 +    for (AddressType offset = low_offset; offset <= high_offset; ++offset) {
   1.154 +      AddressType address =
   1.155 +          offset +
   1.156 +          (!side ? range_test->address :
   1.157 +                   range_test->address + range_test->size - 1);
   1.158 +
   1.159 +      bool expected_result = false;  // This is correct for tests not stored.
   1.160 +      if (range_test->expect_storable) {
   1.161 +        if (offset == 0)             // When checking the target address,
   1.162 +          expected_result = true;    // test should always succeed.
   1.163 +        else if (offset == -1)       // When checking one below the target,
   1.164 +          expected_result = side;    // should fail low and succeed high.
   1.165 +        else                         // When checking one above the target,
   1.166 +          expected_result = !side;   // should succeed low and fail high.
   1.167 +      }
   1.168 +
   1.169 +      linked_ptr<CountedObject> object;
   1.170 +      AddressType retrieved_base = AddressType();
   1.171 +      AddressType retrieved_size = AddressType();
   1.172 +      bool retrieved = range_map->RetrieveRange(address, &object,
   1.173 +                                                &retrieved_base,
   1.174 +                                                &retrieved_size);
   1.175 +
   1.176 +      bool observed_result = retrieved && object->id() == range_test->id;
   1.177 +
   1.178 +      if (observed_result != expected_result) {
   1.179 +        fprintf(stderr, "FAILED: "
   1.180 +                        "RetrieveRange id %d, side %d, offset %d, "
   1.181 +                        "expected %s, observed %s\n",
   1.182 +                        range_test->id,
   1.183 +                        side,
   1.184 +                        offset,
   1.185 +                        expected_result ? "true" : "false",
   1.186 +                        observed_result ? "true" : "false");
   1.187 +        return false;
   1.188 +      }
   1.189 +
   1.190 +      // If a range was successfully retrieved, check that the returned
   1.191 +      // bounds match the range as stored.
   1.192 +      if (observed_result == true &&
   1.193 +          (retrieved_base != range_test->address ||
   1.194 +           retrieved_size != range_test->size)) {
   1.195 +        fprintf(stderr, "FAILED: "
   1.196 +                        "RetrieveRange id %d, side %d, offset %d, "
   1.197 +                        "expected base/size %d/%d, observed %d/%d\n",
   1.198 +                        range_test->id,
   1.199 +                        side,
   1.200 +                        offset,
   1.201 +                        range_test->address, range_test->size,
   1.202 +                        retrieved_base, retrieved_size);
   1.203 +        return false;
   1.204 +      }
   1.205 +
   1.206 +      // Now, check RetrieveNearestRange.  The nearest range is always
   1.207 +      // expected to be different from the test range when checking one
   1.208 +      // less than the low side.
   1.209 +      bool expected_nearest = range_test->expect_storable;
   1.210 +      if (!side && offset < 0)
   1.211 +        expected_nearest = false;
   1.212 +
   1.213 +      linked_ptr<CountedObject> nearest_object;
   1.214 +      AddressType nearest_base = AddressType();
   1.215 +      AddressType nearest_size = AddressType();
   1.216 +      bool retrieved_nearest = range_map->RetrieveNearestRange(address,
   1.217 +                                                               &nearest_object,
   1.218 +                                                               &nearest_base,
   1.219 +                                                               &nearest_size);
   1.220 +
   1.221 +      // When checking one greater than the high side, RetrieveNearestRange
   1.222 +      // should usually return the test range.  When a different range begins
   1.223 +      // at that address, though, then RetrieveNearestRange should return the
   1.224 +      // range at the address instead of the test range.
   1.225 +      if (side && offset > 0 && nearest_base == address) {
   1.226 +        expected_nearest = false;
   1.227 +      }
   1.228 +
   1.229 +      bool observed_nearest = retrieved_nearest &&
   1.230 +                              nearest_object->id() == range_test->id;
   1.231 +
   1.232 +      if (observed_nearest != expected_nearest) {
   1.233 +        fprintf(stderr, "FAILED: "
   1.234 +                        "RetrieveNearestRange id %d, side %d, offset %d, "
   1.235 +                        "expected %s, observed %s\n",
   1.236 +                        range_test->id,
   1.237 +                        side,
   1.238 +                        offset,
   1.239 +                        expected_nearest ? "true" : "false",
   1.240 +                        observed_nearest ? "true" : "false");
   1.241 +        return false;
   1.242 +      }
   1.243 +
   1.244 +      // If a range was successfully retrieved, check that the returned
   1.245 +      // bounds match the range as stored.
   1.246 +      if (expected_nearest &&
   1.247 +          (nearest_base != range_test->address ||
   1.248 +           nearest_size != range_test->size)) {
   1.249 +        fprintf(stderr, "FAILED: "
   1.250 +                        "RetrieveNearestRange id %d, side %d, offset %d, "
   1.251 +                        "expected base/size %d/%d, observed %d/%d\n",
   1.252 +                        range_test->id,
   1.253 +                        side,
   1.254 +                        offset,
   1.255 +                        range_test->address, range_test->size,
   1.256 +                        nearest_base, nearest_size);
   1.257 +        return false;
   1.258 +      }
   1.259 +    }
   1.260 +  }
   1.261 +
   1.262 +  return true;
   1.263 +}
   1.264 +
   1.265 +
   1.266 +// Test RetrieveRangeAtIndex, which is supposed to return objects in order
   1.267 +// according to their addresses.  This test is performed by looping through
   1.268 +// the map, calling RetrieveRangeAtIndex for all possible indices in sequence,
   1.269 +// and verifying that each call returns a different object than the previous
   1.270 +// call, and that ranges are returned with increasing base addresses.  Returns
   1.271 +// false if the test fails.
   1.272 +static bool RetrieveIndexTest(TestMap *range_map, int set) {
   1.273 +  linked_ptr<CountedObject> object;
   1.274 +  CountedObject *last_object = NULL;
   1.275 +  AddressType last_base = 0;
   1.276 +
   1.277 +  int object_count = range_map->GetCount();
   1.278 +  for (int object_index = 0; object_index < object_count; ++object_index) {
   1.279 +    AddressType base;
   1.280 +    if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) {
   1.281 +      fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, "
   1.282 +              "expected success, observed failure\n",
   1.283 +              set, object_index);
   1.284 +      return false;
   1.285 +    }
   1.286 +
   1.287 +    if (!object.get()) {
   1.288 +      fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, "
   1.289 +              "expected object, observed NULL\n",
   1.290 +              set, object_index);
   1.291 +      return false;
   1.292 +    }
   1.293 +
   1.294 +    // It's impossible to do these comparisons unless there's a previous
   1.295 +    // object to compare against.
   1.296 +    if (last_object) {
   1.297 +      // The object must be different from the last one.
   1.298 +      if (object->id() == last_object->id()) {
   1.299 +        fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, "
   1.300 +                "expected different objects, observed same objects (%d)\n",
   1.301 +                set, object_index, object->id());
   1.302 +        return false;
   1.303 +      }
   1.304 +
   1.305 +      // Each object must have a base greater than the previous object's base.
   1.306 +      if (base <= last_base) {
   1.307 +        fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, "
   1.308 +                "expected different bases, observed same bases (%d)\n",
   1.309 +                set, object_index, base);
   1.310 +        return false;
   1.311 +      }
   1.312 +    }
   1.313 +
   1.314 +    last_object = object.get();
   1.315 +    last_base = base;
   1.316 +  }
   1.317 +
   1.318 +  // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that
   1.319 +  // are too high.
   1.320 +  if (range_map->RetrieveRangeAtIndex(object_count, &object, NULL, NULL)) {
   1.321 +    fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d (too large), "
   1.322 +            "expected failure, observed success\n",
   1.323 +            set, object_count);
   1.324 +    return false;
   1.325 +  }
   1.326 +
   1.327 +  return true;
   1.328 +}
   1.329 +
   1.330 +// Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex().
   1.331 +// Bug info: RetrieveRangeAtIndex() previously retrieves the high address of
   1.332 +// entry, however, it is supposed to retrieve the base address of entry as
   1.333 +// stated in the comment in range_map.h.
   1.334 +static bool RetriveAtIndexTest2() {
   1.335 +  scoped_ptr<TestMap> range_map(new TestMap());
   1.336 +
   1.337 +  // Store ranges with base address = 2 * object_id:
   1.338 +  const int range_size = 2;
   1.339 +  for (int object_id = 0; object_id < 100; ++object_id) {
   1.340 +    linked_ptr<CountedObject> object(new CountedObject(object_id));
   1.341 +    int base_address = 2 * object_id;
   1.342 +    range_map->StoreRange(base_address, range_size, object);
   1.343 +  }
   1.344 +
   1.345 +  linked_ptr<CountedObject> object;
   1.346 +  int object_count = range_map->GetCount();
   1.347 +  for (int object_index = 0; object_index < object_count; ++object_index) {
   1.348 +    AddressType base;
   1.349 +    if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) {
   1.350 +      fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, "
   1.351 +              "expected success, observed failure\n", object_index);
   1.352 +      return false;
   1.353 +    }
   1.354 +
   1.355 +    int expected_base = 2 * object->id();
   1.356 +    if (base != expected_base) {
   1.357 +      fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, "
   1.358 +              "expected base %d, observed base %d",
   1.359 +              object_index, expected_base, base);
   1.360 +      return false;
   1.361 +    }
   1.362 +  }
   1.363 +
   1.364 +  return true;
   1.365 +}
   1.366 +
   1.367 +
   1.368 +// RunTests runs a series of test sets.
   1.369 +static bool RunTests() {
   1.370 +  // These tests will be run sequentially.  The first set of tests exercises
   1.371 +  // most functions of RangeTest, and verifies all of the bounds-checking.
   1.372 +  const RangeTest range_tests_0[] = {
   1.373 +    { INT_MIN,     16,      1,  true },   // lowest possible range
   1.374 +    { -2,          5,       2,  true },   // a range through zero
   1.375 +    { INT_MAX - 9, 11,      3,  false },  // tests anti-overflow
   1.376 +    { INT_MAX - 9, 10,      4,  true },   // highest possible range
   1.377 +    { 5,           0,       5,  false },  // tests anti-zero-size
   1.378 +    { 5,           1,       6,  true },   // smallest possible range
   1.379 +    { -20,         15,      7,  true },   // entirely negative
   1.380 +
   1.381 +    { 10,          10,      10, true },   // causes the following tests to fail
   1.382 +    { 9,           10,      11, false },  // one-less base, one-less high
   1.383 +    { 9,           11,      12, false },  // one-less base, identical high
   1.384 +    { 9,           12,      13, false },  // completely contains existing
   1.385 +    { 10,          9,       14, false },  // identical base, one-less high
   1.386 +    { 10,          10,      15, false },  // exactly identical to existing range
   1.387 +    { 10,          11,      16, false },  // identical base, one-greater high
   1.388 +    { 11,          8,       17, false },  // contained completely within
   1.389 +    { 11,          9,       18, false },  // one-greater base, identical high
   1.390 +    { 11,          10,      19, false },  // one-greater base, one-greater high
   1.391 +    { 9,           2,       20, false },  // overlaps bottom by one
   1.392 +    { 10,          1,       21, false },  // overlaps bottom by one, contained
   1.393 +    { 19,          1,       22, false },  // overlaps top by one, contained
   1.394 +    { 19,          2,       23, false },  // overlaps top by one
   1.395 +
   1.396 +    { 9,           1,       24, true },   // directly below without overlap
   1.397 +    { 20,          1,       25, true },   // directly above without overlap
   1.398 +
   1.399 +    { 6,           3,       26, true },   // exactly between two ranges, gapless
   1.400 +    { 7,           3,       27, false },  // tries to span two ranges
   1.401 +    { 7,           5,       28, false },  // tries to span three ranges
   1.402 +    { 4,           20,      29, false },  // tries to contain several ranges
   1.403 +
   1.404 +    { 30,          50,      30, true },
   1.405 +    { 90,          25,      31, true },
   1.406 +    { 35,          65,      32, false },  // tries to span two noncontiguous
   1.407 +    { 120,         10000,   33, true },   // > 8-bit
   1.408 +    { 20000,       20000,   34, true },   // > 8-bit
   1.409 +    { 0x10001,     0x10001, 35, true },   // > 16-bit
   1.410 +
   1.411 +    { 27,          -1,      36, false }   // tests high < base
   1.412 +  };
   1.413 +
   1.414 +  // Attempt to fill the entire space.  The entire space must be filled with
   1.415 +  // three stores because AddressType is signed for these tests, so RangeMap
   1.416 +  // treats the size as signed and rejects sizes that appear to be negative.
   1.417 +  // Even if these tests were run as unsigned, two stores would be needed
   1.418 +  // to fill the space because the entire size of the space could only be
   1.419 +  // described by using one more bit than would be present in AddressType.
   1.420 +  const RangeTest range_tests_1[] = {
   1.421 +    { INT_MIN, INT_MAX, 50, true },   // From INT_MIN to -2, inclusive
   1.422 +    { -1,      2,       51, true },   // From -1 to 0, inclusive
   1.423 +    { 1,       INT_MAX, 52, true },   // From 1 to INT_MAX, inclusive
   1.424 +    { INT_MIN, INT_MAX, 53, false },  // Can't fill the space twice
   1.425 +    { -1,      2,       54, false },
   1.426 +    { 1,       INT_MAX, 55, false },
   1.427 +    { -3,      6,       56, false },  // -3 to 2, inclusive - spans 3 ranges
   1.428 +  };
   1.429 +
   1.430 +  // A light round of testing to verify that RetrieveRange does the right
   1.431 +  // the right thing at the extremities of the range when nothing is stored
   1.432 +  // there.  Checks are forced without storing anything at the extremities
   1.433 +  // by setting size = 0.
   1.434 +  const RangeTest range_tests_2[] = {
   1.435 +    { INT_MIN, 0, 100, false },  // makes RetrieveRange check low end
   1.436 +    { -1,      3, 101, true },
   1.437 +    { INT_MAX, 0, 102, false },  // makes RetrieveRange check high end
   1.438 +  };
   1.439 +
   1.440 +  // Similar to the previous test set, but with a couple of ranges closer
   1.441 +  // to the extremities.
   1.442 +  const RangeTest range_tests_3[] = {
   1.443 +    { INT_MIN + 1, 1, 110, true },
   1.444 +    { INT_MAX - 1, 1, 111, true },
   1.445 +    { INT_MIN,     0, 112, false },  // makes RetrieveRange check low end
   1.446 +    { INT_MAX,     0, 113, false }   // makes RetrieveRange check high end
   1.447 +  };
   1.448 +
   1.449 +  // The range map is cleared between sets of tests listed here.
   1.450 +  const RangeTestSet range_test_sets[] = {
   1.451 +    { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) },
   1.452 +    { range_tests_1, sizeof(range_tests_1) / sizeof(RangeTest) },
   1.453 +    { range_tests_2, sizeof(range_tests_2) / sizeof(RangeTest) },
   1.454 +    { range_tests_3, sizeof(range_tests_3) / sizeof(RangeTest) },
   1.455 +    { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) }   // Run again
   1.456 +  };
   1.457 +
   1.458 +  // Maintain the range map in a pointer so that deletion can be meaningfully
   1.459 +  // tested.
   1.460 +  scoped_ptr<TestMap> range_map(new TestMap());
   1.461 +
   1.462 +  // Run all of the test sets in sequence.
   1.463 +  unsigned int range_test_set_count = sizeof(range_test_sets) /
   1.464 +                                      sizeof(RangeTestSet);
   1.465 +  for (unsigned int range_test_set_index = 0;
   1.466 +       range_test_set_index < range_test_set_count;
   1.467 +       ++range_test_set_index) {
   1.468 +    const RangeTest *range_tests =
   1.469 +        range_test_sets[range_test_set_index].range_tests;
   1.470 +    unsigned int range_test_count =
   1.471 +        range_test_sets[range_test_set_index].range_test_count;
   1.472 +
   1.473 +    // Run the StoreRange test, which validates StoreRange and initializes
   1.474 +    // the RangeMap with data for the RetrieveRange test.
   1.475 +    int stored_count = 0;  // The number of ranges successfully stored
   1.476 +    for (unsigned int range_test_index = 0;
   1.477 +         range_test_index < range_test_count;
   1.478 +         ++range_test_index) {
   1.479 +      const RangeTest *range_test = &range_tests[range_test_index];
   1.480 +      if (!StoreTest(range_map.get(), range_test))
   1.481 +        return false;
   1.482 +
   1.483 +      if (range_test->expect_storable)
   1.484 +        ++stored_count;
   1.485 +    }
   1.486 +
   1.487 +    // There should be exactly one CountedObject for everything successfully
   1.488 +    // stored in the RangeMap.
   1.489 +    if (CountedObject::count() != stored_count) {
   1.490 +      fprintf(stderr, "FAILED: "
   1.491 +              "stored object counts don't match, expected %d, observed %d\n",
   1.492 +              stored_count,
   1.493 +              CountedObject::count());
   1.494 +
   1.495 +      return false;
   1.496 +    }
   1.497 +
   1.498 +    // The RangeMap's own count of objects should also match.
   1.499 +    if (range_map->GetCount() != stored_count) {
   1.500 +      fprintf(stderr, "FAILED: stored object count doesn't match GetCount, "
   1.501 +              "expected %d, observed %d\n",
   1.502 +              stored_count, range_map->GetCount());
   1.503 +
   1.504 +      return false;
   1.505 +    }
   1.506 +
   1.507 +    // Run the RetrieveRange test
   1.508 +    for (unsigned int range_test_index = 0;
   1.509 +         range_test_index < range_test_count;
   1.510 +         ++range_test_index) {
   1.511 +      const RangeTest *range_test = &range_tests[range_test_index];
   1.512 +      if (!RetrieveTest(range_map.get(), range_test))
   1.513 +        return false;
   1.514 +    }
   1.515 +
   1.516 +    if (!RetrieveIndexTest(range_map.get(), range_test_set_index))
   1.517 +      return false;
   1.518 +
   1.519 +    // Clear the map between test sets.  If this is the final test set,
   1.520 +    // delete the map instead to test destruction.
   1.521 +    if (range_test_set_index < range_test_set_count - 1)
   1.522 +      range_map->Clear();
   1.523 +    else
   1.524 +      range_map.reset();
   1.525 +
   1.526 +    // Test that all stored objects are freed when the RangeMap is cleared
   1.527 +    // or deleted.
   1.528 +    if (CountedObject::count() != 0) {
   1.529 +      fprintf(stderr, "FAILED: "
   1.530 +              "did not free all objects after %s, %d still allocated\n",
   1.531 +              range_test_set_index < range_test_set_count - 1 ? "clear"
   1.532 +                                                              : "delete",
   1.533 +              CountedObject::count());
   1.534 +
   1.535 +      return false;
   1.536 +    }
   1.537 +  }
   1.538 +
   1.539 +  if (!RetriveAtIndexTest2()) {
   1.540 +    fprintf(stderr, "FAILED: did not pass RetrieveAtIndexTest2()\n");
   1.541 +    return false;
   1.542 +  }
   1.543 +
   1.544 +  return true;
   1.545 +}
   1.546 +
   1.547 +
   1.548 +}  // namespace
   1.549 +
   1.550 +
   1.551 +int main(int argc, char **argv) {
   1.552 +  BPLOG_INIT(&argc, &argv);
   1.553 +
   1.554 +  return RunTests() ? 0 : 1;
   1.555 +}

mercurial