1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
8 #include "ie_allocator.hpp"
9 #include "details/ie_pre_allocator.hpp"
12 using namespace ::testing;
14 using namespace InferenceEngine;
16 class PreallocatorTests: public ::testing::Test {
18 std::vector<float> mybuf;
20 virtual void TearDown() {
23 virtual void SetUp() {
25 allocator = details::make_pre_allocator(&*mybuf.begin(), mybuf.size());
27 std::shared_ptr<IAllocator> allocator;
31 TEST_F(PreallocatorTests, canAccessPreAllocatedMemory) {
32 void * handle = allocator->alloc(3);
33 float * ptr = (float *)allocator->lock(handle);
35 mybuf = {1.1f,2.2f,3.3f};
37 ASSERT_EQ(ptr, &*mybuf.begin());
38 ASSERT_EQ(ptr[0], 1.1f);
39 ASSERT_EQ(ptr[1], 2.2f);
40 ASSERT_EQ(ptr[2], 3.3f);
43 TEST_F(PreallocatorTests, canNotAllocateMoreMemory) {
44 //large block such as 10k will result in nullptr
45 EXPECT_EQ(nullptr, allocator->lock(allocator->alloc(10* sizeof(float) + 1)));
46 EXPECT_NE(nullptr, allocator->lock(allocator->alloc(10* sizeof(float))));
49 TEST_F(PreallocatorTests, canNotLockWrongHandle) {
50 void * handle = allocator->alloc(3);
51 EXPECT_EQ(nullptr, allocator->lock(1 + (int*)handle));