ActsUtilDataStructApiTest.cpp 6.6 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <search.h>

#include "gtest/gtest.h"
#include "log.h"
#include "utils.h"

using namespace testing::ext;

class ActsUtilDataStructApiTest : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void)
    {
    }
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void)
    {
    }
    // Testcase setup
    virtual void SetUp()
    {
    }
    // Testcase teardown
    virtual void TearDown()
    {
    }
};

M
mamingshuai 已提交
45 46 47 48 49 50
/* comparison function */
static int NumCompare(const void *p1, const void *p2)
{
    return ((*(int *)p1) - (*(int *)p2));
}

W
wenjun 已提交
51 52
/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0100
M
mamingshuai 已提交
53
* @tc.name       test bsearch api with key in array
W
wenjun 已提交
54 55
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
56
HWTEST_F(ActsUtilDataStructApiTest, testBsearch0100, Function | MediumTest | Level1) {
W
wenjun 已提交
57 58 59 60 61 62 63 64 65 66 67 68
    int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int *itemPtr = nullptr;
    int keyVal;

    keyVal = 5;
    itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]),
        sizeof(int), (int (*)(const void *, const void *))NumCompare);
    ASSERT_TRUE(&numArray[5] == itemPtr) << "ErrInfo: bsearch  returnVal:='" << *itemPtr << "'";
}

/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_BSEARCH_0200
M
mamingshuai 已提交
69
* @tc.name       test bsearch api with key not in array
W
wenjun 已提交
70 71
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
72
HWTEST_F(ActsUtilDataStructApiTest, testBsearch0200, Function | MediumTest | Level1) {
W
wenjun 已提交
73 74 75 76 77 78 79 80 81 82 83 84
    int numArray[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int *itemPtr = nullptr;
    int keyVal;

    keyVal = 10;
    itemPtr = (int *)bsearch(&keyVal, numArray, sizeof(numArray) / sizeof(numArray[0]),
        sizeof(int), (int (*)(const void *, const void *))NumCompare);
    ASSERT_TRUE(nullptr == itemPtr) << "ErrInfo: bsearch  returnVal:='" << *itemPtr << "'";
}

/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0300
M
mamingshuai 已提交
85
* @tc.name       test hcreate api with key and value in hash
W
wenjun 已提交
86 87
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
88
HWTEST_F(ActsUtilDataStructApiTest, testHcreate0300, Function | MediumTest | Level1) {
W
wenjun 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    int createResult;
    size_t netVal = 10;
    ENTRY item;
    ENTRY *searchResult = nullptr;
    char hashKey[10] = "key1";
    char hashValue[10] = "value1";

    createResult = hcreate(netVal);
    LOGD("    hcreate createResult:='%d'\n", createResult);
    ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'";

    item.key = hashKey;
    item.data = hashValue;

    searchResult = hsearch(item, ENTER);
    ASSERT_TRUE(searchResult != nullptr) << "ErrInfo: hsearch searchResult:='" << searchResult << "'";

    item.key = hashKey;
    searchResult = hsearch(item, FIND);
    LOGD("    hsearch searchResult->data:='%s'\n", searchResult->data);
    ASSERT_TRUE(searchResult != nullptr && strcmp((const char *)searchResult->data, "value1") == 0)
         << "ErrInfo: hsearch searchResult->data:='" << searchResult->data << "'";

    hdestroy();
}

/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_0400
M
mamingshuai 已提交
117
* @tc.name       test hcreate api with key and value not in hash
W
wenjun 已提交
118 119
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
120
HWTEST_F(ActsUtilDataStructApiTest, testHcreate0400, Function | MediumTest | Level1) {
W
wenjun 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    int createResult;
    size_t netVal = 10;
    ENTRY item;
    ENTRY *searchResult = nullptr;
    char hashKey[10] = "key1";

    createResult = hcreate(netVal);
    LOGD("    hcreate createResult:='%d'\n", createResult);
    ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate returnVal:='" << createResult << "'";

    item.key = hashKey;
    searchResult = hsearch(item, FIND);
    ASSERT_TRUE(searchResult == nullptr)
         << "ErrInfo: hsearch searchResult:='" << searchResult << "'";

    hdestroy();
}

/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0500
M
mamingshuai 已提交
141
* @tc.name       test hcreate_r api with key and value in hash
W
wenjun 已提交
142 143
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
144
HWTEST_F(ActsUtilDataStructApiTest, testHcreateR0500, Function | MediumTest | Level1) {
W
wenjun 已提交
145 146 147 148 149 150 151
    int createResult;
    int returnVal = 0;
    size_t netVal = 10;
    ENTRY item;
    ENTRY *searchResult = nullptr;
    char hashKey[10] = "key1";
    char hashValue[10] = "value1";
M
mamingshuai 已提交
152
    struct hsearch_data hTab = {0};
W
wenjun 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    createResult = hcreate_r(netVal, &hTab);
    LOGD("    hcreate_r createResult:='%d'\n", createResult);
    ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'";

    item.key = hashKey;
    item.data = hashValue;

    returnVal = hsearch_r(item, ENTER, &searchResult, &hTab);
    ASSERT_TRUE(returnVal != 0) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'";

    item.key = hashKey;
    returnVal = hsearch_r(item, FIND, &searchResult, &hTab);
    LOGD("    hsearch_r searchResult->data:='%s'\n", searchResult->data);
    ASSERT_TRUE(returnVal != 0 && strcmp((const char *)searchResult->data, "value1") == 0)
         << "ErrInfo: hsearch_r searchResult->data:='" << searchResult->data << "'";

    hdestroy_r(&hTab);
}

/**
* @tc.number     SUB_KERNEL_UTIL_DATASTRUCT_HCREATE_R_0600
M
mamingshuai 已提交
175
* @tc.name       test hcreate_r api with key and value not in hash
W
wenjun 已提交
176 177
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
178
HWTEST_F(ActsUtilDataStructApiTest, testHcreateR0600, Function | MediumTest | Level1) {
W
wenjun 已提交
179 180 181 182 183 184
    int createResult;
    int returnVal = 0;
    size_t netVal = 10;
    ENTRY item;
    ENTRY *searchResult = nullptr;
    char hashKey[10] = "key1";
M
mamingshuai 已提交
185
    struct hsearch_data hTab = {0};
W
wenjun 已提交
186 187 188 189 190 191 192 193 194 195 196
    createResult = hcreate_r(netVal, &hTab);
    LOGD("    hcreate_r createResult:='%d'\n", createResult);
    ASSERT_TRUE(createResult != 0) << "ErrInfo: hcreate_r returnVal:='" << createResult << "'";

    item.key = hashKey;
    returnVal = hsearch_r(item, FIND, &searchResult, &hTab);
    LOGD("    hsearch_r searchResult='%s'\n", searchResult);
    ASSERT_TRUE(0 == returnVal) << "ErrInfo: hsearch_r searchResult:='" << searchResult << "'";

    hdestroy_r(&hTab);
}