Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / BuddyMemoryManager.test.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "luci_interpreter/BuddyMemoryManager.h"
18 #include <gtest/gtest.h>
19
20 namespace luci_interpreter
21 {
22 namespace
23 {
24
25 using namespace testing;
26
27 TEST(BuddyMemoryManager, basic)
28 {
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");
32
33   buddy_memory_manager->allocate_memory(first_tensor);
34
35   uint8_t data_1[] = {1, 2, 3, 4, 5, 6, 7, 8};
36
37   first_tensor.writeData(data_1, 8);
38   uint8_t array_1[8];
39   first_tensor.readData(array_1, 8);
40   for (int i = 0; i < 8; i++)
41   {
42     EXPECT_EQ(data_1[i], array_1[i]);
43   }
44
45   Tensor second_tensor(DataType::U8, Shape({2, 5}), AffineQuantization{}, "second_tensor");
46   buddy_memory_manager->allocate_memory(second_tensor);
47
48   uint8_t data_2[2][5] = {{11, 22, 33, 44, 55}, {12, 23, 34, 45, 56}};
49   second_tensor.writeData(data_2, 10);
50
51   uint8_t array_2[2][5];
52   second_tensor.readData(array_2, 10);
53   for (int i = 0; i < 2; i++)
54   {
55     for (int j = 0; j < 5; j++)
56     {
57       EXPECT_EQ(data_2[i][j], array_2[i][j]);
58     }
59   }
60
61   buddy_memory_manager->release_memory(first_tensor);
62   EXPECT_EQ(first_tensor.data<void>(), nullptr);
63
64   buddy_memory_manager->release_memory(second_tensor);
65   EXPECT_EQ(second_tensor.data<void>(), nullptr);
66 }
67
68 } // namespace
69 } // namespace luci_interpreter