Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2010, 2012 Joel Rosdahl
3 : : *
4 : : * This program is free software; you can redistribute it and/or modify it
5 : : * under the terms of the GNU General Public License as published by the Free
6 : : * Software Foundation; either version 3 of the License, or (at your option)
7 : : * any later version.
8 : : *
9 : : * This program is distributed in the hope that it will be useful, but WITHOUT
10 : : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 : : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 : : * more details.
13 : : *
14 : : * You should have received a copy of the GNU General Public License along with
15 : : * this program; if not, write to the Free Software Foundation, Inc., 51
16 : : * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 : : */
18 : :
19 : : /*
20 : : * This file contains tests for the functions operating on struct args.
21 : : */
22 : :
23 : : #include "ccache.h"
24 : : #include "test/framework.h"
25 : : #include "test/util.h"
26 : :
27 : 1 : TEST_SUITE(args)
28 : :
29 [ + - ]: 1 : TEST(args_init_empty)
30 : : {
31 : 1 : struct args *args = args_init(0, NULL);
32 [ + - ]: 1 : CHECK(args);
33 [ - + ]: 1 : CHECK_INT_EQ(0, args->argc);
34 [ + - ]: 1 : CHECK(!args->argv[0]);
35 : 1 : args_free(args);
36 : : }
37 : :
38 [ + - ]: 1 : TEST(args_init_populated)
39 : : {
40 : 1 : char *argv[] = {"first", "second"};
41 : 1 : struct args *args = args_init(2, argv);
42 [ + - ]: 1 : CHECK(args);
43 [ - + ]: 1 : CHECK_INT_EQ(2, args->argc);
44 [ - + ]: 1 : CHECK_STR_EQ("first", args->argv[0]);
45 [ - + ]: 1 : CHECK_STR_EQ("second", args->argv[1]);
46 [ + - ]: 1 : CHECK(!args->argv[2]);
47 : 1 : args_free(args);
48 : : }
49 : :
50 [ + - ]: 1 : TEST(args_init_from_string)
51 : : {
52 : 1 : struct args *args = args_init_from_string("first second\tthird\nfourth");
53 [ + - ]: 1 : CHECK(args);
54 [ - + ]: 1 : CHECK_INT_EQ(4, args->argc);
55 [ - + ]: 1 : CHECK_STR_EQ("first", args->argv[0]);
56 [ - + ]: 1 : CHECK_STR_EQ("second", args->argv[1]);
57 [ - + ]: 1 : CHECK_STR_EQ("third", args->argv[2]);
58 [ - + ]: 1 : CHECK_STR_EQ("fourth", args->argv[3]);
59 [ + - ]: 1 : CHECK(!args->argv[4]);
60 : 1 : args_free(args);
61 : : }
62 : :
63 [ + - ]: 1 : TEST(args_init_from_gcc_atfile)
64 : : {
65 : : struct args *args;
66 : : const char *argtext =
67 : : "first\rsec\\\tond\tthi\\\\rd\nfourth \tfif\\ th \"si'x\\\" th\""
68 : 1 : " 'seve\nth'\\";
69 : :
70 : 1 : create_file("gcc_atfile", argtext);
71 : :
72 : 1 : args = args_init_from_gcc_atfile("gcc_atfile");
73 [ + - ]: 1 : CHECK(args);
74 [ - + ]: 1 : CHECK_INT_EQ(7, args->argc);
75 [ - + ]: 1 : CHECK_STR_EQ("first", args->argv[0]);
76 [ - + ]: 1 : CHECK_STR_EQ("sec\tond", args->argv[1]);
77 [ - + ]: 1 : CHECK_STR_EQ("thi\\rd", args->argv[2]);
78 [ - + ]: 1 : CHECK_STR_EQ("fourth", args->argv[3]);
79 [ - + ]: 1 : CHECK_STR_EQ("fif th", args->argv[4]);
80 [ - + ]: 1 : CHECK_STR_EQ("si'x\" th", args->argv[5]);
81 [ - + ]: 1 : CHECK_STR_EQ("seve\nth", args->argv[6]);
82 [ + - ]: 1 : CHECK(!args->argv[7]);
83 : 1 : args_free(args);
84 : : }
85 : :
86 [ + - ]: 1 : TEST(args_copy)
87 : : {
88 : 1 : struct args *args1 = args_init_from_string("foo");
89 : 1 : struct args *args2 = args_copy(args1);
90 [ - + ]: 1 : CHECK_ARGS_EQ_FREE12(args1, args2);
91 : : }
92 : :
93 [ + - ]: 1 : TEST(args_add)
94 : : {
95 : 1 : struct args *args = args_init_from_string("first");
96 [ - + ]: 1 : CHECK_INT_EQ(1, args->argc);
97 : 1 : args_add(args, "second");
98 [ - + ]: 1 : CHECK_INT_EQ(2, args->argc);
99 [ - + ]: 1 : CHECK_STR_EQ("second", args->argv[1]);
100 [ + - ]: 1 : CHECK(!args->argv[2]);
101 : 1 : args_free(args);
102 : : }
103 : :
104 [ + - ]: 1 : TEST(args_extend)
105 : : {
106 : 1 : struct args *args1 = args_init_from_string("first");
107 : 1 : struct args *args2 = args_init_from_string("second third");
108 [ - + ]: 1 : CHECK_INT_EQ(1, args1->argc);
109 : 1 : args_extend(args1, args2);
110 [ - + ]: 1 : CHECK_INT_EQ(3, args1->argc);
111 [ - + ]: 1 : CHECK_STR_EQ("second", args1->argv[1]);
112 [ - + ]: 1 : CHECK_STR_EQ("third", args1->argv[2]);
113 [ + - ]: 1 : CHECK(!args1->argv[3]);
114 : 1 : args_free(args1);
115 : 1 : args_free(args2);
116 : : }
117 : :
118 [ + - ]: 1 : TEST(args_pop)
119 : : {
120 : 1 : struct args *args = args_init_from_string("first second third");
121 : 1 : args_pop(args, 2);
122 [ - + ]: 1 : CHECK_INT_EQ(1, args->argc);
123 [ - + ]: 1 : CHECK_STR_EQ("first", args->argv[0]);
124 [ + - ]: 1 : CHECK(!args->argv[1]);
125 : 1 : args_free(args);
126 : : }
127 : :
128 [ + - ]: 1 : TEST(args_set)
129 : : {
130 : 1 : struct args *args = args_init_from_string("first second third");
131 : 1 : args_set(args, 1, "2nd");
132 [ - + ]: 1 : CHECK_INT_EQ(3, args->argc);
133 [ - + ]: 1 : CHECK_STR_EQ("first", args->argv[0]);
134 [ - + ]: 1 : CHECK_STR_EQ("2nd", args->argv[1]);
135 [ - + ]: 1 : CHECK_STR_EQ("third", args->argv[2]);
136 [ + - ]: 1 : CHECK(!args->argv[3]);
137 : 1 : args_free(args);
138 : : }
139 : :
140 [ + - ]: 1 : TEST(args_remove_first)
141 : : {
142 : 1 : struct args *args1 = args_init_from_string("first second third");
143 : 1 : struct args *args2 = args_init_from_string("second third");
144 : 1 : args_remove_first(args1);
145 [ - + ]: 1 : CHECK_ARGS_EQ_FREE12(args1, args2);
146 : : }
147 : :
148 [ + - ]: 1 : TEST(args_add_prefix)
149 : : {
150 : 1 : struct args *args1 = args_init_from_string("second third");
151 : 1 : struct args *args2 = args_init_from_string("first second third");
152 : 1 : args_add_prefix(args1, "first");
153 [ - + ]: 1 : CHECK_ARGS_EQ_FREE12(args1, args2);
154 : : }
155 : :
156 [ + - ]: 1 : TEST(args_strip)
157 : : {
158 : 1 : struct args *args1 = args_init_from_string("first xsecond third xfourth");
159 : 1 : struct args *args2 = args_init_from_string("first third");
160 : 1 : args_strip(args1, "x");
161 [ - + ]: 1 : CHECK_ARGS_EQ_FREE12(args1, args2);
162 : : }
163 : :
164 [ + - ]: 1 : TEST(args_to_string)
165 : : {
166 : 1 : struct args *args = args_init_from_string("first second");
167 [ - + ]: 1 : CHECK_STR_EQ_FREE2("first second", args_to_string(args));
168 : 1 : args_free(args);
169 : : }
170 : :
171 [ + - ]: 1 : TEST(args_insert)
172 : : {
173 : 1 : struct args *args = args_init_from_string("first second third fourth fifth");
174 : :
175 : 1 : struct args *src1 = args_init_from_string("alpha beta gamma");
176 : 1 : struct args *src2 = args_init_from_string("one");
177 : 1 : struct args *src3 = args_init_from_string("");
178 : 1 : struct args *src4 = args_init_from_string("alpha beta gamma");
179 : 1 : struct args *src5 = args_init_from_string("one");
180 : 1 : struct args *src6 = args_init_from_string("");
181 : :
182 : 1 : args_insert(args, 2, src1, true);
183 [ - + ]: 1 : CHECK_STR_EQ_FREE2("first second alpha beta gamma fourth fifth",
184 : : args_to_string(args));
185 [ - + ]: 1 : CHECK_INT_EQ(7, args->argc);
186 : 1 : args_insert(args, 2, src2, true);
187 [ - + ]: 1 : CHECK_STR_EQ_FREE2("first second one beta gamma fourth fifth",
188 : : args_to_string(args));
189 [ - + ]: 1 : CHECK_INT_EQ(7, args->argc);
190 : 1 : args_insert(args, 2, src3, true);
191 [ - + ]: 1 : CHECK_STR_EQ_FREE2("first second beta gamma fourth fifth",
192 : : args_to_string(args));
193 [ - + ]: 1 : CHECK_INT_EQ(6, args->argc);
194 : :
195 : 1 : args_insert(args, 1, src4, false);
196 [ - + ]: 1 : CHECK_STR_EQ_FREE2("first alpha beta gamma second beta gamma fourth fifth",
197 : : args_to_string(args));
198 [ - + ]: 1 : CHECK_INT_EQ(9, args->argc);
199 : 1 : args_insert(args, 1, src5, false);
200 [ - + ]: 1 : CHECK_STR_EQ_FREE2(
201 : : "first one alpha beta gamma second beta gamma fourth fifth",
202 : : args_to_string(args));
203 [ - + ]: 1 : CHECK_INT_EQ(10, args->argc);
204 : 1 : args_insert(args, 1, src6, false);
205 [ - + ]: 1 : CHECK_STR_EQ_FREE2(
206 : : "first one alpha beta gamma second beta gamma fourth fifth",
207 : : args_to_string(args));
208 [ - + ]: 1 : CHECK_INT_EQ(10, args->argc);
209 : :
210 : 1 : args_free(args);
211 : : }
212 : :
213 : 1 : TEST_SUITE_END
|