1  /*
2   * Copyright (c) 2024 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/cos_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 CosBuilderTest : public OpsTest {
28  public:
29      void SetUp() override;
30      void TearDown() override;
31  
32  protected:
33      CosBuilder m_builder;
34      std::vector<uint32_t> m_inputs {0};
35      std::vector<uint32_t> m_outputs {1};
36      std::vector<int32_t> m_dim {1, 2, 2, 1};
37  };
38  
SetUp()39  void CosBuilderTest::SetUp() {}
40  
TearDown()41  void CosBuilderTest::TearDown() {}
42  
43  /**
44   * @tc.name: cos_build_001
45   * @tc.desc: Verify that the build function returns a successful message.
46   * @tc.type: FUNC
47   */
48  HWTEST_F(CosBuilderTest, cos_build_001, TestSize.Level1)
49  {
50      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
51      SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
52  
53      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
54      EXPECT_EQ(OH_NN_SUCCESS, ret);
55  }
56  
57  /**
58   * @tc.name: cos_build_002
59   * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
60   * @tc.type: FUNC
61   */
62  HWTEST_F(CosBuilderTest, cos_build_002, TestSize.Level1)
63  {
64      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
65      SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
66  
67      EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
68      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
69      EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
70  }
71  
72  /**
73   * @tc.name: cos_build_003
74   * @tc.desc: Verify that the build function returns a failed message with invalided input.
75   * @tc.type: FUNC
76   */
77  HWTEST_F(CosBuilderTest, cos_build_003, TestSize.Level1)
78  {
79      m_inputs = {0, 1};
80      m_outputs = {2};
81  
82      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
83      SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
84  
85      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
86      EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
87  }
88  
89  /**
90   * @tc.name: cos_build_004
91   * @tc.desc: Verify that the build function returns a failed message with invalided output.
92   * @tc.type: FUNC
93   */
94  HWTEST_F(CosBuilderTest, cos_build_004, TestSize.Level1)
95  {
96      m_outputs = {1, 2};
97  
98      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
99      SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
100  
101      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
102      EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
103  }
104  
105  /**
106   * @tc.name: cos_build_005
107   * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
108   * @tc.type: FUNC
109   */
110  HWTEST_F(CosBuilderTest, cos_build_005, TestSize.Level1)
111  {
112      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
113      EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
114  }
115  
116  /**
117   * @tc.name: cos_build_006
118   * @tc.desc: Verify that the build function returns a failed message without output tensor.
119   * @tc.type: FUNC
120   */
121  HWTEST_F(CosBuilderTest, cos_build_006, TestSize.Level1)
122  {
123      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
124  
125      OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputs, m_allTensors);
126      EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127  }
128  
129  /**
130   * @tc.name: cos_getprimitive_001
131   * @tc.desc: Verify that the getPrimitive function returns a successful message
132   * @tc.type: FUNC
133   */
134  HWTEST_F(CosBuilderTest, cos_getprimitive_001, TestSize.Level1)
135  {
136      SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
137      SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
138  
139      EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
140      LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
141      LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
142      EXPECT_NE(expectPrimitive, primitive);
143  }
144  
145  /**
146   * @tc.name: cos_getprimitive_002
147   * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
148   * @tc.type: FUNC
149   */
150  HWTEST_F(CosBuilderTest, cos_getprimitive_002, TestSize.Level1)
151  {
152      LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
153      LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
154      EXPECT_EQ(expectPrimitive, primitive);
155  }
156  }
157  }
158  }