Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / src / IR / TensorShape.test.cpp
1 /*
2  * Copyright (c) 2019 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 "loco/IR/TensorShape.h"
18
19 #include <gtest/gtest.h>
20
21 TEST(TensorShapeTest, default_constructor)
22 {
23   loco::TensorShape tensor_shape;
24
25   ASSERT_EQ(0, tensor_shape.rank());
26 }
27
28 TEST(TensorShapeTest, initializer_list_constructor)
29 {
30   loco::TensorShape tensor_shape{3, 5};
31
32   ASSERT_EQ(2, tensor_shape.rank());
33
34   ASSERT_TRUE(tensor_shape.dim(0).known());
35   ASSERT_TRUE(tensor_shape.dim(1).known());
36
37   ASSERT_EQ(3, tensor_shape.dim(0).value());
38   ASSERT_EQ(5, tensor_shape.dim(1).value());
39 }
40
41 TEST(TensorShapeTest, rank)
42 {
43   loco::TensorShape tensor_shape;
44
45   tensor_shape.rank(2);
46
47   ASSERT_EQ(2, tensor_shape.rank());
48   ASSERT_FALSE(tensor_shape.dim(0).known());
49   ASSERT_FALSE(tensor_shape.dim(1).known());
50 }
51
52 TEST(TensorShapeTest, dim)
53 {
54   loco::TensorShape tensor_shape;
55
56   tensor_shape.rank(2);
57
58   tensor_shape.dim(0) = 3;
59
60   ASSERT_TRUE(tensor_shape.dim(0).known());
61   ASSERT_FALSE(tensor_shape.dim(1).known());
62
63   ASSERT_EQ(3, tensor_shape.dim(0));
64 }
65
66 TEST(TensorShapeTest, rank_update)
67 {
68   loco::TensorShape tensor_shape;
69
70   tensor_shape.rank(2);
71
72   tensor_shape.dim(1) = 3;
73
74   tensor_shape.rank(4);
75
76   ASSERT_FALSE(tensor_shape.dim(0).known());
77   ASSERT_TRUE(tensor_shape.dim(1).known());
78   ASSERT_FALSE(tensor_shape.dim(2).known());
79   ASSERT_FALSE(tensor_shape.dim(3).known());
80
81   ASSERT_EQ(3, tensor_shape.dim(1));
82 }
83
84 TEST(TensorShapeTest, copy)
85 {
86   loco::TensorShape src;
87
88   src.rank(2);
89   src.dim(1) = 3;
90
91   loco::TensorShape dst;
92
93   dst = src;
94
95   ASSERT_EQ(2, dst.rank());
96
97   ASSERT_FALSE(dst.dim(0).known());
98   ASSERT_TRUE(dst.dim(1).known());
99
100   ASSERT_EQ(3, dst.dim(1));
101 }
102
103 TEST(TensorShapeTest, element_count)
104 {
105   // Check Rank-0 case
106   loco::TensorShape src;
107
108   ASSERT_EQ(1, loco::element_count(&src));
109 }
110
111 TEST(TensorShapeTest, equal_operator)
112 {
113   loco::TensorShape lhs, rhs;
114
115   lhs.rank(2);
116   lhs.dim(0) = 1;
117   lhs.dim(1) = 3;
118
119   rhs.rank(1);
120   rhs.dim(0) = 1;
121
122   EXPECT_FALSE(lhs == rhs);
123
124   rhs.rank(2);
125   rhs.dim(0) = 1;
126   rhs.dim(1) = 3;
127
128   EXPECT_TRUE(lhs == rhs);
129
130   // for unknown
131   loco::TensorShape lhs_u, rhs_u;
132
133   lhs_u.rank(2);
134   lhs_u.dim(0) = 1;
135
136   rhs_u.rank(2);
137   rhs_u.dim(0) = 1;
138
139   EXPECT_FALSE(lhs == rhs_u);
140 }