Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / angkor / src / ADT / tensor / Overlay.test.cpp
1 /*
2  * Copyright (c) 2018 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 "nncc/core/ADT/tensor/Overlay.h"
18 #include "nncc/core/ADT/tensor/LexicalLayout.h"
19
20 #include <gtest/gtest.h>
21
22 using nncc::core::ADT::tensor::Shape;
23 using nncc::core::ADT::tensor::Index;
24 using nncc::core::ADT::tensor::LexicalLayout;
25 using nncc::core::ADT::tensor::Overlay;
26
27 using nncc::core::ADT::tensor::make_overlay;
28
29 TEST(ADT_TENSOR_OVERLAY, ctor)
30 {
31   const Shape shape{2, 3};
32
33   int data[2 * 3] = {
34       0,
35   };
36   auto view = make_overlay<int, LexicalLayout>(shape, data);
37
38   ASSERT_EQ(shape, view.shape());
39 }
40
41 TEST(ADT_TENSOR_OVERLAY, read)
42 {
43   const Shape shape{2, 3};
44
45   int data[2 * 3] = {
46       0,
47   };
48   const auto view = make_overlay<int, LexicalLayout>(shape, data);
49
50   LexicalLayout layout{};
51
52   const Index index{1, 2};
53
54   ASSERT_EQ(0, data[layout.offset(shape, index)]);
55   data[layout.offset(shape, index)] = 2;
56   ASSERT_EQ(2, view.at(index));
57 }
58
59 TEST(ADT_TENSOR_OVERLAY, access)
60 {
61   const Shape shape{2, 3};
62
63   int data[2 * 3] = {
64       0,
65   };
66   auto view = make_overlay<int, LexicalLayout>(shape, data);
67
68   LexicalLayout layout{};
69
70   const Index index{1, 2};
71
72   ASSERT_EQ(0, data[layout.offset(shape, index)]);
73   view.at(index) = 4;
74   ASSERT_EQ(4, data[layout.offset(shape, index)]);
75 }