memory_segment_local_unittest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "util/memory_segment_local.h"
  15. #include <exceptions/exceptions.h>
  16. #include <gtest/gtest.h>
  17. #include <memory>
  18. using namespace std;
  19. using namespace isc::util;
  20. namespace {
  21. TEST(MemorySegmentLocal, TestLocal) {
  22. auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
  23. // By default, nothing is allocated.
  24. EXPECT_TRUE(segment->allMemoryDeallocated());
  25. void* ptr = segment->allocate(1024);
  26. // Now, we have an allocation:
  27. EXPECT_FALSE(segment->allMemoryDeallocated());
  28. void* ptr2 = segment->allocate(42);
  29. // Still:
  30. EXPECT_FALSE(segment->allMemoryDeallocated());
  31. // These should not fail, because the buffers have been allocated.
  32. EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 1024));
  33. EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 42));
  34. segment->deallocate(ptr, 1024);
  35. // Still:
  36. EXPECT_FALSE(segment->allMemoryDeallocated());
  37. segment->deallocate(ptr2, 42);
  38. // Now, we have an deallocated everything:
  39. EXPECT_TRUE(segment->allMemoryDeallocated());
  40. }
  41. TEST(MemorySegmentLocal, TestTooMuchMemory) {
  42. auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
  43. EXPECT_THROW(segment->allocate(0x7fffffffffffffff), bad_alloc);
  44. }
  45. TEST(MemorySegmentLocal, TestBadDeallocate) {
  46. auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
  47. // By default, nothing is allocated.
  48. EXPECT_TRUE(segment->allMemoryDeallocated());
  49. void* ptr = segment->allocate(1024);
  50. // Now, we have an allocation:
  51. EXPECT_FALSE(segment->allMemoryDeallocated());
  52. // This should not throw
  53. EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
  54. // Now, we have an deallocated everything:
  55. EXPECT_TRUE(segment->allMemoryDeallocated());
  56. ptr = segment->allocate(1024);
  57. // Now, we have another allocation:
  58. EXPECT_FALSE(segment->allMemoryDeallocated());
  59. // This should throw as the size passed to deallocate() is larger
  60. // than what was allocated.
  61. EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
  62. // This should not throw
  63. EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
  64. // Now, we have an deallocated everything:
  65. EXPECT_TRUE(segment->allMemoryDeallocated());
  66. }
  67. TEST(MemorySegmentLocal, TestNullDeallocate) {
  68. auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
  69. // By default, nothing is allocated.
  70. EXPECT_TRUE(segment->allMemoryDeallocated());
  71. // NULL deallocation is a no-op.
  72. EXPECT_NO_THROW(segment->deallocate(NULL, 1024));
  73. // This should still return true.
  74. EXPECT_TRUE(segment->allMemoryDeallocated());
  75. }
  76. } // anonymous namespace