summaryrefslogtreecommitdiff
path: root/inference-engine/tests/unit/inference_engine_tests/locked_memory_test.cpp
blob: 4ba4688a2fd754503c4c572ec5ad6adcf01b26a4 (plain)
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
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

#include "tests_common.hpp"
#include "mock_allocator.hpp"

using namespace std;

class LockedMemoryTest : public TestsCommon {
protected:
    unique_ptr<MockAllocator> createMockAllocator() {
        return unique_ptr<MockAllocator>(new MockAllocator());
    }
};

using namespace InferenceEngine;
using namespace ::testing;


TEST_F(LockedMemoryTest, canUnlockMemoryAfterUsage) {

    auto allocator = createMockAllocator();

    char array [] = {1,2,3};

    EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
    EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
    {
        auto x = LockedMemory<char>(allocator.get(), (void *) 1, 1);
        //force locking of memory
        auto UNUSED t = x[0];
    }
}


TEST_F(LockedMemoryTest, canReadFromLockedMemory) {

    auto allocator = createMockAllocator();

    char array [] = {1,2,3,4,5};

    EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
    EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
    {
        auto x = LockedMemory<char>(allocator.get(), (void *) 1, 0);
        //we are getting first element
        ASSERT_EQ(1, x[0]);
    }
}


TEST_F(LockedMemoryTest, canWriteToLockedMemory) {

    auto allocator = createMockAllocator();

    char array [] = {1,2,3,4,5};

    EXPECT_CALL(*allocator.get(), lock((void*)1, _)).WillRepeatedly(Return((void*)array));
    EXPECT_CALL(*allocator.get(), unlock(_)).Times(1);
    {
        auto x = LockedMemory<char>(allocator.get(), (void *) 1, 0);

        //we are getting first element
        ASSERT_EQ(std::distance(array, &x[0]), 0);
        x[0] = 5;
    }
    EXPECT_EQ(array[0], 5);

}