Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / test / ir / Shape.cc
1 /*
2  * Copyright (c) 2020 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 <ir/Shape.h>
18
19 #include <gtest/gtest.h>
20
21 TEST(ShapeTest, basic_test)
22 {
23   {
24     onert::ir::Shape shape(3);
25
26     shape.dim(0) = 1;
27     shape.dim(1) = 2;
28     shape.dim(2) = 3;
29
30     ASSERT_EQ(shape.rank(), 3);
31     ASSERT_EQ(shape.num_elements(), 6);
32     ASSERT_EQ(onert::ir::rankMaybeUnspecified(shape), false);
33     ASSERT_EQ(shape.hasUnspecifiedDims(), false);
34   }
35   {
36     onert::ir::Shape shape; // scalar or rank is unspecified
37
38     ASSERT_EQ(shape.rank(), 0);
39     ASSERT_EQ(shape.num_elements(), 1);
40     ASSERT_EQ(onert::ir::rankMaybeUnspecified(shape), true);
41     ASSERT_EQ(shape.hasUnspecifiedDims(), false);
42   }
43 }
44
45 TEST(ShapeTest, neg_basic_test)
46 {
47   {
48     onert::ir::Shape shape(2);
49
50     shape.dim(0) = 1;
51     shape.dim(1) = onert::ir::Shape::UNSPECIFIED_DIM;
52
53     ASSERT_EQ(shape.rank(), 2);
54     ASSERT_EQ(onert::ir::rankMaybeUnspecified(shape), false);
55     ASSERT_EQ(shape.hasUnspecifiedDims(), true);
56     EXPECT_ANY_THROW(shape.num_elements());
57   }
58 }