1 /**
2 * @file
3 * Test code for mutt_str_atos()
4 *
5 * @authors
6 * Copyright (C) 2019 Richard Russon <rich@flatcap.org>
7 *
8 * @copyright
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 2 of the License, or (at your option) any later
12 * version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #define TEST_NO_MAIN
24 #include "config.h"
25 #include "acutest.h"
26 #include "mutt/lib.h"
27
28 struct TestValue
29 {
30 const char *str; ///< String to test
31 int retval; ///< Expected return value
32 int result; ///< Expected result (outparam)
33 };
34
35 // clang-format off
36 static const struct TestValue tests[] = {
37 // Valid tests
38 { "0", 0, 0 },
39 { "1", 0, 1 },
40 { "2", 0, 2 },
41 { "3", 0, 3 },
42 { " 3", 0, 3 },
43 { " 3", 0, 3 },
44
45 { "32765", 0, 32765 },
46 { "32766", 0, 32766 },
47 { "32767", 0, 32767 },
48
49 { "-1", 0, -1 },
50 { "-2", 0, -2 },
51 { "-3", 0, -3 },
52 { " -3", 0, -3 },
53 { " -3", 0, -3 },
54
55 { "-32766", 0, -32766 },
56 { "-32767", 0, -32767 },
57 { "-32768", 0, -32768 },
58
59 // Out of range tests
60 { "32768", -2, 0 },
61 { "32769", -2, 0 },
62 { "32770", -2, 0 },
63
64 { "-32769", -2, 0 },
65 { "-32770", -2, 0 },
66 { "-32771", -2, 0 },
67
68 // Invalid tests
69 { "abc", -1, 0 },
70 { "a123", -1, 0 },
71 { "a-123", -1, 0 },
72 { "0a", -1, 0 },
73 { "123a", -1, 0 },
74 { "-123a", -1, 0 },
75 { "1,234", -1, 0 },
76 { "-1,234", -1, 0 },
77 { "1.234", -1, 0 },
78 { "-1.234", -1, 0 },
79 { ".123", -1, 0 },
80 { "-.123", -1, 0 },
81 { "3 ", -1, 0 },
82 { "-3 ", -1, 0 },
83 };
84 // clang-format on
85
86 static const int UNEXPECTED = -9999;
87
test_mutt_str_atos(void)88 void test_mutt_str_atos(void)
89 {
90 // int mutt_str_atos(const char *str, short *dst);
91
92 short result = UNEXPECTED;
93 int retval = 0;
94
95 // Degenerate tests
96 TEST_CHECK(mutt_str_atos(NULL, &result) == 0);
97 TEST_CHECK(mutt_str_atos("42", NULL) == 0);
98
99 // Normal tests
100 for (size_t i = 0; i < mutt_array_size(tests); i++)
101 {
102 TEST_CASE(tests[i].str);
103
104 result = UNEXPECTED;
105 retval = mutt_str_atos(tests[i].str, &result);
106
107 if (!TEST_CHECK((retval == tests[i].retval) && (result == tests[i].result)))
108 {
109 TEST_MSG("retval: Expected: %d, Got: %d\n", tests[i].retval, retval);
110 TEST_MSG("result: Expected: %d, Got: %d\n", tests[i].result, result);
111 }
112 }
113 }
114