ActsMemApiTest.cpp 12.6 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-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
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sched.h>
#include <malloc.h>
#include <sys/time.h>
#include <sys/resource.h>

#include "gtest/gtest.h"
#include "XtsActsUtil.h"
M
mamingshuai 已提交
28
#include "KernelConstants.h"
W
wenjun 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

using namespace testing::ext;

class ActsMemApiTest : 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()
    {
        errno = 0;
    }
    // Testcase teardown
    virtual void TearDown()
    {
    }
};

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_BCMP_0100
* @tc.name       test bcmp api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
58
HWTEST_F(ActsMemApiTest, testMemBcmp_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    char str1[] = "this is string1";
    char str2[] = "this is string2";
    int returnVal;

    returnVal = bcmp(str1, str2, 7);
    LogPrint("    bcmp *s1:='%s' *s2:='%s' size:='7',   --> returnVal:='%d'\n", str1, str2, returnVal);
    EXPECT_EQ(0, returnVal)
        << "ErrInfo: bcmp *s1:='" << str1 << "' *s2:='" << str2
        << "' size:='7',   --> returnVal:='" << returnVal << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_BCMP_1000
* @tc.name       test bcmp api NE
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
75
HWTEST_F(ActsMemApiTest, testMemBcmp_1000, Function | MediumTest | Level1) {
W
wenjun 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    char str1[] = "this is string1";
    char str2[] = "this is string2";
    int returnVal;
    
    returnVal = bcmp(str1, str2, 15);
    LogPrint("    bcmp *s1:='%s' *s2:='%s' size:='15',   --> returnVal:='%d'\n", str1, str2, returnVal);
    EXPECT_NE(0, returnVal)
        << "ErrInfo: bcmp *s1:='" << str1 << "' *s2:='" << str2
        << "' size:='15',   --> returnVal:='" << returnVal << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_BCOPY_0100
* @tc.name       test bcopy api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
92
HWTEST_F(ActsMemApiTest, testMemBcopy_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    char srcStr[] = "this";
    char destStr[] = "00000000";
    int iRtn = 1;

    LogPrint("    bcopy *src:='%s' *dest:='%s', size:='4', \n", srcStr, destStr);
    bcopy(srcStr, destStr, 4);
    LogPrint("    --> *dest:='%s'\n", destStr);
	
    for (int iLoop = 0; iLoop < 4; iLoop++)
    {
        if (srcStr[iLoop] != destStr[iLoop]) {
            iRtn = 0;
            break;
        }
    }

    EXPECT_EQ(1, iRtn)
        << "ErrInfo: bcopy *src:='" << srcStr << "' *dest:='"
        << "' size:='4',   --> *dest:='" << destStr << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_BZERO_0100
* @tc.name       test bzero api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
119
HWTEST_F(ActsMemApiTest, testMemBzero_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    char srcStr[] = "this is string1";
    char destStr[] = "this is string1";
    int iFlag = 1;
	
    LogPrint("    bzero *s:='%s' size:='4',   ", srcStr);
    bzero(destStr, 4);
    LogPrint("--> afterrun s:='%s', ", destStr);
	
    for(int iLoop = 0; iLoop < 4; iLoop++)
    {
        if (destStr[iLoop] != '\0') {
            iFlag = 0;
            break;
        } else {
            destStr[iLoop] = '0';
        }
    }
    LogPrint("  for_print s:='%s'\n", destStr);

    EXPECT_EQ(1, iFlag)
        << "ErrInfo: bzero *:='" << srcStr << "' size:='4',   --> afterrun s:='" << destStr << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MALLOC_USABLE_SIZE_0100
* @tc.name       test malloc_usable_size api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
148
HWTEST_F(ActsMemApiTest, testMallocUsableSize_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161
    char *pt = nullptr;
    size_t size;
	
    pt = (char *)malloc(128);
    if (pt) {
        LogPrint("    Mem allocated at: %x\n", pt);
    } else {
        LogPrint("    No enough Mem!\n");
    }
	
    size = malloc_usable_size(pt);
    LogPrint("    malloc_usable_size *ptr:='%x',   --> return size:='%d'\n", pt, size);

M
mamingshuai 已提交
162
    EXPECT_GT((int)size, 1)
W
wenjun 已提交
163 164 165 166 167 168 169 170 171
        << "ErrInfo: malloc_usable_size *ptr:='" << pt << "',   --> return size:='" << size << "'";
    free(pt);
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MALLOC_USABLE_SIZE_1000
* @tc.name       test malloc_usable_size api exception null
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
172
HWTEST_F(ActsMemApiTest, testMallocUsableSize_1000, Function | MediumTest | Level1) {
W
wenjun 已提交
173 174
    size_t size;
	
M
mamingshuai 已提交
175 176
    size = malloc_usable_size(nullptr);
    LogPrint("    malloc_usable_size *ptr:='nullptr',   --> return size:='%d'\n", size);
W
wenjun 已提交
177

M
mamingshuai 已提交
178 179
    EXPECT_EQ((int)size, 0)
        << "ErrInfo: malloc_usable_size *ptr:='nullptr',   --> return size:='" << size << "'";
W
wenjun 已提交
180 181 182 183 184 185 186
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCCPY_0100
* @tc.name       test memccpy api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
187
HWTEST_F(ActsMemApiTest, testMemCcpy_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    char srcStr[] = "this is str1:stop here";
    char destStr[] = "000000000000000000000000";
    char *pt = nullptr;

    LogPrint("    bcopy *dest:='%s' *src:='%s' c:=':' size:='18', \n", destStr, srcStr);
    pt = (char *)memccpy(destStr, srcStr, ':', 18);
    LogPrint("    --> return pt:='%x', dest:='%s'\n", pt, destStr);
    ASSERT_TRUE(pt)
        << "ErrInfo: bcopy *dest:='destStr' *src:='"
        << srcStr << "' c:=':' size:='18',   --> afterrun dest:='" << destStr << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCCPY_0200
* @tc.name       test memccpy api not find c
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
205
HWTEST_F(ActsMemApiTest, testMemCcpy_0200, Function | MediumTest | Level1) {
W
wenjun 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    char srcStr[] = "this is str1:stop here";
    char destStr[] = "000000000000000000000000";
    char *pt = nullptr;

    LogPrint("    bcopy *dest:='%s' *src:='%s' c:=';' size:='18', \n", destStr, srcStr);
    pt = (char *)memccpy(destStr, srcStr, ';', 18);
    LogPrint("    --> return pt:='%x', *dest:='%s'\n", pt, destStr);
    ASSERT_FALSE(pt)
        << "ErrInfo: bcopy *dest:='destStr' *src:='"
        << srcStr << "' c:=':' size:='18',   --> return pt:='" << pt << "', dest:='" << destStr << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMPCPY_0100
* @tc.name       test mempcpy api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
223
HWTEST_F(ActsMemApiTest, testMempcpy_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    char srcStr[] = "this is str1";
    char destStr[] = "00000000000000000000";
    char *pt = nullptr;

    LogPrint("    mempcpy *dest:='%s' *src:='%s' size:='16',", destStr, srcStr);
    pt = (char *)mempcpy(destStr, srcStr, 16);
    LogPrint("    --> return pt:='%x', *dest:='%s'\n", pt, destStr);
    ASSERT_TRUE(pt)
        << "ErrInfo: mempcpy *dest:='destStr' *src:='"
        << srcStr << "' size:='16',   --> return pt:='" << pt << "', dest:='" << destStr << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_0100
* @tc.name       test memchr api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
241
HWTEST_F(ActsMemApiTest, testMemchr_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    char srcStr[] = "this is str a;";
    char *pos = nullptr;

    LogPrint("    memchr *s:='%s' c:='a' size:='14',", srcStr);
    pos = (char *)memchr(srcStr, 'a', 14);
    LogPrint("    --> return pos:='%x'\n", pos);
	
    ASSERT_TRUE(pos)
        << "ErrInfo: memchr *s:='" << srcStr << "' c:='a' size:='14',"
        << "   --> return pos:='" << pos << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_1000
* @tc.name       test memchr api para len not enough
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
259
HWTEST_F(ActsMemApiTest, testMemchr_1000, Function | MediumTest | Level1) {
W
wenjun 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    char srcStr[] = "this is str a;";
    char *pos = nullptr;

    LogPrint("    memchr *s:='%s' c:='a' size:='4',", srcStr);
    pos = (char *)memchr(srcStr, 'a', 4);
    LogPrint("    --> return pos:='%x'\n", pos);
    ASSERT_FALSE(pos)
        << "ErrInfo: memchr *s:='" << srcStr << "' c:='a' size:='4',"
        << "   --> return pos:='" << pos << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_1100
* @tc.name       test memchr api para c not found
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
276
HWTEST_F(ActsMemApiTest, testMemchr_1100, Function | MediumTest | Level1) {
W
wenjun 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    char srcStr[] = "this is str a;";
    char *pos = nullptr;

    LogPrint("    memchr *s:='%s' c:='b' size:='14',", srcStr);
    pos = (char *)memchr(srcStr, 'b', 14);
    LogPrint("    --> return pos:='%x'\n", pos);
    ASSERT_FALSE(pos)
        << "ErrInfo: memchr *s:='" << srcStr << "' c:='b' size:='14',"
        << "   --> return pos:='" << pos << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMMEM_0100
* @tc.name       test memmem api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
293
HWTEST_F(ActsMemApiTest, testMemmem_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
294 295 296 297 298 299
    char srcStr[] = "this is str a;";
    char srcSubStr[] = "str";
    char *pos = nullptr;

    LogPrint("    memmem *haystack:='%s' size_haystack:='%d' ", srcStr, strlen(srcStr));
    LogPrint("*needle:='%s' size_needle:='%d',\n", srcSubStr, strlen(srcSubStr));
300
    if (strlen(srcStr) >= strlen(srcSubStr)) {
W
wenjun 已提交
301 302
    pos = (char *)memmem(srcStr, strlen(srcStr), srcSubStr, strlen(srcSubStr));
    LogPrint("    --> return pos:='%x'\n", pos);
303
    }
W
wenjun 已提交
304 305 306 307 308 309 310 311 312 313 314 315
	
    ASSERT_TRUE(pos)
        << "ErrInfo: memmem *haystack:='" << srcStr << " size_haystack:='"
        << strlen(srcStr) << "' *needle:='" << srcSubStr << "' size_needle:='" << strlen(srcSubStr)
        << "   --> return pos:='" << pos << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMMEM_1000
* @tc.name       test memmem api para not found
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
316
HWTEST_F(ActsMemApiTest, testMemmem_1000, Function | MediumTest | Level1) {
W
wenjun 已提交
317 318 319
    char srcStr[] = "this is str a;";
    char srcSubStr[] = "strb";
    char *pos = nullptr;
320 321 322 323
    
    if (strlen(srcStr) < strlen(srcSubStr)) {
        FAIL();
    }
W
wenjun 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    pos = (char *)memmem(srcStr, strlen(srcStr), srcSubStr, strlen(srcSubStr));
    LogPrint("    memmem *haystack:='%s' size_haystack:='%d' ", srcStr, strlen(srcStr));
    LogPrint("*needle:='%s' size_needle:='%d',\n", srcSubStr, strlen(srcSubStr));
    LogPrint("    --> return pos:='%x'\n", pos);
	
    ASSERT_FALSE(pos)
        << "ErrInfo: memmem *haystack:='" << srcStr << " size_haystack:='"
        << strlen(srcStr) << "' *needle:='" << srcSubStr << "' size_needle:='" << strlen(srcSubStr)
        << "   --> return pos:='" << pos << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_OPEN_MEMSTREAM_0100
* @tc.name       test open_memstream api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
340
HWTEST_F(ActsMemApiTest, testOpenMemStream_0100, Function | MediumTest | Level1) {
W
wenjun 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    FILE *memStream;
    char *ptr = nullptr;
    size_t sizeLoc;
	
    memStream = open_memstream(&ptr, &sizeLoc);
    LogPrint("    open_memstream **ptr:='*ptr' *sizeloc:='*sizeLoc', ");
    LogPrint("    --> return memStream:='%x'\n", memStream);
    LogPrint("    ptr:='%x', sizeLoc:='%d'\n", ptr, sizeLoc);
	
    ASSERT_TRUE(memStream)
        << "ErrInfo: open_memstream **ptr:='*ptr' *sizeloc:='*sizeLoc', "
        << "   --> return memStream:='" << memStream << "'";

    LogPrint("    fprintf(memStream, 'Hello!');\n");
    fprintf(memStream, "Hello!");
    fflush(memStream);
    LogPrint("    ptr:='%s', sizeloc:='%zu'\n", ptr, sizeLoc);

    LogPrint("    fclose(memStream);\n");
    fclose(memStream);
    LogPrint("    ptr:='%s', sizeloc:='%zu'\n", ptr, sizeLoc);
    free(ptr);
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_MEM_GETPAGESIZE_0100
* @tc.name       test getpagesize api
* @tc.desc       [C- SOFTWARE -0200]
*/
M
mamingshuai 已提交
370 371
HWTEST_F(ActsMemApiTest, testMemGetpagesize_0100, Function | MediumTest | Level1) {
    size_t returnVal;
W
wenjun 已提交
372 373 374

    returnVal = getpagesize();
    LogPrint("    getpagesize,   --> returnVal:='%d'\n", returnVal);
M
mamingshuai 已提交
375 376
    EXPECT_GE(returnVal, 0U);
    EXPECT_EQ(returnVal, PAGE_SIZE)
W
wenjun 已提交
377 378
        << "ErrInfo: getpagesize,   --> returnVal:='" << returnVal << "'";
}