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
|
/**
* @file
* Test code for mutt_str_atoui()
*
* @authors
* Copyright (C) 2019 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TEST_NO_MAIN
#include "config.h"
#include "acutest.h"
#include <stddef.h>
#include "mutt/lib.h"
#include "test_common.h"
struct TestValue
{
const char *str; ///< String to test
int retval; ///< Expected return value
unsigned long result; ///< Expected result (outparam)
};
// clang-format off
static const struct TestValue tests[] = {
// Valid tests
{ "0", 0, 0 },
{ "1", 0, 1 },
{ "2", 0, 2 },
{ "3", 0, 3 },
{ " 3", 0, 3 },
{ " 3", 0, 3 },
{ "4294967293", 0, 4294967293 },
{ "4294967294", 0, 4294967294 },
{ "4294967295", 0, 4294967295 },
// Out of range tests
#if LONG_IS_64
{ "4294967296", -2, 0 },
{ "4294967297", -2, 0 },
{ "4294967298", -2, 0 },
#else
{ "4294967296", -1, 0 },
{ "4294967297", -1, 0 },
{ "4294967298", -1, 0 },
#endif
{ "18446744073709551616", -1, 0 },
// Invalid tests
#if LONG_IS_64
{ "-3", -2, 0 },
{ " -3", -2, 0 },
{ " -3", -2, 0 },
#else
{ "-3", 0, 4294967293 },
{ " -3", 0, 4294967293 },
{ " -3", 0, 4294967293 },
#endif
{ "abc", 1, 0 },
{ "a123", 1, 0 },
{ "a-123", 1, 0 },
{ "0a", 1, 0 },
{ "123a", 1, 123 },
{ "1,234", 1, 1 },
{ "1.234", 1, 1 },
{ ".123", 1, 0 },
{ "3 ", 1, 3 },
};
// clang-format on
static const int UNEXPECTED = -9999;
void test_mutt_str_atoui(void)
{
// int mutt_str_atoui(const char *str, unsigned int *dst);
unsigned int result = UNEXPECTED;
// Degenerate tests
TEST_CHECK(mutt_str_atoui(NULL, &result) == NULL);
TEST_CHECK(result == 0);
TEST_CHECK(mutt_str_atoui("42", NULL) != NULL);
// Normal tests
for (size_t i = 0; i < mutt_array_size(tests); i++)
{
TEST_CASE(tests[i].str);
result = UNEXPECTED;
const char *end = mutt_str_atoui(tests[i].str, &result);
if ((tests[i].retval == 0) && (!end || *end))
{
TEST_MSG("retval: Expected: \\0, Got: %s\n", end);
}
else if ((tests[i].retval == -1) && end)
{
TEST_MSG("retval: Expected: NULL, Got: %s\n", end);
}
else if ((tests[i].retval == -2) && end)
{
TEST_MSG("retval: Expected: NULL, Got: %s\n", end);
}
}
}
|