blob: 2c5d6d49ec2ec5a2027fe2bcd005af89c84ee5aa [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
95 // Allocates a 2 int array
96 int* testPtr = new int[2];
97 int* misalignedPtr = testPtr + 1;
98
99 BOOST_CHECK(!handle.Import(static_cast<void *>(misalignedPtr), MemorySource::Malloc));
100
101 delete[] testPtr;
102}
103
Ferran Balaguerc33882d2019-08-21 13:59:13 +0100104#endif
105
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100106BOOST_AUTO_TEST_SUITE_END()