summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_atoi_strtoi.c
blob: 535b6ab74578cd5fe153a9b33dafa8d510355963 (plain)
1
2
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
117
118
119
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
148
149
150
151
152
153
154
155
156
157
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <stdarg.h>  // Required by <cmocka.h>
#include <stddef.h>  // Required by <cmocka.h>
#include <setjmp.h>  // Required by <cmocka.h>
#include <stdint.h>  // Required by <cmocka.h>
#include <cmocka.h>

#include "atoi/strtoi.h"
#include "atoi/strtou_noneg.h"


static void test_strtoi(void **state);
static void test_strtou(void **state);
static void test_strtou_noneg(void **state);


int
main(void)
{
    const struct CMUnitTest  tests[] = {
        cmocka_unit_test(test_strtoi),
        cmocka_unit_test(test_strtou),
        cmocka_unit_test(test_strtou_noneg),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}


static void
test_strtoi(void **state)
{
	int   status;
	char  *end;

	errno = 0;
	assert_true(strtoi_("42", NULL, -1, 1, 2, &status) == 1);
	assert_true(status == EINVAL);

	assert_true(strtoi_("40", &end, 5, INTMAX_MIN, INTMAX_MAX, &status) == 20);
	assert_true(status == 0);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtoi_("-40", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == -40);
	assert_true(status == 0);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtoi_("z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 0);
	assert_true(status == ECANCELED);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtoi_(" 5", &end, 0, 3, 4, &status) == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, INTMAX_MAX, &status) == 5);
	assert_true(status == ENOTSUP);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtoi_("5z", &end, 0, INTMAX_MIN, 4, &status) == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "z") == 0);

	assert_true(errno == 0);
}


static void
test_strtou(void **state)
{
	int   status;
	char  *end;

	errno = 0;
	assert_true(strtou_("42", NULL, -1, 1, 2, &status) == 1);
	assert_true(status == EINVAL);

	assert_true(strtou_("40", &end, 5, 0, UINTMAX_MAX, &status) == 20);
	assert_true(status == 0);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_("-40", &end, 0, 0, UINTMAX_MAX, &status) == -40ull);
	assert_true(status == 0);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_("z", &end, 0, 0, UINTMAX_MAX, &status) == 0);
	assert_true(status == ECANCELED);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtou_(" 5", &end, 0, 3, 4, &status) == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_("5z", &end, 0, 0, UINTMAX_MAX, &status) == 5);
	assert_true(status == ENOTSUP);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtou_("5z", &end, 0, 0, 4, &status) == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "z") == 0);

	assert_true(errno == 0);
}


static void
test_strtou_noneg(void **state)
{
	int   status;
	char  *end;

	errno = 0;
	assert_true(strtou_noneg("42", NULL, -1, 1, 2, &status)
	            == 1);
	assert_true(status == EINVAL);

	assert_true(strtou_noneg("40", &end, 5, 0, UINTMAX_MAX, &status)
	            == 20);
	assert_true(status == 0);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_noneg("-40", &end, 0, 2, UINTMAX_MAX, &status)
	            == 2);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_noneg("z", &end, 0, 0, UINTMAX_MAX, &status)
	            == 0);
	assert_true(status == ECANCELED);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtou_noneg(" 5", &end, 0, 3, 4, &status)
	            == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "") == 0);

	assert_true(strtou_noneg("5z", &end, 0, 0, UINTMAX_MAX, &status)
	            == 5);
	assert_true(status == ENOTSUP);
	assert_true(strcmp(end, "z") == 0);

	assert_true(strtou_noneg("5z", &end, 0, 0, 4, &status)
	            == 4);
	assert_true(status == ERANGE);
	assert_true(strcmp(end, "z") == 0);

	assert_true(errno == 0);
}