pagemap_unittest.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2003, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Sanjay Ghemawat
  32. #include "config_for_unittests.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #if defined HAVE_STDINT_H
  36. #include <stdint.h> // to get intptr_t
  37. #elif defined HAVE_INTTYPES_H
  38. #include <inttypes.h> // another place intptr_t might be defined
  39. #endif
  40. #include <sys/types.h>
  41. #include <vector>
  42. #include "base/logging.h"
  43. #include "pagemap.h"
  44. using std::vector;
  45. static void Permute(vector<intptr_t>* elements) {
  46. if (elements->empty())
  47. return;
  48. const size_t num_elements = elements->size();
  49. for (size_t i = num_elements - 1; i > 0; --i) {
  50. const size_t newpos = rand() % (i + 1);
  51. const intptr_t tmp = (*elements)[i]; // swap
  52. (*elements)[i] = (*elements)[newpos];
  53. (*elements)[newpos] = tmp;
  54. }
  55. }
  56. // Note: we leak memory every time a map is constructed, so do not
  57. // create too many maps.
  58. // Test specified map type
  59. template <class Type>
  60. void TestMap(int limit, bool limit_is_below_the_overflow_boundary) {
  61. RAW_LOG(INFO, "Running test with %d iterations...\n", limit);
  62. { // Test sequential ensure/assignment
  63. Type map(malloc);
  64. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  65. map.Ensure(i, 1);
  66. map.set(i, (void*)(i+1));
  67. CHECK_EQ(map.get(i), (void*)(i+1));
  68. }
  69. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  70. CHECK_EQ(map.get(i), (void*)(i+1));
  71. }
  72. }
  73. { // Test bulk Ensure
  74. Type map(malloc);
  75. map.Ensure(0, limit);
  76. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  77. map.set(i, (void*)(i+1));
  78. CHECK_EQ(map.get(i), (void*)(i+1));
  79. }
  80. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  81. CHECK_EQ(map.get(i), (void*)(i+1));
  82. }
  83. }
  84. // Test that we correctly notice overflow
  85. {
  86. Type map(malloc);
  87. CHECK_EQ(map.Ensure(limit, limit+1), limit_is_below_the_overflow_boundary);
  88. }
  89. { // Test randomized accesses
  90. srand(301); // srand isn't great, but it's portable
  91. vector<intptr_t> elements;
  92. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) elements.push_back(i);
  93. Permute(&elements);
  94. Type map(malloc);
  95. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  96. map.Ensure(elements[i], 1);
  97. map.set(elements[i], (void*)(elements[i]+1));
  98. CHECK_EQ(map.get(elements[i]), (void*)(elements[i]+1));
  99. }
  100. for (intptr_t i = 0; i < static_cast<intptr_t>(limit); i++) {
  101. CHECK_EQ(map.get(i), (void*)(i+1));
  102. }
  103. }
  104. }
  105. // REQUIRES: BITS==10, i.e., valid range is [0,1023].
  106. // Representations for different types will end up being:
  107. // PageMap1: array[1024]
  108. // PageMap2: array[32][32]
  109. // PageMap3: array[16][16][4]
  110. template <class Type>
  111. void TestNext(const char* name) {
  112. RAW_LOG(ERROR, "Running NextTest %s\n", name);
  113. Type map(malloc);
  114. char a, b, c, d, e;
  115. // When map is empty
  116. CHECK(map.Next(0) == NULL);
  117. CHECK(map.Next(5) == NULL);
  118. CHECK(map.Next(1<<30) == NULL);
  119. // Add a single value
  120. map.Ensure(40, 1);
  121. map.set(40, &a);
  122. CHECK(map.Next(0) == &a);
  123. CHECK(map.Next(39) == &a);
  124. CHECK(map.Next(40) == &a);
  125. CHECK(map.Next(41) == NULL);
  126. CHECK(map.Next(1<<30) == NULL);
  127. // Add a few values
  128. map.Ensure(41, 1);
  129. map.Ensure(100, 3);
  130. map.set(41, &b);
  131. map.set(100, &c);
  132. map.set(101, &d);
  133. map.set(102, &e);
  134. CHECK(map.Next(0) == &a);
  135. CHECK(map.Next(39) == &a);
  136. CHECK(map.Next(40) == &a);
  137. CHECK(map.Next(41) == &b);
  138. CHECK(map.Next(42) == &c);
  139. CHECK(map.Next(63) == &c);
  140. CHECK(map.Next(64) == &c);
  141. CHECK(map.Next(65) == &c);
  142. CHECK(map.Next(99) == &c);
  143. CHECK(map.Next(100) == &c);
  144. CHECK(map.Next(101) == &d);
  145. CHECK(map.Next(102) == &e);
  146. CHECK(map.Next(103) == NULL);
  147. }
  148. int main(int argc, char** argv) {
  149. TestMap< TCMalloc_PageMap1<10> > (100, true);
  150. TestMap< TCMalloc_PageMap1<10> > (1 << 10, false);
  151. TestMap< TCMalloc_PageMap2<20> > (100, true);
  152. TestMap< TCMalloc_PageMap2<20> > (1 << 20, false);
  153. TestMap< TCMalloc_PageMap3<20> > (100, true);
  154. TestMap< TCMalloc_PageMap3<20> > (1 << 20, false);
  155. TestNext< TCMalloc_PageMap1<10> >("PageMap1");
  156. TestNext< TCMalloc_PageMap2<10> >("PageMap2");
  157. TestNext< TCMalloc_PageMap3<10> >("PageMap3");
  158. printf("PASS\n");
  159. return 0;
  160. }