1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ci#include <stdlib.h> 17f6603c60Sopenharmony_ci#include <search.h> 18f6603c60Sopenharmony_ci 19f6603c60Sopenharmony_ci#include "gtest/gtest.h" 20f6603c60Sopenharmony_ci#include "log.h" 21f6603c60Sopenharmony_ci#include "utils.h" 22f6603c60Sopenharmony_ci 23f6603c60Sopenharmony_ciusing namespace testing::ext; 24f6603c60Sopenharmony_ci 25f6603c60Sopenharmony_ciclass ActsUtilDataStructApiTest : public testing::Test { 26f6603c60Sopenharmony_ciprotected: 27f6603c60Sopenharmony_ci // SetUpTestCase: Testsuit setup, run before 1st testcase 28f6603c60Sopenharmony_ci static void SetUpTestCase(void) 29f6603c60Sopenharmony_ci { 30f6603c60Sopenharmony_ci } 31f6603c60Sopenharmony_ci // TearDownTestCase: Testsuit teardown, run after last testcase 32f6603c60Sopenharmony_ci static void TearDownTestCase(void) 33f6603c60Sopenharmony_ci { 34f6603c60Sopenharmony_ci } 35f6603c60Sopenharmony_ci // Testcase setup 36f6603c60Sopenharmony_ci virtual void SetUp() 37f6603c60Sopenharmony_ci { 38f6603c60Sopenharmony_ci } 39f6603c60Sopenharmony_ci // Testcase teardown 40f6603c60Sopenharmony_ci virtual void TearDown() 41f6603c60Sopenharmony_ci { 42f6603c60Sopenharmony_ci } 43f6603c60Sopenharmony_ci}; 44f6603c60Sopenharmony_ci 45f6603c60Sopenharmony_ci/* comparison function */ 46f6603c60Sopenharmony_cistatic int NumCompare(const void *p1, const void *p2) 47f6603c60Sopenharmony_ci{ 48f6603c60Sopenharmony_ci return ((*(int *)p1) - (*(int *)p2)); 49f6603c60Sopenharmony_ci} 50f6603c60Sopenharmony_ci 51f6603c60Sopenharmony_ci/** 52f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0100 53f6603c60Sopenharmony_ci* @tc.name test bsearch api with key in array 54f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 55f6603c60Sopenharmony_ci*/ 56f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testBsearch0100, Function | MediumTest | Level1) { 57f6603c60Sopenharmony_ci int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 58f6603c60Sopenharmony_ci int *itemPtr = nullptr; 59f6603c60Sopenharmony_ci int keyVal; 60f6603c60Sopenharmony_ci 61f6603c60Sopenharmony_ci keyVal = 5; 62f6603c60Sopenharmony_ci itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]), 63f6603c60Sopenharmony_ci sizeof(int), (int (*)(const void *, const void *))NumCompare); 64f6603c60Sopenharmony_ci ASSERT_TRUE(&numArray[5] == itemPtr) << "ErrInfo: bsearch returnVal:='" << *itemPtr << "'"; 65f6603c60Sopenharmony_ci} 66f6603c60Sopenharmony_ci 67f6603c60Sopenharmony_ci/** 68f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0200 69f6603c60Sopenharmony_ci* @tc.name test bsearch api with key not in array 70f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 71f6603c60Sopenharmony_ci*/ 72f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testBsearch0200, Function | MediumTest | Level1) { 73f6603c60Sopenharmony_ci int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 74f6603c60Sopenharmony_ci int *itemPtr = nullptr; 75f6603c60Sopenharmony_ci int keyVal; 76f6603c60Sopenharmony_ci 77f6603c60Sopenharmony_ci keyVal = 10; 78f6603c60Sopenharmony_ci itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]), 79f6603c60Sopenharmony_ci sizeof(int), (int (*)(const void *, const void *))NumCompare); 80f6603c60Sopenharmony_ci ASSERT_TRUE(nullptr == itemPtr) << "ErrInfo: bsearch returnVal:='" << *itemPtr << "'"; 81f6603c60Sopenharmony_ci} 82f6603c60Sopenharmony_ci 83f6603c60Sopenharmony_ci/** 84f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0300 85f6603c60Sopenharmony_ci* @tc.name test hcreate api with key and value in hash 86f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 87f6603c60Sopenharmony_ci*/ 88f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testHcreate0300, Function | MediumTest | Level1) { 89f6603c60Sopenharmony_ci int createResult; 90f6603c60Sopenharmony_ci size_t netVal = 10; 91f6603c60Sopenharmony_ci ENTRY item; 92f6603c60Sopenharmony_ci ENTRY *searchResult = nullptr; 93f6603c60Sopenharmony_ci char hashKey[10] = "key1"; 94f6603c60Sopenharmony_ci char hashValue[10] = "value1"; 95f6603c60Sopenharmony_ci 96f6603c60Sopenharmony_ci createResult = hcreate(netVal); 97f6603c60Sopenharmony_ci LOGD(" hcreate createResult:='%d'\n", createResult); 98f6603c60Sopenharmony_ci ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'"; 99f6603c60Sopenharmony_ci 100f6603c60Sopenharmony_ci item.key = hashKey; 101f6603c60Sopenharmony_ci item.data = hashValue; 102f6603c60Sopenharmony_ci 103f6603c60Sopenharmony_ci searchResult = hsearch(item, ENTER); 104f6603c60Sopenharmony_ci ASSERT_TRUE(searchResult != nullptr) << "ErrInfo: hsearch searchResult:='" << searchResult << "'"; 105f6603c60Sopenharmony_ci 106f6603c60Sopenharmony_ci item.key = hashKey; 107f6603c60Sopenharmony_ci searchResult = hsearch(item, FIND); 108f6603c60Sopenharmony_ci LOGD(" hsearch searchResult->data:='%s'\n", searchResult->data); 109f6603c60Sopenharmony_ci ASSERT_TRUE(searchResult != nullptr && strcmp((const char *)searchResult->data, "value1") == 0) 110f6603c60Sopenharmony_ci << "ErrInfo: hsearch searchResult->data:='" << searchResult->data << "'"; 111f6603c60Sopenharmony_ci 112f6603c60Sopenharmony_ci hdestroy(); 113f6603c60Sopenharmony_ci} 114f6603c60Sopenharmony_ci 115f6603c60Sopenharmony_ci/** 116f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0400 117f6603c60Sopenharmony_ci* @tc.name test hcreate api with key and value not in hash 118f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 119f6603c60Sopenharmony_ci*/ 120f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testHcreate0400, Function | MediumTest | Level1) { 121f6603c60Sopenharmony_ci int createResult; 122f6603c60Sopenharmony_ci size_t netVal = 10; 123f6603c60Sopenharmony_ci ENTRY item; 124f6603c60Sopenharmony_ci ENTRY *searchResult = nullptr; 125f6603c60Sopenharmony_ci char hashKey[10] = "key1"; 126f6603c60Sopenharmony_ci 127f6603c60Sopenharmony_ci createResult = hcreate(netVal); 128f6603c60Sopenharmony_ci LOGD(" hcreate createResult:='%d'\n", createResult); 129f6603c60Sopenharmony_ci ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'"; 130f6603c60Sopenharmony_ci 131f6603c60Sopenharmony_ci item.key = hashKey; 132f6603c60Sopenharmony_ci searchResult = hsearch(item, FIND); 133f6603c60Sopenharmony_ci ASSERT_TRUE(searchResult == nullptr) 134f6603c60Sopenharmony_ci << "ErrInfo: hsearch searchResult:='" << searchResult << "'"; 135f6603c60Sopenharmony_ci 136f6603c60Sopenharmony_ci hdestroy(); 137f6603c60Sopenharmony_ci} 138f6603c60Sopenharmony_ci 139f6603c60Sopenharmony_ci/** 140f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0500 141f6603c60Sopenharmony_ci* @tc.name test hcreate_r api with key and value in hash 142f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 143f6603c60Sopenharmony_ci*/ 144f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testHcreateR0500, Function | MediumTest | Level1) { 145f6603c60Sopenharmony_ci int createResult; 146f6603c60Sopenharmony_ci int returnVal = 0; 147f6603c60Sopenharmony_ci size_t netVal = 10; 148f6603c60Sopenharmony_ci ENTRY item; 149f6603c60Sopenharmony_ci ENTRY *searchResult = nullptr; 150f6603c60Sopenharmony_ci char hashKey[10] = "key1"; 151f6603c60Sopenharmony_ci char hashValue[10] = "value1"; 152f6603c60Sopenharmony_ci struct hsearch_data hTab = {0}; 153f6603c60Sopenharmony_ci 154f6603c60Sopenharmony_ci createResult = hcreate_r(netVal, &hTab); 155f6603c60Sopenharmony_ci LOGD(" hcreate_r createResult:='%d'\n", createResult); 156f6603c60Sopenharmony_ci ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'"; 157f6603c60Sopenharmony_ci 158f6603c60Sopenharmony_ci item.key = hashKey; 159f6603c60Sopenharmony_ci item.data = hashValue; 160f6603c60Sopenharmony_ci 161f6603c60Sopenharmony_ci returnVal = hsearch_r(item, ENTER, &searchResult, &hTab); 162f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal != 0) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'"; 163f6603c60Sopenharmony_ci 164f6603c60Sopenharmony_ci item.key = hashKey; 165f6603c60Sopenharmony_ci returnVal = hsearch_r(item, FIND, &searchResult, &hTab); 166f6603c60Sopenharmony_ci LOGD(" hsearch_r searchResult->data:='%s'\n", searchResult->data); 167f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal != 0 && strcmp((const char *)searchResult->data, "value1") == 0) 168f6603c60Sopenharmony_ci << "ErrInfo: hsearch_r searchResult->data:='" << searchResult->data << "'"; 169f6603c60Sopenharmony_ci 170f6603c60Sopenharmony_ci hdestroy_r(&hTab); 171f6603c60Sopenharmony_ci} 172f6603c60Sopenharmony_ci 173f6603c60Sopenharmony_ci/** 174f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0600 175f6603c60Sopenharmony_ci* @tc.name test hcreate_r api with key and value not in hash 176f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 177f6603c60Sopenharmony_ci*/ 178f6603c60Sopenharmony_ciHWTEST_F(ActsUtilDataStructApiTest, testHcreateR0600, Function | MediumTest | Level1) { 179f6603c60Sopenharmony_ci int createResult; 180f6603c60Sopenharmony_ci int returnVal = 0; 181f6603c60Sopenharmony_ci size_t netVal = 10; 182f6603c60Sopenharmony_ci ENTRY item; 183f6603c60Sopenharmony_ci ENTRY *searchResult = nullptr; 184f6603c60Sopenharmony_ci char hashKey[10] = "key1"; 185f6603c60Sopenharmony_ci struct hsearch_data hTab = {0}; 186f6603c60Sopenharmony_ci createResult = hcreate_r(netVal, &hTab); 187f6603c60Sopenharmony_ci LOGD(" hcreate_r createResult:='%d'\n", createResult); 188f6603c60Sopenharmony_ci ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'"; 189f6603c60Sopenharmony_ci 190f6603c60Sopenharmony_ci item.key = hashKey; 191f6603c60Sopenharmony_ci returnVal = hsearch_r(item, FIND, &searchResult, &hTab); 192f6603c60Sopenharmony_ci LOGD(" hsearch_r searchResult='%s'\n", searchResult); 193f6603c60Sopenharmony_ci ASSERT_TRUE(0 == returnVal) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'"; 194f6603c60Sopenharmony_ci 195f6603c60Sopenharmony_ci hdestroy_r(&hTab); 196f6603c60Sopenharmony_ci} 197