Fix a bug in range_map-inl.h and add a unittest to expose the exisiting bug.

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@683 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
SiyangXie@gmail.com 2010-09-15 01:11:34 +00:00
parent 9fc5812260
commit b5b5f9e520
2 changed files with 43 additions and 1 deletions

View file

@ -184,7 +184,7 @@ bool RangeMap<AddressType, EntryType>::RetrieveRangeAtIndex(
*entry = iterator->second.entry();
if (entry_base)
*entry_base = iterator->first;
*entry_base = iterator->second.base();
if (entry_size)
*entry_size = iterator->first - iterator->second.base() + 1;

View file

@ -325,6 +325,43 @@ static bool RetrieveIndexTest(TestMap *range_map, int set) {
return true;
}
// Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex().
// Bug info: RetrieveRangeAtIndex() previously retrieves the high address of
// entry, however, it is supposed to retrieve the base address of entry as
// stated in the comment in range_map.h.
static bool RetriveAtIndexTest2() {
scoped_ptr<TestMap> range_map(new TestMap());
// Store ranges with base address = 2 * object_id:
const int range_size = 2;
for (int object_id = 0; object_id < 100; ++object_id) {
linked_ptr<CountedObject> object(new CountedObject(object_id));
int base_address = 2 * object_id;
range_map->StoreRange(base_address, range_size, object);
}
linked_ptr<CountedObject> object;
int object_count = range_map->GetCount();
for (int object_index = 0; object_index < object_count; ++object_index) {
AddressType base;
if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) {
fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, "
"expected success, observed failure\n", object_index);
return false;
}
int expected_base = 2 * object->id();
if (base != expected_base) {
fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, "
"expected base %d, observed base %d",
object_index, expected_base, base);
return false;
}
}
return true;
}
// RunTests runs a series of test sets.
static bool RunTests() {
@ -497,6 +534,11 @@ static bool RunTests() {
}
}
if (!RetriveAtIndexTest2()) {
fprintf(stderr, "FAILED: did not pass RetrieveAtIndexTest2()\n");
return false;
}
return true;
}