Blame test/hbw_allocator_tests.cpp

Packit 345191
/*
Packit 345191
 * Copyright (C) 2014 - 2019 Intel Corporation.
Packit 345191
 * All rights reserved.
Packit 345191
 *
Packit 345191
 * Redistribution and use in source and binary forms, with or without
Packit 345191
 * modification, are permitted provided that the following conditions are met:
Packit 345191
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer.
Packit 345191
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer in the documentation
Packit 345191
 *    and/or other materials provided with the distribution.
Packit 345191
 *
Packit 345191
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
 */
Packit 345191
Packit 345191
#include <hbw_allocator.h>
Packit 345191
Packit 345191
#include "common.h"
Packit 345191
#include <vector>
Packit 345191
Packit 345191
// Tests for hbw::allocator class.
Packit 345191
class HbwAllocatorTests: public :: testing::Test
Packit 345191
{
Packit 345191
Packit 345191
protected:
Packit 345191
    void SetUp()
Packit 345191
    {}
Packit 345191
Packit 345191
    void TearDown()
Packit 345191
    {}
Packit 345191
Packit 345191
};
Packit 345191
Packit 345191
//Test standard memory allocation and deallocation.
Packit 345191
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_DetaultAllocatorTest)
Packit 345191
{
Packit 345191
    const size_t size = 512;
Packit 345191
    hbw::allocator<size_t> allocator;
Packit 345191
Packit 345191
    hbw::allocator<size_t>::pointer ptr = allocator.allocate(size);
Packit 345191
Packit 345191
    ASSERT_TRUE(NULL != ptr);
Packit 345191
Packit 345191
    //Do the actually memory writing
Packit 345191
    for (size_t i=0; i
Packit 345191
        ptr[i] = i;
Packit 345191
    }
Packit 345191
Packit 345191
    allocator.deallocate(ptr, size);
Packit 345191
}
Packit 345191
Packit 345191
//Test address conversion functionality.
Packit 345191
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AddressConversion)
Packit 345191
{
Packit 345191
    const size_t size = 512;
Packit 345191
    hbw::allocator<int> allocator;
Packit 345191
Packit 345191
    int *ptr = allocator.allocate(size);
Packit 345191
Packit 345191
    ASSERT_TRUE(NULL != ptr);
Packit 345191
Packit 345191
    const int excpected_val = 4;
Packit 345191
    ptr[0] = excpected_val;
Packit 345191
Packit 345191
    hbw::allocator<int>::reference reference = *ptr;
Packit 345191
    hbw::allocator<int>::const_reference const_reference = *ptr;
Packit 345191
Packit 345191
    hbw::allocator<int>::pointer test_ptr = allocator.address(reference);
Packit 345191
    hbw::allocator<int>::const_pointer test_const_ptr = allocator.address(
Packit 345191
                                                            const_reference);
Packit 345191
Packit 345191
    ASSERT_TRUE(NULL != test_ptr);
Packit 345191
    ASSERT_TRUE(NULL != test_const_ptr);
Packit 345191
Packit 345191
    EXPECT_EQ(excpected_val, test_ptr[0]);
Packit 345191
    EXPECT_EQ(excpected_val, test_const_ptr[0]);
Packit 345191
Packit 345191
    allocator.deallocate(ptr, size);
Packit 345191
}
Packit 345191
Packit 345191
//Test for boundaries of allocation sizes.
Packit 345191
//We expect to catch allocation exceptions caused by out of bound sizes.
Packit 345191
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocationSizeOutOfBounds)
Packit 345191
{
Packit 345191
    hbw::allocator<size_t> allocator;
Packit 345191
Packit 345191
    const size_t over_size = allocator.max_size() + 1;
Packit 345191
Packit 345191
    ASSERT_THROW(allocator.allocate(over_size), std::bad_alloc);
Packit 345191
Packit 345191
    const size_t max_size = -1; //This will give maximum value of size_t.
Packit 345191
Packit 345191
    ASSERT_THROW(allocator.allocate(max_size), std::bad_alloc);
Packit 345191
Packit 345191
    ASSERT_THROW(allocator.allocate(0), std::bad_alloc);
Packit 345191
}
Packit 345191
Packit 345191
//Test if variable will be constructed.
Packit 345191
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocatorConstruct)
Packit 345191
{
Packit 345191
    hbw::allocator<int> allocator;
Packit 345191
Packit 345191
    int x = 0;
Packit 345191
    int expect_val = 4;
Packit 345191
    allocator.construct(&x, expect_val);
Packit 345191
Packit 345191
    EXPECT_EQ(expect_val, x);
Packit 345191
}
Packit 345191
Packit 345191
//Test the integration with std::vector.
Packit 345191
TEST_F(HbwAllocatorTests, test_TC_MEMKIND_StandardVector)
Packit 345191
{
Packit 345191
    std::vector<int, hbw::allocator<int> > vec;
Packit 345191
    const size_t size = 10000;
Packit 345191
Packit 345191
    for (size_t i=0; i
Packit 345191
        vec.push_back(i);
Packit 345191
    }
Packit 345191
Packit 345191
    EXPECT_EQ(size, vec.size());
Packit 345191
    EXPECT_EQ(1, vec[1]);
Packit 345191
Packit 345191
    vec.clear();
Packit 345191
Packit 345191
    EXPECT_EQ((size_t)0, vec.size());
Packit 345191
}