1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ops/maximum_builder.h"
17 
18 #include "ops_test.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Ops;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class MaximumBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     MaximumBuilder m_maximum;
34     std::vector<uint32_t> m_inputs {0, 1};
35     std::vector<uint32_t> m_outputs {2};
36     std::vector<uint32_t> m_params {};
37     std::vector<int32_t> m_inputDim {1, 3, 1, 1};
38     std::vector<int32_t> m_outputDim {1, 3, 1, 1};
39 };
40 
SetUp()41 void MaximumBuilderTest::SetUp() {}
42 
TearDown()43 void MaximumBuilderTest::TearDown() {}
44 
45 
46 /**
47  * @tc.name: maximum_build_001
48  * @tc.desc: Verify that the build function returns a successful message.
49  * @tc.type: FUNC
50  */
51 HWTEST_F(MaximumBuilderTest, maximum_build_001, TestSize.Level0)
52 {
53     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
54     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
55 
56     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
57     EXPECT_EQ(OH_NN_SUCCESS, ret);
58 }
59 
60 /**
61  * @tc.name: maximum_build_002
62  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(MaximumBuilderTest, maximum_build_002, TestSize.Level0)
66 {
67     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
68     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
69 
70     EXPECT_EQ(OH_NN_SUCCESS, m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
71     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
72     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
73 }
74 
75 /**
76  * @tc.name: maximum_build_003
77  * @tc.desc: Verify that the build function returns a failed message with invalided input.
78  * @tc.type: FUNC
79  */
80 HWTEST_F(MaximumBuilderTest, maximum_build_003, TestSize.Level0)
81 {
82     m_inputs = {0, 1, 2};
83     m_outputs = {3};
84 
85     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
86     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
87 
88     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
89     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
90 }
91 
92 /**
93  * @tc.name: maximum_build_004
94  * @tc.desc: Verify that the build function returns a failed message with invalided output.
95  * @tc.type: FUNC
96  */
97 HWTEST_F(MaximumBuilderTest, maximum_build_004, TestSize.Level0)
98 {
99     m_outputs = {2, 3, 4};
100 
101     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
102     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
103 
104     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
105     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
106 }
107 
108 /**
109  * @tc.name: maximum_build_005
110  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(MaximumBuilderTest, maximum_build_005, TestSize.Level0)
114 {
115     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputs, m_outputs, m_allTensors);
116     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
117 }
118 
119 /**
120  * @tc.name: maximum_build_006
121  * @tc.desc: Verify that the build function returns a failed message without output tensor.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(MaximumBuilderTest, maximum_build_006, TestSize.Level0)
125 {
126     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
127 
128     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
129     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
130 }
131 
132 /**
133  * @tc.name: maximum_build_007
134  * @tc.desc: Verify that the build function returns a failed message with a virtual parameter.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(MaximumBuilderTest, maximum_build_007, TestSize.Level0)
138 {
139     std::vector<uint32_t> m_params = {3};
140     std::vector<int32_t> paramDim = {};
141 
142     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
143     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
144     std::shared_ptr<NNTensor> paramTensor;
145     paramTensor = TransToNNTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_TENSOR);
146     m_allTensors.emplace_back(paramTensor);
147 
148     OH_NN_ReturnCode ret = m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
149     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151 
152 /**
153  * @tc.name: maximum_getprimitive_001
154  * @tc.desc: Verify that the getPrimitive function returns a successful message
155  * @tc.type: FUNC
156  */
157 HWTEST_F(MaximumBuilderTest, maximum_getprimitive_001, TestSize.Level0)
158 {
159     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
160     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
161 
162     EXPECT_EQ(OH_NN_SUCCESS, m_maximum.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
163     LiteGraphPrimitvePtr primitive = m_maximum.GetPrimitive();
164     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
165     EXPECT_NE(expectPrimitive, primitive);
166 }
167 
168 /**
169  * @tc.name: maximum_getprimitive_002
170  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(MaximumBuilderTest, maximum_getprimitive_002, TestSize.Level0)
174 {
175     MaximumBuilder maximum;
176     LiteGraphPrimitvePtr primitive = m_maximum.GetPrimitive();
177     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
178     EXPECT_EQ(expectPrimitive, primitive);
179 }
180 }
181 }
182 }