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 <stdlib.h> 17#include <search.h> 18 19#include "gtest/gtest.h" 20#include "log.h" 21#include "utils.h" 22 23using namespace testing::ext; 24 25class ActsUtilDataStructApiTest : public testing::Test { 26protected: 27 // SetUpTestCase: Testsuit setup, run before 1st testcase 28 static void SetUpTestCase(void) 29 { 30 } 31 // TearDownTestCase: Testsuit teardown, run after last testcase 32 static void TearDownTestCase(void) 33 { 34 } 35 // Testcase setup 36 virtual void SetUp() 37 { 38 } 39 // Testcase teardown 40 virtual void TearDown() 41 { 42 } 43}; 44 45/* comparison function */ 46static int NumCompare(const void *p1, const void *p2) 47{ 48 return ((*(int *)p1) - (*(int *)p2)); 49} 50 51/** 52* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0100 53* @tc.name test bsearch api with key in array 54* @tc.desc [C- SOFTWARE -0200] 55*/ 56HWTEST_F(ActsUtilDataStructApiTest, testBsearch0100, Function | MediumTest | Level1) { 57 int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 58 int *itemPtr = nullptr; 59 int keyVal; 60 61 keyVal = 5; 62 itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]), 63 sizeof(int), (int (*)(const void *, const void *))NumCompare); 64 ASSERT_TRUE(&numArray[5] == itemPtr) << "ErrInfo: bsearch returnVal:='" << *itemPtr << "'"; 65} 66 67/** 68* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0200 69* @tc.name test bsearch api with key not in array 70* @tc.desc [C- SOFTWARE -0200] 71*/ 72HWTEST_F(ActsUtilDataStructApiTest, testBsearch0200, Function | MediumTest | Level1) { 73 int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 74 int *itemPtr = nullptr; 75 int keyVal; 76 77 keyVal = 10; 78 itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]), 79 sizeof(int), (int (*)(const void *, const void *))NumCompare); 80 ASSERT_TRUE(nullptr == itemPtr) << "ErrInfo: bsearch returnVal:='" << *itemPtr << "'"; 81} 82 83/** 84* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0300 85* @tc.name test hcreate api with key and value in hash 86* @tc.desc [C- SOFTWARE -0200] 87*/ 88HWTEST_F(ActsUtilDataStructApiTest, testHcreate0300, Function | MediumTest | Level1) { 89 int createResult; 90 size_t netVal = 10; 91 ENTRY item; 92 ENTRY *searchResult = nullptr; 93 char hashKey[10] = "key1"; 94 char hashValue[10] = "value1"; 95 96 createResult = hcreate(netVal); 97 LOGD(" hcreate createResult:='%d'\n", createResult); 98 ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'"; 99 100 item.key = hashKey; 101 item.data = hashValue; 102 103 searchResult = hsearch(item, ENTER); 104 ASSERT_TRUE(searchResult != nullptr) << "ErrInfo: hsearch searchResult:='" << searchResult << "'"; 105 106 item.key = hashKey; 107 searchResult = hsearch(item, FIND); 108 LOGD(" hsearch searchResult->data:='%s'\n", searchResult->data); 109 ASSERT_TRUE(searchResult != nullptr && strcmp((const char *)searchResult->data, "value1") == 0) 110 << "ErrInfo: hsearch searchResult->data:='" << searchResult->data << "'"; 111 112 hdestroy(); 113} 114 115/** 116* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0400 117* @tc.name test hcreate api with key and value not in hash 118* @tc.desc [C- SOFTWARE -0200] 119*/ 120HWTEST_F(ActsUtilDataStructApiTest, testHcreate0400, Function | MediumTest | Level1) { 121 int createResult; 122 size_t netVal = 10; 123 ENTRY item; 124 ENTRY *searchResult = nullptr; 125 char hashKey[10] = "key1"; 126 127 createResult = hcreate(netVal); 128 LOGD(" hcreate createResult:='%d'\n", createResult); 129 ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'"; 130 131 item.key = hashKey; 132 searchResult = hsearch(item, FIND); 133 ASSERT_TRUE(searchResult == nullptr) 134 << "ErrInfo: hsearch searchResult:='" << searchResult << "'"; 135 136 hdestroy(); 137} 138 139/** 140* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0500 141* @tc.name test hcreate_r api with key and value in hash 142* @tc.desc [C- SOFTWARE -0200] 143*/ 144HWTEST_F(ActsUtilDataStructApiTest, testHcreateR0500, Function | MediumTest | Level1) { 145 int createResult; 146 int returnVal = 0; 147 size_t netVal = 10; 148 ENTRY item; 149 ENTRY *searchResult = nullptr; 150 char hashKey[10] = "key1"; 151 char hashValue[10] = "value1"; 152 struct hsearch_data hTab = {0}; 153 154 createResult = hcreate_r(netVal, &hTab); 155 LOGD(" hcreate_r createResult:='%d'\n", createResult); 156 ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'"; 157 158 item.key = hashKey; 159 item.data = hashValue; 160 161 returnVal = hsearch_r(item, ENTER, &searchResult, &hTab); 162 ASSERT_TRUE(returnVal != 0) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'"; 163 164 item.key = hashKey; 165 returnVal = hsearch_r(item, FIND, &searchResult, &hTab); 166 LOGD(" hsearch_r searchResult->data:='%s'\n", searchResult->data); 167 ASSERT_TRUE(returnVal != 0 && strcmp((const char *)searchResult->data, "value1") == 0) 168 << "ErrInfo: hsearch_r searchResult->data:='" << searchResult->data << "'"; 169 170 hdestroy_r(&hTab); 171} 172 173/** 174* @tc.number SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0600 175* @tc.name test hcreate_r api with key and value not in hash 176* @tc.desc [C- SOFTWARE -0200] 177*/ 178HWTEST_F(ActsUtilDataStructApiTest, testHcreateR0600, Function | MediumTest | Level1) { 179 int createResult; 180 int returnVal = 0; 181 size_t netVal = 10; 182 ENTRY item; 183 ENTRY *searchResult = nullptr; 184 char hashKey[10] = "key1"; 185 struct hsearch_data hTab = {0}; 186 createResult = hcreate_r(netVal, &hTab); 187 LOGD(" hcreate_r createResult:='%d'\n", createResult); 188 ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'"; 189 190 item.key = hashKey; 191 returnVal = hsearch_r(item, FIND, &searchResult, &hTab); 192 LOGD(" hsearch_r searchResult='%s'\n", searchResult); 193 ASSERT_TRUE(0 == returnVal) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'"; 194 195 hdestroy_r(&hTab); 196} 197