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