File: | /tmp/automake/lib/Automake/tests/Condition.pl |
Coverage: | 75.2% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Copyright (C) 2001, 2002, 2003, 2009 Free Software Foundation, Inc. | ||||||
2 | # | ||||||
3 | # This file is part of GNU Automake. | ||||||
4 | # | ||||||
5 | # GNU Automake is free software; you can redistribute it and/or modify | ||||||
6 | # it under the terms of the GNU General Public License as published by | ||||||
7 | # the Free Software Foundation; either version 2, or (at your option) | ||||||
8 | # any later version. | ||||||
9 | # | ||||||
10 | # GNU Automake is distributed in the hope that it will be useful, | ||||||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
13 | # GNU General Public License for more details. | ||||||
14 | # | ||||||
15 | # You should have received a copy of the GNU General Public License | ||||||
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||
17 | |||||||
18 | 4 4 4 | 70 5 30 | use Automake::Condition qw/TRUE FALSE/; | ||||
19 | |||||||
20 | sub test_basics () | ||||||
21 | { | ||||||
22 | 4 | 68 | my @tests = (# [[Conditions], is_true?, is_false?, string, subst-string, human] | ||||
23 | [[], 1, 0, 'TRUE', '', 'TRUE'], | ||||||
24 | [['TRUE'], 1, 0, 'TRUE', '', 'TRUE'], | ||||||
25 | [['FALSE'], 0, 1, 'FALSE', '#', 'FALSE'], | ||||||
26 | [['A_TRUE'], 0, 0, 'A_TRUE', '@A_TRUE@', 'A'], | ||||||
27 | [['A_TRUE', 'B_FALSE'], | ||||||
28 | 0, 0, 'A_TRUE B_FALSE', '@A_TRUE@@B_FALSE@', 'A and !B'], | ||||||
29 | [['B_TRUE', 'FALSE'], 0, 1, 'FALSE', '#', 'FALSE'], | ||||||
30 | [['B_TRUE', 'B_FALSE'], 0, 1, 'FALSE', '#', 'FALSE']); | ||||||
31 | |||||||
32 | 4 | 11 | for (@tests) | ||||
33 | { | ||||||
34 | 28 28 | 31 91 | my $a = new Automake::Condition @{$_->[0]}; | ||||
35 | 28 | 81 | return 1 if $_->[1] != $a->true; | ||||
36 | 28 | 69 | return 1 if $_->[1] != ($a == TRUE); | ||||
37 | 28 | 68 | return 1 if $_->[2] != $a->false; | ||||
38 | 28 | 65 | return 1 if $_->[2] != ($a == FALSE); | ||||
39 | 28 | 66 | return 1 if $_->[3] ne $a->string; | ||||
40 | 28 | 70 | return 1 if $_->[4] ne $a->subst_string; | ||||
41 | 28 | 73 | return 1 if $_->[5] ne $a->human; | ||||
42 | } | ||||||
43 | 4 | 38 | return 0; | ||||
44 | } | ||||||
45 | |||||||
46 | sub test_true_when () | ||||||
47 | { | ||||||
48 | 4 | 6 | my $failed = 0; | ||||
49 | |||||||
50 | 4 | 63 | my @tests = (# [When, | ||||
51 | # [Implied-Conditions], | ||||||
52 | # [Not-Implied-Conditions]] | ||||||
53 | [['TRUE'], | ||||||
54 | [['TRUE']], | ||||||
55 | [['A_TRUE'], ['A_TRUE', 'B_FALSE'], ['FALSE']]], | ||||||
56 | [['A_TRUE'], | ||||||
57 | [['TRUE'], ['A_TRUE']], | ||||||
58 | [['A_TRUE', 'B_FALSE'], ['FALSE']]], | ||||||
59 | [['A_TRUE', 'B_FALSE'], | ||||||
60 | [['TRUE'], ['A_TRUE'], ['B_FALSE'], ['A_TRUE', 'B_FALSE']], | ||||||
61 | [['FALSE'], ['C_FALSE'], ['C_FALSE', 'A_TRUE']]]); | ||||||
62 | |||||||
63 | 4 | 12 | for my $t (@tests) | ||||
64 | { | ||||||
65 | 12 12 | 20 37 | my $a = new Automake::Condition @{$t->[0]}; | ||||
66 | 12 12 | 16 25 | for my $u (@{$t->[1]}) | ||||
67 | { | ||||||
68 | 28 | 67 | my $b = new Automake::Condition @$u; | ||||
69 | 28 | 62 | if (! $b->true_when ($a)) | ||||
70 | { | ||||||
71 | 0 | 0 | print "`" . $b->string . | ||||
72 | "' not implied by `" . $a->string . "'?\n"; | ||||||
73 | 0 | 0 | $failed = 1; | ||||
74 | } | ||||||
75 | } | ||||||
76 | 12 12 | 15 28 | for my $u (@{$t->[2]}) | ||||
77 | { | ||||||
78 | 32 | 80 | my $b = new Automake::Condition @$u; | ||||
79 | 32 | 77 | if ($b->true_when ($a)) | ||||
80 | { | ||||||
81 | 0 | 0 | print "`" . $b->string . | ||||
82 | "' implied by `" . $a->string . "'?\n"; | ||||||
83 | 0 | 0 | $failed = 1; | ||||
84 | } | ||||||
85 | |||||||
86 | 32 | 70 | return 1 if $b->true_when ($a); | ||||
87 | } | ||||||
88 | } | ||||||
89 | 4 | 44 | return $failed; | ||||
90 | } | ||||||
91 | |||||||
92 | sub test_reduce_and () | ||||||
93 | { | ||||||
94 | 4 | 270 | my @tests = (# If no conditions are given, TRUE should be returned | ||||
95 | [[], ["TRUE"]], | ||||||
96 | # An empty condition is TRUE | ||||||
97 | [[""], ["TRUE"]], | ||||||
98 | # A single condition should be passed through unchanged | ||||||
99 | [["FOO"], ["FOO"]], | ||||||
100 | [["FALSE"], ["FALSE"]], | ||||||
101 | [["TRUE"], ["TRUE"]], | ||||||
102 | # TRUE and false should be discarded and overwhelm | ||||||
103 | # the result, respectively | ||||||
104 | [["FOO", "TRUE"], ["FOO"]], | ||||||
105 | [["FOO", "FALSE"], ["FALSE"]], | ||||||
106 | # Repetitions should be removed | ||||||
107 | [["FOO", "FOO"], ["FOO"]], | ||||||
108 | [["TRUE", "FOO", "FOO"], ["FOO"]], | ||||||
109 | [["FOO", "TRUE", "FOO"], ["FOO"]], | ||||||
110 | [["FOO", "FOO", "TRUE"], ["FOO"]], | ||||||
111 | # Two different conditions should be preserved, | ||||||
112 | # but TRUEs should be removed | ||||||
113 | [["FOO", "BAR"], ["BAR,FOO"]], | ||||||
114 | [["TRUE", "FOO", "BAR"], ["BAR,FOO"]], | ||||||
115 | [["FOO", "TRUE", "BAR"], ["BAR,FOO"]], | ||||||
116 | [["FOO", "BAR", "TRUE"], ["BAR,FOO"]], | ||||||
117 | # A condition implied by another condition should be removed. | ||||||
118 | [["FOO BAR", "BAR"], ["FOO BAR"]], | ||||||
119 | [["BAR", "FOO BAR"], ["FOO BAR"]], | ||||||
120 | [["TRUE", "FOO BAR", "BAR"], ["FOO BAR"]], | ||||||
121 | [["FOO BAR", "TRUE", "BAR"], ["FOO BAR"]], | ||||||
122 | [["FOO BAR", "BAR", "TRUE"], ["FOO BAR"]], | ||||||
123 | |||||||
124 | [["BAR FOO", "BAR"], ["BAR FOO"]], | ||||||
125 | [["BAR", "BAR FOO"], ["BAR FOO"]], | ||||||
126 | [["TRUE", "BAR FOO", "BAR"], ["BAR FOO"]], | ||||||
127 | [["BAR FOO", "TRUE", "BAR"], ["BAR FOO"]], | ||||||
128 | [["BAR FOO", "BAR", "TRUE"], ["BAR FOO"]], | ||||||
129 | |||||||
130 | # Check that reduction happens even when there are | ||||||
131 | # two conditions to remove. | ||||||
132 | [["FOO", "FOO BAR", "BAR"], ["FOO BAR"]], | ||||||
133 | [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["FOO BAR", "FOO BAZ"]], | ||||||
134 | [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"], | ||||||
135 | ["FOO BAZ BAR"]], | ||||||
136 | |||||||
137 | # Duplicated conditionals should be removed. | ||||||
138 | [["FOO", "BAR", "BAR"], ["BAR,FOO"]], | ||||||
139 | |||||||
140 | # Equivalent conditions in different forms should be | ||||||
141 | # reduced: which one is left is unfortunately order | ||||||
142 | # dependent. | ||||||
143 | [["BAR FOO", "FOO BAR"], ["FOO BAR"]], | ||||||
144 | [["FOO BAR", "BAR FOO"], ["BAR FOO"]]); | ||||||
145 | |||||||
146 | 4 | 17 | my $failed = 0; | ||||
147 | 4 | 8 | foreach (@tests) | ||||
148 | { | ||||||
149 | 124 | 182 | my ($inref, $outref) = @$_; | ||||
150 | 124 300 | 180 682 | my @inconds = map { new Automake::Condition $_ } @$inref; | ||||
151 | 124 128 | 192 312 | my @outconds = map { (new Automake::Condition $_)->string } @$outref; | ||||
152 | 148 | 289 | my @res = | ||||
153 | 124 | 275 | map { $_->string } (Automake::Condition::reduce_and (@inconds)); | ||||
154 | 124 | 325 | my $result = join (",", sort @res); | ||||
155 | 124 | 167 | my $exresult = join (",", @outconds); | ||||
156 | |||||||
157 | 124 | 337 | if ($result ne $exresult) | ||||
158 | { | ||||||
159 | 0 | 0 | print '"' . join(",", @$inref) . '" => "' . | ||||
160 | $result . '" expected "' . | ||||||
161 | $exresult . '"' . "\n"; | ||||||
162 | 0 | 0 | $failed = 1; | ||||
163 | } | ||||||
164 | } | ||||||
165 | 4 | 90 | return $failed; | ||||
166 | } | ||||||
167 | |||||||
168 | sub test_reduce_or () | ||||||
169 | { | ||||||
170 | 4 | 231 | my @tests = (# If no conditions are given, FALSE should be returned | ||||
171 | [[], ["FALSE"]], | ||||||
172 | # An empty condition is TRUE | ||||||
173 | [[""], ["TRUE"]], | ||||||
174 | # A single condition should be passed through unchanged | ||||||
175 | [["FOO"], ["FOO"]], | ||||||
176 | [["FALSE"], ["FALSE"]], | ||||||
177 | [["TRUE"], ["TRUE"]], | ||||||
178 | # FALSE and TRUE should be discarded and overwhelm | ||||||
179 | # the result, respectively | ||||||
180 | [["FOO", "TRUE"], ["TRUE"]], | ||||||
181 | [["FOO", "FALSE"], ["FOO"]], | ||||||
182 | # Repetitions should be removed | ||||||
183 | [["FOO", "FOO"], ["FOO"]], | ||||||
184 | [["FALSE", "FOO", "FOO"], ["FOO"]], | ||||||
185 | [["FOO", "FALSE", "FOO"], ["FOO"]], | ||||||
186 | [["FOO", "FOO", "FALSE"], ["FOO"]], | ||||||
187 | # Two different conditions should be preserved, | ||||||
188 | # but FALSEs should be removed | ||||||
189 | [["FOO", "BAR"], ["BAR,FOO"]], | ||||||
190 | [["FALSE", "FOO", "BAR"], ["BAR,FOO"]], | ||||||
191 | [["FOO", "FALSE", "BAR"], ["BAR,FOO"]], | ||||||
192 | [["FOO", "BAR", "FALSE"], ["BAR,FOO"]], | ||||||
193 | # A condition implying another condition should be removed. | ||||||
194 | [["FOO BAR", "BAR"], ["BAR"]], | ||||||
195 | [["BAR", "FOO BAR"], ["BAR"]], | ||||||
196 | [["FALSE", "FOO BAR", "BAR"], ["BAR"]], | ||||||
197 | [["FOO BAR", "FALSE", "BAR"], ["BAR"]], | ||||||
198 | [["FOO BAR", "BAR", "FALSE"], ["BAR"]], | ||||||
199 | |||||||
200 | [["BAR FOO", "BAR"], ["BAR"]], | ||||||
201 | [["BAR", "BAR FOO"], ["BAR"]], | ||||||
202 | [["FALSE", "BAR FOO", "BAR"], ["BAR"]], | ||||||
203 | [["BAR FOO", "FALSE", "BAR"], ["BAR"]], | ||||||
204 | [["BAR FOO", "BAR", "FALSE"], ["BAR"]], | ||||||
205 | |||||||
206 | # Check that reduction happens even when there are | ||||||
207 | # two conditions to remove. | ||||||
208 | [["FOO", "FOO BAR", "BAR"], ["BAR,FOO"]], | ||||||
209 | [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["BAZ,FOO"]], | ||||||
210 | [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"], | ||||||
211 | ["BAZ,FOO"]], | ||||||
212 | |||||||
213 | # Duplicated conditionals should be removed. | ||||||
214 | [["FOO", "BAR", "BAR"], ["BAR,FOO"]], | ||||||
215 | |||||||
216 | # Equivalent conditions in different forms should be | ||||||
217 | # reduced: which one is left is unfortunately order | ||||||
218 | # dependent. | ||||||
219 | [["BAR FOO", "FOO BAR"], ["FOO BAR"]], | ||||||
220 | [["FOO BAR", "BAR FOO"], ["BAR FOO"]]); | ||||||
221 | |||||||
222 | 4 | 18 | my $failed = 0; | ||||
223 | 4 | 8 | foreach (@tests) | ||||
224 | { | ||||||
225 | 124 | 178 | my ($inref, $outref) = @$_; | ||||
226 | 124 300 | 188 681 | my @inconds = map { new Automake::Condition $_ } @$inref; | ||||
227 | 124 124 | 199 290 | my @outconds = map { (new Automake::Condition $_)->string } @$outref; | ||||
228 | 156 | 305 | my @res = | ||||
229 | 124 | 279 | map { $_->string } (Automake::Condition::reduce_or (@inconds)); | ||||
230 | 124 | 331 | my $result = join (",", sort @res); | ||||
231 | 124 | 168 | my $exresult = join (",", @outconds); | ||||
232 | |||||||
233 | 124 | 337 | if ($result ne $exresult) | ||||
234 | { | ||||||
235 | 0 | 0 | print '"' . join(",", @$inref) . '" => "' . | ||||
236 | $result . '" expected "' . | ||||||
237 | $exresult . '"' . "\n"; | ||||||
238 | 0 | 0 | $failed = 1; | ||||
239 | } | ||||||
240 | } | ||||||
241 | 4 | 109 | return $failed; | ||||
242 | } | ||||||
243 | |||||||
244 | sub test_merge () | ||||||
245 | { | ||||||
246 | 4 | 36 | my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE"; | ||||
247 | 4 | 13 | my $other = new Automake::Condition "COND3_FALSE"; | ||||
248 | 4 | 11 | my $both = $cond->merge ($other); | ||||
249 | 4 | 12 | my $both2 = $cond->merge_conds ("COND3_FALSE"); | ||||
250 | 4 | 10 | $cond = $both->strip ($other); | ||||
251 | 4 | 10 | my @conds = $cond->conds; | ||||
252 | 4 | 12 | return 1 if $both->string ne "COND1_TRUE COND2_FALSE COND3_FALSE"; | ||||
253 | 4 | 12 | return 1 if $cond->string ne "COND1_TRUE COND2_FALSE"; | ||||
254 | 4 | 27 | return 1 if $both != $both2; | ||||
255 | 4 | 4 | return 0; | ||||
256 | } | ||||||
257 | |||||||
258 | 4 | 14 | exit (test_basics | ||||
259 | || test_true_when | ||||||
260 | || test_reduce_and | ||||||
261 | || test_reduce_or | ||||||
262 | || test_merge); | ||||||
263 | |||||||
264 | ### Setup "GNU" style for perl-mode and cperl-mode. | ||||||
265 | ## Local Variables: | ||||||
266 | ## perl-indent-level: 2 | ||||||
267 | ## perl-continued-statement-offset: 2 | ||||||
268 | ## perl-continued-brace-offset: 0 | ||||||
269 | ## perl-brace-offset: 0 | ||||||
270 | ## perl-brace-imaginary-offset: 0 | ||||||
271 | ## perl-label-offset: -2 | ||||||
272 | ## cperl-indent-level: 2 | ||||||
273 | ## cperl-brace-offset: 0 | ||||||
274 | ## cperl-continued-brace-offset: 0 | ||||||
275 | ## cperl-label-offset: -2 | ||||||
276 | ## cperl-extra-newline-before-brace: t | ||||||
277 | ## cperl-merge-trailing-else: nil | ||||||
278 | ## cperl-continued-statement-offset: 2 | ||||||
279 | ## End: |