| OLD | NEW |
| 1 // Copyright (c) 2010 Google Inc. | 1 // Copyright (c) 2010 Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 expected_result = true; // test should always succeed. | 159 expected_result = true; // test should always succeed. |
| 160 else if (offset == -1) // When checking one below the target, | 160 else if (offset == -1) // When checking one below the target, |
| 161 expected_result = side; // should fail low and succeed high. | 161 expected_result = side; // should fail low and succeed high. |
| 162 else // When checking one above the target, | 162 else // When checking one above the target, |
| 163 expected_result = !side; // should succeed low and fail high. | 163 expected_result = !side; // should succeed low and fail high. |
| 164 } | 164 } |
| 165 | 165 |
| 166 linked_ptr<CountedObject> object; | 166 linked_ptr<CountedObject> object; |
| 167 AddressType retrieved_base = AddressType(); | 167 AddressType retrieved_base = AddressType(); |
| 168 AddressType retrieved_size = AddressType(); | 168 AddressType retrieved_size = AddressType(); |
| 169 AddressType retrieved_delta = AddressType(); |
| 169 bool retrieved = range_map->RetrieveRange(address, &object, | 170 bool retrieved = range_map->RetrieveRange(address, &object, |
| 170 &retrieved_base, | 171 &retrieved_base, |
| 172 &retrieved_delta, |
| 171 &retrieved_size); | 173 &retrieved_size); |
| 172 | 174 |
| 173 bool observed_result = retrieved && object->id() == range_test->id; | 175 bool observed_result = retrieved && object->id() == range_test->id; |
| 174 | 176 |
| 175 if (observed_result != expected_result) { | 177 if (observed_result != expected_result) { |
| 176 fprintf(stderr, "FAILED: " | 178 fprintf(stderr, "FAILED: " |
| 177 "RetrieveRange id %d, side %d, offset %d, " | 179 "RetrieveRange id %d, side %d, offset %d, " |
| 178 "expected %s, observed %s\n", | 180 "expected %s, observed %s\n", |
| 179 range_test->id, | 181 range_test->id, |
| 180 side, | 182 side, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 202 | 204 |
| 203 // Now, check RetrieveNearestRange. The nearest range is always | 205 // Now, check RetrieveNearestRange. The nearest range is always |
| 204 // expected to be different from the test range when checking one | 206 // expected to be different from the test range when checking one |
| 205 // less than the low side. | 207 // less than the low side. |
| 206 bool expected_nearest = range_test->expect_storable; | 208 bool expected_nearest = range_test->expect_storable; |
| 207 if (!side && offset < 0) | 209 if (!side && offset < 0) |
| 208 expected_nearest = false; | 210 expected_nearest = false; |
| 209 | 211 |
| 210 linked_ptr<CountedObject> nearest_object; | 212 linked_ptr<CountedObject> nearest_object; |
| 211 AddressType nearest_base = AddressType(); | 213 AddressType nearest_base = AddressType(); |
| 214 AddressType nearest_delta = AddressType(); |
| 212 AddressType nearest_size = AddressType(); | 215 AddressType nearest_size = AddressType(); |
| 213 bool retrieved_nearest = range_map->RetrieveNearestRange(address, | 216 bool retrieved_nearest = range_map->RetrieveNearestRange(address, |
| 214 &nearest_object, | 217 &nearest_object, |
| 215 &nearest_base, | 218 &nearest_base, |
| 219 &nearest_delta, |
| 216 &nearest_size); | 220 &nearest_size); |
| 217 | 221 |
| 218 // When checking one greater than the high side, RetrieveNearestRange | 222 // When checking one greater than the high side, RetrieveNearestRange |
| 219 // should usually return the test range. When a different range begins | 223 // should usually return the test range. When a different range begins |
| 220 // at that address, though, then RetrieveNearestRange should return the | 224 // at that address, though, then RetrieveNearestRange should return the |
| 221 // range at the address instead of the test range. | 225 // range at the address instead of the test range. |
| 222 if (side && offset > 0 && nearest_base == address) { | 226 if (side && offset > 0 && nearest_base == address) { |
| 223 expected_nearest = false; | 227 expected_nearest = false; |
| 224 } | 228 } |
| 225 | 229 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 // call, and that ranges are returned with increasing base addresses. Returns | 271 // call, and that ranges are returned with increasing base addresses. Returns |
| 268 // false if the test fails. | 272 // false if the test fails. |
| 269 static bool RetrieveIndexTest(TestMap *range_map, int set) { | 273 static bool RetrieveIndexTest(TestMap *range_map, int set) { |
| 270 linked_ptr<CountedObject> object; | 274 linked_ptr<CountedObject> object; |
| 271 CountedObject *last_object = NULL; | 275 CountedObject *last_object = NULL; |
| 272 AddressType last_base = 0; | 276 AddressType last_base = 0; |
| 273 | 277 |
| 274 int object_count = range_map->GetCount(); | 278 int object_count = range_map->GetCount(); |
| 275 for (int object_index = 0; object_index < object_count; ++object_index) { | 279 for (int object_index = 0; object_index < object_count; ++object_index) { |
| 276 AddressType base; | 280 AddressType base; |
| 277 if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { | 281 if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, |
| 282 NULL /* delta */, NULL /* size */)) { |
| 278 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " | 283 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
| 279 "expected success, observed failure\n", | 284 "expected success, observed failure\n", |
| 280 set, object_index); | 285 set, object_index); |
| 281 return false; | 286 return false; |
| 282 } | 287 } |
| 283 | 288 |
| 284 if (!object.get()) { | 289 if (!object.get()) { |
| 285 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " | 290 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d, " |
| 286 "expected object, observed NULL\n", | 291 "expected object, observed NULL\n", |
| 287 set, object_index); | 292 set, object_index); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 307 return false; | 312 return false; |
| 308 } | 313 } |
| 309 } | 314 } |
| 310 | 315 |
| 311 last_object = object.get(); | 316 last_object = object.get(); |
| 312 last_base = base; | 317 last_base = base; |
| 313 } | 318 } |
| 314 | 319 |
| 315 // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that | 320 // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that |
| 316 // are too high. | 321 // are too high. |
| 317 if (range_map->RetrieveRangeAtIndex(object_count, &object, NULL, NULL)) { | 322 if (range_map->RetrieveRangeAtIndex(object_count, &object, NULL /* base */, |
| 323 NULL /* delta */, NULL /* size */)) { |
| 318 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d (too large), " | 324 fprintf(stderr, "FAILED: RetrieveRangeAtIndex set %d index %d (too large), " |
| 319 "expected failure, observed success\n", | 325 "expected failure, observed success\n", |
| 320 set, object_count); | 326 set, object_count); |
| 321 return false; | 327 return false; |
| 322 } | 328 } |
| 323 | 329 |
| 324 return true; | 330 return true; |
| 325 } | 331 } |
| 326 | 332 |
| 327 // Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex(). | 333 // Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex(). |
| 328 // Bug info: RetrieveRangeAtIndex() previously retrieves the high address of | 334 // Bug info: RetrieveRangeAtIndex() previously retrieves the high address of |
| 329 // entry, however, it is supposed to retrieve the base address of entry as | 335 // entry, however, it is supposed to retrieve the base address of entry as |
| 330 // stated in the comment in range_map.h. | 336 // stated in the comment in range_map.h. |
| 331 static bool RetriveAtIndexTest2() { | 337 static bool RetriveAtIndexTest2() { |
| 332 scoped_ptr<TestMap> range_map(new TestMap()); | 338 scoped_ptr<TestMap> range_map(new TestMap()); |
| 333 | 339 |
| 334 // Store ranges with base address = 2 * object_id: | 340 // Store ranges with base address = 2 * object_id: |
| 335 const int range_size = 2; | 341 const int range_size = 2; |
| 336 for (int object_id = 0; object_id < 100; ++object_id) { | 342 for (int object_id = 0; object_id < 100; ++object_id) { |
| 337 linked_ptr<CountedObject> object(new CountedObject(object_id)); | 343 linked_ptr<CountedObject> object(new CountedObject(object_id)); |
| 338 int base_address = 2 * object_id; | 344 int base_address = 2 * object_id; |
| 339 range_map->StoreRange(base_address, range_size, object); | 345 range_map->StoreRange(base_address, range_size, object); |
| 340 } | 346 } |
| 341 | 347 |
| 342 linked_ptr<CountedObject> object; | 348 linked_ptr<CountedObject> object; |
| 343 int object_count = range_map->GetCount(); | 349 int object_count = range_map->GetCount(); |
| 344 for (int object_index = 0; object_index < object_count; ++object_index) { | 350 for (int object_index = 0; object_index < object_count; ++object_index) { |
| 345 AddressType base; | 351 AddressType base; |
| 346 if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { | 352 if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, |
| 353 NULL /* delta */, NULL /* size */)) { |
| 347 fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, " | 354 fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, " |
| 348 "expected success, observed failure\n", object_index); | 355 "expected success, observed failure\n", object_index); |
| 349 return false; | 356 return false; |
| 350 } | 357 } |
| 351 | 358 |
| 352 int expected_base = 2 * object->id(); | 359 int expected_base = 2 * object->id(); |
| 353 if (base != expected_base) { | 360 if (base != expected_base) { |
| 354 fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, " | 361 fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, " |
| 355 "expected base %d, observed base %d", | 362 "expected base %d, observed base %d", |
| 356 object_index, expected_base, base); | 363 object_index, expected_base, base); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 | 550 |
| 544 | 551 |
| 545 } // namespace | 552 } // namespace |
| 546 | 553 |
| 547 | 554 |
| 548 int main(int argc, char **argv) { | 555 int main(int argc, char **argv) { |
| 549 BPLOG_INIT(&argc, &argv); | 556 BPLOG_INIT(&argc, &argv); |
| 550 | 557 |
| 551 return RunTests() ? 0 : 1; | 558 return RunTests() ? 0 : 1; |
| 552 } | 559 } |
| OLD | NEW |