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 
18 #include "quant_param.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class QuantParamsTest : public testing::Test {
28 public:
29     QuantParamsTest() = default;
30     ~QuantParamsTest() = default;
31 };
32 
33 /**
34  * @tc.name: quantparamstest_setscales_001
35  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
36  * @tc.type: FUNC
37  */
38 HWTEST_F(QuantParamsTest, quantparamstest_setscales_001, TestSize.Level0)
39 {
40     QuantParams quantParams;
41     std::vector<double> scales = {1, 2, 3, 4};
42     quantParams.SetScales(scales);
43     EXPECT_EQ(false, quantParams.GetScales().empty());
44 }
45 
46 /**
47  * @tc.name: quantparamstest_setscales_002
48  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
49  * @tc.type: FUNC
50  */
51 HWTEST_F(QuantParamsTest, quantparamstest_setscales_002, TestSize.Level0)
52 {
53     QuantParams quantParams;
54     EXPECT_EQ(true, quantParams.GetScales().empty());
55 }
56 
57 /**
58  * @tc.name: quantparamstest_setzeropoints_001
59  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
60  * @tc.type: FUNC
61  */
62 HWTEST_F(QuantParamsTest, quantparamstest_setzeropoints_001, TestSize.Level0)
63 {
64     QuantParams quantParams;
65     std::vector<int32_t> zeroPoints = {1, 2, 3, 4};
66     quantParams.SetZeroPoints(zeroPoints);
67     EXPECT_EQ(false, quantParams.GetZeroPoints().empty());
68 }
69 
70 /**
71  * @tc.name: quantparamstest_setzeropoints_002
72  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(QuantParamsTest, quantparamstest_setzeropoints_002, TestSize.Level0)
76 {
77     QuantParams quantParams;
78     EXPECT_EQ(true, quantParams.GetZeroPoints().empty());
79 }
80 
81 /**
82  * @tc.name: quantparamstest_setnumbits_001
83  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
84  * @tc.type: FUNC
85  */
86 HWTEST_F(QuantParamsTest, quantparamstest_setnumbits_001, TestSize.Level0)
87 {
88     QuantParams quantParams;
89     std::vector<uint32_t> numBits = {1, 2, 3, 4};
90     quantParams.SetNumBits(numBits);
91     EXPECT_EQ(false, quantParams.GetNumBits().empty());
92 }
93 
94 /**
95  * @tc.name: quantparamstest_setnumbits_002
96  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(QuantParamsTest, quantparamstest_setnumbits_002, TestSize.Level0)
100 {
101     QuantParams quantParams;
102     EXPECT_EQ(true, quantParams.GetNumBits().empty());
103 }
104 
105 /**
106  * @tc.name: quantparamstest_copytocompat_001
107  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(QuantParamsTest, quantparamstest_copytocompat_001, TestSize.Level0)
111 {
112     QuantParams quantParams;
113     std::vector<double> scales = {1, 2};
114     quantParams.SetScales(scales);
115     std::vector<int32_t> zeroPoints = {1, 2, 3};
116     quantParams.SetZeroPoints(zeroPoints);
117     std::vector<uint32_t> numBits = {1, 2, 3, 4};
118     quantParams.SetNumBits(numBits);
119     std::vector<OHOS::NeuralNetworkRuntime::QuantParam> compatQuantParams;
120     EXPECT_EQ(OH_NN_INVALID_PARAMETER, quantParams.CopyToCompat(compatQuantParams));
121 }
122 
123 /**
124  * @tc.name: quantparamstest_copytocompat_002
125  * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.
126  * @tc.type: FUNC
127  */
128 HWTEST_F(QuantParamsTest, quantparamstest_copytocompat_002, TestSize.Level0)
129 {
130     QuantParams quantParams;
131     std::vector<double> scales = {1, 2, 3, 4};
132     quantParams.SetScales(scales);
133     std::vector<int32_t> zeroPoints = {1, 2, 3, 4};
134     quantParams.SetZeroPoints(zeroPoints);
135     std::vector<uint32_t> numBits = {1, 2, 3, 4};
136     quantParams.SetNumBits(numBits);
137     std::vector<OHOS::NeuralNetworkRuntime::QuantParam> compatQuantParams;
138     EXPECT_EQ(OH_NN_SUCCESS, quantParams.CopyToCompat(compatQuantParams));
139 }
140 
141 } // namespace UnitTest
142 } // namespace NeuralNetworkRuntime
143 } // namespace OHOS