blob: be229bf84421cf11d1e71bc76e880dbb56cee56e [file] [log] [blame]
Matthew Bentham7c1603a2019-06-21 17:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include <reference/RefTensorHandle.hpp>
6
7#include <boost/test/unit_test.hpp>
8
9BOOST_AUTO_TEST_SUITE(RefTensorHandleTests)
10using namespace armnn;
11
12BOOST_AUTO_TEST_CASE(AcquireAndRelease)
13{
14 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
15
16 TensorInfo info({1,1,1,1}, DataType::Float32);
17 RefTensorHandle handle(info, memoryManager);
18
19 handle.Manage();
20 handle.Allocate();
21
22 memoryManager->Acquire();
23 {
24 float *buffer = reinterpret_cast<float *>(handle.Map());
25
26 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
27
28 buffer[0] = 2.5f;
29
30 BOOST_CHECK(buffer[0] == 2.5f); // Memory is writable and readable
31
32 }
33 memoryManager->Release();
34
35 memoryManager->Acquire();
36 {
37 float *buffer = reinterpret_cast<float *>(handle.Map());
38
39 BOOST_CHECK(buffer != nullptr); // Yields a valid pointer
40
41 buffer[0] = 3.5f;
42
43 BOOST_CHECK(buffer[0] == 3.5f); // Memory is writable and readable
44 }
45 memoryManager->Release();
46}
47
Ferran Balaguerc33882d2019-08-21 13:59:13 +010048#if !defined(__ANDROID__)
49// Only run these tests on non Android platforms
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010050BOOST_AUTO_TEST_CASE(CheckSourceType)
51{
52 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
53
54 TensorInfo info({1}, DataType::Float32);
55 RefTensorHandle handle(info, memoryManager, static_cast<unsigned int>(MemorySource::Malloc));
56
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010057 int* testPtr = new int(4);
58
59 // Not supported
60 BOOST_CHECK(!handle.Import(static_cast<void *>(testPtr), MemorySource::DmaBuf));
61
62 // Not supported
63 BOOST_CHECK(!handle.Import(static_cast<void *>(testPtr), MemorySource::DmaBufProtected));
64
65 // Supported
66 BOOST_CHECK(handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc));
Ferran Balaguer1cd451c2019-08-22 14:09:44 +010067
68 delete testPtr;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010069}
70
71BOOST_AUTO_TEST_CASE(ReusePointer)
72{
73 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
74
75 TensorInfo info({1}, DataType::Float32);
76 RefTensorHandle handle(info, memoryManager, static_cast<unsigned int>(MemorySource::Malloc));
77
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010078 int* testPtr = new int(4);
79
80 handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc);
81
82 // Reusing previously Imported pointer
83 BOOST_CHECK(handle.Import(static_cast<void *>(testPtr), MemorySource::Malloc));
Ferran Balaguer1cd451c2019-08-22 14:09:44 +010084
85 delete testPtr;
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010086}
87
88BOOST_AUTO_TEST_CASE(MisalignedPointer)
89{
90 std::shared_ptr<RefMemoryManager> memoryManager = std::make_shared<RefMemoryManager>();
91
92 TensorInfo info({2}, DataType::Float32);
93 RefTensorHandle handle(info, memoryManager, static_cast<unsigned int>(MemorySource::Malloc));
94
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +010095 // Allocate a 2 int array
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010096 int* testPtr = new int[2];
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010097
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +010098 // Increment pointer by 1 byte
99 void* misalignedPtr = static_cast<void*>(reinterpret_cast<char*>(testPtr) + 1);
100
101 BOOST_CHECK(!handle.Import(misalignedPtr, MemorySource::Malloc));
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100102
103 delete[] testPtr;
104}
105
Ferran Balaguerc33882d2019-08-21 13:59:13 +0100106#endif
107
Aron Virginas-Tard9f7c8b2019-09-13 13:37:03 +0100108BOOST_AUTO_TEST_SUITE_END()