2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "luci_interpreter/BuddyMemoryManager.h"
18 #include <gtest/gtest.h>
20 namespace luci_interpreter
25 using namespace testing;
27 TEST(BuddyMemoryManager, basic)
29 auto mem_pool = std::make_unique<uint8_t[]>(200);
30 auto buddy_memory_manager = std::make_unique<BuddyMemoryManager>(mem_pool.get(), 130);
31 Tensor first_tensor(DataType::U8, Shape({8}), AffineQuantization{}, "first_tensor");
33 buddy_memory_manager->allocate_memory(first_tensor);
35 uint8_t data_1[] = {1, 2, 3, 4, 5, 6, 7, 8};
37 first_tensor.writeData(data_1, 8);
39 first_tensor.readData(array_1, 8);
40 for (int i = 0; i < 8; i++)
42 EXPECT_EQ(data_1[i], array_1[i]);
45 Tensor second_tensor(DataType::U8, Shape({2, 5}), AffineQuantization{}, "second_tensor");
46 buddy_memory_manager->allocate_memory(second_tensor);
48 uint8_t data_2[2][5] = {{11, 22, 33, 44, 55}, {12, 23, 34, 45, 56}};
49 second_tensor.writeData(data_2, 10);
51 uint8_t array_2[2][5];
52 second_tensor.readData(array_2, 10);
53 for (int i = 0; i < 2; i++)
55 for (int j = 0; j < 5; j++)
57 EXPECT_EQ(data_2[i][j], array_2[i][j]);
61 buddy_memory_manager->release_memory(first_tensor);
62 EXPECT_EQ(first_tensor.data<void>(), nullptr);
64 buddy_memory_manager->release_memory(second_tensor);
65 EXPECT_EQ(second_tensor.data<void>(), nullptr);
69 } // namespace luci_interpreter