PageRenderTime 104ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/processor/static_range_map_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 421 lines | 256 code | 62 blank | 103 comment | 30 complexity | 6b5eaeb31a0702c3bc9c30d3e3e51771 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 2010 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // static_range_map_unittest.cc: Unit tests for StaticRangeMap.
  30. //
  31. // Author: Siyang Xie (lambxsy@google.com)
  32. #include "breakpad_googletest_includes.h"
  33. #include "processor/range_map-inl.h"
  34. #include "processor/static_range_map-inl.h"
  35. #include "processor/simple_serializer-inl.h"
  36. #include "processor/map_serializers-inl.h"
  37. #include "processor/logging.h"
  38. #include "processor/scoped_ptr.h"
  39. namespace {
  40. // Types used for testing.
  41. typedef int AddressType;
  42. typedef int EntryType;
  43. typedef google_breakpad::StaticRangeMap< AddressType, EntryType > TestMap;
  44. typedef google_breakpad::RangeMap< AddressType, EntryType > RMap;
  45. // RangeTest contains data to use for store and retrieve tests. See
  46. // RunTests for descriptions of the tests.
  47. struct RangeTest {
  48. // Base address to use for test
  49. AddressType address;
  50. // Size of range to use for test
  51. AddressType size;
  52. // Unique ID of range - unstorable ranges must have unique IDs too
  53. EntryType id;
  54. // Whether this range is expected to be stored successfully or not
  55. bool expect_storable;
  56. };
  57. // A RangeTestSet encompasses multiple RangeTests, which are run in
  58. // sequence on the same RangeMap.
  59. struct RangeTestSet {
  60. // An array of RangeTests
  61. const RangeTest* range_tests;
  62. // The number of tests in the set
  63. unsigned int range_test_count;
  64. };
  65. // These tests will be run sequentially. The first set of tests exercises
  66. // most functions of RangeTest, and verifies all of the bounds-checking.
  67. const RangeTest range_tests_0[] = {
  68. { INT_MIN, 16, 1, true }, // lowest possible range
  69. { -2, 5, 2, true }, // a range through zero
  70. { INT_MAX - 9, 11, 3, false }, // tests anti-overflow
  71. { INT_MAX - 9, 10, 4, true }, // highest possible range
  72. { 5, 0, 5, false }, // tests anti-zero-size
  73. { 5, 1, 6, true }, // smallest possible range
  74. { -20, 15, 7, true }, // entirely negative
  75. { 10, 10, 10, true }, // causes the following tests to fail
  76. { 9, 10, 11, false }, // one-less base, one-less high
  77. { 9, 11, 12, false }, // one-less base, identical high
  78. { 9, 12, 13, false }, // completely contains existing
  79. { 10, 9, 14, false }, // identical base, one-less high
  80. { 10, 10, 15, false }, // exactly identical to existing range
  81. { 10, 11, 16, false }, // identical base, one-greater high
  82. { 11, 8, 17, false }, // contained completely within
  83. { 11, 9, 18, false }, // one-greater base, identical high
  84. { 11, 10, 19, false }, // one-greater base, one-greater high
  85. { 9, 2, 20, false }, // overlaps bottom by one
  86. { 10, 1, 21, false }, // overlaps bottom by one, contained
  87. { 19, 1, 22, false }, // overlaps top by one, contained
  88. { 19, 2, 23, false }, // overlaps top by one
  89. { 9, 1, 24, true }, // directly below without overlap
  90. { 20, 1, 25, true }, // directly above without overlap
  91. { 6, 3, 26, true }, // exactly between two ranges, gapless
  92. { 7, 3, 27, false }, // tries to span two ranges
  93. { 7, 5, 28, false }, // tries to span three ranges
  94. { 4, 20, 29, false }, // tries to contain several ranges
  95. { 30, 50, 30, true },
  96. { 90, 25, 31, true },
  97. { 35, 65, 32, false }, // tries to span two noncontiguous
  98. { 120, 10000, 33, true }, // > 8-bit
  99. { 20000, 20000, 34, true }, // > 8-bit
  100. { 0x10001, 0x10001, 35, true }, // > 16-bit
  101. { 27, -1, 36, false } // tests high < base
  102. };
  103. // Attempt to fill the entire space. The entire space must be filled with
  104. // three stores because AddressType is signed for these tests, so RangeMap
  105. // treats the size as signed and rejects sizes that appear to be negative.
  106. // Even if these tests were run as unsigned, two stores would be needed
  107. // to fill the space because the entire size of the space could only be
  108. // described by using one more bit than would be present in AddressType.
  109. const RangeTest range_tests_1[] = {
  110. { INT_MIN, INT_MAX, 50, true }, // From INT_MIN to -2, inclusive
  111. { -1, 2, 51, true }, // From -1 to 0, inclusive
  112. { 1, INT_MAX, 52, true }, // From 1 to INT_MAX, inclusive
  113. { INT_MIN, INT_MAX, 53, false }, // Can't fill the space twice
  114. { -1, 2, 54, false },
  115. { 1, INT_MAX, 55, false },
  116. { -3, 6, 56, false }, // -3 to 2, inclusive - spans 3 ranges
  117. };
  118. // A light round of testing to verify that RetrieveRange does the right
  119. // the right thing at the extremities of the range when nothing is stored
  120. // there. Checks are forced without storing anything at the extremities
  121. // by setting size = 0.
  122. const RangeTest range_tests_2[] = {
  123. { INT_MIN, 0, 100, false }, // makes RetrieveRange check low end
  124. { -1, 3, 101, true },
  125. { INT_MAX, 0, 102, false }, // makes RetrieveRange check high end
  126. };
  127. // Similar to the previous test set, but with a couple of ranges closer
  128. // to the extremities.
  129. const RangeTest range_tests_3[] = {
  130. { INT_MIN + 1, 1, 110, true },
  131. { INT_MAX - 1, 1, 111, true },
  132. { INT_MIN, 0, 112, false }, // makes RetrieveRange check low end
  133. { INT_MAX, 0, 113, false } // makes RetrieveRange check high end
  134. };
  135. // The range map is cleared between sets of tests listed here.
  136. const RangeTestSet range_test_sets[] = {
  137. { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) },
  138. { range_tests_1, sizeof(range_tests_1) / sizeof(RangeTest) },
  139. { range_tests_2, sizeof(range_tests_2) / sizeof(RangeTest) },
  140. { range_tests_3, sizeof(range_tests_3) / sizeof(RangeTest) },
  141. { range_tests_0, sizeof(range_tests_0) / sizeof(RangeTest) } // Run again
  142. };
  143. } // namespace
  144. namespace google_breakpad {
  145. class TestStaticRangeMap : public ::testing::Test {
  146. protected:
  147. void SetUp() {
  148. kTestCasesCount_ = sizeof(range_test_sets) / sizeof(RangeTestSet);
  149. }
  150. // StoreTest uses the data in a RangeTest and calls StoreRange on the
  151. // test RangeMap. It returns true if the expected result occurred, and
  152. // false if something else happened.
  153. void StoreTest(RMap* range_map, const RangeTest* range_test);
  154. // RetrieveTest uses the data in RangeTest and calls RetrieveRange on the
  155. // test RangeMap. If it retrieves the expected value (which can be no
  156. // map entry at the specified range,) it returns true, otherwise, it returns
  157. // false. RetrieveTest will check the values around the base address and
  158. // the high address of a range to guard against off-by-one errors.
  159. void RetrieveTest(TestMap* range_map, const RangeTest* range_test);
  160. // Test RetrieveRangeAtIndex, which is supposed to return objects in order
  161. // according to their addresses. This test is performed by looping through
  162. // the map, calling RetrieveRangeAtIndex for all possible indices in sequence,
  163. // and verifying that each call returns a different object than the previous
  164. // call, and that ranges are returned with increasing base addresses. Returns
  165. // false if the test fails.
  166. void RetrieveIndexTest(const TestMap* range_map, int set);
  167. void RunTestCase(int test_case);
  168. unsigned int kTestCasesCount_;
  169. RangeMapSerializer<AddressType, EntryType> serializer_;
  170. };
  171. void TestStaticRangeMap::StoreTest(RMap* range_map,
  172. const RangeTest* range_test) {
  173. bool stored = range_map->StoreRange(range_test->address,
  174. range_test->size,
  175. range_test->id);
  176. EXPECT_EQ(stored, range_test->expect_storable)
  177. << "StoreRange id " << range_test->id << "FAILED";
  178. }
  179. void TestStaticRangeMap::RetrieveTest(TestMap* range_map,
  180. const RangeTest* range_test) {
  181. for (unsigned int side = 0; side <= 1; ++side) {
  182. // When side == 0, check the low side (base address) of each range.
  183. // When side == 1, check the high side (base + size) of each range.
  184. // Check one-less and one-greater than the target address in addition
  185. // to the target address itself.
  186. // If the size of the range is only 1, don't check one greater than
  187. // the base or one less than the high - for a successfully stored
  188. // range, these tests would erroneously fail because the range is too
  189. // small.
  190. AddressType low_offset = -1;
  191. AddressType high_offset = 1;
  192. if (range_test->size == 1) {
  193. if (!side) // When checking the low side,
  194. high_offset = 0; // don't check one over the target.
  195. else // When checking the high side,
  196. low_offset = 0; // don't check one under the target.
  197. }
  198. for (AddressType offset = low_offset; offset <= high_offset; ++offset) {
  199. AddressType address =
  200. offset +
  201. (!side ? range_test->address :
  202. range_test->address + range_test->size - 1);
  203. bool expected_result = false; // This is correct for tests not stored.
  204. if (range_test->expect_storable) {
  205. if (offset == 0) // When checking the target address,
  206. expected_result = true; // test should always succeed.
  207. else if (offset == -1) // When checking one below the target,
  208. expected_result = side; // should fail low and succeed high.
  209. else // When checking one above the target,
  210. expected_result = !side; // should succeed low and fail high.
  211. }
  212. const EntryType* id;
  213. AddressType retrieved_base;
  214. AddressType retrieved_size;
  215. bool retrieved = range_map->RetrieveRange(address, id,
  216. &retrieved_base,
  217. &retrieved_size);
  218. bool observed_result = retrieved && *id == range_test->id;
  219. EXPECT_EQ(observed_result, expected_result)
  220. << "RetrieveRange id " << range_test->id
  221. << ", side " << side << ", offset " << offset << " FAILED.";
  222. // If a range was successfully retrieved, check that the returned
  223. // bounds match the range as stored.
  224. if (observed_result == true) {
  225. EXPECT_EQ(retrieved_base, range_test->address)
  226. << "RetrieveRange id " << range_test->id
  227. << ", side " << side << ", offset " << offset << " FAILED.";
  228. EXPECT_EQ(retrieved_size, range_test->size)
  229. << "RetrieveRange id " << range_test->id
  230. << ", side " << side << ", offset " << offset << " FAILED.";
  231. }
  232. // Now, check RetrieveNearestRange. The nearest range is always
  233. // expected to be different from the test range when checking one
  234. // less than the low side.
  235. bool expected_nearest = range_test->expect_storable;
  236. if (!side && offset < 0)
  237. expected_nearest = false;
  238. AddressType nearest_base;
  239. AddressType nearest_size;
  240. bool retrieved_nearest = range_map->RetrieveNearestRange(address,
  241. id,
  242. &nearest_base,
  243. &nearest_size);
  244. // When checking one greater than the high side, RetrieveNearestRange
  245. // should usually return the test range. When a different range begins
  246. // at that address, though, then RetrieveNearestRange should return the
  247. // range at the address instead of the test range.
  248. if (side && offset > 0 && nearest_base == address) {
  249. expected_nearest = false;
  250. }
  251. bool observed_nearest = retrieved_nearest &&
  252. *id == range_test->id;
  253. EXPECT_EQ(observed_nearest, expected_nearest)
  254. << "RetrieveRange id " << range_test->id
  255. << ", side " << side << ", offset " << offset << " FAILED.";
  256. // If a range was successfully retrieved, check that the returned
  257. // bounds match the range as stored.
  258. if (expected_nearest ==true) {
  259. EXPECT_EQ(nearest_base, range_test->address)
  260. << "RetrieveRange id " << range_test->id
  261. << ", side " << side << ", offset " << offset << " FAILED.";
  262. EXPECT_EQ(nearest_size, range_test->size)
  263. << "RetrieveRange id " << range_test->id
  264. << ", side " << side << ", offset " << offset << " FAILED.";
  265. }
  266. }
  267. }
  268. }
  269. void TestStaticRangeMap::RetrieveIndexTest(const TestMap* range_map, int set) {
  270. AddressType last_base = 0;
  271. const EntryType* last_entry = 0;
  272. const EntryType* entry;
  273. int object_count = range_map->GetCount();
  274. for (int object_index = 0; object_index < object_count; ++object_index) {
  275. AddressType base;
  276. ASSERT_TRUE(range_map->RetrieveRangeAtIndex(object_index,
  277. entry,
  278. &base,
  279. NULL))
  280. << "FAILED: RetrieveRangeAtIndex set " << set
  281. << " index " << object_index;
  282. ASSERT_TRUE(entry) << "FAILED: RetrieveRangeAtIndex set " << set
  283. << " index " << object_index;
  284. // It's impossible to do these comparisons unless there's a previous
  285. // object to compare against.
  286. if (last_entry) {
  287. // The object must be different from the last_entry one.
  288. EXPECT_NE(*entry, *last_entry) << "FAILED: RetrieveRangeAtIndex set "
  289. << set << " index " << object_index;
  290. // Each object must have a base greater than the previous object's base.
  291. EXPECT_GT(base, last_base) << "FAILED: RetrieveRangeAtIndex set " << set
  292. << " index " << object_index;
  293. }
  294. last_entry = entry;
  295. last_base = base;
  296. }
  297. // Make sure that RetrieveRangeAtIndex doesn't allow lookups at indices that
  298. // are too high.
  299. ASSERT_FALSE(range_map->RetrieveRangeAtIndex(
  300. object_count, entry, NULL, NULL)) << "FAILED: RetrieveRangeAtIndex set "
  301. << set << " index " << object_count
  302. << " (too large)";
  303. }
  304. // RunTests runs a series of test sets.
  305. void TestStaticRangeMap::RunTestCase(int test_case) {
  306. // Maintain the range map in a pointer so that deletion can be meaningfully
  307. // tested.
  308. scoped_ptr<RMap> rmap(new RMap());
  309. const RangeTest* range_tests = range_test_sets[test_case].range_tests;
  310. unsigned int range_test_count = range_test_sets[test_case].range_test_count;
  311. // Run the StoreRange test, which validates StoreRange and initializes
  312. // the RangeMap with data for the RetrieveRange test.
  313. int stored_count = 0; // The number of ranges successfully stored
  314. for (unsigned int range_test_index = 0;
  315. range_test_index < range_test_count;
  316. ++range_test_index) {
  317. const RangeTest* range_test = &range_tests[range_test_index];
  318. StoreTest(rmap.get(), range_test);
  319. if (range_test->expect_storable)
  320. ++stored_count;
  321. }
  322. scoped_array<char> memaddr(serializer_.Serialize(*rmap, NULL));
  323. scoped_ptr<TestMap> static_range_map(new TestMap(memaddr.get()));
  324. // The RangeMap's own count of objects should also match.
  325. EXPECT_EQ(static_range_map->GetCount(), stored_count);
  326. // Run the RetrieveRange test
  327. for (unsigned int range_test_index = 0;
  328. range_test_index < range_test_count;
  329. ++range_test_index) {
  330. const RangeTest* range_test = &range_tests[range_test_index];
  331. RetrieveTest(static_range_map.get(), range_test);
  332. }
  333. RetrieveIndexTest(static_range_map.get(), test_case);
  334. }
  335. TEST_F(TestStaticRangeMap, TestCase0) {
  336. int test_case = 0;
  337. RunTestCase(test_case);
  338. }
  339. TEST_F(TestStaticRangeMap, TestCase1) {
  340. int test_case = 1;
  341. RunTestCase(test_case);
  342. }
  343. TEST_F(TestStaticRangeMap, TestCase2) {
  344. int test_case = 2;
  345. RunTestCase(test_case);
  346. }
  347. TEST_F(TestStaticRangeMap, TestCase3) {
  348. int test_case = 3;
  349. RunTestCase(test_case);
  350. }
  351. TEST_F(TestStaticRangeMap, RunTestCase0Again) {
  352. int test_case = 0;
  353. RunTestCase(test_case);
  354. }
  355. } // namespace google_breakpad
  356. int main(int argc, char *argv[]) {
  357. ::testing::InitGoogleTest(&argc, argv);
  358. return RUN_ALL_TESTS();
  359. }