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: // range_map_unittest.cc: Unit tests for RangeMap michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "processor/range_map-inl.h" michael@0: michael@0: #include "common/scoped_ptr.h" michael@0: #include "processor/linked_ptr.h" michael@0: #include "processor/logging.h" michael@0: michael@0: namespace { michael@0: michael@0: michael@0: using google_breakpad::linked_ptr; michael@0: using google_breakpad::scoped_ptr; michael@0: using google_breakpad::RangeMap; michael@0: 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: michael@0: typedef int AddressType; michael@0: typedef RangeMap< AddressType, linked_ptr > TestMap; michael@0: michael@0: michael@0: // RangeTest contains data to use for store and retrieve tests. See michael@0: // RunTests for descriptions of the tests. michael@0: struct RangeTest { michael@0: // Base address to use for test michael@0: AddressType address; michael@0: michael@0: // Size of range to use for test michael@0: AddressType size; michael@0: michael@0: // Unique ID of range - unstorable ranges must have unique IDs too michael@0: int id; michael@0: michael@0: // Whether this range is expected to be stored successfully or not michael@0: bool expect_storable; michael@0: }; michael@0: michael@0: michael@0: // A RangeTestSet encompasses multiple RangeTests, which are run in michael@0: // sequence on the same RangeMap. michael@0: struct RangeTestSet { michael@0: // An array of RangeTests michael@0: const RangeTest *range_tests; michael@0: michael@0: // The number of tests in the set michael@0: unsigned int range_test_count; michael@0: }; michael@0: michael@0: michael@0: // StoreTest uses the data in a RangeTest and calls StoreRange on the michael@0: // test RangeMap. It returns true if the expected result occurred, and michael@0: // false if something else happened. michael@0: static bool StoreTest(TestMap *range_map, const RangeTest *range_test) { michael@0: linked_ptr object(new CountedObject(range_test->id)); michael@0: bool stored = range_map->StoreRange(range_test->address, michael@0: range_test->size, michael@0: object); michael@0: michael@0: if (stored != range_test->expect_storable) { michael@0: fprintf(stderr, "FAILED: " michael@0: "StoreRange id %d, expected %s, observed %s\n", michael@0: range_test->id, michael@0: range_test->expect_storable ? "storable" : "not storable", michael@0: stored ? "stored" : "not stored"); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // RetrieveTest uses the data in RangeTest and calls RetrieveRange on the michael@0: // test RangeMap. If it retrieves the expected value (which can be no michael@0: // map entry at the specified range,) it returns true, otherwise, it returns michael@0: // false. RetrieveTest will check the values around the base address and michael@0: // the high address of a range to guard against off-by-one errors. michael@0: static bool RetrieveTest(TestMap *range_map, const RangeTest *range_test) { michael@0: for (unsigned int side = 0; side <= 1; ++side) { michael@0: // When side == 0, check the low side (base address) of each range. michael@0: // When side == 1, check the high side (base + size) of each range. michael@0: michael@0: // Check one-less and one-greater than the target address in addition michael@0: // to the target address itself. michael@0: michael@0: // If the size of the range is only 1, don't check one greater than michael@0: // the base or one less than the high - for a successfully stored michael@0: // range, these tests would erroneously fail because the range is too michael@0: // small. michael@0: AddressType low_offset = -1; michael@0: AddressType high_offset = 1; michael@0: if (range_test->size == 1) { michael@0: if (!side) // When checking the low side, michael@0: high_offset = 0; // don't check one over the target. michael@0: else // When checking the high side, michael@0: low_offset = 0; // don't check one under the target. michael@0: } michael@0: michael@0: for (AddressType offset = low_offset; offset <= high_offset; ++offset) { michael@0: AddressType address = michael@0: offset + michael@0: (!side ? range_test->address : michael@0: range_test->address + range_test->size - 1); michael@0: michael@0: bool expected_result = false; // This is correct for tests not stored. michael@0: if (range_test->expect_storable) { michael@0: if (offset == 0) // When checking the target address, michael@0: expected_result = true; // test should always succeed. michael@0: else if (offset == -1) // When checking one below the target, michael@0: expected_result = side; // should fail low and succeed high. michael@0: else // When checking one above the target, michael@0: expected_result = !side; // should succeed low and fail high. michael@0: } michael@0: michael@0: linked_ptr object; michael@0: AddressType retrieved_base = AddressType(); michael@0: AddressType retrieved_size = AddressType(); michael@0: bool retrieved = range_map->RetrieveRange(address, &object, michael@0: &retrieved_base, michael@0: &retrieved_size); michael@0: michael@0: bool observed_result = retrieved && object->id() == range_test->id; michael@0: michael@0: if (observed_result != expected_result) { michael@0: fprintf(stderr, "FAILED: " michael@0: "RetrieveRange id %d, side %d, offset %d, " michael@0: "expected %s, observed %s\n", michael@0: range_test->id, michael@0: side, michael@0: offset, michael@0: expected_result ? "true" : "false", michael@0: observed_result ? "true" : "false"); michael@0: return false; michael@0: } michael@0: michael@0: // If a range was successfully retrieved, check that the returned michael@0: // bounds match the range as stored. michael@0: if (observed_result == true && michael@0: (retrieved_base != range_test->address || michael@0: retrieved_size != range_test->size)) { michael@0: fprintf(stderr, "FAILED: " michael@0: "RetrieveRange id %d, side %d, offset %d, " michael@0: "expected base/size %d/%d, observed %d/%d\n", michael@0: range_test->id, michael@0: side, michael@0: offset, michael@0: range_test->address, range_test->size, michael@0: retrieved_base, retrieved_size); michael@0: return false; michael@0: } michael@0: michael@0: // Now, check RetrieveNearestRange. The nearest range is always michael@0: // expected to be different from the test range when checking one michael@0: // less than the low side. michael@0: bool expected_nearest = range_test->expect_storable; michael@0: if (!side && offset < 0) michael@0: expected_nearest = false; michael@0: michael@0: linked_ptr nearest_object; michael@0: AddressType nearest_base = AddressType(); michael@0: AddressType nearest_size = AddressType(); michael@0: bool retrieved_nearest = range_map->RetrieveNearestRange(address, michael@0: &nearest_object, michael@0: &nearest_base, michael@0: &nearest_size); michael@0: michael@0: // When checking one greater than the high side, RetrieveNearestRange michael@0: // should usually return the test range. When a different range begins michael@0: // at that address, though, then RetrieveNearestRange should return the michael@0: // range at the address instead of the test range. michael@0: if (side && offset > 0 && nearest_base == address) { michael@0: expected_nearest = false; michael@0: } michael@0: michael@0: bool observed_nearest = retrieved_nearest && michael@0: nearest_object->id() == range_test->id; michael@0: michael@0: if (observed_nearest != expected_nearest) { michael@0: fprintf(stderr, "FAILED: " michael@0: "RetrieveNearestRange id %d, side %d, offset %d, " michael@0: "expected %s, observed %s\n", michael@0: range_test->id, michael@0: side, michael@0: offset, michael@0: expected_nearest ? "true" : "false", michael@0: observed_nearest ? "true" : "false"); michael@0: return false; michael@0: } michael@0: michael@0: // If a range was successfully retrieved, check that the returned michael@0: // bounds match the range as stored. michael@0: if (expected_nearest && michael@0: (nearest_base != range_test->address || michael@0: nearest_size != range_test->size)) { michael@0: fprintf(stderr, "FAILED: " michael@0: "RetrieveNearestRange id %d, side %d, offset %d, " michael@0: "expected base/size %d/%d, observed %d/%d\n", michael@0: range_test->id, michael@0: side, michael@0: offset, michael@0: range_test->address, range_test->size, michael@0: nearest_base, nearest_size); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // Test RetrieveRangeAtIndex, which is supposed to return objects in order michael@0: // according to their addresses. This test is performed by looping through michael@0: // the map, calling RetrieveRangeAtIndex for all possible indices in sequence, michael@0: // and verifying that each call returns a different object than the previous michael@0: // call, and that ranges are returned with increasing base addresses. Returns michael@0: // false if the test fails. michael@0: static bool RetrieveIndexTest(TestMap *range_map, int set) { michael@0: linked_ptr object; michael@0: CountedObject *last_object = NULL; michael@0: AddressType last_base = 0; michael@0: michael@0: int object_count = range_map->GetCount(); michael@0: for (int object_index = 0; object_index < object_count; ++object_index) { michael@0: AddressType base; michael@0: if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { michael@0: fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " michael@0: "expected success, observed failure\n", michael@0: set, object_index); michael@0: return false; michael@0: } michael@0: michael@0: if (!object.get()) { michael@0: fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " michael@0: "expected object, observed NULL\n", michael@0: set, object_index); michael@0: return false; michael@0: } michael@0: michael@0: // It's impossible to do these comparisons unless there's a previous michael@0: // object to compare against. michael@0: if (last_object) { michael@0: // The object must be different from the last one. michael@0: if (object->id() == last_object->id()) { michael@0: fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " michael@0: "expected different objects, observed same objects (%d)\n", michael@0: set, object_index, object->id()); michael@0: return false; michael@0: } michael@0: michael@0: // Each object must have a base greater than the previous object's base. michael@0: if (base <= last_base) { michael@0: fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " michael@0: "expected different bases, observed same bases (%d)\n", michael@0: set, object_index, base); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: last_object = object.get(); michael@0: last_base = base; michael@0: } michael@0: michael@0: // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that michael@0: // are too high. michael@0: if (range_map->RetrieveRangeAtIndex(object_count, &object, NULL, NULL)) { michael@0: fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d (too large), " michael@0: "expected failure, observed success\n", michael@0: set, object_count); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex(). michael@0: // Bug info: RetrieveRangeAtIndex() previously retrieves the high address of michael@0: // entry, however, it is supposed to retrieve the base address of entry as michael@0: // stated in the comment in range_map.h. michael@0: static bool RetriveAtIndexTest2() { michael@0: scoped_ptr range_map(new TestMap()); michael@0: michael@0: // Store ranges with base address = 2 * object_id: michael@0: const int range_size = 2; michael@0: for (int object_id = 0; object_id < 100; ++object_id) { michael@0: linked_ptr object(new CountedObject(object_id)); michael@0: int base_address = 2 * object_id; michael@0: range_map->StoreRange(base_address, range_size, object); michael@0: } michael@0: michael@0: linked_ptr object; michael@0: int object_count = range_map->GetCount(); michael@0: for (int object_index = 0; object_index < object_count; ++object_index) { michael@0: AddressType base; michael@0: if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { michael@0: fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, " michael@0: "expected success, observed failure\n", object_index); michael@0: return false; michael@0: } michael@0: michael@0: int expected_base = 2 * object->id(); michael@0: if (base != expected_base) { michael@0: fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, " michael@0: "expected base %d, observed base %d", michael@0: object_index, expected_base, base); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // RunTests runs a series of test sets. michael@0: static bool RunTests() { michael@0: // These tests will be run sequentially. The first set of tests exercises michael@0: // most functions of RangeTest, and verifies all of the bounds-checking. michael@0: const RangeTest range_tests_0[] = { michael@0: { INT_MIN, 16, 1, true }, // lowest possible range michael@0: { -2, 5, 2, true }, // a range through zero michael@0: { INT_MAX - 9, 11, 3, false }, // tests anti-overflow michael@0: { INT_MAX - 9, 10, 4, true }, // highest possible range michael@0: { 5, 0, 5, false }, // tests anti-zero-size michael@0: { 5, 1, 6, true }, // smallest possible range michael@0: { -20, 15, 7, true }, // entirely negative michael@0: michael@0: { 10, 10, 10, true }, // causes the following tests to fail michael@0: { 9, 10, 11, false }, // one-less base, one-less high michael@0: { 9, 11, 12, false }, // one-less base, identical high michael@0: { 9, 12, 13, false }, // completely contains existing michael@0: { 10, 9, 14, false }, // identical base, one-less high michael@0: { 10, 10, 15, false }, // exactly identical to existing range michael@0: { 10, 11, 16, false }, // identical base, one-greater high michael@0: { 11, 8, 17, false }, // contained completely within michael@0: { 11, 9, 18, false }, // one-greater base, identical high michael@0: { 11, 10, 19, false }, // one-greater base, one-greater high michael@0: { 9, 2, 20, false }, // overlaps bottom by one michael@0: { 10, 1, 21, false }, // overlaps bottom by one, contained michael@0: { 19, 1, 22, false }, // overlaps top by one, contained michael@0: { 19, 2, 23, false }, // overlaps top by one michael@0: michael@0: { 9, 1, 24, true }, // directly below without overlap michael@0: { 20, 1, 25, true }, // directly above without overlap michael@0: michael@0: { 6, 3, 26, true }, // exactly between two ranges, gapless michael@0: { 7, 3, 27, false }, // tries to span two ranges michael@0: { 7, 5, 28, false }, // tries to span three ranges michael@0: { 4, 20, 29, false }, // tries to contain several ranges michael@0: michael@0: { 30, 50, 30, true }, michael@0: { 90, 25, 31, true }, michael@0: { 35, 65, 32, false }, // tries to span two noncontiguous michael@0: { 120, 10000, 33, true }, // > 8-bit michael@0: { 20000, 20000, 34, true }, // > 8-bit michael@0: { 0x10001, 0x10001, 35, true }, // > 16-bit michael@0: michael@0: { 27, -1, 36, false } // tests high < base michael@0: }; michael@0: michael@0: // Attempt to fill the entire space. The entire space must be filled with michael@0: // three stores because AddressType is signed for these tests, so RangeMap michael@0: // treats the size as signed and rejects sizes that appear to be negative. michael@0: // Even if these tests were run as unsigned, two stores would be needed michael@0: // to fill the space because the entire size of the space could only be michael@0: // described by using one more bit than would be present in AddressType. michael@0: const RangeTest range_tests_1[] = { michael@0: { INT_MIN, INT_MAX, 50, true }, // From INT_MIN to -2, inclusive michael@0: { -1, 2, 51, true }, // From -1 to 0, inclusive michael@0: { 1, INT_MAX, 52, true }, // From 1 to INT_MAX, inclusive michael@0: { INT_MIN, INT_MAX, 53, false }, // Can't fill the space twice michael@0: { -1, 2, 54, false }, michael@0: { 1, INT_MAX, 55, false }, michael@0: { -3, 6, 56, false }, // -3 to 2, inclusive - spans 3 ranges michael@0: }; michael@0: michael@0: // A light round of testing to verify that RetrieveRange does the right michael@0: // the right thing at the extremities of the range when nothing is stored michael@0: // there. Checks are forced without storing anything at the extremities michael@0: // by setting size = 0. michael@0: const RangeTest range_tests_2[] = { michael@0: { INT_MIN, 0, 100, false }, // makes RetrieveRange check low end michael@0: { -1, 3, 101, true }, michael@0: { INT_MAX, 0, 102, false }, // makes RetrieveRange check high end michael@0: }; michael@0: michael@0: // Similar to the previous test set, but with a couple of ranges closer michael@0: // to the extremities. michael@0: const RangeTest range_tests_3[] = { michael@0: { INT_MIN + 1, 1, 110, true }, michael@0: { INT_MAX - 1, 1, 111, true }, michael@0: { INT_MIN, 0, 112, false }, // makes RetrieveRange check low end michael@0: { INT_MAX, 0, 113, false } // makes RetrieveRange check high end michael@0: }; michael@0: michael@0: // The range map is cleared between sets of tests listed here. michael@0: const RangeTestSet range_test_sets[] = { michael@0: { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) }, michael@0: { range_tests_1, sizeof(range_tests_1) / sizeof(RangeTest) }, michael@0: { range_tests_2, sizeof(range_tests_2) / sizeof(RangeTest) }, michael@0: { range_tests_3, sizeof(range_tests_3) / sizeof(RangeTest) }, michael@0: { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) } // Run again michael@0: }; michael@0: michael@0: // Maintain the range map in a pointer so that deletion can be meaningfully michael@0: // tested. michael@0: scoped_ptr range_map(new TestMap()); michael@0: michael@0: // Run all of the test sets in sequence. michael@0: unsigned int range_test_set_count = sizeof(range_test_sets) / michael@0: sizeof(RangeTestSet); michael@0: for (unsigned int range_test_set_index = 0; michael@0: range_test_set_index < range_test_set_count; michael@0: ++range_test_set_index) { michael@0: const RangeTest *range_tests = michael@0: range_test_sets[range_test_set_index].range_tests; michael@0: unsigned int range_test_count = michael@0: range_test_sets[range_test_set_index].range_test_count; michael@0: michael@0: // Run the StoreRange test, which validates StoreRange and initializes michael@0: // the RangeMap with data for the RetrieveRange test. michael@0: int stored_count = 0; // The number of ranges successfully stored michael@0: for (unsigned int range_test_index = 0; michael@0: range_test_index < range_test_count; michael@0: ++range_test_index) { michael@0: const RangeTest *range_test = &range_tests[range_test_index]; michael@0: if (!StoreTest(range_map.get(), range_test)) michael@0: return false; michael@0: michael@0: if (range_test->expect_storable) michael@0: ++stored_count; michael@0: } michael@0: michael@0: // There should be exactly one CountedObject for everything successfully michael@0: // stored in the RangeMap. michael@0: if (CountedObject::count() != stored_count) { michael@0: fprintf(stderr, "FAILED: " michael@0: "stored object counts don't match, expected %d, observed %d\n", michael@0: stored_count, michael@0: CountedObject::count()); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // The RangeMap's own count of objects should also match. michael@0: if (range_map->GetCount() != stored_count) { michael@0: fprintf(stderr, "FAILED: stored object count doesn't match GetCount, " michael@0: "expected %d, observed %d\n", michael@0: stored_count, range_map->GetCount()); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // Run the RetrieveRange test michael@0: for (unsigned int range_test_index = 0; michael@0: range_test_index < range_test_count; michael@0: ++range_test_index) { michael@0: const RangeTest *range_test = &range_tests[range_test_index]; michael@0: if (!RetrieveTest(range_map.get(), range_test)) michael@0: return false; michael@0: } michael@0: michael@0: if (!RetrieveIndexTest(range_map.get(), range_test_set_index)) michael@0: return false; michael@0: michael@0: // Clear the map between test sets. If this is the final test set, michael@0: // delete the map instead to test destruction. michael@0: if (range_test_set_index < range_test_set_count - 1) michael@0: range_map->Clear(); michael@0: else michael@0: range_map.reset(); michael@0: michael@0: // Test that all stored objects are freed when the RangeMap is cleared michael@0: // or deleted. michael@0: if (CountedObject::count() != 0) { michael@0: fprintf(stderr, "FAILED: " michael@0: "did not free all objects after %s, %d still allocated\n", michael@0: range_test_set_index < range_test_set_count - 1 ? "clear" michael@0: : "delete", michael@0: CountedObject::count()); michael@0: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: if (!RetriveAtIndexTest2()) { michael@0: fprintf(stderr, "FAILED: did not pass RetrieveAtIndexTest2()\n"); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: } // namespace michael@0: 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: }