Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / angkor / src / ADT / tensor / Shape.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/Shape.h"
18
19 #include <gtest/gtest.h>
20
21 TEST(ADT_TENSOR_SHAPE, ctor)
22 {
23   nncc::core::ADT::tensor::Shape shape;
24
25   ASSERT_EQ(0, shape.rank());
26 }
27
28 TEST(ADT_TENSOR_SHAPE, ctor_initializer_list)
29 {
30   nncc::core::ADT::tensor::Shape shape{1, 3, 5, 7};
31
32   ASSERT_EQ(4, shape.rank());
33
34   ASSERT_EQ(1, shape.dim(0));
35   ASSERT_EQ(3, shape.dim(1));
36   ASSERT_EQ(5, shape.dim(2));
37   ASSERT_EQ(7, shape.dim(3));
38 }
39
40 TEST(ADT_TENSOR_SHAPE, resize)
41 {
42   nncc::core::ADT::tensor::Shape shape;
43
44   shape.resize(4);
45
46   ASSERT_EQ(4, shape.rank());
47 }
48
49 TEST(ADT_TENSOR_SHAPE, dim)
50 {
51   nncc::core::ADT::tensor::Shape shape;
52
53   shape.resize(4);
54
55   uint32_t dims[4] = {3, 5, 2, 7};
56
57   for (uint32_t axis = 0; axis < 4; ++axis)
58   {
59     shape.dim(axis) = dims[axis];
60     ASSERT_EQ(dims[axis], shape.dim(axis));
61   }
62 }
63
64 TEST(ADT_TENSOR_SHAPE, copy)
65 {
66   const nncc::core::ADT::tensor::Shape original{3, 5, 2, 7};
67   const nncc::core::ADT::tensor::Shape copied{original};
68
69   ASSERT_EQ(copied.rank(), original.rank());
70
71   for (uint32_t axis = 0; axis < 4; ++axis)
72   {
73     ASSERT_EQ(copied.dim(axis), original.dim(axis));
74   }
75 }
76
77 TEST(ADT_TENSOR_SHAPE, num_elements_rank_0)
78 {
79   using nncc::core::ADT::tensor::Shape;
80   using nncc::core::ADT::tensor::num_elements;
81
82   Shape rank_0_shape;
83
84   ASSERT_EQ(1, num_elements(rank_0_shape));
85 }
86
87 TEST(ADT_TENSOR_SHAPE, num_elements_zero)
88 {
89   using nncc::core::ADT::tensor::Shape;
90   using nncc::core::ADT::tensor::num_elements;
91
92   ASSERT_EQ(0, num_elements(Shape{0, 0, 0, 0}));
93 }
94
95 TEST(ADT_TENSOR_SHAPE, num_elements_nonzero)
96 {
97   using nncc::core::ADT::tensor::Shape;
98   using nncc::core::ADT::tensor::num_elements;
99
100   ASSERT_EQ(6, num_elements(Shape{2, 3}));
101 }
102
103 TEST(ADT_TENSOR_SHAPE, num_elements_nulldim)
104 {
105   using nncc::core::ADT::tensor::Shape;
106   using nncc::core::ADT::tensor::num_elements;
107
108   ASSERT_EQ(0, num_elements(Shape{2, 0, 3}));
109 }
110
111 TEST(ADT_TENSOR_SHAPE, squeeze_neg)
112 {
113   using nncc::core::ADT::tensor::Shape;
114   using nncc::core::ADT::tensor::squeeze;
115
116   auto squeezed = squeeze(Shape{3, 5, 2});
117
118   ASSERT_EQ(3, squeezed.rank());
119   ASSERT_EQ(3, squeezed.dim(0));
120   ASSERT_EQ(5, squeezed.dim(1));
121   ASSERT_EQ(2, squeezed.dim(2));
122 }
123
124 TEST(ADT_TENSOR_SHAPE, squeeze_neg_0)
125 {
126   using nncc::core::ADT::tensor::Shape;
127   using nncc::core::ADT::tensor::squeeze;
128
129   auto squeezed = squeeze(Shape{3, 0, 2});
130
131   ASSERT_EQ(3, squeezed.rank());
132   ASSERT_EQ(3, squeezed.dim(0));
133   ASSERT_EQ(0, squeezed.dim(1));
134   ASSERT_EQ(2, squeezed.dim(2));
135 }
136
137 TEST(ADT_TENSOR_SHAPE, squeeze_pos)
138 {
139   using nncc::core::ADT::tensor::Shape;
140   using nncc::core::ADT::tensor::squeeze;
141
142   auto squeezed = squeeze(Shape{3, 1, 2});
143
144   ASSERT_EQ(2, squeezed.rank());
145   ASSERT_EQ(3, squeezed.dim(0));
146   ASSERT_EQ(2, squeezed.dim(1));
147 }
148
149 TEST(ADT_TENSOR_SHAPE, squeeze_nested)
150 {
151   using nncc::core::ADT::tensor::Shape;
152   using nncc::core::ADT::tensor::squeeze;
153
154   Shape shape{3, 1, 2};
155
156   shape.squeeze().squeeze();
157
158   ASSERT_EQ(2, shape.rank());
159   ASSERT_EQ(3, shape.dim(0));
160   ASSERT_EQ(2, shape.dim(1));
161 }
162
163 TEST(ADT_TENSOR_SHAPE, eq_negative_on_unmatched_rank)
164 {
165   const nncc::core::ADT::tensor::Shape left{1, 1, 1};
166   const nncc::core::ADT::tensor::Shape right{1, 1, 1, 1};
167
168   ASSERT_FALSE(left == right);
169 }
170
171 TEST(ADT_TENSOR_SHAPE, eq_negative_on_unmatched_dim)
172 {
173   const nncc::core::ADT::tensor::Shape left{2, 3};
174   const nncc::core::ADT::tensor::Shape right{2, 4};
175
176   ASSERT_FALSE(left == right);
177 }
178
179 TEST(ADT_TENSOR_SHAPE, eq_positive)
180 {
181   const nncc::core::ADT::tensor::Shape left{2, 3};
182   const nncc::core::ADT::tensor::Shape right{2, 3};
183
184   ASSERT_TRUE(left == right);
185 }