1/*
2 * Copyright (c) 2021 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 <cinttypes>
17#include <gtest/gtest.h>
18#include "init_utils.h"
19
20using namespace testing::ext;
21using namespace std;
22
23namespace init_ut {
24class StrUtilUnitTest : public testing::Test {
25public:
26    static void SetUpTestCase(void) {};
27    static void TearDownTestCase(void) {};
28    void SetUp() {};
29    void TearDown() {};
30};
31
32HWTEST_F(StrUtilUnitTest, StrArrayGetIndex_unit_test, TestSize.Level1)
33{
34    int ret;
35    const char *strArray[] = { "a1", "a2", "a3", NULL};
36
37    // Invalid arguments test
38    ret = OH_StrArrayGetIndex(NULL, "test", 0);
39    EXPECT_EQ(ret, -1);
40    ret = OH_StrArrayGetIndex(NULL, NULL, 0);
41    EXPECT_EQ(ret, -1);
42    ret = OH_StrArrayGetIndex(strArray, NULL, 0);
43    EXPECT_EQ(ret, -1);
44
45    // Matched
46    ret = OH_StrArrayGetIndex(strArray, "a1", 0);
47    EXPECT_EQ(ret, 0);
48    ret = OH_StrArrayGetIndex(strArray, "a2", 0);
49    EXPECT_EQ(ret, 1);
50    ret = OH_StrArrayGetIndex(strArray, "a3", 0);
51    EXPECT_EQ(ret, 2);
52
53    // Not matched
54    ret = OH_StrArrayGetIndex(strArray, "aa1", 0);
55    EXPECT_EQ(ret, -1);
56    ret = OH_StrArrayGetIndex(strArray, "A1", 0);
57    EXPECT_EQ(ret, -1);
58    ret = OH_StrArrayGetIndex(strArray, "A2", 0);
59    EXPECT_EQ(ret, -1);
60    ret = OH_StrArrayGetIndex(strArray, "A3", 0);
61    EXPECT_EQ(ret, -1);
62
63    // Ignore case
64    ret = OH_StrArrayGetIndex(strArray, "A1", 1);
65    EXPECT_EQ(ret, 0);
66    ret = OH_StrArrayGetIndex(strArray, "A2", 2);
67    EXPECT_EQ(ret, 1);
68    ret = OH_StrArrayGetIndex(strArray, "A3", 3);
69    EXPECT_EQ(ret, 2);
70}
71
72HWTEST_F(StrUtilUnitTest, OH_ExtendableStrArrayGetIndex_unitest, TestSize.Level1)
73{
74    int ret;
75    const char *strArray[] = { "a1", "a2", "a3", NULL};
76    const char *extendStrArray[] = { "a4", "a5", "a6", NULL};
77
78    // Matched
79    ret = OH_ExtendableStrArrayGetIndex(strArray, "a4", 0, extendStrArray);
80    EXPECT_EQ(ret, 3);
81    ret = OH_ExtendableStrArrayGetIndex(strArray, "a5", 0, extendStrArray);
82    EXPECT_EQ(ret, 4);
83    ret = OH_ExtendableStrArrayGetIndex(strArray, "a6", 0, extendStrArray);
84    EXPECT_EQ(ret, 5);
85
86    // Not matched
87    ret = OH_ExtendableStrArrayGetIndex(strArray, "aa1", 0, extendStrArray);
88    EXPECT_EQ(ret, -1);
89    ret = OH_ExtendableStrArrayGetIndex(strArray, "A4", 0, extendStrArray);
90    EXPECT_EQ(ret, -1);
91    ret = OH_ExtendableStrArrayGetIndex(strArray, "A5", 0, extendStrArray);
92    EXPECT_EQ(ret, -1);
93    ret = OH_ExtendableStrArrayGetIndex(strArray, "A6", 0, extendStrArray);
94    EXPECT_EQ(ret, -1);
95
96    // Ignore case
97    ret = OH_ExtendableStrArrayGetIndex(strArray, "A4", 1, extendStrArray);
98    EXPECT_EQ(ret, 3);
99    ret = OH_ExtendableStrArrayGetIndex(strArray, "A5", 2, extendStrArray);
100    EXPECT_EQ(ret, 4);
101    ret = OH_ExtendableStrArrayGetIndex(strArray, "A6", 3, extendStrArray);
102    EXPECT_EQ(ret, 5);
103}
104
105HWTEST_F(StrUtilUnitTest, StrDictGet_unit_test_str_array, TestSize.Level1)
106{
107    int ret;
108    void *res;
109    const char *strArray[] = { "a1", "a2", "a3", NULL};
110
111    // Invalid arguments test
112    res = OH_StrDictGet(NULL, 0, "test", 0);
113    EXPECT_EQ(res, NULL);
114    res = OH_StrDictGet(NULL, 0, NULL, 0);
115    EXPECT_EQ(res, NULL);
116    res = OH_StrDictGet((void **)strArray, 1, NULL, 0);
117    EXPECT_EQ(res, NULL);
118    res = OH_StrDictGet((void **)strArray, 3, NULL, 0);
119    EXPECT_EQ(res, NULL);
120
121    // Matched
122    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "a1", 0);
123    ASSERT_NE(res, nullptr);
124    ret = strcmp(*(const char **)res, "a1");
125    EXPECT_EQ(ret, 0);
126    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "a2", 0);
127    ASSERT_NE(res, nullptr);
128    ret = strcmp(*(const char **)res, "a2");
129    EXPECT_EQ(ret, 0);
130    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "a3", 0);
131    ASSERT_NE(res, nullptr);
132    ret = strcmp(*(const char **)res, "a3");
133    EXPECT_EQ(ret, 0);
134
135    // Not matched
136    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "aa1", 0);
137    EXPECT_EQ(res, nullptr);
138    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A1", 0);
139    EXPECT_EQ(res, nullptr);
140    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A2", 0);
141    EXPECT_EQ(res, nullptr);
142    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A3", 0);
143    EXPECT_EQ(res, nullptr);
144
145    // Ignore case
146    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A1", 1);
147    ASSERT_NE(res, nullptr);
148    ret = strcmp(*(const char **)res, "a1");
149    EXPECT_EQ(ret, 0);
150    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A2", 2);
151    ASSERT_NE(res, nullptr);
152    ret = strcmp(*(const char **)res, "a2");
153    EXPECT_EQ(ret, 0);
154    res = OH_StrDictGet((void **)strArray, sizeof(const char *), "A3", 3);
155    ASSERT_NE(res, nullptr);
156    ret = strcmp(*(const char **)res, "a3");
157    EXPECT_EQ(ret, 0);
158}
159
160using STRING_INT_DICT = struct {
161    const char *key;
162    int val;
163};
164
165HWTEST_F(StrUtilUnitTest, StrDictGet_unit_test_str_int_dict, TestSize.Level1)
166{
167    const STRING_INT_DICT *res;
168    const STRING_INT_DICT strIntDict[] = {
169        {"a1", 1},
170        {"a2", 2},
171        {"a3", 3},
172        {NULL, 0}
173    };
174
175    // Matched
176    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "a1", 0);
177    ASSERT_NE(res, nullptr);
178    EXPECT_EQ(res->val, 1);
179    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "a2", 0);
180    ASSERT_NE(res, nullptr);
181    EXPECT_EQ(res->val, 2);
182    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "a3", 0);
183    ASSERT_NE(res, nullptr);
184    EXPECT_EQ(res->val, 3);
185
186    // Not matched
187    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "aa1", 0);
188    EXPECT_EQ(res, nullptr);
189    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A1", 0);
190    EXPECT_EQ(res, nullptr);
191    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A2", 0);
192    EXPECT_EQ(res, nullptr);
193    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A3", 0);
194    EXPECT_EQ(res, nullptr);
195
196    // Ignore case
197    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A1", 3);
198    ASSERT_NE(res, nullptr);
199    EXPECT_EQ(res->val, 1);
200    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A2", 2);
201    ASSERT_NE(res, nullptr);
202    EXPECT_EQ(res->val, 2);
203    res = (const STRING_INT_DICT *)OH_StrDictGet((void **)strIntDict, sizeof(STRING_INT_DICT), "A3", 1);
204    ASSERT_NE(res, nullptr);
205    EXPECT_EQ(res->val, 3);
206}
207
208using STRING_STRING_DICT = struct {
209    const char *key;
210    const char *val;
211};
212
213HWTEST_F(StrUtilUnitTest, StrDictGet_unit_test_str_str_dict, TestSize.Level1)
214{
215    int ret;
216    const STRING_STRING_DICT *res;
217    const STRING_STRING_DICT strStrDict[] = {
218        {"a1", "val1"},
219        {"a2", "val2"},
220        {"a3", "val3"},
221        {NULL, NULL}
222    };
223
224    // Matched
225    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
226        sizeof(STRING_STRING_DICT), "a1", 0);
227    ASSERT_NE(res, nullptr);
228    ret = strcmp(res->val, "val1");
229    EXPECT_EQ(ret, 0);
230    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
231        sizeof(STRING_STRING_DICT), "a2", 0);
232    ASSERT_NE(res, nullptr);
233    ret = strcmp(res->val, "val2");
234    EXPECT_EQ(ret, 0);
235    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
236        sizeof(STRING_STRING_DICT), "a3", 0);
237    ASSERT_NE(res, nullptr);
238    ret = strcmp(res->val, "val3");
239    EXPECT_EQ(ret, 0);
240
241    // Not matched
242    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
243        sizeof(STRING_STRING_DICT), "aa1", 0);
244    EXPECT_EQ(res, nullptr);
245    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
246        sizeof(STRING_STRING_DICT), "A1", 0);
247    EXPECT_EQ(res, nullptr);
248    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
249        sizeof(STRING_STRING_DICT), "A2", 0);
250    EXPECT_EQ(res, nullptr);
251    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
252        sizeof(STRING_STRING_DICT), "A3", 0);
253    EXPECT_EQ(res, nullptr);
254
255    // Ignore case
256    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
257        sizeof(STRING_STRING_DICT), "A1", 3);
258    ASSERT_NE(res, nullptr);
259    ret = strcmp(res->val, "val1");
260    EXPECT_EQ(ret, 0);
261    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
262        sizeof(STRING_STRING_DICT), "A2", 2);
263    ASSERT_NE(res, nullptr);
264    ret = strcmp(res->val, "val2");
265    EXPECT_EQ(ret, 0);
266    res = (const STRING_STRING_DICT *)OH_StrDictGet((void **)strStrDict,
267        sizeof(STRING_STRING_DICT), "A3", 1);
268    ASSERT_NE(res, nullptr);
269    ret = strcmp(res->val, "val3");
270    EXPECT_EQ(ret, 0);
271}
272
273using STRING_HYBRID_DICT = struct {
274    const char *key;
275    int cnt;
276    const char *val;
277};
278
279HWTEST_F(StrUtilUnitTest, StrDictGet_unit_test_str_hybrid_dict, TestSize.Level1)
280{
281    int ret;
282    const STRING_HYBRID_DICT *res;
283    const STRING_HYBRID_DICT strHybridDict[] = {
284        {"a1", 1, "val1"},
285        {"a2", 2, "val2"},
286        {"a3", 3, "val3"},
287        {NULL, 0, NULL}
288    };
289
290    // string array Matched
291    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
292        sizeof(STRING_HYBRID_DICT), "a1", 0);
293    ASSERT_NE(res, nullptr);
294    ret = strcmp(res->val, "val1");
295    EXPECT_EQ(ret, 0);
296    EXPECT_EQ(res->cnt, 1);
297    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
298        sizeof(STRING_HYBRID_DICT), "a2", 0);
299    ASSERT_NE(res, nullptr);
300    ret = strcmp(res->val, "val2");
301    EXPECT_EQ(ret, 0);
302    EXPECT_EQ(res->cnt, 2);
303    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
304        sizeof(STRING_HYBRID_DICT), "a3", 0);
305    ASSERT_NE(res, nullptr);
306    ret = strcmp(res->val, "val3");
307    EXPECT_EQ(ret, 0);
308    EXPECT_EQ(res->cnt, 3);
309
310    // Not matched
311    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
312        sizeof(STRING_HYBRID_DICT), "aa1", 0);
313    EXPECT_EQ(res, nullptr);
314    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
315        sizeof(STRING_HYBRID_DICT), "A1", 0);
316    EXPECT_EQ(res, nullptr);
317    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
318        sizeof(STRING_HYBRID_DICT), "A2", 0);
319    EXPECT_EQ(res, nullptr);
320    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
321        sizeof(STRING_HYBRID_DICT), "A3", 0);
322    EXPECT_EQ(res, nullptr);
323
324    // Ignore case
325    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
326        sizeof(STRING_HYBRID_DICT), "A1", 3);
327    ASSERT_NE(res, nullptr);
328    ret = strcmp(res->val, "val1");
329    EXPECT_EQ(ret, 0);
330    EXPECT_EQ(res->cnt, 1);
331    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
332        sizeof(STRING_HYBRID_DICT), "A2", 2);
333    ASSERT_NE(res, nullptr);
334    ret = strcmp(res->val, "val2");
335    EXPECT_EQ(ret, 0);
336    EXPECT_EQ(res->cnt, 2);
337    res = (const STRING_HYBRID_DICT *)OH_StrDictGet((void **)strHybridDict,
338        sizeof(STRING_HYBRID_DICT), "A3", 1);
339    ASSERT_NE(res, nullptr);
340    ret = strcmp(res->val, "val3");
341    EXPECT_EQ(ret, 0);
342    EXPECT_EQ(res->cnt, 3);
343}
344
345HWTEST_F(StrUtilUnitTest, ExtendableStrDictGet_unit_test, TestSize.Level1)
346{
347    int ret;
348    const STRING_HYBRID_DICT *res;
349    const STRING_HYBRID_DICT strHybridDict[] = {
350        {"a1", 1, "val1"},
351        {"a2", 2, "val2"},
352        {"a3", 3, "val3"},
353        {NULL, 0, NULL}
354    };
355    const STRING_HYBRID_DICT extendHybridDict[] = {
356        {"a4", 4, "val4"},
357        {"a5", 5, "val5"},
358        {"a6", 6, "val6"},
359        {NULL, 0, NULL}
360    };
361
362    // string array Matched
363    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
364        sizeof(STRING_HYBRID_DICT), "a4", 0, (void **)extendHybridDict);
365    ASSERT_NE(res, nullptr);
366    ret = strcmp(res->val, "val4");
367    EXPECT_EQ(ret, 0);
368    EXPECT_EQ(res->cnt, 4);
369    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
370        sizeof(STRING_HYBRID_DICT), "a5", 0, (void **)extendHybridDict);
371    ASSERT_NE(res, nullptr);
372    ret = strcmp(res->val, "val5");
373    EXPECT_EQ(ret, 0);
374    EXPECT_EQ(res->cnt, 5);
375    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
376        sizeof(STRING_HYBRID_DICT), "a6", 0, (void **)extendHybridDict);
377    ASSERT_NE(res, nullptr);
378    ret = strcmp(res->val, "val6");
379    EXPECT_EQ(ret, 0);
380    EXPECT_EQ(res->cnt, 6);
381
382    // Not matched
383    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
384        sizeof(STRING_HYBRID_DICT), "aa1", 0, (void **)extendHybridDict);
385    EXPECT_EQ(res, nullptr);
386    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
387        sizeof(STRING_HYBRID_DICT), "A4", 0, (void **)extendHybridDict);
388    EXPECT_EQ(res, nullptr);
389    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
390        sizeof(STRING_HYBRID_DICT), "A5", 0, (void **)extendHybridDict);
391    EXPECT_EQ(res, nullptr);
392    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
393        sizeof(STRING_HYBRID_DICT), "A6", 0, (void **)extendHybridDict);
394    EXPECT_EQ(res, nullptr);
395
396    // Ignore case
397    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
398        sizeof(STRING_HYBRID_DICT), "A4", 3, (void **)extendHybridDict);
399    ASSERT_NE(res, nullptr);
400    ret = strcmp(res->val, "val4");
401    EXPECT_EQ(ret, 0);
402    EXPECT_EQ(res->cnt, 4);
403    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
404        sizeof(STRING_HYBRID_DICT), "A5", 2, (void **)extendHybridDict);
405    ASSERT_NE(res, nullptr);
406    ret = strcmp(res->val, "val5");
407    EXPECT_EQ(ret, 0);
408    EXPECT_EQ(res->cnt, 5);
409    res = (const STRING_HYBRID_DICT *)OH_ExtendableStrDictGet((void **)strHybridDict,
410        sizeof(STRING_HYBRID_DICT), "A6", 1, (void **)extendHybridDict);
411    ASSERT_NE(res, nullptr);
412    ret = strcmp(res->val, "val6");
413    EXPECT_EQ(ret, 0);
414    EXPECT_EQ(res->cnt, 6);
415}
416} // namespace init_ut
417