1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "ohos_types.h"
33#include "posix_test.h"
34#include "los_config.h"
35#include "kernel_test.h"
36#include "ctype.h"
37#include "limits.h"
38#include "stdlib.h"
39#include "string.h"
40#include "log.h"
41
42/* *
43 * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
44 * @param        : subsystem name is utils
45 * @param        : module name is utilsFile
46 * @param        : test suit name is CmsisTaskFuncTestSuite
47 */
48LITE_TEST_SUIT(Posix, Posixtimer, PosixStdlibAtollTest);
49
50/* *
51 * @tc.setup     : setup for all testcases
52 * @return       : setup result, TRUE is success, FALSE is fail
53 */
54static BOOL PosixStdlibAtollTestSetUp(void)
55{
56    return TRUE;
57}
58
59/* *
60 * @tc.teardown  : teardown for all testcases
61 * @return       : teardown result, TRUE is success, FALSE is fail
62 */
63static BOOL PosixStdlibAtollTestTearDown(void)
64{
65    LOG("+-------------------------------------------+\n");
66    return TRUE;
67}
68
69/* *
70 * @tc.number    : TEST_STDLIB_ATOLL_001
71 * @tc.name      : convert string to long long integer
72 * @tc.desc      : [C- SOFTWARE -0200]
73 */
74LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll001, Function | MediumTest | Level1)
75{
76    long long value = atoll("9223372036854775807");
77    if (value == 9223372036854775807LL) {
78        LOG("[DEMO] posix stdlib test case 1:atoll(%lld) ok.\n", value);
79    } else {
80        LOG("[DEMO] posix stdlib test case 1:atoll(%lld) fail.\n", value);
81    }
82    ICUNIT_ASSERT_EQUAL(value, 9223372036854775807LL, 0);
83    return 0;
84}
85
86/* *
87 * @tc.number    : TEST_STDLIB_ATOLL_002
88 * @tc.name      : convert string to long long integer
89 * @tc.desc      : [C- SOFTWARE -0200]
90 */
91LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll002, Function | MediumTest | Level1)
92{
93    long long value = atoll("-9223372036854775808");
94    if (value == -9223372036854775808ULL) {
95        LOG("[DEMO] posix stdlib test case 2:atoll(%lld) ok.\n", value);
96    } else {
97        LOG("[DEMO] posix stdlib test case 2:atoll(%lld) fail.\n", value);
98    }
99    ICUNIT_ASSERT_EQUAL(value, -9223372036854775808ULL, 0);
100    return 0;
101}
102
103/* *
104 * @tc.number    : TEST_STDLIB_ATOLL_003
105 * @tc.name      : convert string to long long integer
106 * @tc.desc      : [C- SOFTWARE -0200]
107 */
108LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll003, Function | MediumTest | Level1)
109{
110    long long value = atoll("100");
111    if (value == 100LL) {
112        LOG("[DEMO] posix stdlib test case 3:atoll(%lld) ok.\n", value);
113    } else {
114        LOG("[DEMO] posix stdlib test case 3:atoll(%lld) fail.\n", value);
115    }
116    ICUNIT_ASSERT_EQUAL(value, 100LL, 0);
117    return 0;
118}
119
120#if (LOSCFG_LIBC_MUSL == 1)
121/* *
122 * @tc.number    : TEST_STDLIB_ATOLL_004
123 * @tc.name      : convert string to long long integer
124 * @tc.desc      : [C- SOFTWARE -0200]
125 */
126LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll004, Function | MediumTest | Level1)
127{
128    long long value = atoll("9223372036854775808");
129    if (value == -9223372036854775808ULL) {
130        LOG("[DEMO] posix stdlib test case 4(except):atoll(%lld) != 9223372036854775808 ok.\n", value);
131    } else {
132        LOG("[DEMO] posix stdlib test case 4(except):atoll(%lld) fail.\n", value);
133    }
134    ICUNIT_ASSERT_EQUAL(value, LLONG_MIN, 0);
135    return 0;
136}
137
138/* *
139 * @tc.number    : TEST_STDLIB_ATOLL_005
140 * @tc.name      : convert string to long long integer
141 * @tc.desc      : [C- SOFTWARE -0200]
142 */
143LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll005, Function | MediumTest | Level1)
144{
145    long long value = atoll("-9223372036854775809");
146    if (value == 9223372036854775807LL) {
147        LOG("[DEMO] posix stdlib test case 5(except):atoll(%lld) != -9223372036854775809 ok.\n", value);
148    } else {
149        LOG("[DEMO] posix stdlib test case 5(except):atoll(%lld) fail.\n", value);
150    }
151
152    ICUNIT_ASSERT_EQUAL(value, LLONG_MAX, 0);
153    return 0;
154}
155#endif
156
157/* *
158 * @tc.number    : TEST_STDLIB_ATOLL_006
159 * @tc.name      : convert string to long long integer
160 * @tc.desc      : [C- SOFTWARE -0200]
161 */
162LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll006, Function | MediumTest | Level1)
163{
164    long long value = atoll("+100");
165    if (value == 100LL) {
166        LOG("[DEMO] posix stdlib test case 6:atoll(%lld) == +100 ok.\n", value);
167    } else {
168        LOG("[DEMO] posix stdlib test case 6:atoll(%lld) fail.\n", value);
169    }
170    ICUNIT_ASSERT_EQUAL(value, 100LL, 0);
171    return 0;
172}
173
174/* *
175 * @tc.number    : TEST_STDLIB_ATOLL_007
176 * @tc.name      : convert string to long long integer
177 * @tc.desc      : [C- SOFTWARE -0200]
178 */
179LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll007, Function | MediumTest | Level1)
180{
181    long long value = atoll("-100");
182    if (value == -100LL) {
183        LOG("[DEMO] posix stdlib test case 7:atoll(%lld) == -100 ok.\n", value);
184    } else {
185        LOG("[DEMO] posix stdlib test case 7:atoll(%lld) fail.\n", value);
186    }
187    ICUNIT_ASSERT_EQUAL(value, -100LL, 0);
188    return 0;
189}
190
191/* *
192 * @tc.number    : TEST_STDLIB_ATOLL_008
193 * @tc.name      : convert string to long long integer
194 * @tc.desc      : [C- SOFTWARE -0200]
195 */
196LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll008, Function | MediumTest | Level1)
197{
198    long long value = atoll("+-100");
199    if (value == 0LL) {
200        LOG("[DEMO] posix stdlib test case 8(except):atoll(%lld) ==  +-100 ok.\n", value);
201    } else {
202        LOG("[DEMO] posix stdlib test case 8(except):atoll(%lld) fail.\n", value);
203    }
204    ICUNIT_ASSERT_EQUAL(value, 0LL, 0);
205    return 0;
206}
207
208/* *
209 * @tc.number    : TEST_STDLIB_ATOLL_009
210 * @tc.name      : convert string to long long integer
211 * @tc.desc      : [C- SOFTWARE -0200]
212 */
213LITE_TEST_CASE(PosixStdlibAtollTest, testStdlibAtoll009, Function | MediumTest | Level1)
214{
215    long long value = atoll("12+-100");
216    if (value == 12LL) {
217        LOG("[DEMO] posix stdlib test case 9(except):atoll(%lld) ok == 12+-100.\n", value);
218    } else {
219        LOG("[DEMO] posix stdlib test case 9(except):atoll(%lld) fail.\n", value);
220    }
221    ICUNIT_ASSERT_EQUAL(value, 12LL, 0);
222    return 0;
223}
224
225RUN_TEST_SUITE(PosixStdlibAtollTest);
226
227void PosixStdlibAtollFuncTest()
228{
229    LOG("begin PosixStdlibAtollFuncTest....");
230    RUN_ONE_TESTCASE(testStdlibAtoll001);
231    RUN_ONE_TESTCASE(testStdlibAtoll002);
232    RUN_ONE_TESTCASE(testStdlibAtoll003);
233#if (LOSCFG_LIBC_MUSL == 1)
234    RUN_ONE_TESTCASE(testStdlibAtoll004);
235    RUN_ONE_TESTCASE(testStdlibAtoll005);
236#endif
237    RUN_ONE_TESTCASE(testStdlibAtoll006);
238    RUN_ONE_TESTCASE(testStdlibAtoll007);
239    RUN_ONE_TESTCASE(testStdlibAtoll008);
240    RUN_ONE_TESTCASE(testStdlibAtoll009);
241
242    return;
243}