libc_allocator_with_realloc_test.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // ---
  30. #include <sparsehash/internal/sparseconfig.h>
  31. #include <config.h>
  32. #include <sparsehash/internal/libc_allocator_with_realloc.h>
  33. #include <stdlib.h>
  34. #include <string>
  35. #include <vector>
  36. #include <iostream>
  37. #include "testutil.h"
  38. using std::cerr;
  39. using std::cout;
  40. using std::string;
  41. using std::basic_string;
  42. using std::char_traits;
  43. using std::vector;
  44. using GOOGLE_NAMESPACE::libc_allocator_with_realloc;
  45. #define arraysize(a) ( sizeof(a) / sizeof(*(a)) )
  46. namespace {
  47. typedef libc_allocator_with_realloc<int> int_alloc;
  48. typedef int_alloc::rebind<int*>::other intp_alloc;
  49. // cstring allocates from libc_allocator_with_realloc.
  50. typedef basic_string<char, char_traits<char>,
  51. libc_allocator_with_realloc<char> > cstring;
  52. typedef vector<cstring, libc_allocator_with_realloc<cstring> > cstring_vector;
  53. TEST(LibcAllocatorWithReallocTest, Allocate) {
  54. int_alloc alloc;
  55. intp_alloc palloc;
  56. int** parray = palloc.allocate(1024);
  57. for (int i = 0; i < 16; ++i) {
  58. parray[i] = alloc.allocate(i * 1024 + 1);
  59. }
  60. for (int i = 0; i < 16; ++i) {
  61. alloc.deallocate(parray[i], i * 1024 + 1);
  62. }
  63. palloc.deallocate(parray, 1024);
  64. int* p = alloc.allocate(4096);
  65. p[0] = 1;
  66. p[1023] = 2;
  67. p[4095] = 3;
  68. p = alloc.reallocate(p, 8192);
  69. EXPECT_EQ(1, p[0]);
  70. EXPECT_EQ(2, p[1023]);
  71. EXPECT_EQ(3, p[4095]);
  72. p = alloc.reallocate(p, 1024);
  73. EXPECT_EQ(1, p[0]);
  74. EXPECT_EQ(2, p[1023]);
  75. alloc.deallocate(p, 1024);
  76. }
  77. TEST(LibcAllocatorWithReallocTest, TestSTL) {
  78. // Test strings copied from base/arena_unittest.cc
  79. static const char* test_strings[] = {
  80. "aback", "abaft", "abandon", "abandoned", "abandoning",
  81. "abandonment", "abandons", "abase", "abased", "abasement",
  82. "abasements", "abases", "abash", "abashed", "abashes", "abashing",
  83. "abasing", "abate", "abated", "abatement", "abatements", "abater",
  84. "abates", "abating", "abbe", "abbey", "abbeys", "abbot", "abbots",
  85. "abbreviate", "abbreviated", "abbreviates", "abbreviating",
  86. "abbreviation", "abbreviations", "abdomen", "abdomens", "abdominal",
  87. "abduct", "abducted", "abduction", "abductions", "abductor", "abductors",
  88. "abducts", "Abe", "abed", "Abel", "Abelian", "Abelson", "Aberdeen",
  89. "Abernathy", "aberrant", "aberration", "aberrations", "abet", "abets",
  90. "abetted", "abetter", "abetting", "abeyance", "abhor", "abhorred",
  91. "abhorrent", "abhorrer", "abhorring", "abhors", "abide", "abided",
  92. "abides", "abiding"};
  93. cstring_vector v;
  94. for (size_t i = 0; i < arraysize(test_strings); ++i) {
  95. v.push_back(test_strings[i]);
  96. }
  97. for (size_t i = arraysize(test_strings); i > 0; --i) {
  98. EXPECT_EQ(cstring(test_strings[i-1]), v.back());
  99. v.pop_back();
  100. }
  101. }
  102. } // namespace
  103. int main(int, char **) {
  104. // All the work is done in the static constructors. If they don't
  105. // die, the tests have all passed.
  106. cout << "PASS\n";
  107. return 0;
  108. }