/thirdparty/breakpad/processor/contained_range_map_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 263 lines · 194 code · 25 blank · 44 comment · 6 complexity · 865f868fbaf6f73f6635552183088f67 MD5 · raw file

  1. // Copyright (c) 2006, 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. // contained_range_map_unittest.cc: Unit tests for ContainedRangeMap
  30. //
  31. // Author: Mark Mentovai
  32. #include <stdio.h>
  33. #include "processor/contained_range_map-inl.h"
  34. #include "processor/logging.h"
  35. #define ASSERT_TRUE(condition) \
  36. if (!(condition)) { \
  37. fprintf(stderr, "FAIL: %s @ %s:%d\n", #condition, __FILE__, __LINE__); \
  38. return false; \
  39. }
  40. #define ASSERT_FALSE(condition) ASSERT_TRUE(!(condition))
  41. namespace {
  42. using google_breakpad::ContainedRangeMap;
  43. static bool RunTests() {
  44. ContainedRangeMap<unsigned int, int> crm;
  45. // First, do the StoreRange tests. This validates the containment
  46. // rules.
  47. ASSERT_TRUE (crm.StoreRange(10, 10, 1));
  48. ASSERT_FALSE(crm.StoreRange(10, 10, 2)); // exactly equal to 1
  49. ASSERT_FALSE(crm.StoreRange(11, 10, 3)); // begins inside 1 and extends up
  50. ASSERT_FALSE(crm.StoreRange( 9, 10, 4)); // begins below 1 and ends inside
  51. ASSERT_TRUE (crm.StoreRange(11, 9, 5)); // contained by existing
  52. ASSERT_TRUE (crm.StoreRange(12, 7, 6));
  53. ASSERT_TRUE (crm.StoreRange( 9, 12, 7)); // contains existing
  54. ASSERT_TRUE (crm.StoreRange( 9, 13, 8));
  55. ASSERT_TRUE (crm.StoreRange( 8, 14, 9));
  56. ASSERT_TRUE (crm.StoreRange(30, 3, 10));
  57. ASSERT_TRUE (crm.StoreRange(33, 3, 11));
  58. ASSERT_TRUE (crm.StoreRange(30, 6, 12)); // storable but totally masked
  59. ASSERT_TRUE (crm.StoreRange(40, 8, 13)); // will be totally masked
  60. ASSERT_TRUE (crm.StoreRange(40, 4, 14));
  61. ASSERT_TRUE (crm.StoreRange(44, 4, 15));
  62. ASSERT_FALSE(crm.StoreRange(32, 10, 16)); // begins in #10, ends in #14
  63. ASSERT_FALSE(crm.StoreRange(50, 0, 17)); // zero length
  64. ASSERT_TRUE (crm.StoreRange(50, 10, 18));
  65. ASSERT_TRUE (crm.StoreRange(50, 1, 19));
  66. ASSERT_TRUE (crm.StoreRange(59, 1, 20));
  67. ASSERT_TRUE (crm.StoreRange(60, 1, 21));
  68. ASSERT_TRUE (crm.StoreRange(69, 1, 22));
  69. ASSERT_TRUE (crm.StoreRange(60, 10, 23));
  70. ASSERT_TRUE (crm.StoreRange(68, 1, 24));
  71. ASSERT_TRUE (crm.StoreRange(61, 1, 25));
  72. ASSERT_TRUE (crm.StoreRange(61, 8, 26));
  73. ASSERT_FALSE(crm.StoreRange(59, 9, 27));
  74. ASSERT_FALSE(crm.StoreRange(59, 10, 28));
  75. ASSERT_FALSE(crm.StoreRange(59, 11, 29));
  76. ASSERT_TRUE (crm.StoreRange(70, 10, 30));
  77. ASSERT_TRUE (crm.StoreRange(74, 2, 31));
  78. ASSERT_TRUE (crm.StoreRange(77, 2, 32));
  79. ASSERT_FALSE(crm.StoreRange(72, 6, 33));
  80. ASSERT_TRUE (crm.StoreRange(80, 3, 34));
  81. ASSERT_TRUE (crm.StoreRange(81, 1, 35));
  82. ASSERT_TRUE (crm.StoreRange(82, 1, 36));
  83. ASSERT_TRUE (crm.StoreRange(83, 3, 37));
  84. ASSERT_TRUE (crm.StoreRange(84, 1, 38));
  85. ASSERT_TRUE (crm.StoreRange(83, 1, 39));
  86. ASSERT_TRUE (crm.StoreRange(86, 5, 40));
  87. ASSERT_TRUE (crm.StoreRange(88, 1, 41));
  88. ASSERT_TRUE (crm.StoreRange(90, 1, 42));
  89. ASSERT_TRUE (crm.StoreRange(86, 1, 43));
  90. ASSERT_TRUE (crm.StoreRange(87, 1, 44));
  91. ASSERT_TRUE (crm.StoreRange(89, 1, 45));
  92. ASSERT_TRUE (crm.StoreRange(87, 4, 46));
  93. ASSERT_TRUE (crm.StoreRange(87, 3, 47));
  94. ASSERT_FALSE(crm.StoreRange(86, 2, 48));
  95. // Each element in test_data contains the expected result when calling
  96. // RetrieveRange on an address.
  97. const int test_data[] = {
  98. 0, // 0
  99. 0, // 1
  100. 0, // 2
  101. 0, // 3
  102. 0, // 4
  103. 0, // 5
  104. 0, // 6
  105. 0, // 7
  106. 9, // 8
  107. 7, // 9
  108. 1, // 10
  109. 5, // 11
  110. 6, // 12
  111. 6, // 13
  112. 6, // 14
  113. 6, // 15
  114. 6, // 16
  115. 6, // 17
  116. 6, // 18
  117. 5, // 19
  118. 7, // 20
  119. 8, // 21
  120. 0, // 22
  121. 0, // 23
  122. 0, // 24
  123. 0, // 25
  124. 0, // 26
  125. 0, // 27
  126. 0, // 28
  127. 0, // 29
  128. 10, // 30
  129. 10, // 31
  130. 10, // 32
  131. 11, // 33
  132. 11, // 34
  133. 11, // 35
  134. 0, // 36
  135. 0, // 37
  136. 0, // 38
  137. 0, // 39
  138. 14, // 40
  139. 14, // 41
  140. 14, // 42
  141. 14, // 43
  142. 15, // 44
  143. 15, // 45
  144. 15, // 46
  145. 15, // 47
  146. 0, // 48
  147. 0, // 49
  148. 19, // 50
  149. 18, // 51
  150. 18, // 52
  151. 18, // 53
  152. 18, // 54
  153. 18, // 55
  154. 18, // 56
  155. 18, // 57
  156. 18, // 58
  157. 20, // 59
  158. 21, // 60
  159. 25, // 61
  160. 26, // 62
  161. 26, // 63
  162. 26, // 64
  163. 26, // 65
  164. 26, // 66
  165. 26, // 67
  166. 24, // 68
  167. 22, // 69
  168. 30, // 70
  169. 30, // 71
  170. 30, // 72
  171. 30, // 73
  172. 31, // 74
  173. 31, // 75
  174. 30, // 76
  175. 32, // 77
  176. 32, // 78
  177. 30, // 79
  178. 34, // 80
  179. 35, // 81
  180. 36, // 82
  181. 39, // 83
  182. 38, // 84
  183. 37, // 85
  184. 43, // 86
  185. 44, // 87
  186. 41, // 88
  187. 45, // 89
  188. 42, // 90
  189. 0, // 91
  190. 0, // 92
  191. 0, // 93
  192. 0, // 94
  193. 0, // 95
  194. 0, // 96
  195. 0, // 97
  196. 0, // 98
  197. 0 // 99
  198. };
  199. unsigned int test_high = sizeof(test_data) / sizeof(int);
  200. // Now, do the RetrieveRange tests. This further validates that the
  201. // objects were stored properly and that retrieval returns the correct
  202. // object.
  203. // If GENERATE_TEST_DATA is defined, instead of the retrieval tests, a
  204. // new test_data array will be printed. Exercise caution when doing this.
  205. // Be sure to verify the results manually!
  206. #ifdef GENERATE_TEST_DATA
  207. printf(" const int test_data[] = {\n");
  208. #endif // GENERATE_TEST_DATA
  209. for (unsigned int address = 0; address < test_high; ++address) {
  210. int value;
  211. if (!crm.RetrieveRange(address, &value))
  212. value = 0;
  213. #ifndef GENERATE_TEST_DATA
  214. // Don't use ASSERT inside the loop because it won't show the failed
  215. // |address|, and the line number will always be the same. That makes
  216. // it difficult to figure out which test failed.
  217. if (value != test_data[address]) {
  218. fprintf(stderr, "FAIL: retrieve %d expected %d observed %d @ %s:%d\n",
  219. address, test_data[address], value, __FILE__, __LINE__);
  220. return false;
  221. }
  222. #else // !GENERATE_TEST_DATA
  223. printf(" %d%c%s // %d\n", value,
  224. address == test_high - 1 ? ' ' : ',',
  225. value < 10 ? " " : "",
  226. address);
  227. #endif // !GENERATE_TEST_DATA
  228. }
  229. #ifdef GENERATE_TEST_DATA
  230. printf(" };\n");
  231. #endif // GENERATE_TEST_DATA
  232. return true;
  233. }
  234. } // namespace
  235. int main(int argc, char **argv) {
  236. BPLOG_INIT(&argc, &argv);
  237. return RunTests() ? 0 : 1;
  238. }