Skip to content

Commit fa4d4fc

Browse files
committed
unit tests for my_getopt
1 parent 584c07b commit fa4d4fc

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

unittest/mysys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1515

1616
MY_ADD_TESTS(bitmap base64 my_vsnprintf my_atomic my_rdtsc lf my_malloc
17+
my_getopt
1718
LINK_LIBRARIES mysys)
1819

1920
IF(WIN32)

unittest/mysys/my_getopt-t.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (C) 2015 MariaDB
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15+
16+
#include <tap.h>
17+
#include <my_getopt.h>
18+
#include <stdarg.h>
19+
20+
ulonglong opt_ull;
21+
ulong opt_ul;
22+
int argc, res;
23+
char **argv, *args[100];
24+
25+
struct my_option my_long_options[]=
26+
{
27+
{"ull", 0, "ull", &opt_ull, &opt_ull,
28+
0, GET_ULL, REQUIRED_ARG, 1, 0, ~0ULL, 0, 0, 0},
29+
{"ul", 0, "ul", &opt_ul, &opt_ul,
30+
0, GET_ULONG, REQUIRED_ARG, 1, 0, 0xFFFFFFFF, 0, 0, 0},
31+
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
32+
};
33+
34+
void run(const char *arg, ...)
35+
{
36+
va_list ap;
37+
va_start(ap, arg);
38+
argv= args;
39+
*argv++= (char*)"<skipped>";
40+
while (arg)
41+
{
42+
*argv++= (char*)arg;
43+
arg= va_arg(ap, char*);
44+
}
45+
va_end(ap);
46+
argc= argv - args;
47+
argv= args;
48+
res= handle_options(&argc, &argv, my_long_options, 0);
49+
}
50+
51+
int main() {
52+
plan(3);
53+
54+
run("--ull=100", NULL);
55+
ok(res==0 && argc==0 && opt_ull==100,
56+
"res:%d, argc:%d, opt_ull:%llu", res, argc, opt_ull);
57+
58+
/*
59+
negative numbers are wrapped. this is kinda questionable,
60+
we might want to fix it eventually. but it'd be a change in behavior,
61+
users might've got used to "-1" meaning "max possible value"
62+
*/
63+
run("--ull=-100", NULL);
64+
ok(res==0 && argc==0 && opt_ull==18446744073709551516ULL,
65+
"res:%d, argc:%d, opt_ull:%llu", res, argc, opt_ull);
66+
run("--ul=-100", NULL);
67+
ok(res==0 && argc==0 && opt_ul==4294967295UL,
68+
"res:%d, argc:%d, opt_ul:%lu", res, argc, opt_ul);
69+
return exit_status();
70+
}
71+

0 commit comments

Comments
 (0)