memory_segment_local_unittest.cc 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15 16 17
#include <util/tests/memory_segment_common_unittest.h>

#include <util/memory_segment_local.h>
18
#include <exceptions/exceptions.h>
19 20
#include <gtest/gtest.h>
#include <memory>
21
#include <limits.h>
22 23

using namespace std;
24
using namespace isc::util;
25

26
namespace {
27

Mukund Sivaraman's avatar
Mukund Sivaraman committed
28
TEST(MemorySegmentLocal, TestLocal) {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
29
    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());
30

Mukund Sivaraman's avatar
Mukund Sivaraman committed
31 32
    // By default, nothing is allocated.
    EXPECT_TRUE(segment->allMemoryDeallocated());
33

34
    void* ptr = segment->allocate(1024);
35

Mukund Sivaraman's avatar
Mukund Sivaraman committed
36 37
    // Now, we have an allocation:
    EXPECT_FALSE(segment->allMemoryDeallocated());
38

39
    void* ptr2 = segment->allocate(42);
40

Mukund Sivaraman's avatar
Mukund Sivaraman committed
41 42
    // Still:
    EXPECT_FALSE(segment->allMemoryDeallocated());
43

Mukund Sivaraman's avatar
Mukund Sivaraman committed
44 45 46
    // These should not fail, because the buffers have been allocated.
    EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 1024));
    EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 42));
47

Mukund Sivaraman's avatar
Mukund Sivaraman committed
48
    segment->deallocate(ptr, 1024);
49

Mukund Sivaraman's avatar
Mukund Sivaraman committed
50 51
    // Still:
    EXPECT_FALSE(segment->allMemoryDeallocated());
52

Mukund Sivaraman's avatar
Mukund Sivaraman committed
53
    segment->deallocate(ptr2, 42);
54

Mukund Sivaraman's avatar
Mukund Sivaraman committed
55 56
    // Now, we have an deallocated everything:
    EXPECT_TRUE(segment->allMemoryDeallocated());
57 58
}

59 60 61
TEST(MemorySegmentLocal, TestTooMuchMemory) {
    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());

62 63 64 65 66 67
    // Although it should be perfectly fine to use the ULONG_MAX
    // instead of LONG_MAX as the size_t value should be unsigned,
    // Valgrind appears to be using the signed value and hence the
    // maximum positive value is LONG_MAX for Valgrind. But, this
    // should be sufficient to test the "too much memory" conditions.
    EXPECT_THROW(segment->allocate(LONG_MAX), bad_alloc);
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
TEST(MemorySegmentLocal, TestBadDeallocate) {
    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());

    // By default, nothing is allocated.
    EXPECT_TRUE(segment->allMemoryDeallocated());

    void* ptr = segment->allocate(1024);

    // Now, we have an allocation:
    EXPECT_FALSE(segment->allMemoryDeallocated());

    // This should not throw
    EXPECT_NO_THROW(segment->deallocate(ptr, 1024));

    // Now, we have an deallocated everything:
    EXPECT_TRUE(segment->allMemoryDeallocated());

    ptr = segment->allocate(1024);

    // Now, we have another allocation:
    EXPECT_FALSE(segment->allMemoryDeallocated());

    // This should throw as the size passed to deallocate() is larger
    // than what was allocated.
    EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
95 96 97 98 99 100

    // This should not throw
    EXPECT_NO_THROW(segment->deallocate(ptr, 1024));

    // Now, we have an deallocated everything:
    EXPECT_TRUE(segment->allMemoryDeallocated());
101 102
}

103 104 105 106 107 108 109 110 111 112 113 114 115
TEST(MemorySegmentLocal, TestNullDeallocate) {
    auto_ptr<MemorySegment> segment(new MemorySegmentLocal());

    // By default, nothing is allocated.
    EXPECT_TRUE(segment->allMemoryDeallocated());

    // NULL deallocation is a no-op.
    EXPECT_NO_THROW(segment->deallocate(NULL, 1024));

    // This should still return true.
    EXPECT_TRUE(segment->allMemoryDeallocated());
}

116 117 118 119 120
TEST(MemorySegmentLocal, namedAddress) {
    MemorySegmentLocal segment;
    isc::util::test::checkSegmentNamedAddress(segment, true);
}

121
} // anonymous namespace