Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / inference_engine_tests / locked_memory_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "tests_common.hpp"
6 #include "mock_allocator.hpp"
7
8 using namespace std;
9
10 class LockedMemoryTest : public TestsCommon {
11 protected:
12     unique_ptr<MockAllocator> createMockAllocator() {
13         return unique_ptr<MockAllocator>(new MockAllocator());
14     }
15 };
16
17 using namespace InferenceEngine;
18 using namespace ::testing;
19
20
21 TEST_F(LockedMemoryTest, canUnlockMemoryAfterUsage) {
22
23     auto allocator = createMockAllocator();
24
25     char array [] = {1,2,3};
26
27     EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
28     EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
29     {
30         auto x = LockedMemory<char>(allocator.get(), (void *) 1, 1);
31         //force locking of memory
32         auto UNUSED t = x[0];
33     }
34 }
35
36
37 TEST_F(LockedMemoryTest, canReadFromLockedMemory) {
38
39     auto allocator = createMockAllocator();
40
41     char array [] = {1,2,3,4,5};
42
43     EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
44     EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
45     {
46         auto x = LockedMemory<char>(allocator.get(), (void *) 1, 0);
47         //we are getting first element
48         ASSERT_EQ(1, x[0]);
49     }
50 }
51
52
53 TEST_F(LockedMemoryTest, canWriteToLockedMemory) {
54
55     auto allocator = createMockAllocator();
56
57     char array [] = {1,2,3,4,5};
58
59     EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
60     EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
61     {
62         auto x = LockedMemory<char>(allocator.get(), (void *) 1, 0);
63
64         //we are getting first element
65         ASSERT_EQ(std::distance(array, &x[0]), 0);
66         x[0] = 5;
67     }
68     EXPECT_EQ(array[0], 5);
69
70 }