13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci *
43d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
53d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
63d8536b4Sopenharmony_ci *
73d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
83d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
93d8536b4Sopenharmony_ci *
103d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
113d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
123d8536b4Sopenharmony_ci *    provided with the distribution.
133d8536b4Sopenharmony_ci *
143d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
153d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
163d8536b4Sopenharmony_ci *    permission.
173d8536b4Sopenharmony_ci *
183d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
203d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
213d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
223d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
233d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
243d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
263d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
273d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
283d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293d8536b4Sopenharmony_ci */
303d8536b4Sopenharmony_ci
313d8536b4Sopenharmony_ci#include "xts_mem.h"
323d8536b4Sopenharmony_ci
333d8536b4Sopenharmony_ciLITE_TEST_SUIT(MEM, MemApi, MemApiTestSuite);
343d8536b4Sopenharmony_ci
353d8536b4Sopenharmony_cistatic BOOL MemApiTestSuiteSetUp(void)
363d8536b4Sopenharmony_ci{
373d8536b4Sopenharmony_ci    return TRUE;
383d8536b4Sopenharmony_ci}
393d8536b4Sopenharmony_ci
403d8536b4Sopenharmony_cistatic BOOL MemApiTestSuiteTearDown(void)
413d8536b4Sopenharmony_ci{
423d8536b4Sopenharmony_ci    return TRUE;
433d8536b4Sopenharmony_ci}
443d8536b4Sopenharmony_ci
453d8536b4Sopenharmony_ci/**
463d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_MEM_MEMSET_0100
473d8536b4Sopenharmony_ci * @tc.name   memset function set buffer value test
483d8536b4Sopenharmony_ci * @tc.desc   [C-L*-311]
493d8536b4Sopenharmony_ci */
503d8536b4Sopenharmony_ciLITE_TEST_CASE(MemApiTestSuite, testMemset, Function | MediumTest | Level1)
513d8536b4Sopenharmony_ci{
523d8536b4Sopenharmony_ci    char chr = 'A';
533d8536b4Sopenharmony_ci    int i, len, failure;
543d8536b4Sopenharmony_ci    len = GetRandom(1024); /* 1024, common data for test, no special meaning */
553d8536b4Sopenharmony_ci    errno_t err = EOK;
563d8536b4Sopenharmony_ci
573d8536b4Sopenharmony_ci    char buf[1024]; /* 1024, common data for test, no special meaning */
583d8536b4Sopenharmony_ci    err = memset_s(buf, sizeof(buf), chr, len);
593d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(err, 0, err);
603d8536b4Sopenharmony_ci    failure = 0;
613d8536b4Sopenharmony_ci    for (i = 0; i < len; i++) {
623d8536b4Sopenharmony_ci        if (buf[i] != chr) {
633d8536b4Sopenharmony_ci            failure = 1; /* 1, common data for test, no special meaning */
643d8536b4Sopenharmony_ci            break;
653d8536b4Sopenharmony_ci        }
663d8536b4Sopenharmony_ci    }
673d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
683d8536b4Sopenharmony_ci    return 0;
693d8536b4Sopenharmony_ci}
703d8536b4Sopenharmony_ci
713d8536b4Sopenharmony_ci/**
723d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_MEM_MEMCPY_0100
733d8536b4Sopenharmony_ci * @tc.name   memcpy function copy buffer test
743d8536b4Sopenharmony_ci * @tc.desc   [C-L*-311]
753d8536b4Sopenharmony_ci */
763d8536b4Sopenharmony_ciLITE_TEST_CASE(MemApiTestSuite, testMemcpy, Function | MediumTest | Level2)
773d8536b4Sopenharmony_ci{
783d8536b4Sopenharmony_ci    char chr = 'A';
793d8536b4Sopenharmony_ci    int i, len, failure;
803d8536b4Sopenharmony_ci    char src[1024]; /* 1024, common data for test, no special meaning */
813d8536b4Sopenharmony_ci    char dst[1024]; /* 1024, common data for test, no special meaning */
823d8536b4Sopenharmony_ci
833d8536b4Sopenharmony_ci    len = GetRandom(1024); /* 1024, common data for test, no special meaning */
843d8536b4Sopenharmony_ci
853d8536b4Sopenharmony_ci    for (i = 0; i < len; i++) {
863d8536b4Sopenharmony_ci        src[i] = chr + i % 26; /* 26, common data for test, no special meaning */
873d8536b4Sopenharmony_ci    }
883d8536b4Sopenharmony_ci
893d8536b4Sopenharmony_ci    errno_t err = memcpy_s(dst, sizeof(dst) / sizeof(dst[0]), src, len);
903d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(err, 0, err);
913d8536b4Sopenharmony_ci    failure = 0;
923d8536b4Sopenharmony_ci    for (i = 0; i < len; i++) {
933d8536b4Sopenharmony_ci        if (dst[i] != src[i]) {
943d8536b4Sopenharmony_ci            failure = 1; /* 1, common data for test, no special meaning */
953d8536b4Sopenharmony_ci            break;
963d8536b4Sopenharmony_ci        }
973d8536b4Sopenharmony_ci    }
983d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
993d8536b4Sopenharmony_ci    return 0;
1003d8536b4Sopenharmony_ci}
1013d8536b4Sopenharmony_ci
1023d8536b4Sopenharmony_ci/**
1033d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_MEM_MEMMOVE_0100
1043d8536b4Sopenharmony_ci * @tc.name   memmove function move buffer test
1053d8536b4Sopenharmony_ci * @tc.desc   [C-L*-311]
1063d8536b4Sopenharmony_ci */
1073d8536b4Sopenharmony_ciLITE_TEST_CASE(MemApiTestSuite, testMemmove, Function | MediumTest | Level2)
1083d8536b4Sopenharmony_ci{
1093d8536b4Sopenharmony_ci    char chr = 'A';
1103d8536b4Sopenharmony_ci    char buf[1024]; /* 1024, common data for test, no special meaning */
1113d8536b4Sopenharmony_ci    int i, len, failure;
1123d8536b4Sopenharmony_ci
1133d8536b4Sopenharmony_ci    len = sizeof(buf);
1143d8536b4Sopenharmony_ci    for (i = 0; i < len; i++) {
1153d8536b4Sopenharmony_ci        buf[i] = chr + GetRandom(26); /* 26, common data for test, no special meaning */
1163d8536b4Sopenharmony_ci    }
1173d8536b4Sopenharmony_ci    errno_t err = memmove_s(&buf[0], sizeof(buf) / sizeof(buf[0]), &buf[len / 2], len / 2); /* 2, common data for test, no special meaning */
1183d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(err, 0, err);
1193d8536b4Sopenharmony_ci
1203d8536b4Sopenharmony_ci    failure = 0;
1213d8536b4Sopenharmony_ci    for (i = 0; i < len / 2; i++) { /* 2, common data for test, no special meaning */
1223d8536b4Sopenharmony_ci        if (buf[i] != buf[len / 2 + i]) { /* 2, common data for test, no special meaning */
1233d8536b4Sopenharmony_ci            failure = 1; /* 1, common data for test, no special meaning */
1243d8536b4Sopenharmony_ci            break;
1253d8536b4Sopenharmony_ci        }
1263d8536b4Sopenharmony_ci    }
1273d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
1283d8536b4Sopenharmony_ci    return 0;
1293d8536b4Sopenharmony_ci}
1303d8536b4Sopenharmony_ci
1313d8536b4Sopenharmony_ci/**
1323d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_MEM_MEMMOVE_0200
1333d8536b4Sopenharmony_ci * @tc.name   memmove function overlay move buffer test
1343d8536b4Sopenharmony_ci * @tc.desc   [C-L*-311]
1353d8536b4Sopenharmony_ci */
1363d8536b4Sopenharmony_ciLITE_TEST_CASE(MemApiTestSuite, testMemmoveOverlay, Function | MediumTest | Level3)
1373d8536b4Sopenharmony_ci{
1383d8536b4Sopenharmony_ci    char chr = 'A';
1393d8536b4Sopenharmony_ci    char buf[1024]; /* 1024, common data for test, no special meaning */
1403d8536b4Sopenharmony_ci    char backup[1024]; /* 1024, common data for test, no special meaning */
1413d8536b4Sopenharmony_ci    int i, len, failure;
1423d8536b4Sopenharmony_ci
1433d8536b4Sopenharmony_ci    len = sizeof(buf);
1443d8536b4Sopenharmony_ci    for (i = 0; i < len; i++) {
1453d8536b4Sopenharmony_ci        buf[i] = chr + GetRandom(26); /* 26, common data for test, no special meaning */
1463d8536b4Sopenharmony_ci        backup[i] = buf[i];
1473d8536b4Sopenharmony_ci    }
1483d8536b4Sopenharmony_ci    errno_t err = memmove_s(&buf[16], sizeof(buf) / sizeof(buf[0]) - 16, &buf[0], len / 2); /* 16, 2, common data for test, no special meaning */
1493d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(err, 0, err);
1503d8536b4Sopenharmony_ci
1513d8536b4Sopenharmony_ci    failure = 0;
1523d8536b4Sopenharmony_ci    for (i = 0; i < len / 2; i++) { /* 2, common data for test, no special meaning */
1533d8536b4Sopenharmony_ci        if (buf[i + 16] != backup[i]) { /* 16, common data for test, no special meaning */
1543d8536b4Sopenharmony_ci            failure = 1; /* 1, common data for test, no special meaning */
1553d8536b4Sopenharmony_ci            break;
1563d8536b4Sopenharmony_ci        }
1573d8536b4Sopenharmony_ci    }
1583d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(failure, 0, failure);
1593d8536b4Sopenharmony_ci    return 0;
1603d8536b4Sopenharmony_ci}
1613d8536b4Sopenharmony_ci
1623d8536b4Sopenharmony_ci
1633d8536b4Sopenharmony_ci/**
1643d8536b4Sopenharmony_ci * @tc.number SUB_KERNEL_MEM_MEMCMP_0100
1653d8536b4Sopenharmony_ci * @tc.name   memmove function move buffer test
1663d8536b4Sopenharmony_ci * @tc.desc   [C-L*-311]
1673d8536b4Sopenharmony_ci */
1683d8536b4Sopenharmony_ciLITE_TEST_CASE(MemApiTestSuite, testMemcmp, Function | MediumTest | Level2)
1693d8536b4Sopenharmony_ci{
1703d8536b4Sopenharmony_ci    char orign[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; /* 8, common data for test, no special meaning */
1713d8536b4Sopenharmony_ci    char lt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x77}; /* 8, common data for test, no special meaning */
1723d8536b4Sopenharmony_ci    char eq[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; /* 8, common data for test, no special meaning */
1733d8536b4Sopenharmony_ci    char gt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x99}; /* 8, common data for test, no special meaning */
1743d8536b4Sopenharmony_ci
1753d8536b4Sopenharmony_ci    int ret;
1763d8536b4Sopenharmony_ci    int len = sizeof(orign);
1773d8536b4Sopenharmony_ci
1783d8536b4Sopenharmony_ci    ret = memcmp(lt, orign, len);
1793d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, INT_MIN, 0, ret);
1803d8536b4Sopenharmony_ci
1813d8536b4Sopenharmony_ci    ret = memcmp(eq, orign, len);
1823d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1833d8536b4Sopenharmony_ci
1843d8536b4Sopenharmony_ci    ret = memcmp(gt, orign, len);
1853d8536b4Sopenharmony_ci    ICUNIT_ASSERT_WITHIN_EQUAL(ret, 0, INT_MAX, ret);
1863d8536b4Sopenharmony_ci
1873d8536b4Sopenharmony_ci    ret = memcmp(gt, orign, 0);
1883d8536b4Sopenharmony_ci    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
1893d8536b4Sopenharmony_ci    return 0;
1903d8536b4Sopenharmony_ci}
1913d8536b4Sopenharmony_ci
1923d8536b4Sopenharmony_ciRUN_TEST_SUITE(MemApiTestSuite);
1933d8536b4Sopenharmony_ci
1943d8536b4Sopenharmony_civoid MemApiTest(void)
1953d8536b4Sopenharmony_ci{
1963d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testMemset);
1973d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testMemcpy);
1983d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testMemmove);
1993d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testMemmoveOverlay);
2003d8536b4Sopenharmony_ci    RUN_ONE_TESTCASE(testMemcmp);
2013d8536b4Sopenharmony_ci}
202