Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/TensorAllocator.h" |
| 25 | |
| 26 | #include "arm_compute/runtime/MemoryGroup.h" |
| 27 | #include "support/ToolchainSupport.h" |
| 28 | #include "tests/Utils.h" |
| 29 | #include "tests/framework/Asserts.h" |
| 30 | #include "tests/framework/Macros.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | TEST_SUITE(NEON) |
| 39 | TEST_SUITE(UNIT) |
| 40 | TEST_SUITE(TensorAllocator) |
| 41 | |
| 42 | TEST_CASE(ImportMemory, framework::DatasetMode::ALL) |
| 43 | { |
| 44 | // Init tensor info |
| 45 | TensorInfo info(TensorShape(24U, 16U, 3U), 1, DataType::F32); |
| 46 | |
| 47 | // Allocate memory buffer |
| 48 | std::shared_ptr<uint8_t> buf(new uint8_t[info.total_size()](), [](uint8_t *ptr) |
| 49 | { |
| 50 | delete[] ptr; |
| 51 | }); |
| 52 | |
| 53 | // Negative case : Import empty memory |
| 54 | Tensor t1; |
| 55 | t1.allocator()->init(info); |
| 56 | ARM_COMPUTE_EXPECT(bool(t1.allocator()->import_memory(Memory())), framework::LogLevel::ERRORS); |
| 57 | ARM_COMPUTE_EXPECT(t1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 58 | |
| 59 | // Negative case : Import memory to a tensor that is memory managed |
| 60 | Tensor t2; |
| 61 | MemoryGroup mg; |
| 62 | t2.allocator()->set_associated_memory_group(&mg); |
| 63 | ARM_COMPUTE_EXPECT(bool(t2.allocator()->import_memory(Memory(buf.get()))), framework::LogLevel::ERRORS); |
| 64 | ARM_COMPUTE_EXPECT(t2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 65 | |
| 66 | // Positive case : Set raw pointer |
| 67 | Tensor t3; |
| 68 | t3.allocator()->init(info); |
| 69 | ARM_COMPUTE_EXPECT(!bool(t3.allocator()->import_memory(Memory(buf.get()))), framework::LogLevel::ERRORS); |
| 70 | ARM_COMPUTE_EXPECT(!t3.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 71 | ARM_COMPUTE_EXPECT(t3.buffer() == buf.get(), framework::LogLevel::ERRORS); |
| 72 | t3.allocator()->free(); |
| 73 | ARM_COMPUTE_EXPECT(t3.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 74 | ARM_COMPUTE_EXPECT(t3.buffer() == nullptr, framework::LogLevel::ERRORS); |
| 75 | |
| 76 | // Positive case : Set managed pointer |
| 77 | Tensor t4; |
| 78 | t4.allocator()->init(info); |
| 79 | ARM_COMPUTE_EXPECT(!bool(t4.allocator()->import_memory(Memory(buf))), framework::LogLevel::ERRORS); |
| 80 | ARM_COMPUTE_EXPECT(!t4.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(t4.buffer() == buf.get(), framework::LogLevel::ERRORS); |
| 82 | t4.allocator()->free(); |
| 83 | ARM_COMPUTE_EXPECT(t4.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 84 | ARM_COMPUTE_EXPECT(t4.buffer() == nullptr, framework::LogLevel::ERRORS); |
| 85 | } |
| 86 | |
| 87 | TEST_SUITE_END() |
| 88 | TEST_SUITE_END() |
| 89 | TEST_SUITE_END() |
| 90 | } // namespace validation |
| 91 | } // namespace test |
| 92 | } // namespace arm_compute |