Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / one_op_tests / ExpandDims.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 "GenModelTest.h"
18
19 TEST_F(GenModelTest, OneOp_ExpandDims)
20 {
21   CircleGen cgen;
22
23   std::vector<int32_t> axis_data{1};
24   uint32_t axis_buf = cgen.addBuffer(axis_data);
25   int in = cgen.addTensor({{1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
26   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
27   int out = cgen.addTensor({{1, 1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
28   cgen.addOperatorExpandDims({{in, axis}, {out}});
29   cgen.setInputsAndOutputs({in}, {out});
30
31   _context = std::make_unique<GenModelTestContext>(cgen.finish());
32   _context->addTestCase(
33     TestCaseData{}.addInput<float>({0.1, 0.3, 0.5, 0.7}).addOutput<float>({0.1, 0.3, 0.5, 0.7}));
34   _context->setBackends({"cpu"});
35
36   SUCCEED();
37 }
38
39 TEST_F(GenModelTest, OneOp_ExpandDims_Int64AxisNeg)
40 {
41   CircleGen cgen;
42
43   std::vector<int64_t> axis_data{-1};
44   uint32_t axis_buf = cgen.addBuffer(axis_data);
45   int in = cgen.addTensor({{1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
46   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT64, axis_buf});
47   int out = cgen.addTensor({{1, 4, 1, 1}, circle::TensorType::TensorType_FLOAT32});
48   cgen.addOperatorExpandDims({{in, axis}, {out}});
49   cgen.setInputsAndOutputs({in}, {out});
50
51   _context = std::make_unique<GenModelTestContext>(cgen.finish());
52   _context->addTestCase(
53     TestCaseData{}.addInput<float>({0.1, 0.3, 0.5, 0.7}).addOutput<float>({0.1, 0.3, 0.5, 0.7}));
54   _context->setBackends({"cpu"});
55
56   SUCCEED();
57 }
58
59 TEST_F(GenModelTest, OneOp_neg_ExpandDims_Axis)
60 {
61   CircleGen cgen;
62
63   std::vector<int32_t> axis_data{4};
64   uint32_t axis_buf = cgen.addBuffer(axis_data);
65   int in = cgen.addTensor({{1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
66   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32, axis_buf});
67   int out = cgen.addTensor({{1, 1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
68   cgen.addOperatorExpandDims({{in, axis}, {out}});
69   cgen.setInputsAndOutputs({in}, {out});
70
71   _context = std::make_unique<GenModelTestContext>(cgen.finish());
72   _context->setBackends({"cpu"});
73   _context->expectFailCompile();
74
75   SUCCEED();
76 }
77
78 TEST_F(GenModelTest, OneOp_neg_ExpandDims_AxisNegInput)
79 {
80   CircleGen cgen;
81
82   int in = cgen.addTensor({{1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
83   int axis = cgen.addTensor({{1}, circle::TensorType::TensorType_INT32});
84   int out = cgen.addTensor({{1, 1, 4, 1}, circle::TensorType::TensorType_FLOAT32});
85   cgen.addOperatorExpandDims({{in, axis}, {out}});
86   cgen.setInputsAndOutputs({in, axis}, {out});
87
88   _context = std::make_unique<GenModelTestContext>(cgen.finish());
89   _context->addTestCase(TestCaseData{}
90                           .addInput<float>({0.1, 0.3, 0.5, 0.7})
91                           .addInput<int32_t>({-5})
92                           .addOutput<float>({0.1, 0.3, 0.5, 0.7})
93                           .expectFailRun());
94   _context->setBackends({"cpu"});
95
96   SUCCEED();
97 }