File: hbw_allocator_tests.cpp

package info (click to toggle)
memkind 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,508 kB
  • sloc: ansic: 72,572; cpp: 39,493; sh: 4,594; perl: 4,250; xml: 2,044; python: 1,753; makefile: 1,393; csh: 7
file content (113 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (C) 2014 - 2021 Intel Corporation. */

#include <hbw_allocator.h>

#include "common.h"
#include <vector>

// Tests for hbw::allocator class.
class HbwAllocatorTests: public ::testing::Test
{

protected:
    void SetUp()
    {}

    void TearDown()
    {}
};

// Test standard memory allocation and deallocation.
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_DetaultAllocatorTest)
{
    const size_t size = 512;
    hbw::allocator<size_t> allocator;

    hbw::allocator<size_t>::pointer ptr = allocator.allocate(size);

    ASSERT_TRUE(NULL != ptr);

    // Do the actually memory writing
    for (size_t i = 0; i < size; i++) {
        ptr[i] = i;
    }

    allocator.deallocate(ptr, size);
}

// Test address conversion functionality.
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AddressConversion)
{
    const size_t size = 512;
    hbw::allocator<int> allocator;

    int *ptr = allocator.allocate(size);

    ASSERT_TRUE(NULL != ptr);

    const int excpected_val = 4;
    ptr[0] = excpected_val;

    hbw::allocator<int>::reference reference = *ptr;
    hbw::allocator<int>::const_reference const_reference = *ptr;

    hbw::allocator<int>::pointer test_ptr = allocator.address(reference);
    hbw::allocator<int>::const_pointer test_const_ptr =
        allocator.address(const_reference);

    ASSERT_TRUE(NULL != test_ptr);
    ASSERT_TRUE(NULL != test_const_ptr);

    EXPECT_EQ(excpected_val, test_ptr[0]);
    EXPECT_EQ(excpected_val, test_const_ptr[0]);

    allocator.deallocate(ptr, size);
}

// Test for boundaries of allocation sizes.
// We expect to catch allocation exceptions caused by out of bound sizes.
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocationSizeOutOfBounds)
{
    hbw::allocator<size_t> allocator;

    const size_t over_size = allocator.max_size() + 1;

    ASSERT_THROW(allocator.allocate(over_size), std::bad_alloc);

    const size_t max_size = -1; // This will give maximum value of size_t.

    ASSERT_THROW(allocator.allocate(max_size), std::bad_alloc);

    ASSERT_THROW(allocator.allocate(0), std::bad_alloc);
}

// Test if variable will be constructed.
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocatorConstruct)
{
    hbw::allocator<int> allocator;

    int x = 0;
    int expect_val = 4;
    allocator.construct(&x, expect_val);

    EXPECT_EQ(expect_val, x);
}

// Test the integration with std::vector.
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_StandardVector)
{
    std::vector<int, hbw::allocator<int>> vec;
    const size_t size = 10000;

    for (size_t i = 0; i < size; i++) {
        vec.push_back(i);
    }

    EXPECT_EQ(size, vec.size());
    EXPECT_EQ(1, vec[1]);

    vec.clear();

    EXPECT_EQ((size_t)0, vec.size());
}