Merge branch 'minor'
[automake.git] / t / pm / DisjConditions-t.pl
blobadcc4201f7de5e20a26d04267c1c8aaf9c503165
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 BEGIN {
17 use Config;
18 if (eval { require 5.007_002; } # for CLONE support
19 && $Config{useithreads}
20 && !$ENV{WANT_NO_THREADS})
22 require threads;
23 import threads;
25 else
27 exit 77;
30 use Automake::Condition qw/TRUE FALSE/;
31 use Automake::DisjConditions;
33 sub test_basics ()
35 my $true = new Automake::DisjConditions TRUE;
36 my $false = new Automake::DisjConditions FALSE;
37 my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
38 return threads->new (sub {
39 my $other = new Automake::Condition "COND3_FALSE";
40 my $another = new Automake::Condition "COND3_TRUE", "COND4_FALSE";
41 return threads->new (sub {
42 my $set1 = new Automake::DisjConditions $cond, $other;
43 return threads->new (sub {
44 my $set2 = new Automake::DisjConditions $other, $cond;
45 my $set3 = new Automake::DisjConditions FALSE, $another;
46 return 1 unless $set1 == $set2;
47 return 1 if $set1->false;
48 return 1 if $set1->true;
49 return 1 unless (new Automake::DisjConditions)->false;
50 return 1 if (new Automake::DisjConditions)->true;
51 return 1 unless $true->human eq 'TRUE';
52 return 1 unless $false->human eq 'FALSE';
53 return 1 unless $set1->human eq "(COND1 and !COND2) or (!COND3)";
54 return 1 unless $set2->human eq "(COND1 and !COND2) or (!COND3)";
55 my $one_cond_human = $set1->one_cond->human;
56 return 1 unless $one_cond_human eq "!COND3"
57 || $one_cond_human eq "COND1 and !COND2";
58 return 1 unless $set1->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
60 my $merged1 = $set1->merge ($set2);
61 my $merged2 = $set1->merge ($cond);
62 my $mult1 = $set1->multiply ($set3);
63 return threads->new (sub {
64 my $mult2 = $set1->multiply ($another);
65 return threads->new (sub {
66 return 1 unless $merged1->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
67 return 1 unless $merged2->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
68 return 1 unless $mult1->string eq "COND1_TRUE COND2_FALSE COND3_TRUE COND4_FALSE";
69 return 1 unless $mult1 == $mult2;
70 return 0;
71 })->join;
72 })->join;
73 })->join;
74 })->join;
75 })->join;
78 sub build_set (@)
80 my @conds = @_;
81 my @set = ();
82 for my $cond (@conds)
84 push @set, new Automake::Condition @$cond;
86 return new Automake::DisjConditions @set;
89 sub test_invert ()
91 my @tests = ([[["FALSE"]],
92 [["TRUE"]]],
94 [[["TRUE"]],
95 [["FALSE"]]],
97 [[["COND1_TRUE", "COND2_TRUE"],
98 ["COND3_FALSE", "COND2_TRUE"]],
99 [["COND2_FALSE"],
100 ["COND1_FALSE", "COND3_TRUE"]]],
102 [[["COND1_TRUE", "COND2_TRUE"],
103 ["TRUE"]],
104 [["FALSE"]]],
106 [[["COND1_TRUE", "COND2_TRUE"],
107 ["FALSE"]],
108 [["COND1_FALSE"],
109 ["COND2_FALSE"]]],
111 [[["COND1_TRUE"],
112 ["COND2_FALSE"]],
113 [["COND1_FALSE", "COND2_TRUE"]]]
116 for my $t (@tests)
118 my $set = build_set @{$t->[0]};
119 return 1
120 if threads->new(sub {
121 my $res = build_set @{$t->[1]};
122 my $inv = $set->invert;
123 if ($inv != $res)
125 print " (I) " . $set->string . "\n\t"
126 . $inv->string . ' != ' . $res->string . "\n";
127 return 1;
129 return 0
130 })-> join;
132 return 0;
135 sub test_simplify ()
137 my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
138 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"]],
139 [["FOO_TRUE", "BAR_FALSE"]]],
141 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
142 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
143 ["FOO_TRUE", "BAR_TRUE"]],
144 [["FOO_TRUE"]]],
146 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
147 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
148 ["FOO_TRUE", "BAR_TRUE"],
149 ["FOO_FALSE"]],
150 [["TRUE"]]],
152 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
153 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
154 ["BAR_TRUE", "BAZ_TRUE"],
155 ["BAR_FALSE", "BAZ_TRUE"]],
156 [["BAZ_TRUE"], ["FOO_TRUE", "BAR_FALSE"]]],
158 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
159 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
160 ["BAR_TRUE", "BAZ_TRUE"],
161 ["BAR_FALSE", "BAZ_TRUE"],
162 ["FOO_FALSE"]],
163 [["FOO_FALSE"], ["BAZ_TRUE"], ["BAR_FALSE"]]],
165 [[["B_TRUE"],
166 ["A_FALSE", "B_TRUE"]],
167 [["B_TRUE"]]],
169 [[["B_TRUE"],
170 ["A_FALSE", "B_FALSE", "C_TRUE"],
171 ["A_FALSE", "B_FALSE", "C_FALSE"]],
172 [["A_FALSE"], ["B_TRUE"]]],
174 [[["B_TRUE"],
175 ["A_FALSE", "B_FALSE", "C_TRUE"],
176 ["A_FALSE", "B_FALSE", "C_FALSE"],
177 ["A_TRUE", "B_FALSE"]],
178 [["TRUE"]]],
180 [[["A_TRUE", "B_TRUE"],
181 ["A_TRUE", "B_FALSE"],
182 ["A_TRUE", "C_FALSE", "D_FALSE"]],
183 [["A_TRUE"]]],
185 [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
186 ["A_FALSE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
187 ["A_FALSE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
188 ["A_FALSE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
189 ["A_TRUE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
190 ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
191 ["A_TRUE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
192 ["A_TRUE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"]],
193 [ ["B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
194 ["B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
195 ["B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
196 ["B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"]]],
198 [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
199 ["A_FALSE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
200 ["A_FALSE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
201 ["A_FALSE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
202 ["A_TRUE", "B_TRUE", "C_FALSE", "D_FALSE", "E_FALSE"],
203 ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE", "E_TRUE"],
204 ["A_TRUE", "B_FALSE", "C_TRUE", "D_TRUE", "E_TRUE"],
205 ["A_TRUE", "B_FALSE", "C_FALSE", "D_TRUE", "E_FALSE"],
206 ["A_FALSE", "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"],
207 ["A_FALSE", "B_FALSE", "C_TRUE", "D_FALSE", "E_TRUE"],
208 ["A_FALSE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"],
209 ["A_FALSE", "B_TRUE", "C_FALSE", "D_TRUE", "E_FALSE"],
210 ["A_TRUE", "B_TRUE", "C_FALSE", "D_TRUE", "E_FALSE"],
211 ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"],
212 ["A_TRUE", "B_FALSE", "C_TRUE", "D_FALSE", "E_TRUE"],
213 ["A_TRUE", "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"]],
214 [["C_FALSE", "E_FALSE"],
215 ["C_TRUE", "E_TRUE"]]],
217 [[["A_FALSE"],
218 ["A_TRUE", "B_FALSE"],
219 ["A_TRUE", "B_TRUE", "C_FALSE"],
220 ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE"],
221 ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_FALSE"],
222 ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE", "F_FALSE"],
223 ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"]],
224 [["TRUE"]]],
226 # Simplify should work with up to 31 variables.
227 [[["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
228 "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
229 "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
230 "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
231 "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
232 "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
233 "V31_TRUE"],
234 ["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
235 "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
236 "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
237 "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
238 "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
239 "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
240 "V31_FALSE"],
241 ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
242 "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
243 "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
244 "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
245 "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
246 "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
247 "V31_TRUE"],
248 ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
249 "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
250 "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
251 "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
252 "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
253 "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
254 "V31_FALSE"]],
255 [[ "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
256 "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
257 "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
258 "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
259 "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
260 "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE"
261 ]]]);
263 for my $t (@tests)
265 my $set = build_set @{$t->[0]};
266 return 1
267 if threads->new(sub {
268 my $res = build_set @{$t->[1]};
269 return threads->new(sub {
271 # Make sure simplify() yields the expected result.
272 my $sim = $set->simplify;
273 return threads->new(sub {
274 if ($sim != $res)
276 print " (S1) " . $set->string . "\n\t"
277 . $sim->string . ' != ' . $res->string . "\n";
278 return 1;
281 # Make sure simplify() is idempotent.
282 my $sim2 = $sim->simplify;
283 return threads->new(sub {
284 if ($sim2 != $sim)
286 print " (S2) " . $sim->string . "\n\t"
287 . $sim2->string . ' != ' . $sim->string . "\n";
288 return 1;
291 # Also exercise invert() while we are at it.
293 my $inv1 = $set->invert->simplify;
294 return threads->new(sub {
295 my $inv2 = $sim->invert->simplify;
296 return threads->new(sub {
297 if ($inv1 != $inv2)
299 print " (S3) " . $set->string . ", " . $sim->string . "\n\t"
300 . $inv1->string . ' -= ' . $inv2->string . "\n";
301 return 1;
303 })->join;
304 })->join;
305 })->join;
306 })->join;
307 })->join;
308 })->join;
311 return 0;
314 sub test_sub_conditions ()
316 my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
317 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
318 ["FOO_FALSE"]],
319 ["FOO_TRUE"],
320 [["BAR_FALSE", "BAZ_FALSE"],
321 ["BAR_FALSE", "BAZ_TRUE"]]],
323 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
324 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
325 ["FOO_FALSE"]],
326 ["FOO_TRUE", "BAR_FALSE"],
327 [["BAZ_FALSE"],
328 ["BAZ_TRUE"]]],
330 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
331 ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
332 ["FOO_FALSE"]],
333 ["FOO_TRUE", "BAR_TRUE"],
334 [["FALSE"]]],
336 [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
337 ["FOO_TRUE", "BAZ_TRUE"],
338 ["FOO_FALSE"]],
339 ["FOO_TRUE", "BAR_TRUE"],
340 [["BAZ_TRUE"]]],
342 [[["FOO_TRUE", "BAR_FALSE"],
343 ["FOO_TRUE", "BAR_TRUE"]],
344 ["FOO_TRUE", "BAR_TRUE"],
345 [["TRUE"]]],
347 [[["TRUE"]],
348 ["TRUE"],
349 [["TRUE"]]],
351 [[["FALSE"]],
352 ["TRUE"],
353 [["FALSE"]]],
355 [[["FALSE"]],
356 ["FALSE"],
357 [["FALSE"]]]);
359 for my $t (@tests)
361 my $t1 = build_set @{$t->[0]};
362 return 1
363 if threads->new(sub {
364 my $t2 = new Automake::Condition @{$t->[1]};
365 return threads->new(sub {
366 my $t3 = build_set @{$t->[2]};
367 return threads->new(sub {
369 # Make sure sub_conditions() yields the expected result.
370 my $s = $t1->sub_conditions ($t2);
371 threads->new(sub {
372 if ($s != $t3)
374 print " (SC) " . $t1->string . "\n\t"
375 . $s->string . ' != ' . $t3->string . "\n";
376 return 1;
378 })->join;
379 })->join;
380 })->join;
381 })->join;
385 sub test_ambig ()
387 my @tests = ([[["TRUE"]],
388 ["TRUE"],
389 "multiply defined"],
390 [[["C1_TRUE"]],
391 ["C1_TRUE"],
392 "multiply defined"],
393 [[["TRUE"]],
394 ["C1_FALSE"],
395 "which includes"],
396 [[["C1_TRUE"]],
397 ["C1_TRUE", "C2_TRUE"],
398 "which includes"],
399 [[["C1_TRUE", "C2_TRUE"]],
400 ["C2_TRUE"],
401 "which is included in"],
402 [[["C1_TRUE"]],
403 ["C2_TRUE"],
404 ''],
405 [[["C1_TRUE"],
406 ["C2_FALSE"]],
407 ["C1_FALSE", "C2_TRUE"],
408 '']);
410 my $failed = 0;
411 for my $t (@tests)
413 my $t1 = build_set @{$t->[0]};
414 $failed = 1
415 if threads->new(sub {
416 my $t2 = new Automake::Condition @{$t->[1]};
417 my $t3 = $t->[2];
418 return threads->new(sub {
419 my ($ans, $cond) = $t1->ambiguous_p ("FOO", $t2);
420 return threads->new(sub {
421 if ($t3 && $ans !~ /FOO.*$t3/)
423 print " (A1) " . $t1->string . " vs. " . $t2->string . "\n\t"
424 . "Error message '$ans' does not match '$t3'\n";
425 return 1;
427 if (!$t3 && $ans ne '')
429 print " (A2) " . $t1->string . " vs. " . $t2->string . "\n\t"
430 . "Unexpected error message: $ans\n";
431 return 1;
433 })->join;
434 })->join;
435 })->join;
437 return $failed;
440 exit (test_basics
441 || test_invert
442 || test_simplify
443 || test_sub_conditions
444 || test_ambig);
446 ### Setup "GNU" style for perl-mode and cperl-mode.
447 ## Local Variables:
448 ## perl-indent-level: 2
449 ## perl-continued-statement-offset: 2
450 ## perl-continued-brace-offset: 0
451 ## perl-brace-offset: 0
452 ## perl-brace-imaginary-offset: 0
453 ## perl-label-offset: -2
454 ## cperl-indent-level: 2
455 ## cperl-brace-offset: 0
456 ## cperl-continued-brace-offset: 0
457 ## cperl-label-offset: -2
458 ## cperl-extra-newline-before-brace: t
459 ## cperl-merge-trailing-else: nil
460 ## cperl-continued-statement-offset: 2
461 ## End: