summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/alignedmemory/alignedmemory_test.cpp
blob: 73814d3284ef2160e5ec9f84cf1f0fb7c974600f (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
// 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 <vespa/log/log.h>
LOG_SETUP("alignedmemory_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/alignedmemory.h>

using namespace vespalib;

TEST_SETUP(Test);

int
Test::Main()
{
    TEST_INIT("alignedmemory_test");
    { // aligned alloc
        AlignedMemory mem8(32, 8);
        AlignedMemory mem16(32, 16);
        AlignedMemory mem512(32, 512);
        AlignedMemory mem7(32, 7);

        EXPECT_EQUAL(0u, ((uintptr_t)mem8.get()) % 8);
        EXPECT_EQUAL(0u, ((uintptr_t)mem16.get()) % 16);
        EXPECT_EQUAL(0u, ((uintptr_t)mem512.get()) % 512);
        EXPECT_EQUAL(0u, ((uintptr_t)mem7.get()) % 7);
    }
    { // swap
        AlignedMemory a(32, 8);
        AlignedMemory b(32, 8);
        char *pa = a.get();
        char *pb = b.get();

        EXPECT_EQUAL(pa, a.get());
        EXPECT_EQUAL(pb, b.get());
        a.swap(b);
        EXPECT_EQUAL(pb, a.get());
        EXPECT_EQUAL(pa, b.get());
        b.swap(a);
        EXPECT_EQUAL(pa, a.get());
        EXPECT_EQUAL(pb, b.get());
    }
    { // std::swap
        AlignedMemory a(32, 8);
        AlignedMemory b(32, 8);
        char *pa = a.get();
        char *pb = b.get();

        EXPECT_EQUAL(pa, a.get());
        EXPECT_EQUAL(pb, b.get());
        std::swap(a, b);
        EXPECT_EQUAL(pb, a.get());
        EXPECT_EQUAL(pa, b.get());
        std::swap(a, b);
        EXPECT_EQUAL(pa, a.get());
        EXPECT_EQUAL(pb, b.get());
    }
    { // construct with zero size
        AlignedMemory null(0, 0);
        char *expect = 0;
        EXPECT_EQUAL(expect, null.get());
    }
    { // const get()
        const AlignedMemory null(0, 0);
        const char *expect = 0;
        const char *got = null.get();
        EXPECT_EQUAL(expect, got);
    }
    TEST_DONE();
}