aboutsummaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/buffer_test.cpp
blob: bb31577bf1790bae12eb537d381bda119e806085 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/memfilepersistence/mapper/buffer.h>

namespace storage {
namespace memfile {

class BufferTest : public CppUnit::TestFixture
{
public:
    void getSizeReturnsInitiallyAllocatedSize();
    void getSizeReturnsUnAlignedSizeForMMappedAllocs();
    void resizeRetainsExistingDataWhenSizingUp();
    void resizeRetainsExistingDataWhenSizingDown();
    void bufferAddressIs512ByteAligned();

    CPPUNIT_TEST_SUITE(BufferTest);
    CPPUNIT_TEST(getSizeReturnsInitiallyAllocatedSize);
    CPPUNIT_TEST(getSizeReturnsUnAlignedSizeForMMappedAllocs);
    CPPUNIT_TEST(resizeRetainsExistingDataWhenSizingUp);
    CPPUNIT_TEST(resizeRetainsExistingDataWhenSizingDown);
    CPPUNIT_TEST(bufferAddressIs512ByteAligned);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(BufferTest);

void
BufferTest::getSizeReturnsInitiallyAllocatedSize()
{
    Buffer buf(1234);
    CPPUNIT_ASSERT_EQUAL(size_t(1234), buf.getSize());
}

void
BufferTest::getSizeReturnsUnAlignedSizeForMMappedAllocs()
{
    Buffer buf(vespalib::alloc::MemoryAllocator::HUGEPAGE_SIZE + 1);
    CPPUNIT_ASSERT_EQUAL(size_t(vespalib::alloc::MemoryAllocator::HUGEPAGE_SIZE + 1), buf.getSize());
}

void
BufferTest::resizeRetainsExistingDataWhenSizingUp()
{
    std::string src = "hello world";
    Buffer buf(src.size());
    memcpy(buf.getBuffer(), src.data(), src.size());
    buf.resize(src.size() * 2);
    CPPUNIT_ASSERT_EQUAL(src.size() * 2, buf.getSize());
    CPPUNIT_ASSERT_EQUAL(0, memcmp(buf.getBuffer(), src.data(), src.size()));
}

void
BufferTest::resizeRetainsExistingDataWhenSizingDown()
{
    std::string src = "hello world";
    Buffer buf(src.size());
    memcpy(buf.getBuffer(), src.data(), src.size());
    buf.resize(src.size() / 2);
    CPPUNIT_ASSERT_EQUAL(src.size() / 2, buf.getSize());
    CPPUNIT_ASSERT_EQUAL(0, memcmp(buf.getBuffer(), src.data(), src.size() / 2));
}

void
BufferTest::bufferAddressIs512ByteAligned()
{
    Buffer buf(32);
    CPPUNIT_ASSERT(reinterpret_cast<size_t>(buf.getBuffer()) % 512 == 0);
}

} // memfile
} // storage