libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

libutil/src/test/test_optget.c (183 lines, 4.9 KiB) -rw-r--r-- file download

7a809d7 Jamozed 2020-11-18 22:32:50
0
// test_optget.c
98e5939 Jamozed 2021-11-27 17:53:46
1
// optget unit test for libutil
7a809d7 Jamozed 2020-11-18 22:32:50
2
// Copyright (C) 2020, Jakob Wakeling
7f427d9 Jamozed 2022-03-06 12:55:13
3
// MIT Licence
b03cf63 Jamozed 2021-01-27 14:51:34
4
bd9fc8b Jamozed 2021-01-27 14:44:30
5
#include "../optget.h"
7a809d7 Jamozed 2020-11-18 22:32:50
6
#include "unit.h"
7a809d7 Jamozed 2020-11-18 22:32:50
7
7a809d7 Jamozed 2020-11-18 22:32:50
8
#include <stdbool.h>
7a809d7 Jamozed 2020-11-18 22:32:50
9
#include <stdio.h>
7a809d7 Jamozed 2020-11-18 22:32:50
10
#include <string.h>
7a809d7 Jamozed 2020-11-18 22:32:50
11
7a809d7 Jamozed 2020-11-18 22:32:50
12
#define SEQ(s1, s2) (strcmp(s1, s2) == 0)
7a809d7 Jamozed 2020-11-18 22:32:50
13
7a809d7 Jamozed 2020-11-18 22:32:50
14
int main(void) {
7a809d7 Jamozed 2020-11-18 22:32:50
15
	struct opt opt;
7a809d7 Jamozed 2020-11-18 22:32:50
16
	
7a809d7 Jamozed 2020-11-18 22:32:50
17
	{
7a809d7 Jamozed 2020-11-18 22:32:50
18
		char *av[] = { "G00", "-a", "-bfoo", "-c", "bar", "--", "baz", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
19
		
7a809d7 Jamozed 2020-11-18 22:32:50
20
		opt = OPTGET_INIT; opt.str = ":ab:c:";
7a809d7 Jamozed 2020-11-18 22:32:50
21
		ASSERT("G00T00 option",
7a809d7 Jamozed 2020-11-18 22:32:50
22
				optget(&opt, av, 1) == 'a');
7a809d7 Jamozed 2020-11-18 22:32:50
23
		ASSERT("G00T01 option with shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
24
				optget(&opt, av, 1) == 'b');
7a809d7 Jamozed 2020-11-18 22:32:50
25
		ASSERT("G00T02 optarg shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
26
				opt.arg == av[2] + 2);
7a809d7 Jamozed 2020-11-18 22:32:50
27
		ASSERT("G00T03 option with seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
28
				optget(&opt, av, 1) == 'c');
7a809d7 Jamozed 2020-11-18 22:32:50
29
		ASSERT("G00T04 optarg seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
30
				opt.arg == av[4]);
7a809d7 Jamozed 2020-11-18 22:32:50
31
		ASSERT("G00T05 options end",
7a809d7 Jamozed 2020-11-18 22:32:50
32
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
33
		ASSERT("G00T06 operand",
7a809d7 Jamozed 2020-11-18 22:32:50
34
				SEQ(av[opt.ind], "baz"));
7a809d7 Jamozed 2020-11-18 22:32:50
35
	}
7a809d7 Jamozed 2020-11-18 22:32:50
36
	
7a809d7 Jamozed 2020-11-18 22:32:50
37
	{
7a809d7 Jamozed 2020-11-18 22:32:50
38
		char *av[] = { "G01", "-abcfoo", "bar", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
39
		
7a809d7 Jamozed 2020-11-18 22:32:50
40
		opt = OPTGET_INIT; opt.str = ":abc:";
7a809d7 Jamozed 2020-11-18 22:32:50
41
		ASSERT("G01T00 option",
7a809d7 Jamozed 2020-11-18 22:32:50
42
				optget(&opt, av, 1) == 'a');
7a809d7 Jamozed 2020-11-18 22:32:50
43
		ASSERT("G01T01 option",
7a809d7 Jamozed 2020-11-18 22:32:50
44
				optget(&opt, av, 1) == 'b');
7a809d7 Jamozed 2020-11-18 22:32:50
45
		ASSERT("G01T02 option with shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
46
				optget(&opt, av, 1) == 'c');
7a809d7 Jamozed 2020-11-18 22:32:50
47
		ASSERT("G01T03 optarg shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
48
				opt.arg == av[1] + 4);
7a809d7 Jamozed 2020-11-18 22:32:50
49
		ASSERT("G01T04 options end",
7a809d7 Jamozed 2020-11-18 22:32:50
50
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
51
		ASSERT("G01T05 optind at end",
7a809d7 Jamozed 2020-11-18 22:32:50
52
				opt.ind == 2);
7a809d7 Jamozed 2020-11-18 22:32:50
53
		ASSERT("G01T06 optarg after end",
7a809d7 Jamozed 2020-11-18 22:32:50
54
				opt.arg == av[1] + 4);
7a809d7 Jamozed 2020-11-18 22:32:50
55
		ASSERT("G01T07 options still end",
7a809d7 Jamozed 2020-11-18 22:32:50
56
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
57
		ASSERT("G01T08 optind still at end",
7a809d7 Jamozed 2020-11-18 22:32:50
58
				opt.ind == 2);
7a809d7 Jamozed 2020-11-18 22:32:50
59
		ASSERT("G01T09 operand",
7a809d7 Jamozed 2020-11-18 22:32:50
60
				SEQ(av[opt.ind], "bar"));
7a809d7 Jamozed 2020-11-18 22:32:50
61
	}
7a809d7 Jamozed 2020-11-18 22:32:50
62
	
7a809d7 Jamozed 2020-11-18 22:32:50
63
	{
7a809d7 Jamozed 2020-11-18 22:32:50
64
		char *av[] = { "G02", "-aa", "-b", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
65
		
7a809d7 Jamozed 2020-11-18 22:32:50
66
		opt = OPTGET_INIT; opt.str = ":b:";
7a809d7 Jamozed 2020-11-18 22:32:50
67
		ASSERT("G02T00 invalid option",
7a809d7 Jamozed 2020-11-18 22:32:50
68
				optget(&opt, av, 1) == '?');
7a809d7 Jamozed 2020-11-18 22:32:50
69
		ASSERT("G02T01 optopt",
7a809d7 Jamozed 2020-11-18 22:32:50
70
				opt.opt == 'a');
7a809d7 Jamozed 2020-11-18 22:32:50
71
		ASSERT("G02T00 invalid option again",
7a809d7 Jamozed 2020-11-18 22:32:50
72
				optget(&opt, av, 1) == '?');
7a809d7 Jamozed 2020-11-18 22:32:50
73
		ASSERT("G02T01 optopt",
7a809d7 Jamozed 2020-11-18 22:32:50
74
				opt.opt == 'a');
7a809d7 Jamozed 2020-11-18 22:32:50
75
		ASSERT("G02T02 missing option argument",
7a809d7 Jamozed 2020-11-18 22:32:50
76
				optget(&opt, av, 1) == ':');
7a809d7 Jamozed 2020-11-18 22:32:50
77
		ASSERT("G02T03 optopt",
7a809d7 Jamozed 2020-11-18 22:32:50
78
				opt.opt == 'b');
7a809d7 Jamozed 2020-11-18 22:32:50
79
	}
7a809d7 Jamozed 2020-11-18 22:32:50
80
	
7a809d7 Jamozed 2020-11-18 22:32:50
81
	{
7a809d7 Jamozed 2020-11-18 22:32:50
82
		char *av[] = { "G03", "foo", "-a", "bar", "-b", "baz", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
83
		int argc = (sizeof (av) / sizeof (*av) - 1);
7a809d7 Jamozed 2020-11-18 22:32:50
84
		
7a809d7 Jamozed 2020-11-18 22:32:50
85
		opt = OPTGET_INIT; opt.str = ":ab:";
7a809d7 Jamozed 2020-11-18 22:32:50
86
		ASSERT("G03T00 option",
7a809d7 Jamozed 2020-11-18 22:32:50
87
				optget(&opt, av, 1) == 'a');
7a809d7 Jamozed 2020-11-18 22:32:50
88
		ASSERT("G03T01 option with seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
89
				optget(&opt, av, 1) == 'b');
7a809d7 Jamozed 2020-11-18 22:32:50
90
		ASSERT("G03T02 optarg seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
91
				SEQ(opt.arg, "baz"));
7a809d7 Jamozed 2020-11-18 22:32:50
92
		ASSERT("G03T03 options end",
7a809d7 Jamozed 2020-11-18 22:32:50
93
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
94
		ASSERT("G03T04 optind at end",
7a809d7 Jamozed 2020-11-18 22:32:50
95
				opt.ind == 4);
7a809d7 Jamozed 2020-11-18 22:32:50
96
		ASSERT("G03T05 options still end",
7a809d7 Jamozed 2020-11-18 22:32:50
97
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
98
		ASSERT("G03T06 optind still at end",
7a809d7 Jamozed 2020-11-18 22:32:50
99
				opt.ind == 4);
7a809d7 Jamozed 2020-11-18 22:32:50
100
		ASSERT("G03T07 permuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
101
				SEQ(av[opt.ind], "foo")); ++opt.ind;
7a809d7 Jamozed 2020-11-18 22:32:50
102
		ASSERT("G03T08 permuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
103
				SEQ(av[opt.ind], "bar")); ++opt.ind;
7a809d7 Jamozed 2020-11-18 22:32:50
104
		ASSERT("G03T09 permuted operands end",
7a809d7 Jamozed 2020-11-18 22:32:50
105
				opt.ind >= argc);
7a809d7 Jamozed 2020-11-18 22:32:50
106
	}
7a809d7 Jamozed 2020-11-18 22:32:50
107
	
7a809d7 Jamozed 2020-11-18 22:32:50
108
	{
7a809d7 Jamozed 2020-11-18 22:32:50
109
		char *av[] = { "G04", "--foo", "--bar=arg", "--bar", "argument",
7a809d7 Jamozed 2020-11-18 22:32:50
110
			"--baz", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
111
		
7a809d7 Jamozed 2020-11-18 22:32:50
112
		struct lop lops[] = {
7a809d7 Jamozed 2020-11-18 22:32:50
113
			{ "foo", ARG_NUL, 256 },
7a809d7 Jamozed 2020-11-18 22:32:50
114
			{ "bar", ARG_REQ, 257 },
7a809d7 Jamozed 2020-11-18 22:32:50
115
			{ "baz", ARG_OPT, 258 },
7a809d7 Jamozed 2020-11-18 22:32:50
116
			{ NULL, 0, 0 }
7a809d7 Jamozed 2020-11-18 22:32:50
117
		};
7a809d7 Jamozed 2020-11-18 22:32:50
118
		
7a809d7 Jamozed 2020-11-18 22:32:50
119
		opt = OPTGET_INIT; opt.str = ":"; opt.lops = lops;
7a809d7 Jamozed 2020-11-18 22:32:50
120
		ASSERT("G04T00 long option",
7a809d7 Jamozed 2020-11-18 22:32:50
121
				optget(&opt, av, 1) == 256);
7a809d7 Jamozed 2020-11-18 22:32:50
122
		ASSERT("G04T01 long option with shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
123
				optget(&opt, av, 1) == 257);
7a809d7 Jamozed 2020-11-18 22:32:50
124
		ASSERT("G04T02 optarg shared argument",
7a809d7 Jamozed 2020-11-18 22:32:50
125
				SEQ(opt.arg, "arg"));
7a809d7 Jamozed 2020-11-18 22:32:50
126
		ASSERT("G04T03 long option with seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
127
				optget(&opt, av, 1) == 257);
7a809d7 Jamozed 2020-11-18 22:32:50
128
		ASSERT("G04T04 optarg seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
129
				SEQ(opt.arg, "argument"));
7a809d7 Jamozed 2020-11-18 22:32:50
130
		ASSERT("G04T05 long option without argument",
7a809d7 Jamozed 2020-11-18 22:32:50
131
				optget(&opt, av, 1) == 258);
7a809d7 Jamozed 2020-11-18 22:32:50
132
		ASSERT("G04T06 optarg seperate argument",
7a809d7 Jamozed 2020-11-18 22:32:50
133
				opt.arg == NULL);
7a809d7 Jamozed 2020-11-18 22:32:50
134
	}
7a809d7 Jamozed 2020-11-18 22:32:50
135
	
7a809d7 Jamozed 2020-11-18 22:32:50
136
	{
7a809d7 Jamozed 2020-11-18 22:32:50
137
		char *av[] = { "G05", "foo", "--bar", "--", "baz", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
138
		int argc = (sizeof (av) / sizeof (*av) - 1);
7a809d7 Jamozed 2020-11-18 22:32:50
139
		
7a809d7 Jamozed 2020-11-18 22:32:50
140
		struct lop lops[] = {
7a809d7 Jamozed 2020-11-18 22:32:50
141
			{ "bar", ARG_NUL, 256 },
7a809d7 Jamozed 2020-11-18 22:32:50
142
			{ NULL, 0, 0 }
7a809d7 Jamozed 2020-11-18 22:32:50
143
		};
7a809d7 Jamozed 2020-11-18 22:32:50
144
		
7a809d7 Jamozed 2020-11-18 22:32:50
145
		opt = OPTGET_INIT; opt.str = ":"; opt.lops = lops;
7a809d7 Jamozed 2020-11-18 22:32:50
146
		ASSERT("G05T00 long option",
7a809d7 Jamozed 2020-11-18 22:32:50
147
				optget(&opt, av, 1) == 256);
7a809d7 Jamozed 2020-11-18 22:32:50
148
		ASSERT("G05T01 options end",
7a809d7 Jamozed 2020-11-18 22:32:50
149
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
150
		ASSERT("G05T02 permuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
151
				SEQ(av[opt.ind], "foo")); ++opt.ind;
7a809d7 Jamozed 2020-11-18 22:32:50
152
		ASSERT("G05T03 permuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
153
				SEQ(av[opt.ind], "baz")); ++opt.ind;
7a809d7 Jamozed 2020-11-18 22:32:50
154
		ASSERT("G05T04 permuted operands end",
7a809d7 Jamozed 2020-11-18 22:32:50
155
				opt.ind >= argc);
7a809d7 Jamozed 2020-11-18 22:32:50
156
		ASSERT("G05T05 options still end",
7a809d7 Jamozed 2020-11-18 22:32:50
157
				optget(&opt, av, 1) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
158
		ASSERT("G05T06 permutes operands still end",
7a809d7 Jamozed 2020-11-18 22:32:50
159
				opt.ind >= argc);
7a809d7 Jamozed 2020-11-18 22:32:50
160
		
7a809d7 Jamozed 2020-11-18 22:32:50
161
	}
7a809d7 Jamozed 2020-11-18 22:32:50
162
	
7a809d7 Jamozed 2020-11-18 22:32:50
163
	{
7a809d7 Jamozed 2020-11-18 22:32:50
164
		char *av[] = { "G06", "--foo", "bar", "-a", NULL };
7a809d7 Jamozed 2020-11-18 22:32:50
165
		
7a809d7 Jamozed 2020-11-18 22:32:50
166
		opt = OPTGET_INIT; opt.str = ":a";
7a809d7 Jamozed 2020-11-18 22:32:50
167
		ASSERT("G06T00 invalid option with NULL lopts",
7a809d7 Jamozed 2020-11-18 22:32:50
168
				optget(&opt, av, 0) == '?');
7a809d7 Jamozed 2020-11-18 22:32:50
169
		ASSERT("G06T01 options end without permute",
7a809d7 Jamozed 2020-11-18 22:32:50
170
				optget(&opt, av, 0) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
171
		ASSERT("G06T02 unpermuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
172
				SEQ(av[opt.ind], "bar"));
7a809d7 Jamozed 2020-11-18 22:32:50
173
		ASSERT("G06T03 still no permute",
7a809d7 Jamozed 2020-11-18 22:32:50
174
				optget(&opt, av, 0) == -1);
7a809d7 Jamozed 2020-11-18 22:32:50
175
		ASSERT("G06T04 still unpermuted operand",
7a809d7 Jamozed 2020-11-18 22:32:50
176
				SEQ(av[opt.ind], "bar"));
7a809d7 Jamozed 2020-11-18 22:32:50
177
	}
7a809d7 Jamozed 2020-11-18 22:32:50
178
	
7a809d7 Jamozed 2020-11-18 22:32:50
179
	printf("%d of %d tests passed\n", testspassed, testsrun);
7a809d7 Jamozed 2020-11-18 22:32:50
180
	return testsfailed;
7a809d7 Jamozed 2020-11-18 22:32:50
181
}
182