summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/alloc/alloc_test.cpp
blob: b16afbcc7a6f224c281c70edee20a934dd98ff7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <stddef.h>
#include <vespa/log/log.h>
LOG_SETUP("alloc_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/alloc.h>
#include <vespa/vespalib/util/exceptions.h>

using namespace vespalib;
using namespace vespalib::alloc;

class Test : public TestApp
{
public:
    int Main();
    void testAlignedAllocation();
    void testBasic();
    template <typename T>
    void testSwap(T & a, T & b);
};

int
Test::Main()
{
    TEST_INIT("alloc_test");
    testBasic();
    testAlignedAllocation();
    TEST_DONE();
}

template <typename T>
void
Test::testSwap(T & a, T & b)
{
    void * tmpA(a.get());
    void * tmpB(b.get());
    EXPECT_EQUAL(100u, a.size());
    EXPECT_EQUAL(200u, b.size());
    std::swap(a, b);
    EXPECT_EQUAL(100u, b.size());
    EXPECT_EQUAL(200u, a.size());
    EXPECT_EQUAL(tmpA, b.get());
    EXPECT_EQUAL(tmpB, a.get());
}

void
Test::testBasic()
{
    {
        Alloc h = HeapAllocFactory::create(100);
        EXPECT_EQUAL(100u, h.size());
        EXPECT_TRUE(h.get() != NULL);
    }
    {
        EXPECT_EXCEPTION(AlignedHeapAllocFactory::create(100, 7), IllegalArgumentException, "AlignedHeapAllocFactory::create(100, 7) does not support 7 alignment");
        Alloc h = AlignedHeapAllocFactory::create(100, 1024);
        EXPECT_EQUAL(100u, h.size());
        EXPECT_TRUE(h.get() != NULL);
    }
    {
        Alloc h = MMapAllocFactory::create(100);
        EXPECT_EQUAL(100u, h.size());
        EXPECT_TRUE(h.get() != NULL);
    }
    {
        Alloc a = HeapAllocFactory::create(100), b = HeapAllocFactory::create(200);
        testSwap(a, b);
    }
    {
        Alloc a = MMapAllocFactory::create(100), b = MMapAllocFactory::create(200);
        testSwap(a, b);
    }
    {
        Alloc a = AlignedHeapAllocFactory::create(100, 1024), b = AlignedHeapAllocFactory::create(200, 1024);
        testSwap(a, b);
    }
    {
        Alloc a = HeapAllocFactory::create(100);
        Alloc b = MMapAllocFactory::create(200);
        testSwap(a, b);
    }
}

void
Test::testAlignedAllocation()
{
    {
        Alloc buf = AutoAllocFactory::create(10, MemoryAllocator::HUGEPAGE_SIZE, 1024);
        EXPECT_TRUE(reinterpret_cast<ptrdiff_t>(buf.get()) % 1024 == 0);
    }

    {
        // Mmapped pointers are page-aligned, but sanity test anyway.
        Alloc buf = AutoAllocFactory::create(3000000, MemoryAllocator::HUGEPAGE_SIZE, 512);
        EXPECT_TRUE(reinterpret_cast<ptrdiff_t>(buf.get()) % 512 == 0);
    }
}

TEST_APPHOOK(Test)