Merge branch 'msvc'
[platform/upstream/automake.git] / lib / Automake / tests / Condition-t.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 BEGIN {
20   use Config;
21   if (eval { require 5.007_002; }       # for CLONE support
22       && $Config{useithreads}
23       && !$ENV{WANT_NO_THREADS})
24     {
25       require threads;
26       import threads;
27     }
28   else
29     {
30       exit 77;
31     }
32 }
33 use Automake::Condition qw/TRUE FALSE/;
34
35 sub test_basics ()
36 {
37   my @tests = (# [[Conditions], is_true?, is_false?, string, subst-string, human]
38                [[], 1, 0, 'TRUE', '', 'TRUE'],
39                [['TRUE'], 1, 0, 'TRUE', '', 'TRUE'],
40                [['FALSE'], 0, 1, 'FALSE', '#', 'FALSE'],
41                [['A_TRUE'], 0, 0, 'A_TRUE', '@A_TRUE@', 'A'],
42                [['A_TRUE', 'B_FALSE'],
43                 0, 0, 'A_TRUE B_FALSE', '@A_TRUE@@B_FALSE@', 'A and !B'],
44                [['B_TRUE', 'FALSE'], 0, 1, 'FALSE', '#', 'FALSE'],
45                [['B_TRUE', 'B_FALSE'], 0, 1, 'FALSE', '#', 'FALSE']);
46
47   for (@tests)
48     {
49       my $a = new Automake::Condition @{$_->[0]};
50       return 1
51         if threads->new(sub {
52           return 1 if $_->[1] != $a->true;
53           return 1 if $_->[1] != ($a == TRUE);
54           return 1 if $_->[2] != $a->false;
55           return 1 if $_->[2] != ($a == FALSE);
56           return 1 if $_->[3] ne $a->string;
57           return 1 if $_->[4] ne $a->subst_string;
58           return 1 if $_->[5] ne $a->human;
59         })->join;
60     }
61   return 0;
62 }
63
64 sub test_true_when ()
65 {
66   my $failed = 0;
67
68   my @tests = (# [When,
69                #  [Implied-Conditions],
70                #  [Not-Implied-Conditions]]
71                [['TRUE'],
72                 [['TRUE']],
73                 [['A_TRUE'], ['A_TRUE', 'B_FALSE'], ['FALSE']]],
74                [['A_TRUE'],
75                 [['TRUE'], ['A_TRUE']],
76                 [['A_TRUE', 'B_FALSE'], ['FALSE']]],
77                [['A_TRUE', 'B_FALSE'],
78                 [['TRUE'], ['A_TRUE'], ['B_FALSE'], ['A_TRUE', 'B_FALSE']],
79                 [['FALSE'], ['C_FALSE'], ['C_FALSE', 'A_TRUE']]]);
80
81   for my $t (@tests)
82     {
83       my $a = new Automake::Condition @{$t->[0]};
84       return 1
85         if threads->new(sub {
86           for my $u (@{$t->[1]})
87             {
88               my $b = new Automake::Condition @$u;
89               return threads->new(sub {
90                 if (! $b->true_when ($a))
91                   {
92                     print "`" . $b->string .
93                       "' not implied by `" . $a->string . "'?\n";
94                     $failed = 1;
95                   }
96               })->join;
97             }
98           for my $u (@{$t->[2]})
99             {
100               my $b = new Automake::Condition @$u;
101               return threads->new(sub {
102                 if ($b->true_when ($a))
103                   {
104                     print "`" . $b->string .
105                       "' implied by `" . $a->string . "'?\n";
106                     $failed = 1;
107                   }
108
109                 return threads->new(sub {
110                   return 1 if $b->true_when ($a);
111                 })->join;
112               })->join;
113             }
114          })->join;
115     }
116   return $failed;
117 }
118
119 sub test_reduce_and ()
120 {
121   my @tests = (# If no conditions are given, TRUE should be returned
122                [[], ["TRUE"]],
123                # An empty condition is TRUE
124                [[""], ["TRUE"]],
125                # A single condition should be passed through unchanged
126                [["FOO"], ["FOO"]],
127                [["FALSE"], ["FALSE"]],
128                [["TRUE"], ["TRUE"]],
129                # TRUE and false should be discarded and overwhelm
130                # the result, respectively
131                [["FOO", "TRUE"], ["FOO"]],
132                [["FOO", "FALSE"], ["FALSE"]],
133                # Repetitions should be removed
134                [["FOO", "FOO"], ["FOO"]],
135                [["TRUE", "FOO", "FOO"], ["FOO"]],
136                [["FOO", "TRUE", "FOO"], ["FOO"]],
137                [["FOO", "FOO", "TRUE"], ["FOO"]],
138                # Two different conditions should be preserved,
139                # but TRUEs should be removed
140                [["FOO", "BAR"], ["BAR,FOO"]],
141                [["TRUE", "FOO", "BAR"], ["BAR,FOO"]],
142                [["FOO", "TRUE", "BAR"], ["BAR,FOO"]],
143                [["FOO", "BAR", "TRUE"], ["BAR,FOO"]],
144                # A condition implied by another condition should be removed.
145                [["FOO BAR", "BAR"], ["FOO BAR"]],
146                [["BAR", "FOO BAR"], ["FOO BAR"]],
147                [["TRUE", "FOO BAR", "BAR"], ["FOO BAR"]],
148                [["FOO BAR", "TRUE", "BAR"], ["FOO BAR"]],
149                [["FOO BAR", "BAR", "TRUE"], ["FOO BAR"]],
150
151                [["BAR FOO", "BAR"], ["BAR FOO"]],
152                [["BAR", "BAR FOO"], ["BAR FOO"]],
153                [["TRUE", "BAR FOO", "BAR"], ["BAR FOO"]],
154                [["BAR FOO", "TRUE", "BAR"], ["BAR FOO"]],
155                [["BAR FOO", "BAR", "TRUE"], ["BAR FOO"]],
156
157                # Check that reduction happens even when there are
158                # two conditions to remove.
159                [["FOO", "FOO BAR", "BAR"], ["FOO BAR"]],
160                [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["FOO BAR", "FOO BAZ"]],
161                [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"],
162                 ["FOO BAZ BAR"]],
163
164                # Duplicated conditionals should be removed.
165                [["FOO", "BAR", "BAR"], ["BAR,FOO"]],
166
167                # Equivalent conditions in different forms should be
168                # reduced: which one is left is unfortunately order
169                # dependent.
170                [["BAR FOO", "FOO BAR"], ["FOO BAR"]],
171                [["FOO BAR", "BAR FOO"], ["BAR FOO"]]);
172
173   my $failed = 0;
174   foreach (@tests)
175     {
176       my ($inref, $outref) = @$_;
177       my @inconds = map { new Automake::Condition $_ } @$inref;
178       return 1
179         if threads->new(sub {
180           my @outconds = map { (new Automake::Condition $_)->string } @$outref;
181           return threads->new(sub {
182             my @res =
183               map { $_->string } (Automake::Condition::reduce_and (@inconds));
184             return threads->new(sub {
185               my $result = join (",", sort @res);
186               my $exresult = join (",", @outconds);
187
188               if ($result ne $exresult)
189                 {
190                   print '"' . join(",", @$inref) . '" => "' .
191                     $result . '" expected "' .
192                       $exresult . '"' . "\n";
193                   $failed = 1;
194                 }
195               return $failed;
196             })->join;
197           })->join;
198         })->join;
199     }
200   return $failed;
201 }
202
203 sub test_reduce_or ()
204 {
205   my @tests = (# If no conditions are given, FALSE should be returned
206                [[], ["FALSE"]],
207                # An empty condition is TRUE
208                [[""], ["TRUE"]],
209                # A single condition should be passed through unchanged
210                [["FOO"], ["FOO"]],
211                [["FALSE"], ["FALSE"]],
212                [["TRUE"], ["TRUE"]],
213                # FALSE and TRUE should be discarded and overwhelm
214                # the result, respectively
215                [["FOO", "TRUE"], ["TRUE"]],
216                [["FOO", "FALSE"], ["FOO"]],
217                # Repetitions should be removed
218                [["FOO", "FOO"], ["FOO"]],
219                [["FALSE", "FOO", "FOO"], ["FOO"]],
220                [["FOO", "FALSE", "FOO"], ["FOO"]],
221                [["FOO", "FOO", "FALSE"], ["FOO"]],
222                # Two different conditions should be preserved,
223                # but FALSEs should be removed
224                [["FOO", "BAR"], ["BAR,FOO"]],
225                [["FALSE", "FOO", "BAR"], ["BAR,FOO"]],
226                [["FOO", "FALSE", "BAR"], ["BAR,FOO"]],
227                [["FOO", "BAR", "FALSE"], ["BAR,FOO"]],
228                # A condition implying another condition should be removed.
229                [["FOO BAR", "BAR"], ["BAR"]],
230                [["BAR", "FOO BAR"], ["BAR"]],
231                [["FALSE", "FOO BAR", "BAR"], ["BAR"]],
232                [["FOO BAR", "FALSE", "BAR"], ["BAR"]],
233                [["FOO BAR", "BAR", "FALSE"], ["BAR"]],
234
235                [["BAR FOO", "BAR"], ["BAR"]],
236                [["BAR", "BAR FOO"], ["BAR"]],
237                [["FALSE", "BAR FOO", "BAR"], ["BAR"]],
238                [["BAR FOO", "FALSE", "BAR"], ["BAR"]],
239                [["BAR FOO", "BAR", "FALSE"], ["BAR"]],
240
241                # Check that reduction happens even when there are
242                # two conditions to remove.
243                [["FOO", "FOO BAR", "BAR"], ["BAR,FOO"]],
244                [["FOO", "FOO BAR", "BAZ", "FOO BAZ"], ["BAZ,FOO"]],
245                [["FOO", "FOO BAR", "BAZ", "FOO BAZ", "FOO BAZ BAR"],
246                 ["BAZ,FOO"]],
247
248                # Duplicated conditionals should be removed.
249                [["FOO", "BAR", "BAR"], ["BAR,FOO"]],
250
251                # Equivalent conditions in different forms should be
252                # reduced: which one is left is unfortunately order
253                # dependent.
254                [["BAR FOO", "FOO BAR"], ["FOO BAR"]],
255                [["FOO BAR", "BAR FOO"], ["BAR FOO"]]);
256
257   my $failed = 0;
258   foreach (@tests)
259     {
260       my ($inref, $outref) = @$_;
261       my @inconds = map { new Automake::Condition $_ } @$inref;
262       return 1
263         if threads->new(sub {
264           my @outconds = map { (new Automake::Condition $_)->string } @$outref;
265           return threads->new(sub {
266             my @res =
267               map { $_->string } (Automake::Condition::reduce_or (@inconds));
268             return threads->new(sub {
269               my $result = join (",", sort @res);
270               my $exresult = join (",", @outconds);
271
272               if ($result ne $exresult)
273                 {
274                   print '"' . join(",", @$inref) . '" => "' .
275                     $result . '" expected "' .
276                       $exresult . '"' . "\n";
277                   $failed = 1;
278                 }
279               return $failed;
280             })->join;
281           })->join;
282         })->join;
283     }
284   return $failed;
285 }
286
287 sub test_merge ()
288 {
289   my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
290   return threads->new(sub {
291       my $other = new Automake::Condition "COND3_FALSE";
292       return threads->new(sub {
293         my $both = $cond->merge ($other);
294         return threads->new(sub {
295           my $both2 = $cond->merge_conds ("COND3_FALSE");
296           return threads->new(sub {
297             $cond = $both->strip ($other);
298             my @conds = $cond->conds;
299             return 1 if $both->string ne "COND1_TRUE COND2_FALSE COND3_FALSE";
300             return 1 if $cond->string ne "COND1_TRUE COND2_FALSE";
301             return 1 if $both != $both2;
302           })->join;
303         })->join;
304       })->join;
305     })->join;
306   return 0;
307 }
308
309 exit (test_basics
310       || test_true_when
311       || test_reduce_and
312       || test_reduce_or
313       || test_merge);
314
315 ### Setup "GNU" style for perl-mode and cperl-mode.
316 ## Local Variables:
317 ## perl-indent-level: 2
318 ## perl-continued-statement-offset: 2
319 ## perl-continued-brace-offset: 0
320 ## perl-brace-offset: 0
321 ## perl-brace-imaginary-offset: 0
322 ## perl-label-offset: -2
323 ## cperl-indent-level: 2
324 ## cperl-brace-offset: 0
325 ## cperl-continued-brace-offset: 0
326 ## cperl-label-offset: -2
327 ## cperl-extra-newline-before-brace: t
328 ## cperl-merge-trailing-else: nil
329 ## cperl-continued-statement-offset: 2
330 ## End: