1 /*
2 * Copyright (c) 2022 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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <inttypes.h>
20 #include "test.h"
21
22 /**
23 * @tc.name : strtoumax_0100
24 * @tc.desc : Test the strtoumax function to interpret the string content as a base 10 integer
25 * @tc.level : Level 0
26 */
strtoumax_0100(void)27 void strtoumax_0100(void)
28 {
29 char *p = {0};
30 intmax_t want = 18737357U;
31 intmax_t result = strtoumax("18737357foobar12", &p, 10);
32 if (result != want) {
33 t_error("%s strtoumax get result is %zu are not 18737357U\n", __func__, result);
34 }
35 if (strcmp(p, "foobar12") != 0) {
36 t_error("%s strtoumax get is '%s' are not 'foobar12'\n", __func__, p);
37 }
38 }
39
40 /**
41 * @tc.name : strtoumax_0200
42 * @tc.desc : Test the strtoumax function to interpret the string content as a base 16 integer
43 * @tc.level : Level 1
44 */
strtoumax_0200(void)45 void strtoumax_0200(void)
46 {
47 char *p = {0};
48 intmax_t want = 0x18737357fU;
49 intmax_t result = strtoumax("18737357foobar12", &p, 16);
50 if (result != want) {
51 t_error("%s strtoumax get result is %zu are not 0x18737357fU\n", __func__, result);
52 }
53 if (strcmp(p, "oobar12") != 0) {
54 t_error("%s strtoumax get is '%s' are not 'oobar12'\n", __func__, p);
55 }
56 }
57
58 /**
59 * @tc.name : strtoumax_0300
60 * @tc.desc : Test that strtoumax interprets negative string contents as base 10 integers
61 * @tc.level : Level 1
62 */
strtoumax_0300(void)63 void strtoumax_0300(void)
64 {
65 char *p = {0};
66 intmax_t subnum = 18737357;
67 intmax_t result = strtoumax("-18737357foobar12", &p, 10);
68 if (result != UINTMAX_MAX - subnum + 1) {
69 t_error("%s strtoumax get result error is %zu\n", __func__, result);
70 }
71 if (strcmp(p, "foobar12") != 0) {
72 t_error("%s strtoumax get is '%s' are not 'foobar12'\n", __func__, p);
73 }
74 }
75
main(int argc, char *argv[])76 int main(int argc, char *argv[])
77 {
78 strtoumax_0100();
79 strtoumax_0200();
80 strtoumax_0300();
81 return t_status;
82 }