maint: run "make update-copyright"
[platform/upstream/automake.git] / tests / pm / DisjConditions.pl
1 # Copyright (C) 2001-2012 Free Software Foundation, Inc.
2 #
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.
7 #
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.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 use Automake::Condition qw/TRUE FALSE/;
17 use Automake::DisjConditions;
18
19 sub test_basics ()
20 {
21   my $true = new Automake::DisjConditions TRUE;
22   my $false = new Automake::DisjConditions FALSE;
23   my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
24   my $other = new Automake::Condition "COND3_FALSE";
25   my $another = new Automake::Condition "COND3_TRUE", "COND4_FALSE";
26   my $set1 = new Automake::DisjConditions $cond, $other;
27   my $set2 = new Automake::DisjConditions $other, $cond;
28   my $set3 = new Automake::DisjConditions FALSE, $another;
29   return 1 unless $set1 == $set2;
30   return 1 if $set1->false;
31   return 1 if $set1->true;
32   return 1 unless (new Automake::DisjConditions)->false;
33   return 1 if (new Automake::DisjConditions)->true;
34   return 1 unless $true->human eq 'TRUE';
35   return 1 unless $false->human eq 'FALSE';
36   return 1 unless $set1->human eq "(COND1 and !COND2) or (!COND3)";
37   return 1 unless $set2->human eq "(COND1 and !COND2) or (!COND3)";
38   my $one_cond_human = $set1->one_cond->human;
39   return 1 unless $one_cond_human eq "!COND3"
40                   || $one_cond_human eq "COND1 and !COND2";
41   return 1 unless $set1->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
42
43   my $merged1 = $set1->merge ($set2);
44   my $merged2 = $set1->merge ($cond);
45   my $mult1 = $set1->multiply ($set3);
46   my $mult2 = $set1->multiply ($another);
47   return 1 unless $merged1->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
48   return 1 unless $merged2->simplify->string eq "COND1_TRUE COND2_FALSE | COND3_FALSE";
49   return 1 unless $mult1->string eq "COND1_TRUE COND2_FALSE COND3_TRUE COND4_FALSE";
50   return 1 unless $mult1 == $mult2;
51
52   return 0;
53 }
54
55 sub build_set (@)
56 {
57   my @conds = @_;
58   my @set = ();
59   for my $cond (@conds)
60     {
61       push @set, new Automake::Condition @$cond;
62     }
63   return new Automake::DisjConditions @set;
64 }
65
66 sub test_invert ()
67 {
68   my @tests = ([[["FALSE"]],
69                 [["TRUE"]]],
70
71                [[["TRUE"]],
72                 [["FALSE"]]],
73
74                [[["COND1_TRUE", "COND2_TRUE"],
75                  ["COND3_FALSE", "COND2_TRUE"]],
76                 [["COND2_FALSE"],
77                  ["COND1_FALSE", "COND3_TRUE"]]],
78
79                [[["COND1_TRUE", "COND2_TRUE"],
80                  ["TRUE"]],
81                 [["FALSE"]]],
82
83                [[["COND1_TRUE", "COND2_TRUE"],
84                  ["FALSE"]],
85                 [["COND1_FALSE"],
86                  ["COND2_FALSE"]]],
87
88                [[["COND1_TRUE"],
89                  ["COND2_FALSE"]],
90                 [["COND1_FALSE", "COND2_TRUE"]]]
91                );
92
93   for my $t (@tests)
94     {
95       my $set = build_set @{$t->[0]};
96       my $res = build_set @{$t->[1]};
97       my $inv = $set->invert;
98       if ($inv != $res)
99         {
100           print " (I) " . $set->string . "\n\t"
101             . $inv->string . ' != ' . $res->string . "\n";
102           return 1;
103         }
104     }
105   return 0;
106 }
107
108 sub test_simplify ()
109 {
110   my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
111                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"]],
112                 [["FOO_TRUE", "BAR_FALSE"]]],
113
114                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
115                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
116                  ["FOO_TRUE", "BAR_TRUE"]],
117                 [["FOO_TRUE"]]],
118
119                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
120                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
121                  ["FOO_TRUE", "BAR_TRUE"],
122                  ["FOO_FALSE"]],
123                 [["TRUE"]]],
124
125                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
126                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
127                              ["BAR_TRUE",  "BAZ_TRUE"],
128                              ["BAR_FALSE", "BAZ_TRUE"]],
129                 [["BAZ_TRUE"], ["FOO_TRUE", "BAR_FALSE"]]],
130
131                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
132                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
133                              ["BAR_TRUE",  "BAZ_TRUE"],
134                              ["BAR_FALSE", "BAZ_TRUE"],
135                  ["FOO_FALSE"]],
136                 [["FOO_FALSE"], ["BAZ_TRUE"], ["BAR_FALSE"]]],
137
138                [[["B_TRUE"],
139                  ["A_FALSE", "B_TRUE"]],
140                 [["B_TRUE"]]],
141
142                [[["B_TRUE"],
143                  ["A_FALSE", "B_FALSE", "C_TRUE"],
144                  ["A_FALSE", "B_FALSE", "C_FALSE"]],
145                 [["A_FALSE"], ["B_TRUE"]]],
146
147                [[["B_TRUE"],
148                  ["A_FALSE", "B_FALSE", "C_TRUE"],
149                  ["A_FALSE", "B_FALSE", "C_FALSE"],
150                  ["A_TRUE", "B_FALSE"]],
151                 [["TRUE"]]],
152
153                [[["A_TRUE", "B_TRUE"],
154                  ["A_TRUE", "B_FALSE"],
155                  ["A_TRUE", "C_FALSE", "D_FALSE"]],
156                 [["A_TRUE"]]],
157
158                [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE",  "E_FALSE"],
159                  ["A_FALSE", "B_FALSE", "C_TRUE",  "D_TRUE",  "E_TRUE"],
160                  ["A_FALSE", "B_TRUE",  "C_TRUE",  "D_FALSE", "E_TRUE"],
161                  ["A_FALSE", "B_TRUE",  "C_FALSE", "D_FALSE", "E_FALSE"],
162                  ["A_TRUE",  "B_TRUE",  "C_FALSE", "D_FALSE", "E_FALSE"],
163                  ["A_TRUE",  "B_TRUE",  "C_TRUE",  "D_FALSE", "E_TRUE"],
164                  ["A_TRUE",  "B_FALSE", "C_TRUE",  "D_TRUE",  "E_TRUE"],
165                  ["A_TRUE",  "B_FALSE", "C_FALSE", "D_TRUE",  "E_FALSE"]],
166                 [           ["B_FALSE", "C_FALSE", "D_TRUE",  "E_FALSE"],
167                             ["B_FALSE", "C_TRUE",  "D_TRUE",  "E_TRUE"],
168                             ["B_TRUE",  "C_TRUE",  "D_FALSE", "E_TRUE"],
169                             ["B_TRUE",  "C_FALSE", "D_FALSE", "E_FALSE"]]],
170
171                [[["A_FALSE", "B_FALSE", "C_FALSE", "D_TRUE",  "E_FALSE"],
172                  ["A_FALSE", "B_FALSE", "C_TRUE",  "D_TRUE",  "E_TRUE"],
173                  ["A_FALSE", "B_TRUE",  "C_TRUE",  "D_FALSE", "E_TRUE"],
174                  ["A_FALSE", "B_TRUE",  "C_FALSE", "D_FALSE", "E_FALSE"],
175                  ["A_TRUE",  "B_TRUE",  "C_FALSE", "D_FALSE", "E_FALSE"],
176                  ["A_TRUE",  "B_TRUE",  "C_TRUE",  "D_FALSE", "E_TRUE"],
177                  ["A_TRUE",  "B_FALSE", "C_TRUE",  "D_TRUE",  "E_TRUE"],
178                  ["A_TRUE",  "B_FALSE", "C_FALSE", "D_TRUE",  "E_FALSE"],
179                  ["A_FALSE", "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"],
180                  ["A_FALSE", "B_FALSE", "C_TRUE",  "D_FALSE", "E_TRUE"],
181                  ["A_FALSE", "B_TRUE",  "C_TRUE",  "D_TRUE",  "E_TRUE"],
182                  ["A_FALSE", "B_TRUE",  "C_FALSE", "D_TRUE",  "E_FALSE"],
183                  ["A_TRUE",  "B_TRUE",  "C_FALSE", "D_TRUE",  "E_FALSE"],
184                  ["A_TRUE",  "B_TRUE",  "C_TRUE",  "D_TRUE",  "E_TRUE"],
185                  ["A_TRUE",  "B_FALSE", "C_TRUE",  "D_FALSE", "E_TRUE"],
186                  ["A_TRUE",  "B_FALSE", "C_FALSE", "D_FALSE", "E_FALSE"]],
187                 [["C_FALSE", "E_FALSE"],
188                  ["C_TRUE", "E_TRUE"]]],
189
190                [[["A_FALSE"],
191                  ["A_TRUE", "B_FALSE"],
192                  ["A_TRUE", "B_TRUE", "C_FALSE"],
193                  ["A_TRUE", "B_TRUE", "C_TRUE", "D_FALSE"],
194                  ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_FALSE"],
195                  ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE", "F_FALSE"],
196                  ["A_TRUE", "B_TRUE", "C_TRUE", "D_TRUE", "E_TRUE"]],
197                 [["TRUE"]]],
198
199                # Simplify should work with up to 31 variables.
200                [[["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
201                   "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
202                   "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
203                   "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
204                   "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
205                   "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
206                   "V31_TRUE"],
207                  ["V01_TRUE", "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
208                   "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
209                   "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
210                   "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
211                   "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
212                   "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
213                   "V31_FALSE"],
214                  ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
215                   "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
216                   "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
217                   "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
218                   "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
219                   "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
220                   "V31_TRUE"],
221                  ["V01_FALSE","V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
222                   "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
223                   "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
224                   "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
225                   "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
226                   "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE",
227                   "V31_FALSE"]],
228                 [[            "V02_TRUE", "V03_TRUE", "V04_TRUE", "V05_TRUE",
229                   "V06_TRUE", "V07_TRUE", "V08_TRUE", "V09_TRUE", "V10_TRUE",
230                   "V11_TRUE", "V12_TRUE", "V13_TRUE", "V14_TRUE", "V15_TRUE",
231                   "V16_TRUE", "V17_TRUE", "V18_TRUE", "V19_TRUE", "V20_TRUE",
232                   "V21_TRUE", "V22_TRUE", "V23_TRUE", "V24_TRUE", "V25_TRUE",
233                   "V26_TRUE", "V27_TRUE", "V28_TRUE", "V29_TRUE", "V30_TRUE"
234                   ]]]);
235
236   for my $t (@tests)
237     {
238       my $set = build_set @{$t->[0]};
239       my $res = build_set @{$t->[1]};
240
241       # Make sure simplify() yields the expected result.
242       my $sim = $set->simplify;
243       if ($sim != $res)
244         {
245           print " (S1) " . $set->string . "\n\t"
246             . $sim->string . ' != ' . $res->string . "\n";
247           return 1;
248         }
249
250       # Make sure simplify() is idempotent.
251       my $sim2 = $sim->simplify;
252       if ($sim2 != $sim)
253         {
254           print " (S2) " . $sim->string . "\n\t"
255             . $sim2->string . ' != ' . $sim->string . "\n";
256           return 1;
257         }
258
259       # Also exercise invert() while we are at it.
260
261       my $inv1 = $set->invert->simplify;
262       my $inv2 = $sim->invert->simplify;
263       if ($inv1 != $inv2)
264         {
265           print " (S3) " . $set->string . ", " . $sim->string . "\n\t"
266             . $inv1->string . ' != ' . $inv2->string . "\n";
267           return 1;
268         }
269     }
270
271   return 0;
272 }
273
274 sub test_sub_conditions ()
275 {
276   my @tests = ([[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
277                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
278                  ["FOO_FALSE"]],
279                 ["FOO_TRUE"],
280                 [["BAR_FALSE", "BAZ_FALSE"],
281                  ["BAR_FALSE", "BAZ_TRUE"]]],
282
283                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
284                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
285                  ["FOO_FALSE"]],
286                 ["FOO_TRUE", "BAR_FALSE"],
287                 [["BAZ_FALSE"],
288                  ["BAZ_TRUE"]]],
289
290                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
291                  ["FOO_TRUE", "BAR_FALSE", "BAZ_TRUE"],
292                  ["FOO_FALSE"]],
293                 ["FOO_TRUE", "BAR_TRUE"],
294                 [["FALSE"]]],
295
296                [[["FOO_TRUE", "BAR_FALSE", "BAZ_FALSE"],
297                  ["FOO_TRUE", "BAZ_TRUE"],
298                  ["FOO_FALSE"]],
299                 ["FOO_TRUE", "BAR_TRUE"],
300                 [["BAZ_TRUE"]]],
301
302                [[["FOO_TRUE", "BAR_FALSE"],
303                  ["FOO_TRUE", "BAR_TRUE"]],
304                 ["FOO_TRUE", "BAR_TRUE"],
305                 [["TRUE"]]],
306
307                [[["TRUE"]],
308                 ["TRUE"],
309                 [["TRUE"]]],
310
311                [[["FALSE"]],
312                 ["TRUE"],
313                 [["FALSE"]]],
314
315                [[["FALSE"]],
316                 ["FALSE"],
317                 [["FALSE"]]]);
318
319   for my $t (@tests)
320     {
321       my $t1 = build_set @{$t->[0]};
322       my $t2 = new Automake::Condition @{$t->[1]};
323       my $t3 = build_set @{$t->[2]};
324
325       # Make sure sub_conditions() yields the expected result.
326       my $s = $t1->sub_conditions ($t2);
327       if ($s != $t3)
328         {
329           print " (SC) " . $t1->string . "\n\t"
330             . $s->string . ' != ' . $t3->string . "\n";
331           return 1;
332         }
333     }
334 }
335
336 sub test_ambig ()
337 {
338   my @tests = ([[["TRUE"]],
339                 ["TRUE"],
340                 "multiply defined"],
341                [[["C1_TRUE"]],
342                 ["C1_TRUE"],
343                 "multiply defined"],
344                [[["TRUE"]],
345                 ["C1_FALSE"],
346                 "which includes"],
347                [[["C1_TRUE"]],
348                 ["C1_TRUE", "C2_TRUE"],
349                 "which includes"],
350                [[["C1_TRUE", "C2_TRUE"]],
351                 ["C2_TRUE"],
352                 "which is included in"],
353                [[["C1_TRUE"]],
354                 ["C2_TRUE"],
355                 ''],
356                [[["C1_TRUE"],
357                  ["C2_FALSE"]],
358                 ["C1_FALSE", "C2_TRUE"],
359                 '']);
360
361   for my $t (@tests)
362     {
363       my $t1 = build_set @{$t->[0]};
364       my $t2 = new Automake::Condition @{$t->[1]};
365       my $t3 = $t->[2];
366       my ($ans, $cond) = $t1->ambiguous_p ("FOO", $t2);
367       if ($t3 && $ans !~ /FOO.*$t3/)
368         {
369           print " (A1) " . $t1->string . " vs. " . $t2->string . "\n\t"
370             . "Error message '$ans' does not match '$t3'\n";
371           return 1;
372         }
373       if (!$t3 && $ans ne '')
374         {
375           print " (A2) " . $t1->string . " vs. " . $t2->string . "\n\t"
376             . "Unexpected error message: $ans\n";
377           return 1;
378         }
379     }
380   return 0;
381 }
382
383 exit (test_basics
384       || test_invert
385       || test_simplify
386       || test_sub_conditions
387       || test_ambig);
388
389 ### Setup "GNU" style for perl-mode and cperl-mode.
390 ## Local Variables:
391 ## perl-indent-level: 2
392 ## perl-continued-statement-offset: 2
393 ## perl-continued-brace-offset: 0
394 ## perl-brace-offset: 0
395 ## perl-brace-imaginary-offset: 0
396 ## perl-label-offset: -2
397 ## cperl-indent-level: 2
398 ## cperl-brace-offset: 0
399 ## cperl-continued-brace-offset: 0
400 ## cperl-label-offset: -2
401 ## cperl-extra-newline-before-brace: t
402 ## cperl-merge-trailing-else: nil
403 ## cperl-continued-statement-offset: 2
404 ## End: