Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / pre_allocator_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7
8 #include "ie_allocator.hpp"
9 #include "details/ie_pre_allocator.hpp"
10 #include <vector>
11
12 using namespace ::testing;
13 using namespace std;
14 using namespace InferenceEngine;
15
16 class PreallocatorTests: public ::testing::Test {
17  protected:
18     std::vector<float> mybuf;
19
20     virtual void TearDown() {
21     }
22
23     virtual void SetUp() {
24         mybuf.resize(10);
25         allocator = details::make_pre_allocator(&*mybuf.begin(), mybuf.size());
26     }
27     std::shared_ptr<IAllocator> allocator;
28
29 };
30
31 TEST_F(PreallocatorTests, canAccessPreAllocatedMemory) {
32     void * handle  = allocator->alloc(3);
33     float * ptr = (float *)allocator->lock(handle);
34
35     mybuf = {1.1f,2.2f,3.3f};
36
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);
41 }
42
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))));
47 }
48
49 TEST_F(PreallocatorTests, canNotLockWrongHandle) {
50     void * handle  = allocator->alloc(3);
51     EXPECT_EQ(nullptr, allocator->lock(1 + (int*)handle));
52 }