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 <gtest/gtest.h>
17 #include <iostream>
18 #include <functional>
19 #include <string>
20 
21 #include "interfaces/kits/c/neural_network_runtime/neural_network_runtime.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace SystemTest {
29 class DeviceTest : public testing::Test {
30 public:
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 
34 public:
35     std::string m_deviceName {"RK3568-CPU_Rockchip"};
36     size_t m_deviceId {std::hash<std::string>{}("RK3568-CPU_Rockchip")};
37     OH_NN_DeviceType m_deviceType {OH_NN_CPU};
38 };
39 
40 /*
41  * @tc.name: device_001
42  * @tc.desc: Get all devices id successfully.
43  * @tc.type: FUNC
44  */
45 HWTEST_F(DeviceTest, device_001, testing::ext::TestSize.Level1)
46 {
47     const size_t* allDeviceIds = nullptr;
48     uint32_t count {0};
49 
50     OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(&allDeviceIds, &count);
51     EXPECT_EQ(OH_NN_SUCCESS, ret);
52 
53     uint32_t expectCount = 1;
54     EXPECT_EQ(expectCount, count);
55     EXPECT_EQ(m_deviceId, *allDeviceIds);
56 }
57 
58 /*
59  * @tc.name: device_002
60  * @tc.desc: Get all devices id with nullptr deviceId parameter.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(DeviceTest, device_002, testing::ext::TestSize.Level1)
64 {
65     uint32_t count {0};
66 
67     OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(nullptr, &count);
68     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
69 }
70 
71 /*
72  * @tc.name: device_003
73  * @tc.desc: Get all devices id with nullptr count parameter.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(DeviceTest, device_003, testing::ext::TestSize.Level1)
77 {
78     const size_t* allDeviceIds = nullptr;
79 
80     OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(&allDeviceIds, nullptr);
81     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
82 }
83 
84 /*
85  * @tc.name: device_004
86  * @tc.desc: Get all devices id with not nullptr deviceId pointer.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(DeviceTest, device_004, testing::ext::TestSize.Level1)
90 {
91     const size_t allDeviceIds = 0;
92     const size_t* pAllDeviceIds = &allDeviceIds;
93     uint32_t count {0};
94 
95     OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(&pAllDeviceIds, &count);
96     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
97 }
98 
99 /*
100  * @tc.name: device_005
101  * @tc.desc: Get device name successfully.
102  * @tc.type: FUNC
103  */
104 HWTEST_F(DeviceTest, device_005, testing::ext::TestSize.Level1)
105 {
106     const char* name = nullptr;
107     OH_NN_ReturnCode ret = OH_NNDevice_GetName(m_deviceId, &name);
108     EXPECT_EQ(OH_NN_SUCCESS, ret);
109     std::string sName(name);
110     EXPECT_EQ(m_deviceName, sName);
111 }
112 
113 /*
114  * @tc.name: device_006
115  * @tc.desc: Get device name with invalid deviceId.
116  * @tc.type: FUNC
117  */
118 HWTEST_F(DeviceTest, device_006, testing::ext::TestSize.Level1)
119 {
120     const size_t deviceId = 0;
121     const char* name = nullptr;
122     OH_NN_ReturnCode ret = OH_NNDevice_GetName(deviceId, &name);
123     EXPECT_EQ(OH_NN_FAILED, ret);
124 }
125 
126 /*
127  * @tc.name: device_007
128  * @tc.desc: Get device name without nullptr name pointer.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(DeviceTest, device_007, testing::ext::TestSize.Level1)
132 {
133     const size_t deviceId = 0;
134     const char* name = "name";
135     OH_NN_ReturnCode ret = OH_NNDevice_GetName(deviceId, &name);
136     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
137 }
138 
139 /*
140  * @tc.name: device_008
141  * @tc.desc: Get device name with nullptr name parameter.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(DeviceTest, device_008, testing::ext::TestSize.Level1)
145 {
146     const size_t deviceId = 0;
147     OH_NN_ReturnCode ret = OH_NNDevice_GetName(deviceId, nullptr);
148     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
149 }
150 
151 /*
152  * @tc.name: device_009
153  * @tc.desc: Get device type successfully.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(DeviceTest, device_009, testing::ext::TestSize.Level1)
157 {
158     OH_NN_DeviceType type {OH_NN_OTHERS};
159     OH_NN_ReturnCode ret = OH_NNDevice_GetType(m_deviceId, &type);
160     EXPECT_EQ(OH_NN_SUCCESS, ret);
161     EXPECT_EQ(m_deviceType, type);
162 }
163 
164 /*
165  * @tc.name: device_010
166  * @tc.desc: Get device type with invalid deviceId.
167  * @tc.type: FUNC
168  */
169 HWTEST_F(DeviceTest, device_010, testing::ext::TestSize.Level1)
170 {
171     const size_t deviceId = 0;
172     OH_NN_DeviceType type {OH_NN_OTHERS};
173     OH_NN_ReturnCode ret = OH_NNDevice_GetType(deviceId, &type);
174     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
175 }
176 
177 /*
178  * @tc.name: device_011
179  * @tc.desc: Get device type with nullptr type.
180  * @tc.type: FUNC
181  */
182 HWTEST_F(DeviceTest, device_011, testing::ext::TestSize.Level1)
183 {
184     const size_t deviceId = 0;
185     OH_NN_ReturnCode ret = OH_NNDevice_GetType(deviceId, nullptr);
186     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
187 }
188 } // namespace SystemTest
189 } // namespace NeuralNetworkRuntime
190 } // namespace OHOS