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